Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class VendingMachineLoopCode
- {
- public static void main(String[] args)
- {
- Scanner myInput = new Scanner(System.in);
- // declaration of variables
- // String userCoinInput = ""; // NOT needed in this scope. Moved.
- // char coinChoice = '0'; // NOT needed in this scope. Moved.
- double userCredit = 0.0;
- // double change = 0.0; // NOT needed in this scope (or at all). Moved (Removed)
- // double coinReturn = 0.0; // Not needed at all. Removed
- final double FINAL_CREDIT = 2.75;
- final double TOONIE = 2.00;
- final double LOONIE = 1.00;
- final double QUARTER = 0.25;
- System.out.println("Vending Machine Simulation");
- System.out.println("--------------------------");
- System.out.println("\nThis vending machine dispenses drinks for $2.75 each. Please insert only toonies, loonies, or quarters!");
- do
- {
- System.out.print("\nEnter 'T' for toonie, 'L' for loonie, 'Q' for quarter, or 'C' to cancel: ");
- // userCoinInput = myInput.nextLine();
- // Get the user answer and only accept the first character
- // coinChoice = userCoinInput.toUpperCase().charAt(0);
- // Both userCoinInput and coinChoice only need to exist in this scope
- // This would also be an acceptable place to daisy-chain into the following:
- // char coinChoice = myInput.nextLine().toUpperCase().chartAt(0);
- String userCoinInput = myInput.nextLine();
- char coinChoice = userCoinInput.toUpperCase().charAt(0);
- // These if-elseif-elseif-else are good cases for switches. Not required, just commenting.
- if(coinChoice == 'T')
- {
- userCredit += TOONIE;
- System.out.println("Toonie inserted. Credit: $" + userCredit);
- }
- else if(coinChoice == 'L')
- {
- userCredit += LOONIE;
- System.out.println("Loonie inserted. Credit: $" + userCredit);
- }
- else if (coinChoice == 'Q')
- {
- userCredit += QUARTER;
- System.out.println("Quarter inserted. Credit: $" + userCredit);
- }
- else if (coinChoice == 'C')
- {
- // coinReturn = userCredit;
- // userCredit -= userCredit;
- // System.out.println("\nPurchase cancelled. $" + coinReturn + " returned via the coin return slot. Credit: " + userCredit);
- System.out.print("\nPurchase cancelled. $" + userCredit + " returned via the coin return slot");
- userCredit = 0;
- System.out.println("Credit: " + userCredit);
- break;
- }
- else
- {
- System.out.println("That coin was rejected. Please insert the specified coins only!");
- }
- } while (userCredit < FINAL_CREDIT);
- if (coinChoice != 'C')
- {
- System.out.println("\nThank you. Your drink has been dispensed.");
- // change = userCredit - FINAL_CREDIT;
- double change = userCredit - FINAL_CREDIT; // Now I know this variable is only used here
- System.out.println("\nIf this machine gave change you'd be getting back $" + change);
- }
- else
- {
- System.out.println();
- }
- // housekeeping
- myInput.close();
- } // end main
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement