Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class VendingMachineLoopCode
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner myInput = new Scanner(System.in);
  8.        
  9.         // declaration of variables
  10.         // String userCoinInput = ""; // NOT needed in this scope. Moved.
  11.         // char coinChoice = '0'; // NOT needed in this scope. Moved.
  12.         double userCredit = 0.0;
  13.         // double change = 0.0; // NOT needed in this scope (or at all). Moved (Removed)
  14.         // double coinReturn = 0.0; // Not needed at all. Removed
  15.  
  16.         final double FINAL_CREDIT = 2.75;
  17.         final double TOONIE = 2.00;
  18.         final double LOONIE = 1.00;
  19.         final double QUARTER = 0.25;
  20.        
  21.         System.out.println("Vending Machine Simulation");
  22.         System.out.println("--------------------------");
  23.  
  24.         System.out.println("\nThis vending machine dispenses drinks for $2.75 each. Please insert only toonies, loonies, or quarters!");
  25.        
  26.         do
  27.         {
  28.             System.out.print("\nEnter 'T' for toonie, 'L' for loonie, 'Q' for quarter, or 'C' to cancel: ");
  29.            
  30.             // userCoinInput = myInput.nextLine();
  31.             // Get the user answer and only accept the first character
  32.             // coinChoice = userCoinInput.toUpperCase().charAt(0);
  33.  
  34.             // Both userCoinInput and coinChoice only need to exist in this scope
  35.             // This would also be an acceptable place to daisy-chain into the following:
  36.             // char coinChoice = myInput.nextLine().toUpperCase().chartAt(0);
  37.  
  38.             String userCoinInput = myInput.nextLine();
  39.             char coinChoice = userCoinInput.toUpperCase().charAt(0);
  40.  
  41.             // These if-elseif-elseif-else are good cases for switches. Not required, just commenting.
  42.  
  43.             if(coinChoice == 'T')
  44.             {
  45.                 userCredit += TOONIE;
  46.                 System.out.println("Toonie inserted. Credit: $" + userCredit);
  47.             }
  48.             else if(coinChoice == 'L')
  49.             {
  50.                 userCredit += LOONIE;
  51.                 System.out.println("Loonie inserted. Credit: $" + userCredit);
  52.             }
  53.             else if (coinChoice == 'Q')
  54.             {
  55.                 userCredit += QUARTER;
  56.                 System.out.println("Quarter inserted. Credit: $" + userCredit);
  57.             }
  58.             else if (coinChoice == 'C')
  59.             {
  60.                 // coinReturn = userCredit;
  61.                 // userCredit -= userCredit;
  62.                 // System.out.println("\nPurchase cancelled. $" + coinReturn + " returned via the coin return slot. Credit: " + userCredit);
  63.                
  64.                 System.out.print("\nPurchase cancelled. $" + userCredit + " returned via the coin return slot");
  65.                 userCredit = 0;
  66.                 System.out.println("Credit: " + userCredit);
  67.  
  68.                 break;
  69.             }
  70.             else
  71.             {
  72.                 System.out.println("That coin was rejected. Please insert the specified coins only!");
  73.             }
  74.            
  75.         } while (userCredit < FINAL_CREDIT);
  76.        
  77.         if (coinChoice != 'C')
  78.         {
  79.             System.out.println("\nThank you. Your drink has been dispensed.");
  80.        
  81.             // change = userCredit - FINAL_CREDIT;
  82.             double change = userCredit - FINAL_CREDIT; // Now I know this variable is only used here
  83.             System.out.println("\nIf this machine gave change you'd be getting back $" + change);  
  84.         }  
  85.         else
  86.         {
  87.             System.out.println();
  88.         }
  89.        
  90.         // housekeeping
  91.         myInput.close();
  92.        
  93.     }       // end main
  94. }       // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement