Coke Machine Project: Purposes of this Project: • To get you started using basic Java constructs, including: o The use of Scanner to get user input o Declarations, assignment statements, if and while statements. o System.out.print and println methods. General idea of the assignment: Simulate the operation of a Coke machine. This machine offers Coke, Coke Zero, and Caffeine-free Diet Coke. All drinks cost $1 (100 cents). The machine does not take dollar bills or pennies, but it does take nickels, dimes, and quarters. When enough money has been entered, a drink may be selected, and any change is returned (the specific coins are listed).Only coins in denominations of 5, 10, and 25 cents are accepted. Any other denomination (33 cents, 50 cents, etc.) is rejected and returned. More than one drink may be purchased (that is, the program doesn't quit after selling one drink).There is no way to turn off the coke machine. Example: Here is an example of the use of the program. Program output is blue, user input is brown.
Insert coin: 25 Amount entered: 25 Insert coin: 25 Amount entered: 50 Insert coin: 10 Amount entered: 60 Insert coin: 15 Rejecting 15 cent coin Amount entered: 60 Insert coin: 25 Amount entered: 85 Insert coin: 10 Amount entered: 95 Insert coin: 25 Amount entered: 120 Please make selection: 1 - Coke 2 - Coke Zero 3 - Caffeine Free Diet Coke Your choice: 2 Dispensing Coke Zero Returning dime Returning dime
Insert coin:
import java.util.Scanner;
// CST 1201
public class JavaCokeMachine {
public static void main(String[] args) {
int insertCoin;
int amountEntered = 0;
int choice;
int remainingAmount;
Scanner amountInput = new Scanner(System.in);
if (amountEntered < 100) {
System.out.println("Insert Coin:");
insertCoin = amountInput.nextInt();
if ((insertCoin == 5 || insertCoin == 10 || insertCoin == 25)) {
//taking only 5,10 and 25
amountEntered += insertCoin;
System.out.println("Amount Entered:" + amountEntered);
} else {
//Rejecting other coins, other than 5,10 and 25
System.out.println("Rejecting " + insertCoin + " cent coin");
//recursive call to make coke machine work after dispensing one choice
}
} else {
//showing menu and asking choice to user
System.out.println("please make selection:");
System.out.println("1 - coke");
System.out.println("2 - coke zero");
System.out.println("3 - caffeine free diet coke");
System.out.println("Your choice:");
choice = amountInput.nextInt();
//Based on user choice dispensing drinks
switch (choice) {
case 1:
System.out.println("Dispensing coke");
//calling dispenseRemainingCoins to dispense remaining coins to user
break;
case 2:
System.out.println("Dispensing coke zero");
break;
case 3:
System.out.println("Dispensing caffeine free diet coke");
break;
default:
System.out.println("Sorry ! Wrong Selection");
break;
}
remainingAmount = (enteredAmount - 100);
if (remainingAmount == 5 || remainingAmount == 10 || remainingAmount == 25) {
System.out.println("Returning dime");
} else if (remainingAmount == 0) {
//return nothing when remaining Amount is 0
} else {
System.out.println("Returning dime");
System.out.println("Returning dime");
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire