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