Skip to content
Open

B10 #121

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
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Automata</groupId>
<artifactId>Automata</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
</project>
100 changes: 100 additions & 0 deletions src/main/java/Automata.java
Original file line number Diff line number Diff line change
@@ -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 <String, String> prices = new HashMap<String, String>();
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]);
}
}
13 changes: 13 additions & 0 deletions src/main/java/main.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
54 changes: 54 additions & 0 deletions src/test/java/AutomataTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}