aspose file tools*
The moose likes Servlets and the fly likes Vending Machine Project Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Vending Machine Project" Watch "Vending Machine Project" New topic
Author

Vending Machine Project

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Ok would someone look at this code and please tell me what I need to do to make the program run correctly? It is supposed to give the user a message if they enter any money below 25 cents or over 100. And it's not doing what it's supposed to. Plus, I have no idea how to code the part about only taking multiples of five. I know there are some smart, smart java guys out there that can help me. I'd appreciate anything you can tell me.

Rose

//**********************************************************
//* This program is a Vending Machine that accepts money *
//* from 25 cents to 100. *
//* If the user enters an amount lower than 25 cents, an *
//* error message should say, "A cost under 25 cents." *
//* If the user enters an amount over 100 an error *
//* message should say,"A cost that is more than a dollar."*
//* The user should also only be able to enter a price in *
//* multiple of fives, an error message should say,"A cost *
//* that is not an integer multiple of five." *
//**********************************************************
//* Author: Rose Evans *
//* Date last modified: October 14th 2004. *
//**********************************************************

public class ChangeMakerModified
{
public static void main(String[] args)
{
int CANDYBAR = 55;
int DRINK = 65;
int PEANUTS = 50;
int CHIPS = 45;
int originalAmount, quarters, dimes, nickels, pennies;
int choice = 0;
int money = 0;
int amount = 0;

System.out.println("**********************************");
System.out.println("* Vending Machine *");
System.out.println("* Candy Bar - 55 Press 1 *");
System.out.println("* Drink - 65 Press 2 *");
System.out.println("* Peanuts - 50 Press 3 *");
System.out.println("* Chips - 45 Press 4 *");
System.out.println("**********************************");

System.out.println("Please enter your selection.");
choice = SavitchIn.readLineInt();

System.out.println("Enter an amount of money no less than 25");
System.out.println("cents, and no more than 100.");
System.out.println("I will output a combination of coins");
System.out.println("that equals that amount of change.");

{
money = SavitchIn.readLineInt();

//if (money < 25);
//System.out.println("You must enter an amout over 25 cents.");
//if (money > 100);
//System.out.println("You must enter an amount less than 100.");
// if ((money < 25) && (money > 100));
//System.exit(0);


}
switch(choice)

{
case 1:

System.out.println("You entered: " +money);
System.out.println("You chose a Candy Bar - .55");
amount = money - CANDYBAR;

System.out.println("Your change is: "+ amount + " cents");
System.out.println(amount + " cents in coins can be given as: ");

quarters = amount/25;
amount = amount%25;
dimes = amount/10;
amount = amount%10;
nickels = amount/5;
amount = amount%5;
pennies = amount;

System.out.println(quarters + " quarter(s)");
System.out.println(dimes + " dime(s)");
System.out.println(nickels+ " nickel(s) and ");
System.out.println(pennies + " pennies");
System.out.println("Thank you! Enjoy your snack!");

break;

case 2:

System.out.println("You entered: " +money);
System.out.println("You chose a Drink - .65");
amount = money - DRINK;
System.out.println("Your change is: "+ amount + " cents");
System.out.println(amount + " cents in coins can be given as: ");

quarters = amount/25;
amount = amount%25;
dimes = amount/10;
amount = amount%10;
nickels = amount/5;
amount = amount%5;
pennies = amount;

System.out.println(quarters + " quarter(s)");
System.out.println(dimes + " dime(s)");
System.out.println(nickels + " nickel(s) and ");
System.out.println(pennies + " pennies");
System.out.println("Thank you! Enjoy your snack!");


break;

case 3:

System.out.println("You entered: " +money);
System.out.println("You chose Peanuts - .50");
amount = money - PEANUTS;
System.out.println("Your change is: "+ amount + " cents");
System.out.println(amount + " cents in coins can be given as: ");

quarters = amount/25;
amount = amount%25;
dimes = amount/10;
amount = amount%10;
nickels = amount/5;
amount = amount%5;
pennies = amount;

System.out.println(quarters + " quarter(s)");
System.out.println(dimes + " dime(s)");
System.out.println(nickels + " nickel(s) and ");
System.out.println(pennies + " pennies");
System.out.println("Thank you! Enjoy your snack!");

break;

case 4:

System.out.println("You entered: " +money);
System.out.println("You chose Chips - .45");
amount = money - CHIPS;
System.out.println("Your change is: "+ amount + " cents");
System.out.println(amount + " cents in coins can be given as: ");

quarters = amount/25;
amount = amount%25;
dimes = amount/10;
amount = amount%10;
nickels = amount/5;
amount = amount%5;
pennies = amount;

System.out.println(quarters + " quarters");
System.out.println(dimes + " dimes");
System.out.println(nickels + " nickels and ");
System.out.println(pennies + " pennies");
System.out.println("Thank you! Enjoy your snack!");

break;

//Default message if user makes a choice other than 1 - 4.

default:

System.out.println("You must enter a selection between 1 and 4. Thank You!");
}






}
}


) ) ) )
rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
Why do you have the code commented out ?

// if (money < 25);
// System.out.println("You must enter an amout over 25 cents.");
// if (money > 100);
// System.out.println("You must enter an amount less than 100.");
// if ((money < 25) && (money > 100));
//System.exit(0);

If you look at the third if statement you are having an &&
shouldnt it be ||.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
I had commented it out just to try different things. Ill go change the && to || and see if that does anything. I am just a beginning java programmer, (Im sure you can tell) I am just totally stumped at how to do this.
Thanks so much for your help.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Hum that didn't seem to do anything. I am so stuck on this one part. Ive looked at all sorts of things to try to fix it. Im sure its something simple. My brain must be on vacation. Ha.
rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
I hope this is what you have now.

if (money < 25)
System.out.println("You must enter an amout over 25 cents.");
if (money > 100)
System.out.println("You must enter an amount less than 100.");
if ((money < 25) || (money > 100))
System.exit(0);


Note that there is no semicolon at the end of if statements like
if (money < 25);

Regarding the second part of your question

You know what the user wants to purchase and the price of the item.
Immediately after that selection call a function.

The job of the function is to prompt the user to enter an amount
in multiples of five or any amount (it depends on your requirement).
Call the function recursively If the user keys in wrong data or enters an
amount which is less then then price of the item.
If everything seems right then successfully return the amount.

Hope this helps.

[ October 14, 2004: Message edited by: rahul V kumar ]
[ October 14, 2004: Message edited by: rahul V kumar ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Hi Rose, it's me again, moving your threads to the Java In General (Beginner's) forum. Also be very careful about posting homework assignments. We can help answer questions for you, but we can't do your homework for you.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vending Machine Project
 
Similar Threads
almost there!
Need help creating classes
What am I doing wrong
Vending Machine.
Vending Machine Project