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

Your task for this assignment is to extend the \"Vending Machine\" you wrote for

ID: 3691206 • Letter: Y

Question

Your task for this assignment is to extend the "Vending Machine" you wrote for assignment seven to add the ability to hold the snacks that you implemented in assignment 8 (i.e. combine the seventh and eighth assignments). The resultant vending machine class should hold 100 snacks. Your program should present the user with a menu of choices ("Add money", "Vend", and "Restock" as before in assignment seven – with appropriate modifications) and process the user's choice until he/she selects "Quit'. Utilize good modularity and object-oriented design/programming in your solution.

Assignment 7: Your task in this assignment is to write a simple class definition for a vending machine. The class should have: Attributes Soda count Money credit (how much money has been inserted) Methods Add money Vend Restock Constructor For simplicity sake, you may assume that the machine can hold up to 50 of one type of soda; you may also assume that all sodas cost $0.65. When adding money, simply take in a floating-point number representing how much money to add (i.e. you do not have to manage different coins). Be sure to make sure that enough money was added to the machine before a soda is vended. Also, when a soda is vended, return the appropriate change (i.e. if more than $0.65 was added to the machine, then return the extra money). Again, don't worry about managing different kinds of coins – just display the amount returned. When the machine is "restocked," just set the soda count to the maximum that the machine will hold. Once you have written the class, create a main/driver program that interacts with an instance of the class. Your program should present a menu of options to the user and process the user's choice; use creativity and object-oriented design in your solution.

Assignment 8: Your task for this assignment is to define multiple classes via inheritance. Your classes should implement various "snacks" including "M&Ms", "Popcorn", etc. To begin, create an abstract "Snack" class. Then, create two classes "Salty" and "Sugary" that inherit from the "Snack" class. Then create classes "M&Ms", "Popcorn", "Snickers", "Gum", "Crackers", and "Chips" that inherit from "Salty" or "Sugary" as appropriate. Once you have your inheritance hierarchy defined, implement the toString method for each class. Finally, let's presume that each snack has two additional properties: "calories" and "cost". Calories is an integer that represents how many calories the snack contains, and cost is a floating-point number that represents how much the snack costs to buy. Implement these properties within your hierarchy as appropriate.

Explanation / Answer

import java.util.Scanner;

public class Assignment9_main {
  
    public static VendingMachine vending1 = new VendingMachine(100, 0.00f, 0.00f);
  
/*VENDING MACHINE SETUP STARTS HERE*/   
    public static class VendingMachine {
    //attributes
    int snackCount;
    float moneyCredit;
    float itemCost;

   //Constructor
   public VendingMachine(int snackCountInput, float moneyCreditInput, float itemCostInput) {
    this.snackCount = snackCountInput ;
    this.moneyCredit = moneyCreditInput;
    this.itemCost = itemCostInput;
    }

   public void setItemCost(float snackItemCost){
       this.itemCost = snackItemCost;
   }

   //add credit
   public float addMoney() {
       System.out.println("Please insert cash ");
       Scanner input = new Scanner(System.in);
       float newMoneyCredit = input.nextFloat();
       moneyCredit = newMoneyCredit + moneyCredit;
       return moneyCredit;
   }

   //vend beverage and return change
   public void vend() {
    while (moneyCredit < itemCost){
                    addMoney();
                }
    snackCount--;
    System.out.println("Enjoy your snack.");
    //changed to deduct variable cost from moneyCredit here
    System.out.println(moneyCredit-itemCost + " is your change");
    }
  
     
   //restock machine
   public void restock() {
       //add in options to add different snacks up to a count of 100 total
       snackCount = 100;
       System.out.println("Snacks have been restocked.");
       }
     
    }

/*SNACK INFO STARTS HERE*/       
      
//create superclass Snack
    abstract class Snack {
         public int calories;
         public float cost;
         public String name;
       
         @Override
         public String toString() {
             return name + " cost" + cost + "and have " + calories + "calories.";
        }
    }
  
    //create subclasses Salty and Sweet that inherit from Snack
    abstract class Salty extends Snack{
    }

    abstract class Sugary extends Snack{  
    }
  
    //create individual classes for snack types that inherit from Salty and Sweet
    //each should have it's own implementation of toString()
    //program could be further improved by defining toString in the Snack class rather than repeating the code in each subclass
    class Mms extends Sugary {

        public final static String NAME = "M & M's";
        public final static int CALORIES = 90;
        public final static float COST = .75f;

        @Override
        public String toString() {
            return super.toString();
        }
    }
  
    class Popcorn extends Salty{
          public final static String NAME = "Popcorn";
        public final static int CALORIES = 50;
        public final static float COST = 1.25f;
      
      
        @Override
        public String toString() {
            return super.toString();
        }
    }
  
    class Snickers extends Sugary{
          public final static String NAME = "Snickers";
        public final static int CALORIES = 125;
        public final static float COST = 1.25f;
     
      
        @Override
        public String toString() {
            return super.toString();
        }
    }
  
    class Gum extends Sugary{
          public final static String NAME = "Gum";
        public final static int CALORIES = 10;
        public final static float COST = .50f;
    
        @Override
        public String toString() {
            return super.toString();
        }
    }
  
    class Crackers extends Salty{
          public final static String NAME = "Crackers";
        public final static int CALORIES = 80;
        public final static float COST = .85f;
      
        @Override
        public String toString() {
            return super.toString();
        }
    }

    class Chips extends Salty{
          public final static String NAME = "Chips";
        public final static int CALORIES = 80;
        public final static float COST = .85f;
     
      
        @Override
        public String toString() {
            return super.toString();
        }
    }      
      
      
  
  
    public static void snackMenu(){
        Scanner input = new Scanner(System.in);
         while(true){
           //display submenu of shack options  
           System.out.println("* * * * * * * * * * * * * * ");
           System.out.println("Snack-Happy Vending Company");
           System.out.println("* * * * * * * * * * * * * * ");

           System.out.println("Select a Snack Option: ");
           System.out.println("Press 1 for M & M's");
           System.out.println("Press 2 for Popcorn");
           System.out.println("Press 3 for Snickers");
           System.out.println("Press 4 for Gum");
           System.out.println("Press 5 for Crackers");
           System.out.println("Press 6 for Chips");
           System.out.println("Press 0 to Quit");
         
           int snackSelection = input.nextInt();
           switch (snackSelection) {
            case 1:
                vending1.setItemCost(Mms.COST);
                vending1.vend();
                break;
            case 2:
                vending1.setItemCost(Popcorn.COST);
                vending1.vend();
                break;
            case 3:
                vending1.setItemCost(Snickers.COST);
                vending1.vend();
                break;
            case 4:
                vending1.setItemCost(Gum.COST);
                vending1.vend();
                break;
            case 5:
                vending1.setItemCost(Crackers.COST);
                vending1.vend();
                break;
            case 6:
                vending1.setItemCost(Chips.COST);
                vending1.vend();
                break;
            case 0:
                return;  
            }
             
           }
    }
  
    /*MAIN STARTS HERE*/
    public static void main(String[] args) {
       //Main Menu
       
       System.out.println("* * * * * * * * * * * * * * ");
       System.out.println("Snack-Happy Vending Company");
       System.out.println("* * * * * * * * * * * * * * ");
     
       System.out.println("Please Select an Option: ");
       System.out.println("Press 1 to Choose a Snack, or Press 0 to Restock the Machine");
       Scanner input = new Scanner(System.in);
       int userInput = input.nextInt();
     
       switch (userInput) {
            case 1:
            snackMenu();
            break;
            case 0:
            vending1.restock();
            break;
       }
    }
}


sample output

* * * * * * * * * * * * * *                                                                                                                                 
Snack Vending Company                                                                                                                                       
* * * * * * * * * * * * * *                                                                                                                                 
Please Select an Option:                                                                                                                                    
Press 1 to Choose a Snack, or Press 0 to Restock the Machine                                                                                                
1                                                                                                                                                           
* * * * * * * * * * * * * *                                                                                                                                 
Snack-Happy Vending Company                                                                                                                                 
* * * * * * * * * * * * * *                                                                                                                                 
Select a Snack Option:                                                                                                                                      
Press 1 for M & M's                                                                                                                                         
Press 2 for Popcorn                                                                                                                                         
Press 3 for Snickers                                                                                                                                        
Press 4 for Gum                                                                                                                                             
Press 5 for Crackers                                                                                                                                        
Press 6 for Chips                                                                                                                                           
Press 0 to Quit                                                                                                                                             
2                                                                                                                                                           
Please insert cash                                                                                                                                          

100                                                                                                                                                         
Enjoy your snack.                                                                                                                                           
98.75 is your change                                                                                                                                        
* * * * * * * * * * * * * *                                                                                                                                 
Snack-Happy Vending Company                                                                                                                                 
* * * * * * * * * * * * * *                                                                                                                                 
Select a Snack Option:                                                                                                                                      
Press 1 for M & M's                                                                                                                                         
Press 2 for Popcorn                                                                                                                                         
Press 3 for Snickers                                                                                                                                        
Press 4 for Gum                                                                                                                                             
Press 5 for Crackers                                                                                                                                        
Press 6 for Chips                                                                                                                                           
Press 0 to Quit                                                                                                                                             
0