Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java, a common place to buy candy is from a candy machine. A candy machine is

ID: 3567306 • Letter: I

Question

In Java, a common place to buy candy is from a candy machine. A candy machine is bought for the gym, but it is not working properly. The machine sells candies, chips, gum, and cookies. You have been asked to write a program for this candy machine so that it can be put into operation.

The program should do the following:

1. Show the customer the different products sold by the candy machine.
2. Let the customer make the selection.
3. Show the customer the cost of the item selected.
4. Accept money from the customer.
5. Release the item.

Input: The item selection and the cost of the item.
Output: The selected item.

Cash Register: Let us first discuss the properties of a cash register. The register has some cash on hand, it accepts the amount from the customer, and if the amount entered is more than the cost of the item, then -if possible- it returns the change. For simplicity, we assume that the user enters the exact amount for the product. The cash register should also be able to show the candy machine's owner the amount of money in the register at any given time.

Dispenser: The dispenser releases the selected item if it is not empty. It should show the number of items in the dispenser and the of the item.

Main Program:
When the program executes, it must do the following:
1. Show the different products sold by the candy machine.
2. Show how to selected a particular product.
3. Show how to terminate the program.

Explanation / Answer

import java.util.Scanner;


public class CandyMachine {
private static int choice;
private static int yAmount;
private static int rSum=10;
private static int item[]={10,10,10,10};
private static int password=1234;

public static void print(){
   System.out.println("Items Available:");
   System.out.println("1.OrangeCandy");
   System.out.println("2.chips");
   System.out.println("3.Gum");
   System.out.println("4.Cookie");
   System.out.println("5.Cash Register Balance:");
   System.out.println("6.Exit");
   System.out.println("Enter your desired choice in terms of intergers(Example for candy enter 1)");
   Scanner s=new Scanner(System.in);
   choice=s.nextInt();
   System.out.println("Enter your amount in terms of dollars(example $1,$2):");
   yAmount=s.nextInt();
   cashRegister();
   }

public static void cashRegister(){
   switch(choice){
   case 1:
       if(yAmount<10){
           System.out.println("Insufficient amount to buy that item please try again");
       }else if(yAmount==10){
           dispense(1,0);
       }else if(yAmount>10){
           dispense(1,yAmount-10);
       }
       break;
   case 2:
       if(yAmount<20){
           System.out.println("Insufficient amount to buy that item please try again");
       }else if(yAmount==20){
           dispense(2,0);
       }else if(yAmount>20){
           dispense(2,yAmount-20);
       }
       break;
   case 3:
       if(yAmount<30){
           System.out.println("Insufficient amount to buy that item please try again");
       }else if(yAmount==30){
           dispense(3,0);
       }else if(yAmount>30){
           dispense(1,yAmount-30);
       }
       break;
   case 4:
       if(yAmount<5){
           System.out.println("Insufficient amount to buy that item please try again");
       }else if(yAmount==5){
           dispense(4,0);
       }else if(yAmount>5){
           dispense(1,yAmount-5);
       }
       break;
   case 5:
       registerC();
       break;
   default :
       System.out.println("Byee!!!!!");
       break;
              
   }
}
   private static void registerC() {
       System.out.println("Enter the four digit password for the balance:");
       Scanner s=new Scanner(System.in);
       int pass = s.nextInt();
       if(pass==password){
           System.out.println("Total Balance:"+rSum);
       }
       else{
           System.out.println("wrong password!!Byeeee!!!");
       }
  
}
   private static void dispense(int i,int j) {
   switch(i){
   case 1:
       if(item[0]<1){
           System.out.println("Sorry this item is out of stock!!!");
       }else{
           item[0]=item[0]-1;
           System.out.println("Items left of Chips:"+item[0]);
           System.out.println("Dispensing Orange Candy collect it!!!");
           System.out.println("Collect your Balance $"+j);
       }
       break;
   case 2:
  
       if(item[1]<1){
           System.out.println("Sorry this item is out of stock!!!");
       }else{
           item[1]=item[1]-1;
           System.out.println("Items left of Chips:"+item[1]);
           System.out.println("Dispensing Chips collect it!!!");
           System.out.println("Collect your Balance $"+j);
       }
       break;
   case 3:
       if(item[2]<1){
           System.out.println("Sorry this item is out of stock!!!");
       }else{
           item[2]=item[2]-1;
           System.out.println("Items left of Chips:"+item[2]);
           System.out.println("Dispensing Gum collect it!!!");
           System.out.println("Collect your Balance $"+j);
       }
       break;
   case 4:
       if(item[3]<1){
           System.out.println("Sorry this item is out of stock!!!");
       }else{
           item[3]=item[3]-1;
           System.out.println("Items left of Chips:"+item[0]);
           System.out.println("Dispensing Cookie collect it!!!");
           System.out.println("Collect your Balance $"+j);
       }
       break;
   default :
       break;
   }
  
}
   public static void main(String[] args) {
       int n=1;
       Scanner s=new Scanner(System.in);
       while(n==1)
       {
           print();
           System.out.println("Do you want to continue:1.yes 2.no");
           n=s.nextInt();
       }
      
      

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote