I'm trying to finish up this vending machine. It worked well until I tried to add multiple items via inheritance while using a toString method. I'm not sure what is wrong about how I've done this.

import java.util.Scanner;

public class VendingMachine
{
        public static double moneyCredit;
        public static double itemCost;
        public static double change;
        public static int itemCount= 100;
        public static double credit = 0;
        public static double creditTotal=0;
        public static double needMoreCredit;
        public static String snackType;
        public static int calories;
 public static float snackCost;


// Case 1
    public static void addMoney()
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Please input a dollar amount");
            credit = input.nextFloat();
            creditTotal = (creditTotal + credit);


            if (credit < itemCost)
            {
             needMoreCredit = (itemCost - credit);
             System.out.println("Your input of " + credit + " has been returned to you.");
             System.out.println("Please input at least " + needMoreCredit + " more that you did previously.");
            }

            else
            {
                System.out.println("You have a credit of " + creditTotal);
            }

            credit = 0;
    }

    public static double getChange()
    {
        if (creditTotal > itemCost)
        {
         change = (creditTotal - itemCost);
        System.out.println("Please accept your change in the amount of " + change); 
        }

        creditTotal = 0;
        return change;
    }  

// Case 2
    public static void vend()
            {
                if (creditTotal >= itemCost && itemCount != 0)
                {
                 System.out.println("Your " + snackType + "has been dispensed! \n Please Enjoy!!!");
                 itemCount--; 

                }

                if (itemCount == 0)
                {
                 System.out.println("Sorry the machine is empty!");
                 System.out.print("Perhaps you should restock!");
                }

                else if (creditTotal < itemCost)
                        {
                            System.out.println("Your mom's house is usually FREE.");
                            System.out.println("If you wish to purchase a snack...");
                            System.out.println("Please select option 1 to input money!");
                        }
                credit = 0;

            }

// Case 3
    public static void restock()
    {
        if (itemCount < 100)
        {    
        System.out.println("The current snack count is " + itemCount); 
        System.out.println( "We will now restock the machine.");
        System.out.println("The machine is full again, with 100 snacks.");
        System.out.println("Thank you!");
        itemCount = 100;
        }

        else if (itemCount == 100)
        {
            System.out.println("The snack machine is already full.");
            System.out.println("Perhaps you might consider buying some snacks?!?");
        }
    }

// Case 4
    public static void exitVendingMachine()
    {

        System.out.println( "Enjoy your snack!!!" );
        System.exit(0);    
    }


//this isn't right, im not sure what I've done wrong
    public static String toString()
{
 return "Your " +snackType + "has " + calories + "calories and costs " + snackCost;     
}


public static class Snack 
{
 public static void Snack()
 {
    calories = 0;
    snackCost = .0f;
}        

public static class Salty extends Snack
{

}

public static class Sugary extends Snack
{

}

    public final class Pepsi extends Sugary
    {
        public Pepsi()
        {
            calories = 150;
            snackCost = 0.65f;
        }
    }
    public final class MandMs extends Sugary
        {
         public MandMs()
         {
            calories = 34;
            snackCost = 1.00f;   
         }
        }

    public final class Popcorn extends Salty
        {
        public Popcorn()
        {
           calories = 64;
           snackCost = 1.25f;
        }
        }

    public final class Snickers extends Sugary
        {
        public Snickers()
        {
            calories = 296;
            snackCost = 1.50f;
        }
        }

    public final class Gum extends Sugary
        {
        public Gum()
        {
            calories = 5;
            snackCost = 1.00f;
        }
        }


    public final class Crackers extends Salty
        {
        public Crackers()
        {
            calories = 80;
            snackCost = 1.50f;
        }
        }

    public final class Chips extends Salty
        {
        public Chips()
        {
            calories = 147;
            snackCost = 1.50f;
        }
        }
}

   //not sure how to make this account for the selection
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        int inputChoice;

        do
        {
            System.out.println("Please make a selection");
            System.out.println("1. Add money to your credit amount.");
            System.out.println("2. Dispense Soda.");
            System.out.println("3. Dispense MandMs.");
            System.out.println("4. Dispense Popcorn.");
            System.out.println("5. Dispense Snickers.");
            System.out.println("6. Dispense Gum.");
            System.out.println("7. Dispense Crackers.");
            System.out.println("8. Dispense chips.");
            System.out.println("9. Resock the machine.");
            System.out.println("Enter 0 to exit the program");

            inputChoice = input.nextInt();

        {
            if(inputChoice <0 || inputChoice > 9)
            {
                System.out.println("Invalid Input.  Please enter a valid selection!");
            }

            if(inputChoice >= 1 || inputChoice <= 9)
            {
                switch(inputChoice)
                {

                    case 1:
                    {
                     addMoney();
                    }
                    break;


                    case 2:
                    {
                     toString();
                     vend();                   
                     getChange();
                    }
                    break;

                    case 3:
                    {
                     vend();
                     getChange();
                    }
                    break;

                    case 4:
                    {
                     vend();
                     getChange();
                    } 
                    break;

                      case 5:
                    {
                     vend();
                     getChange();
                    } 
                    break;

                    case 6:
                    {
                     vend();
                     getChange();
                    } 
                    break;

                    case 7:
                    {
                     vend();
                     getChange();
                    } 
                    break;

                    case 8:
                    {
                     vend();
                     getChange();
                    } 
                    break;

                    case 9:
                    {
                     exitVendingMachine();
                    } 
                    break;      
              } 
          } 
       }
    } while (inputChoice != 0);
}
}`

Recommended Answers

All 6 Replies

Use arrays. Pick one item then put in array. Pick another item then put in array. And so on. Well that is one of the ways. There are better ways to solve that problem.

Hope that helps :)

Can I use an array and still have the different classes for each item?

Post the new code since you have modified it

I'm fairly certain that I'm working in circles.
I tried adding something along the lines of what is below to Case 3, but couldn't get it to work so I deleted it.

                snackType = "MandMs";
                MandMs Snack = new MandMs();
                itemCost = Snack.snackCost;
                calories = Snack.calories;
                snackDetails = Snack.toString();
                 vend();
                 getChange();

My program looks like this at the moment:

import java.util.Scanner;

public class VendingMachine
{
        public static double moneyCredit;
        //how do I make  each item's cost update by case and then work within the 
        //VendingMachind_main class?
        public static float itemCost;
        public static double change;
        public static int itemCount= 100;
        public static double credit = 0;
        public static double creditTotal=0;
        public static double needMoreCredit;
        public static String snackType;
        public static int calories;
        public static String snackDetails;




// Case 1
    public static void addMoney()
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Please input a dollar amount");
            credit = input.nextFloat();
            creditTotal = (creditTotal + credit);

                System.out.println("You have a credit of " + creditTotal);

            credit = 0;
    }

    //Why doesn't this getChange method work after adding more classes
    //to represent the different items?
    public static double getChange()
    {
        if (creditTotal > itemCost)
        {
         change = (creditTotal - itemCost);
        System.out.println("Please accept your change in the amount of " + change); 
        }

        creditTotal = 0;
        return change;
    }  

// Case 2
    public static void vend()
    //why isn't ved actually checking to see if creditTotal is enough 
            {
                if (creditTotal >= itemCost && itemCount != 0)
                {
                 System.out.println("Your " + snackType + " can be found in the collection area! \n Please Enjoy!!!"); 

                }

                if (itemCount == 0)
                {
                 System.out.println("Sorry the machine is empty!");
                 System.out.print("Perhaps you should restock!");
                }

                if (creditTotal < itemCost)
                {
                needMoreCredit = (itemCost - credit);
                System.out.println("Your input of " + credit + " has been returned to you.");
                System.out.println("Please input at least " + needMoreCredit + " more that you did previously.");
                }

                else if (creditTotal < itemCost)
                        {
                            System.out.println("Your mom's house is usually FREE.");
                            System.out.println("If you wish to purchase a snack...");
                            System.out.println("Please select option 1 to input money!");
                        }
                credit = 0;

            }

// Case 3
    public static void restock()
    {
        if (itemCount < 100)
        {    
        System.out.println("The current snack count is " + itemCount); 
        System.out.println( "We will now restock the machine.");
        System.out.println("The machine is full again, with 100 snacks.");
        System.out.println("Thank you!");
        itemCount = 100;
        }

        else if (itemCount == 100)
        {
            System.out.println("The snack machine is already full.");
            System.out.println("Perhaps you might consider buying some snacks?!?");
        }
    }

// Case 4
    public static void exitVendingMachine()
    {

        System.out.println( "Enjoy your snack!!!" );
        System.exit(0);    
    }

 public static float getCost()
 {
     //how do I tell it which class to pull this from?
     return itemCost;
 }

 public static int getCalories()
 {
     //how do I tell it which class to pull this from?
     return calories;
 }


 //I cant call non-static content into switch statement, 
 //but I can only override if it is non-static... Not sure how to proceed.
        @Override
    public String toString() 
    {
     return "Your " +snackType + "indicates " + calories + "calories and costs " + snackCost;   
    }




abstract class Snack 
{

    public class Salty extends VendingMachine_main.Snack
    {

    }

    public class Sugary extends VendingMachine_main.Snack
    {


    }

    public final class Pepsi extends VendingMachine_main.Snack.Sugary
    {
        public Pepsi()
        {            
            calories = 150;
            itemCost = 0.65f;
        }
    }
    public final class MandMs extends VendingMachine_main.Snack.Sugary
        {
         public MandMs()
         {
            calories = 34;
            itemCost = 1.00f;
         }
        }

    public final class Popcorn extends VendingMachine.Snack.Salty
        {
        public Popcorn()
        {
           calories = 64;
           itemCost = 1.25f;
        }
        }

    public final class Snickers extends VendingMachine_main.Snack.Sugary
        {
        public Snickers()
        {
            calories = 296;
            itemCost = 1.50f;
        }
        }

    public final class Gum extends VendingMachine_main.Snack.Sugary
        {
        public Gum()
        {
            calories = 5;
            itemCost = 1.00f;
        }
        }


    public final class Crackers extends VendingMachine_main.Snack.Salty
        {
        public Crackers()
        {
            calories = 80;
            itemCost = 1.50f;
        }
        }

    public final class Chips extends VendingMachine_main.Snack.Salty
        {
        public Chips()
        {
            calories = 147;
            itemCost = 1.50f;
        }
        }
        }

    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        int inputChoice;

        do
        {
            System.out.println("Please make a selection");
            System.out.println("1. Add money to your credit amount.");
            System.out.println("2. Dispense Soda.");
            System.out.println("3. Dispense MandMs.");
            System.out.println("4. Dispense Popcorn.");
            System.out.println("5. Dispense Snickers.");
            System.out.println("6. Dispense Gum.");
            System.out.println("7. Dispense Crackers.");
            System.out.println("8. Dispense Chips.");
            System.out.println("9. Resock the machine.");
            System.out.println("Enter 0 to exit the program");

            inputChoice = input.nextInt();

        {
            if(inputChoice <0 || inputChoice > 9)
            {
                System.out.println("Invalid Input.  Please enter a valid selection!");
            }

            if(inputChoice >= 0 || inputChoice <= 9)
            {
                switch(inputChoice)
                {

                    case 1:
                    {
                     addMoney();
                    }
                    break;


                    case 2:
                    {
                     snackType = "Pepsi";
                     getCost();
                     vend();                   
                     getChange();
                     itemCount--;
                    }
                    break;

                    case 3:
                    {
                     snackType = "MandMs";
                     vend();
                     getChange();
                     itemCount--;
                    }

                    break;

                    case 4:
                    {
                     snackType = "Popcorn";
                     getCost();
                     vend();
                     getChange();
                     itemCount--;
                    } 
                    break;

                      case 5:
                    {
                     snackType= "Snickers";
                     getCost();
                     vend();
                     getChange();
                     itemCount--;
                    } 
                    break;

                    case 6:
                    {
                     snackType = "Gum";
                     getCost();
                     vend();
                     getChange();
                     itemCount--;
                    } 
                    break;

                    case 7:
                    {
                     snackType = "Crackers";
                     getCost();
                     vend();
                     getChange();
                     itemCount--;
                    } 
                    break;

                    case 8:
                    {
                     snackType = "Chips";
                     getCost();
                     vend();
                     getChange();
                     itemCount--;
                    } 
                    break;

                    case 9:
                    {
                     restock();
                    } 
                    break;  

                    case 0:
                    {
                        exitVendingMachine();
                    }
                    break;
              } 
          } 
       }
        }while (inputChoice != 0);
        }
}

Do you really need to use inheritance?

Sorry, I was so tired I didn't include my instructions. I have to use inheritance, with snack being abstract and each different item in its own class. That's part of the reason I'm stuck.

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.