Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added work/Automate/AtmWithGUI.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions work/Automate/src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: sample.Main

144 changes: 144 additions & 0 deletions work/Automate/src/atm/Automata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package atm;

import java.util.*;

public class Automata {
public float cash;
private ArrayList<Beverage> menu;
public HashMap<String, Boolean> 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<Beverage> 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<String, Boolean> 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
}
32 changes: 32 additions & 0 deletions work/Automate/src/atm/Beverage.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
145 changes: 145 additions & 0 deletions work/Automate/src/sample/Controller.java
Original file line number Diff line number Diff line change
@@ -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<Toggle>() {

@Override
public void changed(ObservableValue<? extends Toggle> 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)
);
}
}
23 changes: 23 additions & 0 deletions work/Automate/src/sample/Main.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Binary file added work/Automate/src/sample/coffee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added work/Automate/src/sample/orange-juice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading