Advertisement
piffy

The Java Vending Machine: VENDING

Nov 25th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package vending;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Vending {
  6.  
  7. Prodotto []p;
  8.        
  9. int amount,credit;
  10. int choice ;
  11. int coin ;
  12.  
  13.     public Vending() {
  14.         amount=0;credit=0;
  15.         choice = 0;
  16.         coin = 0;
  17.         p=new Prodotto[4];
  18.         p[0]=new Prodotto(55,"LION",1);
  19.         p[1]=new Prodotto(65,"COCA",2);
  20.         p[2]=new Prodotto(50,"NOCCIOLINE",3);
  21.         p[3]=new Prodotto(45,"PATATINE",4);
  22.     }
  23.  
  24.     public void setCredit(int credit) {
  25.         this.credit = credit;
  26.     }
  27.  
  28. public void stampaMenu() {
  29.            System.out.println("**********************************");
  30.             System.out.println("* Minibar *");
  31.             for(int i=0;i<4;i++)
  32.                 System.out.println(p[i]);
  33.             System.out.println("**********************************");
  34.  
  35. }    
  36.  
  37. public void scelta() {
  38.             Coin coin;
  39.             Scanner scan=new Scanner(System.in);
  40.             System.out.println("Inserisci la tua scelta.");
  41.             choice = scan.nextInt();
  42.             switch(choice)
  43.  
  44.             {
  45.                 case 1:
  46.                 case 2:
  47.                 case 3:
  48.                 case 4:
  49.                     System.out.println("Hai scelto:"+p[choice-1]);
  50.                     amount=p[choice-1].getPrezzo();
  51.                     while (credit<amount)  {
  52.  
  53.                         System.out.println("Inserisci il valore della moneta, in centesimi");
  54.                         coin = new Coin (scan.nextInt());
  55.                         if (!coin.isValid()) coin=new Coin(0);
  56.                         credit+=coin.getValue();
  57.                     }
  58.                     break;
  59.                 default:
  60.                     System.out.println("Devi scegliere un valore tra 1 e 4. Grazie!");
  61.             }
  62.  
  63.  
  64. }
  65.  
  66. public void eroga() {
  67.             System.out.print("\007");
  68.             System.out.println("Ecco la bibita");
  69.             System.out.println("Il tuo resto รƒยจ: "+(credit-amount));
  70.  
  71.  
  72. }
  73.  
  74. public static void main(String[] args)
  75.     {
  76.         Vending v = new Vending();
  77.  
  78.  
  79.        
  80.         while (true) {
  81.             v.setCredit(0);
  82.             v.stampaMenu();
  83.             v.scelta();
  84.             v.eroga();
  85.  
  86.         }
  87.  
  88.     }
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement