From c51a7cc167489cb68b5e83122d1c632aec92e920 Mon Sep 17 00:00:00 2001 From: Bear346 <57682132+Bear346@users.noreply.github.com> Date: Mon, 9 Nov 2020 13:05:24 +0300 Subject: [PATCH 1/4] Create pom.xml --- pom.xml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 pom.xml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..29c722a --- /dev/null +++ b/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + Automata + Automata + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + junit + junit + RELEASE + + + From df991688eb5651056ddc21772e16e901cae1bab3 Mon Sep 17 00:00:00 2001 From: Bear346 <57682132+Bear346@users.noreply.github.com> Date: Mon, 9 Nov 2020 13:06:16 +0300 Subject: [PATCH 2/4] Create Automata.java --- src/main/java/Automata.java | 100 ++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/main/java/Automata.java diff --git a/src/main/java/Automata.java b/src/main/java/Automata.java new file mode 100644 index 0000000..3a37d65 --- /dev/null +++ b/src/main/java/Automata.java @@ -0,0 +1,100 @@ +import java.util.HashMap; +public class Automata { + + enum STATES { + OFF, WAIT, ACCEPT, CHECK, COOK + } + + private int cash; + private STATES state; + + static HashMap prices = new HashMap(); + static { + prices.put("1", "Coffee with milk,30"); + prices.put("2", "Green Tea,40"); + prices.put("3", "Cappuchino,50"); + prices.put("4", "Latthe,30"); + prices.put("5", "Orange Juice,40"); + } + + public Automata() { + state = STATES.OFF; + this.cash = 0; + } + + public STATES getState() { + return state; + } + + public void setState(STATES state) { + this.state = state; + } + + public int getCash() { + return cash; + } + + public void setCash(int cash) { + this.cash = cash; + } + + public void on() { + if (state == STATES.OFF) { + state = STATES.WAIT; + } + } + + public void off() { + state = STATES.OFF; + } + + public void coin(int amount) { + if (state == STATES.WAIT || state == STATES.ACCEPT) { + state = STATES.ACCEPT; + cash +=amount; + } + } + public void choice(String menuNum) { + if (state != STATES.OFF) { + if (check(menuNum)) { + cook(menuNum); + } else { + cancel(); + } + } + } + + private boolean check(String menuNum) { + state = STATES.CHECK; + return Integer.parseInt(prices.get(menuNum).split(",")[1]) <= cash; + } + + public void printState() { + System.out.println("Current state is " + state); + } + + public void printMenu(){ + prices.forEach((k,v) -> System.out.println(" "+k+" "+v)); + } + + private void cook (String menuNum){ + state = STATES.COOK; + finish(menuNum); + } + + public int cancel() { + int sum = cash; + if (state == STATES.ACCEPT || state == STATES.CHECK ) { + cash = 0; + state = STATES.WAIT; + } else { + sum = -1; + } + return sum; + } + + private void finish(String menuNum){ + state = STATES.WAIT; + cash -= Integer.parseInt(prices.get(menuNum).split(",")[1]); + } +} From 08a32b4b565fe197636faca80c7575d0099c1bb3 Mon Sep 17 00:00:00 2001 From: Bear346 <57682132+Bear346@users.noreply.github.com> Date: Mon, 9 Nov 2020 13:06:52 +0300 Subject: [PATCH 3/4] Create main.java --- src/main/java/main.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/java/main.java diff --git a/src/main/java/main.java b/src/main/java/main.java new file mode 100644 index 0000000..2d0f536 --- /dev/null +++ b/src/main/java/main.java @@ -0,0 +1,13 @@ +class main { + public static void main(String[] args) { + Automata automata = new Automata(); + automata.on(); + automata.printState(); + automata.printMenu(); + automata.coin(50); + automata.printState(); + automata.choice("5"); + automata.printState(); + automata.off(); + } +} From fd1b10508addb95cd26e47f11c94330dcc044832 Mon Sep 17 00:00:00 2001 From: Bear346 <57682132+Bear346@users.noreply.github.com> Date: Mon, 9 Nov 2020 13:07:40 +0300 Subject: [PATCH 4/4] Create AutomataTest.java --- src/test/java/AutomataTest.java | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/test/java/AutomataTest.java diff --git a/src/test/java/AutomataTest.java b/src/test/java/AutomataTest.java new file mode 100644 index 0000000..1b70b4e --- /dev/null +++ b/src/test/java/AutomataTest.java @@ -0,0 +1,54 @@ +import static org.junit.Assert.*; + +import static org.junit.Assert.*; + +public class AutomataTest { + + @org.junit.Test + public void on() { + Automata a = new Automata(); + a.on(); + assertEquals(Automata.STATES.WAIT, a.getState()); + assertEquals(0, a.getCash()); + } + + @org.junit.Test + public void off() { + Automata a = new Automata(); + a.on(); + a.off(); + assertEquals(Automata.STATES.OFF, a.getState()); + } + + @org.junit.Test + public void coin() { + Automata a = new Automata(); + a.on(); + a.coin(20); + a.coin(20); + assertEquals(40, a.getCash()); + } + + @org.junit.Test + public void choice() { + Automata a = new Automata(); + a.on(); + a.coin(50); + assertEquals(50, a.getCash()); + a.choice("3"); + assertEquals(0, a.getCash()); + a.coin(40); + a.choice("4"); + assertEquals(10, a.getCash()); + } + + @org.junit.Test + public void cancel() { + Automata testAutomata = new Automata(); + testAutomata.on(); + testAutomata.coin(50); + assertEquals(50, testAutomata.getCash()); + testAutomata.cancel(); + assertEquals(0, testAutomata.getCash()); + } +}