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

JAVA- Your task for this assignment is to extend the \"Vending Machine\" you wro

ID: 674888 • Letter: J

Question

JAVA-

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.

Explanation / Answer

public class VendingMachine

{

private int Snackcount;

private double moneyCredit;

private double Snackcost;

private final double Snickers = .95;

private final double Gum = .75;

private final double Mnms = .85;

private final double Popcorn = 1.25;

private final double Crackers = 1.00;

private final double Chips = 1.15;

private double returnAmount;

public VendingMachine()

{

Snackcount = 100;

moneyCredit = 0.0;

returnAmount = 0.0;

}

public void restock()

{

Snackcount = 100;

moneyCredit = 0.0;

}

public void addMoney ( double amount )

{

moneyCredit += amount;

System.out.println ( "Total inserted: " + moneyCredit );

}

public void vend()

{

double returnAmount = 0.0;

if ( moneyCredit >= Snackcost() )

{

   System.out.println ( "Here is your snack" );

   returnAmount -= ( moneyCredit - Snackcost );

   if ( returnAmount >= 0.0 )

    System.out.println ( "Your change is: " + ( moneyCredit - Snackcost ) + " cents." );

   moneyCredit = 0.0;

}

else

System.out.println( "Vending machine only has " + moneyCredit + " cents. Please insert more." );

}

}

class VendingMachineTester

{

public static void main( String[] args )

{

boolean doAgain = true;

char choice;

VendingMachine myMachine = new VendingMachine();

Snack mySnack = new Snack();

while ( doAgain )

{

   JOptionPane.showMessageDialog(null, "Enter your choice of snack!: " +

                                        "1) Exit /n2) Incert Money /n3) Snickers" +

                                                    "4) Gum /n5) M&ms /n6) PopCorn" +

                                                    "7) Crackers /n8)Chips /n9) Restock" +

   /*System.out.println();

   System.out.println( "Options:" );

   System.out.println();

   System.out.println( "1] Exit" );

   System.out.println( "2] Insert money" );

   System.out.println( "3] Vend Snickers" );

   System.out.println( "4] Vend Gum" );

    System.out.println( "5] Vend M&ms" );

   System.out.println( "6] Vend Popcorn" );

    System.out.println( "7] Vend Crackers" );

   System.out.println( "8] Vend Chips" );

   System.out.println( "9] Restock vending machine" );

   System.out.println();

   System.out.print( "Choice: " );

    */

   choice = Keyboard.readChar()

   if(choice == '1')

    doAgain = false;

   if( choice == '2' )

   {

    System.out.print( "Enter amount of cents to insert: " );

    double cents = Keyboard.readDouble();

    myMachine.addMoney ( cents );

   }

   if ( choice == '3' )

   {

    System.out.println( "Vend a Snickers and get change." );

    myMachine.vend();

   }

   if ( choice == '4' )

   {

     System.out.println ( "Vend a Gum and get change." );

     myMachine.vend();

   }

    if ( choice == '3' )

   {

    System.out.println( "Vend M&ms and get change." );

    myMachine.vend();

   }

   if ( choice == '4' )

   {

     System.out.println ( "Vend PopCorn and get change." );

     myMachine.vend();

   }

   if ( choice == '3' )

   {

    System.out.println( "Vend Crackers and get change." );

    myMachine.vend();

   }

   if ( choice == '8' )

   {

     System.out.println ( "Vend Chips and get change." );

     myMachine.vend();

   }

   if ( choice == '9' )

   {

    System.out.println ( "Restock vending machine." );

    myMachine.restock();

   }

}

System.out.println();

System.out.println();

System.out.println( "Thanks for using my vending machine." );

}

}

abstract class Snack

{

int calories;

double cost;

public Snack()

{

calories = 0;

cost = 0.0;

}

public double Snackcost()

{

return cost;

}

public int calories()

{

return calories;

}

}

class Salty extends Snack

{

public Salty(int cal, double amt)

{

super();

calories = cal;

cost = amt;

}

}

class Sugary extends Snack

{

public Sugary(int cal, double amt)

{

super();

calories = cal;

cost = amt;

}

}

class Snickers extends Sugary

{

public Snickers()

{

setCalories(15);

setCost(.95);

}

}

class Gum extends Sugary

{

public Gum()

{

setCalories(5);

setCost(.75);

}

}

class Mnms extends Sugary

{

public Mnms()

{

setCalories(20);

setCost(.85);

}

}

class Popcorn extends Salty

{

public Popcorn()

{

setCalories(50);

setCost(1.25);

}

}

class Crackers extends Salty

{

public Crackers()

{

setCalories(25);

setCost(1.00);

}

}

class Chips extends Salty

{

public Chips()

{

setCalories(30);

setCost(1.15);

}