diff --git a/work/Automate/AtmWithGUI.jar b/work/Automate/AtmWithGUI.jar new file mode 100644 index 0000000..5abcf76 Binary files /dev/null and b/work/Automate/AtmWithGUI.jar differ diff --git a/work/Automate/src/META-INF/MANIFEST.MF b/work/Automate/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..440fa71 --- /dev/null +++ b/work/Automate/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: sample.Main + diff --git a/work/Automate/src/atm/Automata.java b/work/Automate/src/atm/Automata.java new file mode 100644 index 0000000..cbd4f05 --- /dev/null +++ b/work/Automate/src/atm/Automata.java @@ -0,0 +1,144 @@ +package atm; + +import java.util.*; + +public class Automata { + public float cash; + private ArrayList menu; + public HashMap additions; + private float addPrice = 15f; + private STATES state; + + public Beverage order; + public String prompt = "The Automata is ready to work"; + public String answer = ""; + + public List getMenu() { + return Collections.unmodifiableList(menu); + } + + public Automata() { + Init(); + } + //Initialization of fields + private void Init(){ + cash = 0f; + menu = new ArrayList<>(); + additions = new HashMap<>(); + menu.add(new Beverage("Coffee", 150)); + menu.add(new Beverage("Tea", 80)); + menu.add(new Beverage("Juice", 200)); + additions.put("Sugar", false); + additions.put("Milk", false); + additions.put("Ice", false); + additions.put("Syrup", false); + setState(STATES.OFF); + } + + public void on() { + setState(STATES.WAIT); + answer = "Waiting for your order"; + } + + public void off() { + setState(STATES.OFF); + cash = 0f; + answer = ""; + } + + public void coin(float money) { + if (state != STATES.WAIT && state != STATES.ACCEPT) { + return; + } + if (money <= 0) { + answer = "Invalid value of money!"; + } + cash += money; + if (state == STATES.WAIT) + setState(STATES.ACCEPT); + answer = "cash : " + cash; + } + + public void getState() { + answer = "The Automate is " + state + ". Cash : " + cash; + } + + public void check() { + if (findOut()) { + answer = "Cash = " + cash + + "Price of " + order + " = " + order.getName() + + "You have to add " + (order.getPrice() - cash); + } else + answer = "Cash is enough!"; + } + + //Check, is there cash enough for the order + private boolean findOut() { + return cash < fullOrderPrice(); + } + private float fullOrderPrice() { + float orderPrice = order.getPrice(); + for (Map.Entry add : additions.entrySet()) { + if (add.getValue()){ + orderPrice += addPrice; + } + } + return orderPrice; + } + + public void cancel() { + if (state != STATES.OFF && state != STATES.WAIT) { + return; + } + setState(STATES.WAIT); + answer = "Your order is canceled"; + } + + public void cook() { + if (!findOut()) { + cash -= fullOrderPrice(); + setState(STATES.COOK); + finish(); + answer = "Please, your " + order; + } else + answer = "Cash isn't enough!"; + } + + private void finish() { + on(); + } + + //Change the automate state and give a describe of the state + private void setState(STATES state){ + switch (state) { + case OFF: + this.state = STATES.OFF; + prompt = "The automate is disable.\nPossible actions : ON"; + break; + case WAIT: + this.state = STATES.WAIT; + prompt = "The automate is waiting for your command.\nPossible action : OFF, COIN"; + break; + case ACCEPT: + this.state = STATES.ACCEPT; + prompt = "You money is accepted.\nPossible actions : COIN, CHOICE, CANCEL"; + break; + case CHECK: + this.state = STATES.CHECK; + prompt = "You made a choice.\nPossible actions : CANCEL, CHECK, COOK"; + break; + case COOK: + this.state = STATES.COOK; + prompt = "The cooking has started.\nPossible actions : just relax and wait"; + break; + default: + prompt = "Unknown state!"; + break; + } + } +} + +//types of automate state +enum STATES { + OFF, WAIT, ACCEPT, CHECK, COOK +} diff --git a/work/Automate/src/atm/Beverage.java b/work/Automate/src/atm/Beverage.java new file mode 100644 index 0000000..41eee26 --- /dev/null +++ b/work/Automate/src/atm/Beverage.java @@ -0,0 +1,32 @@ +package atm; + +public class Beverage { + private String name; + private int price; + + public Beverage(String name, int price) { + this.name = name; + this.price = price; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + @Override + public String toString() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void setPrice(int price) { + this.price = price; + } +} diff --git a/work/Automate/src/sample/Controller.java b/work/Automate/src/sample/Controller.java new file mode 100644 index 0000000..8a710f8 --- /dev/null +++ b/work/Automate/src/sample/Controller.java @@ -0,0 +1,145 @@ +package sample; + +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.*; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; + +import java.net.URL; +import java.util.Collections; +import java.util.Date; +import java.util.ResourceBundle; +import atm.*; + +public class Controller implements Initializable { + + ToggleGroup tgFST = new ToggleGroup(); + Automata automata = new Automata(); + + @FXML + private TextArea taPrompt; + @FXML + private TextArea taAnswer; + @FXML + private TextField tfYourCash; + @FXML + private TextField tfAddCash; + @FXML + private Button btnCoin; + @FXML + private Button btnCancel; + @FXML + private Button btnCook; + @FXML + private ToggleButton btnOnOff; + @FXML + public RadioButton rbCoffee; + @FXML + public RadioButton rbTea; + @FXML + public RadioButton rbJuice; + @FXML + public ImageView ivIcon = new ImageView(); + @FXML + private CheckBox cbSugar; + @FXML + private CheckBox cbMilk; + @FXML + private CheckBox cbIce; + @FXML + private CheckBox cbSyrup; + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + automata = new Automata(); + rbCoffee.setToggleGroup(tgFST); + rbCoffee.setSelected(true); + rbCoffee.setUserData("coffee.png"); + rbTea.setToggleGroup(tgFST); + rbTea.setSelected(false); + rbTea.setUserData("tea-cup.png"); + rbJuice.setToggleGroup(tgFST); + rbJuice.setSelected(false); + rbJuice.setUserData("orange-juice.png"); + ivIcon.setImage(new Image(getClass().getResourceAsStream(rbCoffee.getUserData().toString()))); + tfYourCash.setText(String.valueOf(automata.cash)); + taAnswer.setText(automata.answer); + taPrompt.setText(automata.prompt); + automata.order = automata.getMenu().get(0); + cbSugar.setUserData("Sugar"); + cbMilk.setUserData("Milk"); + cbIce.setUserData("Ice"); + cbSyrup.setUserData("Syrup"); + setListener(cbSugar); + setListener(cbMilk); + setListener(cbIce); + setListener(cbSyrup); + tgFST.selectedToggleProperty().addListener(new ChangeListener() { + + @Override + public void changed(ObservableValue observableValue, Toggle toggle, Toggle t1) { + Toggle item = tgFST.getSelectedToggle(); + if (item != null) { + if (item == rbCoffee) { + automata.order = automata.getMenu().get(0); + } else if (item == rbTea) { + automata.order = automata.getMenu().get(1); + } else if (item == rbJuice) { + automata.order = automata.getMenu().get(2); + } + final Image image = new Image( + getClass().getResourceAsStream(tgFST.getSelectedToggle().getUserData().toString()) + ); + ivIcon.setImage(image); + } + } + }); + } + private void refresh() { + taPrompt.setText(automata.prompt); + taAnswer.setText(automata.answer); + tfYourCash.setText(String.valueOf(automata.cash)); + } + + @FXML + public void onClickCoinBtn() { + try { + automata.coin(Float.parseFloat(tfAddCash.getText())); + tfAddCash.setText(""); + refresh(); + } catch (NumberFormatException e) { + taAnswer.setText("Invalid input data for cash!"); + } + } + @FXML + public void onClickOnOffBtn() { + if (btnOnOff.isSelected()) { + automata.on(); + } else { + automata.off(); + } + refresh(); + } + @FXML + public void onClickCookBtn() throws InterruptedException { + automata.cook(); + refresh(); + } + @FXML + public void onClickCancelBtn() { + tgFST.selectToggle(rbCoffee); + cbSugar.setSelected(false); + cbMilk.setSelected(false); + cbIce.setSelected(false); + cbSyrup.setSelected(false); + } + + private void setListener(CheckBox cb) { + cb.selectedProperty().addListener((observableValue, old_val, new_val) -> + automata.additions.replace(cb.getUserData().toString(), new_val) + ); + } +} diff --git a/work/Automate/src/sample/Main.java b/work/Automate/src/sample/Main.java new file mode 100644 index 0000000..82018fb --- /dev/null +++ b/work/Automate/src/sample/Main.java @@ -0,0 +1,23 @@ +package sample; + +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("sample.fxml")); + primaryStage.setTitle("Automata"); + primaryStage.setScene(new Scene(root)); + primaryStage.show(); + } + + + public static void main(String[] args) { + launch(args); + } +} diff --git a/work/Automate/src/sample/coffee.png b/work/Automate/src/sample/coffee.png new file mode 100644 index 0000000..b8862c3 Binary files /dev/null and b/work/Automate/src/sample/coffee.png differ diff --git a/work/Automate/src/sample/orange-juice.png b/work/Automate/src/sample/orange-juice.png new file mode 100644 index 0000000..75833e8 Binary files /dev/null and b/work/Automate/src/sample/orange-juice.png differ diff --git a/work/Automate/src/sample/sample.fxml b/work/Automate/src/sample/sample.fxml new file mode 100644 index 0000000..e537bb5 --- /dev/null +++ b/work/Automate/src/sample/sample.fxml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + +