Question

Vending Machine In this assignment, you are to vend six different snacks. The Vending Machine has...

Vending Machine

In this assignment, you are to vend six different snacks. The Vending Machine has a capacity of storing up to 100 packs of snacks. Therefore, emulating the real vending machine , it is appropriate to allocate a fixed number of packages to each type of snacks. For example, you can stock 20 packs of Chips, 20 packs of M&Ms, 16 packs of Popcorn, 16 packs of Snickers, 14 packs of Gum, 14 packs of Crackers for a total of 100 packs of snacks. The following is one possible solution, and it is *not* the only solution. Your application can be different from this write up. Your classes should implement various snacks including “M&Ms”, “Popcorn”, “Snickers”, “Gum”, “Crackers”, and “Chips” . Implement the toString method to display the state of the snack . Let’s presume that each snack has these properties: “name”, “count”, “limit” and “cost”. Name is of course the name of the snack , count is an integer that represents how many pieces of the snack is in ven ing machine, limit represents the maximum number of the snack vending machine can hold, and cost is a floating - point number that represents how much the snack costs to buy.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package vendingMachine;

public class Snack {

   // Instance variables
   private String name;
   private int count;
   private int limit;
   private double cost;
  
   /**
   * Constructor
   * @param name
   * @param cost
   */
   public Snack(String name, double cost) {
       this(name, 0, 0, cost);
   }
  
   /**
   * Constructor
   * @param name
   * @param count
   * @param limit
   * @param cost
   */
   public Snack(String name, int count, int limit, double cost) {
       this.name = name;
       this.count = count;
       this.limit = limit;
       this.cost = cost;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @return the count
   */
   public int getCount() {
       return count;
   }

   /**
   * @return the limit
   */
   public int getLimit() {
       return limit;
   }

   /**
   * @return the cost
   */
   public double getCost() {
       return cost;
   }

   /**
   * @param name the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @param count the count to set
   */
   public void setCount(int count) {
       this.count = count;
   }

   /**
   * @param limit the limit to set
   */
   public void setLimit(int limit) {
       this.limit = limit;
   }

   /**
   * @param cost the cost to set
   */
   public void setCost(double cost) {
       this.cost = cost;
   }

   @Override
   public String toString() {
       return getCount() + " packs of " + getName();
   }
}

package vendingMachine;

import java.util.Scanner;

public class VendingMachine {

   private static final int CAPACITY = 100;

   // Instance variables
   private Snack[] snacks;

   /**
   * Constructor
   */
   public VendingMachine() {
       init();
   }

   /**
   * Initializes the vending machine with some snacks
   */
   void init() {
       this.snacks = new Snack[6];

       // Add 20 packs of Chips
       this.snacks[0] = new Snack("Chips", 20, 20, 1.0);

       // 20 packs of M&Ms
       this.snacks[1] = new Snack("M&Ms", 20, 20, 2.0);

       // 16 packs of Popcorn
       this.snacks[2] = new Snack("Popcorn", 16, 16, 4.5);

       // 16 packs of Snickers
       this.snacks[3] = new Snack("Snickers", 16, 16, 1.24);

       // 14 packs of Gum
       this.snacks[4] = new Snack("Gum", 14, 14, 2.0);

       // 14 packs of Crackers
       this.snacks[5] = new Snack("Crackers", 14, 14, 5.0);
   }

   /**
   * Get the total number of snacks in the machine
   *
   * @return
   */
   int getTotalPacks() {
       int count = 0;
       for (Snack snack : snacks) {
           count += snack.getCount();
       }

       return count;
   }

   /**
   * Gets a pack of snack from the vending machine
   *
   * @param i
   * @return
   */
   Snack getSnack(int i) {
       Snack snack = this.snacks[i];

       // If packets of snack is available
       if (snack.getCount() > 0) {
           snack.setCount(snack.getCount() - 1);
           return snack;
       }
       return null;
   }
  
