Advertisement
Guest User

Untitled

a guest
May 25th, 2019
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. package machine;
  2.  
  3. import java.util.*;
  4.  
  5. public class CoffeeMachine {
  6.     private int water = 400;
  7.     private int milk = 540;
  8.     private int coffee = 120;
  9.     private int cup = 9;
  10.     private int money = 550;
  11.     private int stateOfMach = 1;
  12.     private Scanner scan = new Scanner(System.in);
  13.  
  14.  
  15.         public String inputM(){
  16.             String input = scan.nextLine();
  17.             String inputCh = input.toUpperCase();
  18.             if(inputCh.equals("1")){
  19.                 inputCh="ONE";
  20.             }
  21.             if(inputCh.equals("2")){
  22.                 inputCh="TWO";
  23.             }
  24.             if(inputCh.equals("3")){
  25.                 inputCh="TREE";
  26.             }
  27.             State state = State.valueOf(inputCh);
  28.             if(stateOfMach==3){
  29.                 return input;
  30.             }else{
  31.                 if(stateOfMach==State.getNum()){
  32.                     return input;
  33.                 }
  34.             }
  35.             return "wrong";
  36.         }
  37.  
  38.         enum State{
  39.             BUY(1),
  40.             FILL(1),
  41.             TAKE(1),
  42.             REMAINING(1),
  43.             EXIT(1),
  44.             BACK(2),
  45.             ONE(2),
  46.             TWO(2),
  47.             TREE(2);
  48.  
  49.             int valState;
  50.  
  51.             State(int valState){
  52.                 this.valState=valState;
  53.             }
  54.  
  55.             public int getNum(){
  56.                 return valState;
  57.             }
  58.            
  59.         }
  60.        
  61.         public void outputFill () {
  62.             System.out.println("The coffee machine has:");
  63.             System.out.println(water + " of water");
  64.             System.out.println(milk + " of milk");
  65.             System.out.println(coffee + " of coffee beans");
  66.             System.out.println(cup + " of disposable cups");
  67.             System.out.println(money + " of money");
  68.         }
  69.  
  70.         public void buyOption () {
  71.             System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu: ");
  72.             switch (inputM()) {
  73.                 case "1":
  74.                     espresso();
  75.                     break;
  76.                 case "2":
  77.                     latte();
  78.                     break;
  79.                 case "3":
  80.                     cappuccino();
  81.                     break;
  82.                 case "back":
  83.                     break;
  84.                 default:
  85.                     break;
  86.             }
  87.             stateOfMach = 1;
  88.         }
  89.  
  90.         public void espresso () {
  91.             if(water<250){
  92.             System.out.println("Sorry, not enough water!");
  93.             }else if(coffee<16){
  94.             System.out.println("Sorry, not enough coffee beans!");  
  95.             }else if(cup<1){
  96.             System.out.println("Sorry, not enough cups!");  
  97.             }else{
  98.                 water-=250;
  99.                 coffee-=16;
  100.                 cup-=1;
  101.                 money+=4;
  102.                 System.out.println("I have enough resources, making you a coffee!");
  103.             }
  104.         }
  105.  
  106.         public void latte () {
  107.             if(water<350){
  108.             System.out.println("Sorry, not enough water!");
  109.             }else if(milk<75){
  110.             System.out.println("Sorry, not enough milk!");  
  111.             }else if(coffee<20){
  112.             System.out.println("Sorry, not enough coffee beans!");  
  113.             }else if(cup<1){
  114.             System.out.println("Sorry, not enough cups!");  
  115.             }else{
  116.                 water-=350;
  117.                 milk-=75;
  118.                 coffee-=20;
  119.                 cup-=1;
  120.                 money+=7;
  121.                 System.out.println("I have enough resources, making you a coffee!");
  122.             }
  123.         }
  124.  
  125.         public void cappuccino () {
  126.             if(water<200){
  127.             System.out.println("Sorry, not enough water!");
  128.             }else if(milk<100){
  129.             System.out.println("Sorry, not enough milk!");  
  130.             }else if(coffee<12){
  131.             System.out.println("Sorry, not enough coffee beans!");  
  132.             }else if(cup<1){
  133.             System.out.println("Sorry, not enough cups!");  
  134.             }else{
  135.                 water-=200;
  136.                 milk-=100;
  137.                 coffee-=12;
  138.                 cup-=1;
  139.                 money+=6;
  140.                 System.out.println("I have enough resources, making you a coffee!");
  141.             }
  142.         }
  143.  
  144.         public void fillOption () {
  145.             System.out.println("Write how many ml of water do you want to add: ");
  146.             water+=Integer.parseInt(inputM(), 10);
  147.             System.out.println("Write how many ml of milk do you want to add: ");
  148.             milk+=Integer.parseInt(inputM(), 10);
  149.             System.out.println("Write how many grams of coffee beans do you want to add: ");
  150.             coffee+=Integer.parseInt(inputM(), 10);
  151.             System.out.println("Write how many disposable cups of coffee do you want to add: ");
  152.             cup+=Integer.parseInt(inputM(), 10);
  153.             stateOfMach = 1;
  154.         }
  155.  
  156.         public void takeOption () {
  157.             System.out.println("I gave you "+money);
  158.             money=0;
  159.         }
  160.  
  161.         public void main (String[]args){
  162.                 one:
  163.                 while(true){
  164.                 System.out.println("Write action (buy, fill, take, remaining, exit): ");
  165.                 switch (inputM()) {
  166.                     case "buy":
  167.                         stateOfMach = 2;
  168.                         buyOption();
  169.                         break;
  170.                     case "fill":
  171.                         stateOfMach = 3;
  172.                         fillOption();
  173.                         break;
  174.                     case "take":
  175.                         takeOption();
  176.                         break;
  177.                     case "remaining":
  178.                         outputFill ();
  179.                         break;
  180.                     case "exit":
  181.                         outputFill ();
  182.                         break one;    
  183.                     default:
  184.                         break;
  185.                 }
  186.             }
  187.         }
  188.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement