Skip to content
Open

b35 #30

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
1 change: 0 additions & 1 deletion .travis.yml

This file was deleted.

Binary file added ClockJFX.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions 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

Binary file added automata.jar
Binary file not shown.
143 changes: 143 additions & 0 deletions automata/src/sample/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import sample.automata.Automata;
import sample.automata.Product;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class Controller implements Initializable {

@FXML
private Button btnOn;
@FXML
private Button btnOff;
@FXML
private Button btnCancel;
@FXML
private Button btnCoin;
@FXML
private Button btnMenu1;
@FXML
private Button btnMenu2;
@FXML
private Button btnMenu3;
@FXML
private Button btnMenu4;
@FXML
private Button btnMenu5;
@FXML
private Button btnCoin1;
@FXML
private Button btnCoin2;
@FXML
private Button btnCoin3;

@FXML
private TextArea taState;

@FXML
private TextField tfCash;
@FXML
private TextField tfChange;

@FXML
private TableView<Product> tblvMenu;
@FXML
private TableColumn<Product, Integer> tcProdNumber;
@FXML
private TableColumn<Product, String> tcProdName;
@FXML
private TableColumn<Product, Double> tcProdPrice;

@FXML
private ProgressBar pbCookingProgress;

private Automata automata;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
automata = new Automata();
automata.getMenufromJSON();
getAutomataState();
tcProdNumber.setCellValueFactory(new PropertyValueFactory<Product,Integer>("prodNumber"));
tcProdName.setCellValueFactory(new PropertyValueFactory<Product,String>("prodName"));
tcProdPrice.setCellValueFactory(new PropertyValueFactory<Product,Double>("prodPrice"));
}

private void getAutomataState(){
taState.textProperty().unbind();
taState.setText(automata.getState().toString());
tfCash.setText(automata.getStrCash());
tfChange.setText(automata.getStrChange());
}

private void setMenu(List<Product> products){
ObservableList<Product> olProducts = FXCollections.observableArrayList(products);
tblvMenu.setItems(olProducts);
}

public void coinClick(int coinValue){
automata.coin(coinValue);
getAutomataState();
}

public void setBtnMenu(int prodNumber){
automata.choice(prodNumber);
pbCookingProgress.progressProperty().bind(automata.getProcess().progressProperty());
taState.textProperty().bind(automata.getProcess().messageProperty());
tfCash.setText(automata.getStrCash());
tfChange.setText(automata.getStrChange());
}

public void onClick(){
automata.on();
getAutomataState();
setMenu(automata.getProducts());
}

public void offClick(){
automata.off();
getAutomataState();
setMenu(new ArrayList<Product>());
}

public void coin1Click(){
coinClick(5);
}
public void coin2Click(){
coinClick(10);
}
public void coin3Click(){
coinClick(50);
}

public void cancelClick(){
automata.cancel();
getAutomataState();
}

public void setBtnMenu1(){
setBtnMenu(0);
}
public void setBtnMenu2(){
setBtnMenu(1);
}
public void setBtnMenu3(){
setBtnMenu(2);
}
public void setBtnMenu4(){
setBtnMenu(3);
}
public void setBtnMenu5(){
setBtnMenu(4);
}
}
31 changes: 31 additions & 0 deletions automata/src/sample/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.File;
import java.io.InputStream;
import java.net.URL;

public class Main extends Application {
protected static String jsnFdir;

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Automata coffee");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}


public static void main(String[] args) {
launch(args);
}



}
192 changes: 192 additions & 0 deletions automata/src/sample/automata/Automata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package sample.automata;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import sample.processes.CookingProcess;
import sample.processes.NeedCoinProcess;
import sample.processes.Process;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class Automata {
private STATES state;
private double cash;
private double change;
private String strCash;
private String strChange;
private ArrayList<Product> products;
private int prodNumber;
private Process process;
private String jsonFileDirection;

//constructor
public Automata() {
this.state = STATES.OFF;
this.cash = 0.0;
this.change = 0.0;
this.strCash = "";
this.strChange = "";
this.products = new ArrayList<>();
this.process = new CookingProcess();
this.jsonFileDirection = jsonFileDirection;
}

//getters
public ArrayList<Product> getProducts(){
return this.products;
}
public STATES getState(){
return this.state;
}
public String getStrCash() { return this.strCash; }
public String getStrChange(){ return this.strChange; }
public Process getProcess(){ return this.process; }

//setters
public void setState(STATES state){ this.state = state; }
public void setCash(double value){
this.cash += value;
this.strCash = Double.toString(this.cash);
}
public void setChange(double value){
this.change += value;
this.strChange = Double.toString(this.change);
}
public void setJsonFileDirection(String jsonFileDirection){
this.jsonFileDirection = jsonFileDirection;
}

//methods
private double getProdPrice(){
return products.get(this.prodNumber).getProdPrice();
}

public void on(){
if (this.state == STATES.OFF) {
setState(STATES.WAIT);
setCash(-this.cash);
setChange(-this.change);
}
}

public void off(){
if (this.cash == 0){
setState(STATES.OFF);
this.strCash = "";
this.strChange = "";
}
}

public void coin(double coinValue){
if (this.state == STATES.WAIT || this.state == STATES.NEEDCOIN){
setState(STATES.ACCEPT);
setCash(coinValue);
}
else if (this.state == STATES.ACCEPT){
setCash(coinValue);
}
else{
setChange(coinValue);
}
}

public void cancel(){
if (this.state == STATES.ACCEPT || this.state == STATES.CHECK || this.state == STATES.NEEDCOIN){
setState(STATES.WAIT);
setChange(this.cash);
setCash(-this.cash);
}
}

public void choice(int prodNumber){
if (this.state == STATES.ACCEPT || this.state == STATES.WAIT) {
setState(STATES.CHECK);
setChange(-this.change);
this.prodNumber = prodNumber;
this.check();
}
}

private void check(){
if (this.cash < this.getProdPrice())
this.needCoin();
else
this.cook();
}

private void needCoin(){
setState(STATES.NEEDCOIN);
this.process = new NeedCoinProcess();
Thread thread = new Thread(this.process);
thread.setDaemon(true);
thread.start();
}

private void cook(){
setState(STATES.COOK);
this.process = new CookingProcess();
Thread thread = new Thread(this.process);
thread.setDaemon(true);
thread.start();
this.finish();
}

private void finish(){
if (this.state == STATES.COOK) {
setState(STATES.WAIT);
setChange(this.cash - this.getProdPrice());
setCash(-this.cash);
}
}

public void getMenufromJSON() {
//File jsfile = new File("./products_and_prices.json");
File jsfile = this.getResourceAsFile();
try {
String content = FileUtils.readFileToString(jsfile, "utf-8");
JSONObject jsList = new JSONObject(content);
JSONArray jsProducts = (JSONArray) jsList.get("products");
for (Object obj : jsProducts) {
int prodNumber = Integer.parseInt(((JSONObject) obj).get("number").toString());
String prodName = ((JSONObject) obj).get("name").toString();
double prodPrice = Double.parseDouble(((JSONObject) obj).get("price").toString());
Product product = new Product(prodNumber, prodName, prodPrice);
this.products.add(product);
}
}
catch (IOException e) {
}
}

public File getResourceAsFile() {
try {
InputStream inStream = ClassLoader.getSystemClassLoader()
.getResourceAsStream("sample/products_and_prices.json");
if (inStream == null) {
return null;
}
File tempFile = File.createTempFile(String.valueOf(inStream.hashCode()), ".tmp");
tempFile.deleteOnExit();

try (FileOutputStream outStream = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
}
return tempFile;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
17 changes: 17 additions & 0 deletions automata/src/sample/automata/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sample.automata;

public class Product {
private int prodNumber;
private String prodName;
private double prodPrice;

public Product(int prodNumber, String prodName, double prodPrice) {
this.prodNumber = prodNumber;
this.prodName = prodName;
this.prodPrice = prodPrice;
}

public int getProdNumber() {return prodNumber;}
public String getProdName() {return prodName;}
public double getProdPrice() {return prodPrice;}
}
Loading