diff --git a/AutomataLatest.jar b/AutomataLatest.jar new file mode 100644 index 0000000..128ba2d Binary files /dev/null and b/AutomataLatest.jar differ diff --git a/ClockLatestLatest.jar b/ClockLatestLatest.jar new file mode 100644 index 0000000..8d24d27 Binary files /dev/null and b/ClockLatestLatest.jar differ diff --git a/pomAutomata.xml b/pomAutomata.xml new file mode 100644 index 0000000..8c34cb9 --- /dev/null +++ b/pomAutomata.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + org.example + MavenDirectory + 1.0-SNAPSHOT + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + org.openjfx + javafx-graphics + 11.0.2 + + + org.openjfx + javafx-fxml + 15-ea+6 + + + + \ No newline at end of file diff --git a/srcAutomata/main/java/Automata/Automata.java b/srcAutomata/main/java/Automata/Automata.java new file mode 100644 index 0000000..0e85e5f --- /dev/null +++ b/srcAutomata/main/java/Automata/Automata.java @@ -0,0 +1,206 @@ +package Automata; +import java.io.File; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.io.FileReader; +import java.io.BufferedReader; +import org.json.simple.parser.JSONParser; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +public class Automata { + enum STATES{Off,Wait,Accept,Check,Cook}; + private int cash=0; + private int drinkIndex; + private ArrayList menu; + private ArrayList prices; + private STATES state=STATES.Off; + private Boolean checkResult=false; + private String nameOfMenuFile; + private File fileMenu=null; + private Exception exception=null; + public Automata(String nameOfMenuFile) + { + this.nameOfMenuFile=nameOfMenuFile; + } + private void loadMenuFromTXT() + { + try + { + FileReader fileReader = new FileReader(fileMenu); + BufferedReader bufferedReader = new BufferedReader(fileReader); + String line; + menu=new ArrayList(); + prices=new ArrayList(); + while ((line = bufferedReader.readLine()) != null) + { + String[] splitedLine = line.split(" "); + String name = splitedLine[0]; + menu.add(name); + int price = Integer.parseInt(splitedLine[1]); + prices.add(price); + } + if(menu.size()==0) + throw new Exception("File is empty"); + if(prices.size()!=menu.size()) + throw new Exception("File is incomplete"); + + } + catch (Exception ex) + { + exception=ex; + } + } + private void loadMenuFromJson() + { + menu=new ArrayList<>(); + prices=new ArrayList<>(); + try { + JSONParser parser = new JSONParser(); + JSONObject jsonObject=(JSONObject) parser.parse(new FileReader(fileMenu)); + JSONArray jsonArray =(JSONArray) jsonObject.get("Drinks"); + for(int i=0;i getMenu() + { + if(state!=STATES.Off) + { + ArrayList menuList = new ArrayList<>(); + for (int i = 0; i < menu.size(); i++) { + menuList.add(menu.get(i)+" : "+prices.get(i).toString()); + } + + return menuList; + } + return null; + } + public STATES getState() + { + return state; + } + public void choice(String name) + { + checkResult=false; + for(int i=0;i-1) + { + state = STATES.Cook; + cash -= prices.get(drinkIndex); + return new Drink(menu.get(drinkIndex)); + } + else + return null; + } + public void finish() + { + state=STATES.Wait; + } + public int returnMoney() + { + int money=cash; + cash=0; + return money; + } + public int getCash() { + return cash; + } + public void choice(int index) + { + if(check(index)) + drinkIndex=index; + else + drinkIndex=-1; + } +} diff --git a/srcAutomata/main/java/Automata/Drink.java b/srcAutomata/main/java/Automata/Drink.java new file mode 100644 index 0000000..1c9713a --- /dev/null +++ b/srcAutomata/main/java/Automata/Drink.java @@ -0,0 +1,15 @@ +package Automata; + +public class Drink +{ + String name; + public Drink(String name) + { + this.name=name; + } + public String getName() + { + return name; + } +} + diff --git a/srcAutomata/main/java/META-INF/MANIFEST.MF b/srcAutomata/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000..440fa71 --- /dev/null +++ b/srcAutomata/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: sample.Main + diff --git a/srcAutomata/main/java/sample/Controller.java b/srcAutomata/main/java/sample/Controller.java new file mode 100644 index 0000000..632edf7 --- /dev/null +++ b/srcAutomata/main/java/sample/Controller.java @@ -0,0 +1,151 @@ +package sample; + +import Automata.Automata; +import Automata.Drink; + +import javafx.application.Platform; +import javafx.beans.InvalidationListener; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.concurrent.Task; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.*; + +import java.net.URL; +import java.util.ResourceBundle; + +public class Controller implements Initializable { + private Automata automata; + @FXML + private Button buttonOn; + @FXML + private Button buttonOff; + @FXML + private Label labelState; + @FXML + private Label labelCash; + @FXML + private TextField textFieldMoney; + @FXML + private Button buttonPutMoney; + @FXML + private Button buttonBuy; + @FXML + private Button buttonCancel; + @FXML + private Button buttonReturnMoney; + @FXML + private ComboBox comboBoxMenu; + @FXML + private ProgressBar progressBar; + SimpleStringProperty stringPropertyState; + private boolean isBusy=false; + @Override + public void initialize(URL location, ResourceBundle resources) + { + stringPropertyState=new SimpleStringProperty(); + stringPropertyState.setValue("State : Off"); + labelState.textProperty().bind(stringPropertyState); + } + @FXML + public void clickOn() + { + if(automata==null) + { + automata = new Automata("Menu.json"); + automata.on(); + comboBoxMenu.getItems().addAll(automata.getMenu()); + } + else + automata.on(); + printState(); + } + @FXML + public void clickOff() + { + if(automata!=null) { + automata.off(); + printState(); + } + } + @FXML + public void clickPutMoney() + { + try + { + automata.coin(Integer.parseInt(textFieldMoney.getText())); + printState(); + } + catch (Exception ex) + { + textFieldMoney.setText(""); + } + } + @FXML + public void clickBuy() throws Exception + { + if(automata!=null) { + automata.choice(comboBoxMenu.getSelectionModel().getSelectedIndex()); + printState(); + Task task = new Task() { + @Override + protected Void call() { + try { + Platform.runLater(()->isBusy=true); + waitSeconds(2000); + Platform.runLater(() -> automata.cook()); + waitSeconds(6000); + Platform.runLater(() -> automata.finish()); + printStats(1); + Platform.runLater(()->isBusy=false); + } catch (Exception ex) { + + } + return null; + } + + private void printStats(float progress) { + Platform.runLater(() -> printState()); + Platform.runLater(() -> progressBar.setProgress(progress)); + } + + private void waitSeconds(int seconds) throws Exception { + for (int i = 1; i <= seconds; i++) { + Thread.sleep(1); + printStats((float) i / seconds); + } + } + }; + + Thread th = new Thread(task); + th.setDaemon(true); + th.start(); + } + } + @FXML + public void clickCancel() + { + if(automata!=null && !isBusy) + automata.cancel(); + printState(); + } + @FXML + public void clickReturnMoney() + { + if(automata!=null) { + automata.returnMoney(); + printState(); + } + } + private void printState() + { + if(automata!=null) + { + stringPropertyState.setValue("State : "+automata.getState()); + labelCash.setText("Cash : "+automata.getCash()); + } + } +} diff --git a/srcAutomata/main/java/sample/Main.java b/srcAutomata/main/java/sample/Main.java new file mode 100644 index 0000000..21c4362 --- /dev/null +++ b/srcAutomata/main/java/sample/Main.java @@ -0,0 +1,24 @@ +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("Hello World"); + primaryStage.setScene(new Scene(root)); + primaryStage.show(); + } + + + public static void main(String[] args) { + launch(args); + } +} diff --git a/srcAutomata/main/resources/META-INF/MANIFEST.MF b/srcAutomata/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..440fa71 --- /dev/null +++ b/srcAutomata/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: sample.Main + diff --git a/srcAutomata/main/resources/Menu.json b/srcAutomata/main/resources/Menu.json new file mode 100644 index 0000000..ef0301c --- /dev/null +++ b/srcAutomata/main/resources/Menu.json @@ -0,0 +1,9 @@ +{ + "Drinks" : + [ + {"Name" : "Tea","Price" : 10}, + {"Name" : "Coffee","Price" : 15}, + {"Name" : "Cappucino","Price" : 20}, + {"Name" : "Water","Price" : 10} + ] +} \ No newline at end of file diff --git a/srcAutomata/main/resources/Menu.txt b/srcAutomata/main/resources/Menu.txt new file mode 100644 index 0000000..598cc17 --- /dev/null +++ b/srcAutomata/main/resources/Menu.txt @@ -0,0 +1 @@ +Tea 5 \ No newline at end of file diff --git a/srcAutomata/main/resources/sample.fxml b/srcAutomata/main/resources/sample.fxml new file mode 100644 index 0000000..819f410 --- /dev/null +++ b/srcAutomata/main/resources/sample.fxml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +