If anyone can help me or guide me with the last part of my program, il appreciat
ID: 673227 • Letter: I
Question
If anyone can help me or guide me with the last part of my program, il appreciate it. I'm stuck on the ChangeReturn method, which i need to make it into a NON-STATIC method - this will be called when the customer wants change. You will pass it nothing, give it the appropriate amount of quarters, dimes, and nickels (no dollars). It will return a string with nothing in it if everything is fine. However, if you run out of a certain coin, short-change the customer, put his credit at zero, and return a String from the changeReturn method to be displayed by the calling method. The String being returned from changeReturn() should inform the customer he's just been swindled.
This is the output the results
// example of an item which has been out of stock since the late 1970's
Credit: $0.00
0. Insert Money
1. Twix 95 cents
2. Snickers $1.00
3. Marathon Bar 25 cents
4. Change Return
Selection: 3
Please insert more money for this item.
This is what i have to far on my program..
import java.util.Scanner;
public class testingvendingmachine {
public static int amount,nickles,quarters,dimes = 0;
public static void main(String[] args) {
//Declare and start a boolean rules using the "while-loop"
while(true){
showSelection();
}
}
//printf use this and the use thhe fucntions on the global variables
public static void showSelection() {
System.out.printf("credit: $%.2f", (double)amount/100);
System.out.printf(" 0, Please insert money $%.2f ",(double)amount/100);
System.out.println(" 1, Sourpatches - $0.75 ");
System.out.println(" 2, Sunchips - $1.00 ");
System.out.println(" 3, Mountaindew - $1.25 ");
System.out.println(" 4, Lifesavers - $0.25 ");
//Declare the scanner and input the results into "Decision"
Scanner input = new Scanner(System.in);
int decision = input.nextInt();
// Declare how much each item cost in the form of "ints"
int sourpatches = 75;
int sunchips = 100;
int mountaindew = 125;
int lifesavers = 25;
//Declare the quantity of items
int candy = 3;
int chips = 6;
int sodapop = 4;
int mints = 2;
//start the switch cases (range: case 0-case 4)
switch(decision){
//declare the insertmoney method and a case to include it in
case 0:
insertMoney(); //ask the customer to insert coins
break;
case 1:
if(amount < sunchips)
System.out.println("Please insert more money for this item.");
else if(chips == 0)
vend(" The item is unavailable ");
else if (amount == sunchips)
System.out.println(" Dispensed one sunchip bag, enjoy ");
else{
chips--;
vend(chips);
}
break;
case 2:
if(amount < sourpatches)
System.out.println("Please insert more money for this item.");
else if(candy == 0)
vend(" The item is unavailable ");
else if (amount == sourpatches)
System.out.println(" Dispensed one sourpatch candy, enjoy ");
else{
candy--;
vend(candy);
}
break;
case 3:
if(amount < mountaindew)
System.out.println("Please insert more money for this item.");
else if(sodapop == 0)
vend(" The item is unavailable ");
else if (amount == mountaindew)
System.out.println(" Dispensed one mountaindew drink, enjoy. ");
else{
sodapop--;
vend(sodapop);
}
break;
case 4:
if (amount < lifesavers)
System.out.println(" Please insert more money for this item. ");
else if(mints == 0)
vend(" The item is unavailable ");
else if (amount == lifesavers)
System.out.println(" Dispensed one lifesaver candy ");
else{
lifesavers--;
vend(mints);
break;
}
case 5:
//System.out.print(changeReturn());
break;
default:
showSelection();
break;
}
}
public static void insertMoney() {
System.out.printf(" credit: $%.2f",(double)amount/100);
System.out.println(" 1,insert a nickle ");
System.out.println(" 2,insert a dime ");
System.out.println(" 3, insert a quarter");
System.out.println(" 4, insert one dollar ");
Scanner input = new Scanner(System.in);
int moneyChoice = input.nextInt();
System.out.println(moneyChoice);
switch(moneyChoice){
case 1:
nickles++;
amount += 5;
showSelection();
System.out.printf("Credit: $%.2",(double)amount/100);
break;
case 2:
dimes++;
amount += 10;
showSelection();
break;
case 3:
quarters++;
amount += 25;
showSelection();
break;
case 4:
amount += 100;
showSelection();
break;
default:
showSelection();
break;
}
}
public static void vend(String jj){
System.out.println(jj);
}
public static void vend(int charge){
amount -= charge;
}
public String changeReturn(){
return "";
//for this part of the project, do not give the customer's change until they select "change return" from the menu
}
}
Explanation / Answer
import java.util.Scanner;
public class testingvendingmachine {
public static int amount,nickles,quarters,dimes = 0;
public static void main(String[] args) {
//Declare and start a boolean rules using the "while-loop"
while(true){
showSelection();
}
}
//printf use this and the use thhe fucntions on the global variables
public static void showSelection() {
System.out.printf("credit: $%.2f", (double)amount/100);
System.out.printf(" 0, Please insert money $%.2f ",(double)amount/100);
System.out.println(" 1, Sourpatches - $0.75 ");
System.out.println(" 2, Sunchips - $1.00 ");
System.out.println(" 3, Mountaindew - $1.25 ");
System.out.println(" 4, Lifesavers - $0.25 ");
System.out.println(" 5, Change Return");
//Declare the scanner and input the results into "Decision"
Scanner input = new Scanner(System.in);
int decision = input.nextInt();
// Declare how much each item cost in the form of "ints"
int sourpatches = 75;
int sunchips = 100;
int mountaindew = 125;
int lifesavers = 25;
//Declare the quantity of items
int candy = 3;
int chips = 6;
int sodapop = 4;
int mints = 2;
//start the switch cases (range: case 0-case 4)
switch(decision){
// declare the insertmoney method and a case to include it in
case 0:
insertMoney(); //ask the customer to insert coins
break;
case 1:
if(amount < sunchips)
System.out.println("Please insert more money for this item.");
else if(chips == 0)
vend(" The item is unavailable ");
else if (amount == sunchips)
System.out.println(" Dispensed one sunchip bag, enjoy ");
else{
chips--;
vend(chips);
}
break;
case 2:
if(amount < sourpatches)
System.out.println("Please insert more money for this item.");
else if(candy == 0)
vend(" The item is unavailable ");
else if (amount == sourpatches)
System.out.println(" Dispensed one sourpatch candy, enjoy ");
else{
candy--;
vend(candy);
}
break;
case 3:
if(amount < mountaindew)
System.out.println("Please insert more money for this item.");
else if(sodapop == 0)
vend(" The item is unavailable ");
else if (amount == mountaindew)
System.out.println(" Dispensed one mountaindew drink, enjoy. ");
else{
sodapop--;
vend(sodapop);
}
break;
case 4:
if (amount < lifesavers)
System.out.println(" Please insert more money for this item. ");
else if(mints == 0)
vend(" The item is unavailable ");
else if (amount == lifesavers)
System.out.println(" Dispensed one lifesaver candy ");
else{
lifesavers--;
vend(mints);
break;
}
case 5:
//System.out.print(changeReturn());
break;
default:
showSelection();
break;
}
}
public static void insertMoney() {
System.out.printf(" credit: $%.2f",(double)amount/100);
System.out.println(" 1,insert a nickle ");
System.out.println(" 2,insert a dime ");
System.out.println(" 3, insert a quarter");
System.out.println(" 4, insert one dollar ");
Scanner input = new Scanner(System.in);
int moneyChoice = input.nextInt();
System.out.println(moneyChoice);
switch(moneyChoice){
case 1:
nickles++;
amount += 5;
showSelection();
System.out.printf("Credit: $%.2",(double)amount/100);
break;
case 2:
dimes++;
amount += 10;
showSelection();
break;
case 3:
quarters++;
amount += 25;
showSelection();
break;
case 4:
amount += 100;
showSelection();
break;
default:
showSelection();
break;
}
}
public static void vend(String jj){
System.out.println(jj);
}
public static void vend(int charge){
amount -= charge;
}
public String changeReturn(){
return "";
//for this part of the project, do not give the customer's change until they select "change return" from the menu
// The language is not clear. Please explain it again in a better way, what to put in this method!!
}
}