aspose file tools*
The moose likes Beginning Java and the fly likes Vending Machine. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vending Machine." Watch "Vending Machine." New topic
Author

Vending Machine.

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Need help with a vending machine program. Please reply.


) ) ) )
r phipps
Ranch Hand

Joined: Sep 14, 2004
Posts: 60
What Kind of Help?
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!");
}






}
}
r phipps
Ranch Hand

Joined: Sep 14, 2004
Posts: 60
As far as being a mutiple of 5 try the following.

if (money % 5 == 0) \\ Then is it is a Mutiple of 5.


The % only returns the remainder of divison so if it is zero it is a mutiple otherwise it is not.
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
To test for multiples of 5, you could try something like:

You can use this approach (with the modulo or remainder operator or whatever you want to call it) to test for multiples, odd or even numbers, etc.

Hope this helps,

Dun Dagda


SCJP 1.4<br />SCWCD (in progress)
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Thank you Dun Dagda! I will surely try this approach. I appreciate your help greatly! Thank you so much.

Rose
jesspher mesia
Greenhorn

Joined: Oct 10, 2013
Posts: 2
what does aspose do?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Vending Machine.