package javaapplication3;
import java.util.Scanner;
public class CandyMachine {
Scanner getinput = new Scanner(System.in);
static int quarters;
static int dollars;
static int dimes;
static int nickels;
public static int credit;
//stock of candy
static int twix = 10;
static int snickers = 10;
static int marathon = 8;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("*******************************************");
System.out.println("0. Insert Money: ");
System.out.println("1. Twix 2.00 dollars");
System.out.println("2. Snickers 1.00 dollar");
System.out.println("3. Marathon Bar 1.00 dollar");
System.out.println("4. Change Return");
System.out.println("*********************************************");
int userSelection = input.nextInt();
switch (userSelection) {
case 0:
insertMoney();
break;
case 1:
System.out.println("Enter 2.00 dollars please");
if (credit >= 200) {
twix -= 1;
} else {
System.out.println("Enter more money");
}
break;
case 2:
System.out.println("Enter 1.00 dollar please");
if (credit >= 100) {
snickers -= 1;
} else {
System.out.println("Enter more money");
}
break;
case 3:
System.out.println("Enter 1.00 dollar please");
if (credit >= 100) {
marathon -= 1;
} else {
System.out.println("Enter more money");
}
break;
case 4:
if (credit > 0) {
System.out.println("Good luck getting your money back!! ");
}
break;
default:
System.out.println("Please enter a correct number.");
break;
}
}
}
public static void insertMoney() {
int moneySelection;
Scanner getinput = new Scanner(System.in);
do {
System.out.println("1: Enter quarters");
System.out.println("2: Enter dimes");
System.out.println("3: Enter nickels");
System.out.println("4: Enter Dollars");
System.out.println("5: When all money is entered");
moneySelection = getinput.nextInt();
switch (moneySelection) {
case 1:
System.out.println("How many quarters? ");
int userQuarters = getinput.nextInt();
credit = quarters * 25;
break;
case 2:
System.out.println("How many dimes?");
int userdimes = getinput.nextInt();
credit = dimes * 10;
break;
case 3:
System.out.println("How many Nickels?");
int userNickels = getinput.nextInt();
credit = nickels * 5;
break;
case 4:
System.out.println("How many Dollars?");
int userDollar = getinput.nextInt();
credit = credit * 100;
break;
case 5:
System.out.println("Thank you");
break;
}
} while (moneySelection != 5);
}
}
candy vending machine (calling methods)
Page 1 of 13 Replies - 1020 Views - Last Post: 02 April 2015 - 01:01 AM
#1
candy vending machine (calling methods)
Posted 30 March 2015 - 08:22 PM
Hello everyone! I am sort of new to java and was given the task to create a java application that vends candy. I am having problems calling my insertmoney() method.When "insert money" is selected it is not updating the credit amount in the machine. I was told that you cannot call a non static method in a static method but i cannot seem to fix the code. When "insert money" is selected it is not updating the credit amount in the machine.
Replies To: candy vending machine (calling methods)
#2
Re: candy vending machine (calling methods)
Posted 30 March 2015 - 09:57 PM
Well this is just one place, but apply this fix to the other examples..
Now remember one more thing, you have to add to the credits, not overwrite the credits. So if I enter in 1 quarter and then add a dime, I should be reading 35 cents. So I need to take credit (which is .25 after a quarter) and then ADD the dime to it. So you should probably set credit to zero first and then be using lines like credit += userQuarters * 25; which will multiply quarters times 25 and add it to whatever might be on credit at the time.
Hope this helps!
int userQuarters = getinput.nextInt(); credit = quarters * 25; //<--- Notice you have "quarters" but you put the value in "userQuarters" so you should be using that // Fixed int userQuarters = getinput.nextInt(); credit = userQuarters * 25;
Now remember one more thing, you have to add to the credits, not overwrite the credits. So if I enter in 1 quarter and then add a dime, I should be reading 35 cents. So I need to take credit (which is .25 after a quarter) and then ADD the dime to it. So you should probably set credit to zero first and then be using lines like credit += userQuarters * 25; which will multiply quarters times 25 and add it to whatever might be on credit at the time.
Hope this helps!
#3
Re: candy vending machine (calling methods)
Posted 31 March 2015 - 04:07 PM
Thanks Martyr2 That makes sense..
I would have probably found that problem if i got the "machine" to update the money; however, i still cant get it to update the amount of money inserted. I researched a bit on how to change static to non-static and i am still stumped. From what i understood i have two options: changed the static to non-static or creating an instance with insertmoney.
#4
Re: candy vending machine (calling methods)
Posted 02 April 2015 - 01:01 AM
Any advice?
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|