• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Vending Machine Project

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!");
}






}
}
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Round these parts, posting the same question in multiple forums is bad practice. It can make it rather difficult to follow a conversation, plus folks spend time saying things that were just said.

Please continue this conversation in the duplicate thread. I'm closing this thread.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic