diff --git a/L5T1/pom.xml b/L5T1/pom.xml new file mode 100644 index 0000000..60a3b6e --- /dev/null +++ b/L5T1/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + org.example + L5T1 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + ${project.basedir}/src/main/resources + + + + \ No newline at end of file diff --git a/L5T1/src/main/java/example/AutomataGUIDemo.java b/L5T1/src/main/java/example/AutomataGUIDemo.java new file mode 100644 index 0000000..a69f5b8 --- /dev/null +++ b/L5T1/src/main/java/example/AutomataGUIDemo.java @@ -0,0 +1,154 @@ +package example; +import java.util.ArrayList; +public class AutomataGUIDemo +{ + enum STATES {OFF, WAIT, ACCEPT, CHECK, COOK} + private int cash; + private int change; + public void setMenu() + { + menu.add("Кофе"); + menu.add("Чай"); + menu.add("Крепкий кофе"); + menu.add("Чай с молоком"); + menu.add("Холодный чай"); + menu.add("Кофе с лимоном"); + menu.add("Вода"); + } + public void setPrices() + { + prices.add(25); + prices.add(25); + prices.add(30); + prices.add(40); + prices.add(20); + prices.add(40); + prices.add(10); + } + private ArrayList menu = new ArrayList<>(); + private ArrayList prices = new ArrayList<>(); + private STATES state; + private String message; + public AutomataGUIDemo() + { + cash = 0; + state = STATES.OFF; + } + public ArrayList getMenu() + { + return menu; + } + public ArrayList getPrices() + { + return prices; + } + public int getCash() + { + return cash; + } + public int getChange() + { + return change; + } + public STATES getState() + { + return state; + } + public String getMessage() + { + return message; + } + public void on() + { + if(state.equals(STATES.OFF)) + { + message="Внесите нужную сумму!"; + state = STATES.WAIT; + } + else + message="Ошибка!"; + } + public void coin(int value) + { + if(state.equals(STATES.WAIT)) + { + change=0; + cash+=value; + state=STATES.ACCEPT; + message="Выберите напиток!"; + } + else if(state.equals(STATES.ACCEPT)) + { + cash+=value; + } + else + message="Ошибка!"; + } + public int choice(int numberOfButton) + { + int price=0; + if(state.equals(STATES.ACCEPT)) + { + price = prices.get(numberOfButton); + state = STATES.CHECK; + } + else + message="Ошибка!"; + return price; + } + public boolean check(int price) + { + boolean check = false; + if(state.equals(STATES.CHECK)) + { + if (cash cbCash; + @FXML + private ProgressIndicator piProgress; + @FXML + private ImageView ivCup; + List menu; + List price; + AutomataGUIDemo automata; + @Override + public void initialize(URL url, ResourceBundle resourceBundle) + { + automata = new AutomataGUIDemo(); + automata.setMenu(); + automata.setPrices(); + menu = automata.getMenu(); + price = automata.getPrices(); + setComboBox(); + setLblNames(); + setlblPrices(); + ivCup.setVisible(false); + } + public void setLblNames() + { + String nameLeft1 = menu.get(0); + lblNameLeft1.setText(nameLeft1); + String nameLeft2 = menu.get(1); + lblNameLeft2.setText(nameLeft2); + String nameLeft3 = menu.get(2); + lblNameLeft3.setText(nameLeft3); + String nameLeft4 = menu.get(3); + lblNameLeft4.setText(nameLeft4); + String nameLeft5 = menu.get(4); + lblNameLeft5.setText(nameLeft5); + String nameLeft6 = menu.get(5); + lblNameLeft6.setText(nameLeft6); + } + public void setlblPrices() + { + String priceLeft1 = price.get(0).toString(); + lblPriceLeft1.setText(priceLeft1); + String priceLeft2 = price.get(1).toString(); + lblPriceLeft2.setText(priceLeft2); + String priceLeft3 = price.get(2).toString(); + lblPriceLeft3.setText(priceLeft3); + String priceLeft4 = price.get(3).toString(); + lblPriceLeft4.setText(priceLeft4); + String priceLeft5 = price.get(4).toString(); + lblPriceLeft5.setText(priceLeft5); + String priceLeft6 = price.get(5).toString(); + lblPriceLeft6.setText(priceLeft6); + String priceRight1 = price.get(6).toString(); + lblPriceRight1.setText(priceRight1); + } + public void switchOn() + { + automata.on(); + lblMessage.setText(automata.getMessage()); + } + public void switchOff() + { + automata.off(); + lblMessage.setText(automata.getMessage()); + piProgress.setProgress(0); + } + public void setComboBox() + { + ObservableList cashAvailable = FXCollections.observableArrayList(5,10,50,100); + cbCash.setItems(cashAvailable); + } + public void getCash() + { + try + { + int cash = cbCash.getValue(); + automata.coin(cash); + lblMessage.setText(automata.getMessage()); + } + catch (java.lang.NullPointerException ignored){}; + } + public void btnLeft1Pressed() + { + choiceAndCook(0); + } + public void btnLeft2Pressed() + { + choiceAndCook(1); + } + public void btnLeft3Pressed() + { + choiceAndCook(2); + } + public void btnLeft4Pressed() + { + choiceAndCook(3); + } + public void btnLeft5Pressed() + { + choiceAndCook(4); + } + public void btnLeft6Pressed() + { + choiceAndCook(5); + } + public void btnRight1Pressed() + { + choiceAndCook(6); + } + public void choiceAndCook (int number) + { + int price = automata.choice(number); + lblMessage.setText(automata.getMessage()); + boolean check = automata.check(price); + if(check) + { + int change = automata.getChange(); + try { + cbCash.setValue(null); + } + catch (java.lang.NullPointerException ignored){}; + lblChange.setText(Integer.toString(change)); + new Thread(() -> + { + cook(number, piProgress, lblMessage, ivCup); + } + ).start(); + } + else + lblMessage.setText("Недостаточно средств!"); + } + public void cook(int number, ProgressIndicator piProgress, Label lblMessage, ImageView ivCup){ + automata.cook(); + try + { + for(int i=0; i<=100; i++) + { + Thread.sleep(50); + double progress=(double)i/100; + Platform.runLater(() -> + { + piProgress.setProgress(progress); + if (piProgress.getProgress()<1) + lblMessage.setText(menu.get(number) + " готовится"); + else + { + lblMessage.setText(menu.get(number) + " готов!"); + ivCup.setVisible(true); + } + }); + } + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + public void cancel() + { + automata.cancel(); + int change = automata.getChange(); + lblChange.setText(Integer.toString(change)); + try + { + cbCash.setValue(null); + } + catch (java.lang.NullPointerException ignored){}; + lblMessage.setText(automata.getMessage()); + } +} \ No newline at end of file diff --git a/L5T1/src/main/java/example/Main.java b/L5T1/src/main/java/example/Main.java new file mode 100644 index 0000000..23c9d4a --- /dev/null +++ b/L5T1/src/main/java/example/Main.java @@ -0,0 +1,21 @@ +package example; +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; +public class Main extends Application +{ + @Override + public void start(Stage primaryStage) throws Exception + { + Parent root = FXMLLoader.load(getClass().getResource("/example.fxml")); + primaryStage.setTitle("Automata"); + primaryStage.setScene(new Scene(root)); + primaryStage.show(); + } + public static void main(String[] args) + { + launch(args); + } +} \ No newline at end of file diff --git a/L5T1/src/main/resources/Automata.png b/L5T1/src/main/resources/Automata.png new file mode 100644 index 0000000..a74cfbf Binary files /dev/null and b/L5T1/src/main/resources/Automata.png differ diff --git a/L5T1/src/main/resources/cup.png b/L5T1/src/main/resources/cup.png new file mode 100644 index 0000000..877d5d7 Binary files /dev/null and b/L5T1/src/main/resources/cup.png differ diff --git a/L5T1/src/main/resources/example.fxml b/L5T1/src/main/resources/example.fxml new file mode 100644 index 0000000..df83885 --- /dev/null +++ b/L5T1/src/main/resources/example.fxml @@ -0,0 +1,110 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +