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

Write a program that simulates the functionality of a vending machine having the

ID: 3843654 • Letter: W

Question

Write a program that simulates the functionality of a vending machine having the following characteristics:

The vending machine offers 5 products

The vending machine accepts coins, 1 dollar bills, and 5 dollar bills

The change is always given in coins, with maximum possible number of coins in each value: 25, 10, 5 or 1 cent.

The selections available for user are numbers from 1 to 5.

The user enters the money – simulate the action through a loop that ends when the user enters 0. Each coin, or paper bill will be read individually.

The user makes the selection, and the machine allows a maximum 4 other selections if the amount entered doesn’t cover the price of the item.

Once an item is delivered, the machine gives the change in coins.

There is no increment for the money during one selection.

The user can stop the selection at any time by entering 0 for the product selection.

If the user chooses to cancel the selection, the machine returns the initial amount in coins.

Display the outcome of the operation for each alternative you consider possible for the vending machine.

Make sure that the machine returns the correct change at all times.

Use appropriate selection and repetition loops to solve the problem.

Write a test program that would take at least 3 combinations of amounts entered and choices of products, and displays the results of all three trials. You can include the vending machine program as a method in the testing program.

Explanation / Answer

Items.java

public class Items {

   private int price;

   private String description;

   private String name;

   public int getPrice() {

       return price;

   }

   public void setPrice(int price) {

       this.price = price;

   }

   public String getDescription() {

       return description;

   }

   public void setDescription(String description) {

       this.description = description;

   }

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public Items() {

       // TODO Auto-generated constructor stub

   }

   public Items(int price,String description,String name) {

       this.price = price;

       this.description = description;

       this.name = name;

              

   }

}

=============================================================================
VendingMacine.java

import java.util.ArrayList;

import java.util.List;

public class VendingMacine {

   private List<Items> list = new ArrayList<Items>();

   public VendingMacine() {

       // TODO Auto-generated constructor stub

   }

   public List<Items> getList() {

       return list;

   }

   public void setList(List<Items> list) {

       this.list = list;

   }

  

}

=====================================================================================

Application.java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.List;

public class Application {

   public Application() {

       // TODO Auto-generated constructor stub

   }

   static Items i1 = new Items(20, "Tropicana", "Tropicana Apple");

   static Items i2 = new Items(20, "Tropicana", "Tropicana Orange");

   static Items i3 = new Items(20, "Tropicana", "Tropicana Anar");

   static Items i4 = new Items(20, "Tropicana", "Tropicana Guava");

   static Items i5 = new Items(20, "Tropicana", "Tropicana Jamun");

   static Items i6 = new Items(30, "Pepsi Can", "Pepsi");

   static Items i7 = new Items(30, "Thumps Up Can", "Thumps Up");

   static Items i8 = new Items(30, "Coca Cola Can", "Coca Cola");

   static Items i9 = new Items(30, "Sprite Can", "Sprite");

   static Items i10 = new Items(10, "Britianna", "Biscuits");

  

  

  

   public static void main(String args[]) throws IOException

   {

       BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

       VendingMacine v = new VendingMacine();

       List<Items> list = v.getList();

       list.add(i1);list.add(i2);

       list.add(i3);list.add(i4);

       list.add(i5);list.add(i6);

       list.add(i7);list.add(i8);

       list.add(i9);list.add(i10);

       int money =0;

      

       while (true) {

      

            System.out.println("Choose the following Operation");

      System.out.println("A : List all Items");

      System.out.println("B : Accept Money");

      System.out.println("C : Purchase an item");

      System.out.println("D : Return change");

     

      System.out.println("X : To Exit Gracefully");

      String in = input.readLine();

   char operator = in.charAt(0);

   operator = Character.toUpperCase(operator);

      switch( operator )

      {

      case 'A':

       System.out.println("Price Name Description");

       System.out.println("======================================");

       for(int i=0;i<list.size();++i)

       {

             Items i1 = list.get(i);

             System.out.println(i1.getPrice() + " " + i1.getName() + " " + i1.getDescription());

       }

     

      break;

      case 'B':

         int temp = Integer.parseInt(input.readLine());

         money = money + temp;

      break;

  

      case 'C':

       System.out.println("Enter the item name you want to purchase");

       String name = input.readLine();

       System.out.println(name);

       int i=0;

       for(;i<list.size();++i)

       {

             Items i1 = list.get(i);

             if(i1.getName().equals(name))

             {

                 break;

             }

       }

  

            if(i==list.size())

                System.out.println("Item does not exists");

            else{

                Items i1 = list.get(i);

                int price = i1.getPrice();

                if(price>money)

                {

                    System.out.println("Not enough Money, Please Add Money");

                }

                else{

                    System.out.println("You bought " + i1.getName());

                    list.remove(i);

                    money = money - price;

                }

          

            }

      break;

  

      case 'D':

       System.out.println("Return Money is " + money);

     

   break;

      case 'X':

     

   System.out.println("Program Exited Gracefully");

   System.exit(0);

      break;

      default:

      System.err.println("You typed an invalid Option");

      }

      }

   }

}

=======================================================================================

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