After trying different things for about a week i have come to the conclusion i am lost. Can someone please show me how to do this.

This assignment, based on Vending Machine we did in class, you are to now 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 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.

Recommended Answers

All 8 Replies

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

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);

 }
}

You're missing the toString() methods, but overall you are doing well. You have the vast majority of =what you were asked for in this assignment. Maybe you are worrying unnecessarily?

maybe but can you show me how to do a toString() method

When java wants to print an object it uses that object's toString() method to get a suitable string to print. Every class inherits a vesion from Object, but it's not much use, so you override it to be something helpful. Eg In a class Dog you could have

public String toString() { 
   return "I am a dog, my name is " + name;
}

how would i subtract one from the snack count? would i just use count-- under each indvidual snack or would i put it somewhere else?

hey jones13, im actually in the same class and i was wondering if you ever completed this assingment.

The interesting question is whether you will ever complete it. Copying someone else's answers is likely to get you an automatic fail of your course, and possibly being thrown out (quite rightly!).
The only way to learn is to do it yourself. If you make that effort people here will help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.