   /**
   * Removes a pack of snack from the macine
   * @param in
   */
   void removeSnack(Scanner in) {
       // Display the snack list
       displaySnack();
       System.out.print("\nEnter the snack to get: ");
       int i = in.nextInt();
      
       // Get snack
       Snack snack = getSnack(i - 1);
      
       if (snack == null)
           System.out.print("Sorry this snack is not available");
   }

   /**
   * Add a packets of snack to the machine if the capacity is not full then
   * add (limit - count) or count packs of the snack, whichever if applicable
   *
   * @param i
   * @return - The number of packs added or returns -1 is no packs are added
   */
   int addSnack(int i, int count) {
       if (getTotalPacks() < CAPACITY) {
           Snack snack = this.snacks[i];

           if (snack.getLimit() > snack.getCount()) {
               if (snack.getLimit() >= (snack.getCount() + count)) {
                   count = snack.getCount() + count;
                   snack.setCount(snack.getCount() + count);
               } else {
                   count = snack.getLimit() - snack.getCount();
                   snack.setCount(snack.getCount() + count);
               }
               return count;
           }
       }

       return 0;
   }
  
   /**
   * Adds packs of snack to the macine
   * @param in
   */
   void addSnack(Scanner in) {
       // Display the snack list
       displaySnack();
       int i = 0;
       do {
           System.out.print("\nEnter the choice of snack to add: ");
           i = in.nextInt();

           if ((i < 1) || (i > this.snacks.length)) {
               System.out.print("\nInvalid choice. Please try again");
           }
       } while ((i < 1) || (i > this.snacks.length));
      
       // Ask for the number of packs to add
       int noOfPacks = 0;
       do {
           System.out.print("\nEnter the number of packs to add: ");
           noOfPacks = in.nextInt();
          
           if (noOfPacks < 0) {
               System.out.print("\nInvalid choice. Please try again");
           }
       } while (noOfPacks < 0);
      
       // Add packs
       System.out.print("\nPacks added: " + addSnack(i - 1, noOfPacks));
   }
  
   /**
   * Displays the list of Snack
   */
   void displaySnack() {
       int i = 1;
       for (Snack snack : snacks) {
           System.out.print("\n" + i + " " + snack.getName());
           i += 1;
       }
   }
  
   /**
   * Displays the available number of packs of each Snack
   */
   void displayMachineState() {
       for (Snack snack : snacks) {
           System.out.print("\n" + snack);
       }
      
       System.out.println();
   }
  
   /**
   * Displays the menu
   */
   static void menu() {
       System.out.print("\n1. Add snacks");
       System.out.print("\n2. Remove snacks");
       System.out.print("\n3. Display machine status");
       System.out.print("\n4. Exit");
       System.out.print("\nEnter your choice: ");
   }
  
   public static void main(String[] args) {

       // Scanner to get user input
       Scanner in = new Scanner(System.in);

       // Create Vending machine object
       VendingMachine machine = new VendingMachine();

       while (true) {
           // Display menu
           VendingMachine.menu();
           int choice = in.nextInt();

           switch (choice) {
           case 1: // Add snack
               machine.addSnack(in);
               break;

           case 2: // Remove snack
               machine.removeSnack(in);
               break;

           case 3: // Display machine state
               machine.displayMachineState();
               break;

           case 4:   // Exit
               System.exit(0);
               // Close scanner
               in.close();

           default:   // Invalid choice
               System.out.print("\nInvalid choice. Please try again");
           }
           System.out.println();
       }
   }
}


SAMPLE OUTPUT:

1. Add snacks 2. Remove snacks 3. Display machine status 4. Exit Enter your choice: 5 Invalid choice. Please try again 1. Add1. Add snacks 2. Remove snacks 3. Display machine status 4. Exit Enter your choice: 2 1 Chips 2 M&Ms 3 Popcorn 4 Snickers 5 G

Add a comment
Know the answer?
Add Answer to:
Vending Machine In this assignment, you are to vend six different snacks. The Vending Machine has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose you have decided to start a small business selling snacks from vending machines. You have...

    Suppose you have decided to start a small business selling snacks from vending machines. You have secured a location for one candy vending machine in a local bookstore. Rental for the space will cost $200 per month. Vending machines can be purchased at wholesale clubs such as Sam’s Club and Costco. You can also purchase the snacks to stock the machines in bulk there. 1. Either visit a local warehouse club or review its website to determine the initial cost...

  • Suppose you have decided to start a small business selling snacks from vending machines. You have...

    Suppose you have decided to start a small business selling snacks from vending machines. You have secured a location for one candy vending machine in a local bookstore. Rental for the space will cost $200 per month. Vending machines can be purchased at wholesale clubs such as Sam’s Club and Costco. You can also purchase the snacks to stock the machines in bulk there. 1. Either visit a local warehouse club or review its website to determine the initial cost...

  • Suppose you have decided to start a small business selling snacks from vending machines. You have...

    Suppose you have decided to start a small business selling snacks from vending machines. You have secured a location for one candy vending machine in a local bookstore. Rental for the space will cost $200 per month. Vending machines can be purchased at wholesale clubs such as Sam’s Club and Costco. You can also purchase the snacks to stock the machines in bulk there. REQUIRED: Assume your machine can accommodate more than one product, for example, a snack machine that...

  • Suppose you have decided to start a small business selling snacks from vending machines. You have...

    Suppose you have decided to start a small business selling snacks from vending machines. You have secured a location for one candy vending machine in a local bookstore. Rental for the space will cost $200 per month. Vending machines can be purchased at wholesale clubs such as Sam’s Club and Costco. You can also purchase the snacks to stock the machines in bulk there. REQUIRED: Assume your machine can accommodate more than one product, for example, a snack machine that...

  • A vending machine sells a variety of snacks for $0.65; it only accepts dollars. It provides...

    A vending machine sells a variety of snacks for $0.65; it only accepts dollars. It provides $0.35 in change in nickels, dimes and quarters, using the smallest number of coins possible. The change controller takes inputs from a quarter counter (1-bit), dime counter (2-bits), and nickel counter (3-bits). It outputs the number of quarters (1-bit), dimes (2-bits) and nickels (3-bits) to be dispensed, or asserts "no change" (1- bit) if the machine is unable to make exact change. Design a...

  • Description In this homework, you are asked to implement a multithreaded program that will allow ...

    Description In this homework, you are asked to implement a multithreaded program that will allow us to measure the performance (i.e, CPU utilization, Throughput, Turnaround time, and Waiting time in Ready Queue) of the four basic CPU scheduling algorithms (namely, FIFO, SJE PR, and RR). Your program will be emulating/simulating the processes whose priority, sequence of CPU burst time(ms) and I'O burst time(ms) will be given in an input file. Assume that all scheduling algorithms except RR will be non-preemptive,...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

  • ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag...

    ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag collection using a linked list and, in order to focus on the linked list implementation details, we will implement the collection to store only one type of object of your choice (i.e., not generic). You can use the object you created for program #2 IMPORTANT: You may not use the LinkedList class from the java library. Instead, you must implement your own linked list...

  • In this lab, you will design a finite state machine to control the tail lights of...

    In this lab, you will design a finite state machine to control the tail lights of an unsual car. There are three lights on each side that operate in sequence to indicate thedirection of a turn. Figure ! shows the tail lights and Figure 2 shows the flashing sequence for (a) left turns and (b) right rums. ZOTTAS Figure 28:8: BCECECece BCECECECes BCECECECB BCECECBCB 8888 Figure 2 Part 1 - FSM Design Start with designing the state transition diagram for...

  • Solve it for java Question Remember: You will need to read this assignment many times to...

    Solve it for java Question Remember: You will need to read this assignment many times to understand all the details of the you need to write. program Goal: The purp0se of this assignment is to write a Java program that models an elevator, where the elevator itself is a stack of people on the elevator and people wait in queues on each floor to get on the elevator. Scenario: A hospital in a block of old buildings has a nearly-antique...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT