Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package machine;
- import java.util.*;
- public class CoffeeMachine {
- private int water = 400;
- private int milk = 540;
- private int coffee = 120;
- private int cup = 9;
- private int money = 550;
- private int stateOfMach = 1;
- private Scanner scan = new Scanner(System.in);
- public String inputM(){
- String input = scan.nextLine();
- String inputCh = input.toUpperCase();
- if(inputCh.equals("1")){
- inputCh="ONE";
- }
- if(inputCh.equals("2")){
- inputCh="TWO";
- }
- if(inputCh.equals("3")){
- inputCh="TREE";
- }
- State state = State.valueOf(inputCh);
- if(stateOfMach==3){
- return input;
- }else{
- if(stateOfMach==State.getNum()){
- return input;
- }
- }
- return "wrong";
- }
- enum State{
- BUY(1),
- FILL(1),
- TAKE(1),
- REMAINING(1),
- EXIT(1),
- BACK(2),
- ONE(2),
- TWO(2),
- TREE(2);
- int valState;
- State(int valState){
- this.valState=valState;
- }
- public int getNum(){
- return valState;
- }
- }
- public void outputFill () {
- System.out.println("The coffee machine has:");
- System.out.println(water + " of water");
- System.out.println(milk + " of milk");
- System.out.println(coffee + " of coffee beans");
- System.out.println(cup + " of disposable cups");
- System.out.println(money + " of money");
- }
- public void buyOption () {
- System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu: ");
- switch (inputM()) {
- case "1":
- espresso();
- break;
- case "2":
- latte();
- break;
- case "3":
- cappuccino();
- break;
- case "back":
- break;
- default:
- break;
- }
- stateOfMach = 1;
- }
- public void espresso () {
- if(water<250){
- System.out.println("Sorry, not enough water!");
- }else if(coffee<16){
- System.out.println("Sorry, not enough coffee beans!");
- }else if(cup<1){
- System.out.println("Sorry, not enough cups!");
- }else{
- water-=250;
- coffee-=16;
- cup-=1;
- money+=4;
- System.out.println("I have enough resources, making you a coffee!");
- }
- }
- public void latte () {
- if(water<350){
- System.out.println("Sorry, not enough water!");
- }else if(milk<75){
- System.out.println("Sorry, not enough milk!");
- }else if(coffee<20){
- System.out.println("Sorry, not enough coffee beans!");
- }else if(cup<1){
- System.out.println("Sorry, not enough cups!");
- }else{
- water-=350;
- milk-=75;
- coffee-=20;
- cup-=1;
- money+=7;
- System.out.println("I have enough resources, making you a coffee!");
- }
- }
- public void cappuccino () {
- if(water<200){
- System.out.println("Sorry, not enough water!");
- }else if(milk<100){
- System.out.println("Sorry, not enough milk!");
- }else if(coffee<12){
- System.out.println("Sorry, not enough coffee beans!");
- }else if(cup<1){
- System.out.println("Sorry, not enough cups!");
- }else{
- water-=200;
- milk-=100;
- coffee-=12;
- cup-=1;
- money+=6;
- System.out.println("I have enough resources, making you a coffee!");
- }
- }
- public void fillOption () {
- System.out.println("Write how many ml of water do you want to add: ");
- water+=Integer.parseInt(inputM(), 10);
- System.out.println("Write how many ml of milk do you want to add: ");
- milk+=Integer.parseInt(inputM(), 10);
- System.out.println("Write how many grams of coffee beans do you want to add: ");
- coffee+=Integer.parseInt(inputM(), 10);
- System.out.println("Write how many disposable cups of coffee do you want to add: ");
- cup+=Integer.parseInt(inputM(), 10);
- stateOfMach = 1;
- }
- public void takeOption () {
- System.out.println("I gave you "+money);
- money=0;
- }
- public void main (String[]args){
- one:
- while(true){
- System.out.println("Write action (buy, fill, take, remaining, exit): ");
- switch (inputM()) {
- case "buy":
- stateOfMach = 2;
- buyOption();
- break;
- case "fill":
- stateOfMach = 3;
- fillOption();
- break;
- case "take":
- takeOption();
- break;
- case "remaining":
- outputFill ();
- break;
- case "exit":
- outputFill ();
- break one;
- default:
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement