4 Replies - 4175 Views - Last Post: 14 February 2011 - 08:11 AM Rate Topic: -----

#1 shortman12012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 09-February 11

vending machine

Posted 14 February 2011 - 07:35 AM

I need to make a program that creates a vending machine that only accepts more then 25 cents and less then 100 cents and increments of 5. I need to display error messages if one of these rules arent applied.
public class VendingMachine {
    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);


    int amount, originalAmount,
            quarters, dimes, nickels, change, endChange, quarter, dime, nickel, money;

        System.out.println("Enter price of item");
        System.out.println("from 25 cents to a dollar, in 5-cent increments:");





        amount = keyboard.nextInt( );

        if (amount < 25);
        System.out.println("You must enter an amout over 25 cents.");
        if (amount > 100);
        System.out.println("You must enter an amount less than 100.");
        if ((amount < 25) || (amount > 100));
        System.exit(0);
        if (amount % 5 == 0);
        System.out.println("You must enter in increments of 5");


        if ((amount>25) || (amount<100));
        originalAmount = amount ;
        quarters = ((100-amount) / 25);
        amount= (100-amount) % 25;
        dimes = amount / 10;
        amount = amount % 10;
        nickels = amount / 5;
        amount = amount % 5;

        System.out.println("You bought an item for " + originalAmount + " cents and gave me a dollar, so your change is:");
        System.out.println(quarters + " quarters,");
        System.out.println(dimes + " dimes,");
        System.out.println(nickels + " nickels");

    }
}



the problem im having is the program doesnt work and it just displays the two error messages
You must enter an amout over 25 cents.
You must enter an amount less than 100.

Is This A Good Question/Topic? 0
  • +

Replies To: vending machine

#2 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: vending machine

Posted 14 February 2011 - 07:44 AM

Hi,

The problem is that all your if statements have semi colons after them, thus meaning that they do nothing in the program as they don't have a body. For example,

your writing this:

if (amount < 25); //note the sneaky semi colon
 System.out.println("You must enter an amout over 25 cents.");



you should be doing this:

if (amount < 25)
        System.out.println("You must enter an amout over 25 cents.");



Also, just to make things a bit clearer/easier for yourself, I would use curly braces or inline statements like this:

if (amount < 25){ //if you have a curly brace here, you may be less tempted to put a semi colon!
   System.out.println("You must enter an amout over 25 cents.");
}

//OR...

if (amount < 25) System.out.println("You must enter an amout over 25 cents."); //inline - again, this may stop you putting a semi colon



This post has been edited by [email protected]: 14 February 2011 - 07:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 shortman12012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 09-February 11

Re: vending machine

Posted 14 February 2011 - 07:54 AM

okay i did all of that and now have
 int amount, originalAmount,
            quarters, dimes, nickels;

        System.out.println("Enter price of item");
        System.out.println("from 25 cents to a dollar, in 5-cent increments:");





        amount = keyboard.nextInt( );

        if (amount < 25)
        {
        System.out.println("You must enter an amout over 25 cents.");
        }

        if (amount > 100)
        {
        System.out.println("You must enter an amount less than 100.");
        }

        if ((amount < 25) || (amount > 100))
        {
        System.exit(0);
        }
        
        if (amount % 5 == 0)
        {
        System.out.println("You must enter in increments of 5");
        }

        if ((amount>25) || (amount<100))
        {
        originalAmount = amount ;
        quarters = ((100-amount) / 25);
        amount= (100-amount) % 25;
        dimes = amount / 10;
        amount = amount % 10;
        nickels = amount / 5;
        amount = amount % 5;

        System.out.println("You bought an item for " + originalAmount + " cents and gave me a dollar, so your change is:");
        System.out.println(quarters + " quarters,");
        System.out.println(dimes + " dimes,");
        System.out.println(nickels + " nickels");



however now it skips all the first if statments that checks for the <25 and >100 and 5 cent increments and just goes and displays the change
Was This Post Helpful? 0
  • +
  • -

#4 shortman12012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 09-February 11

Re: vending machine

Posted 14 February 2011 - 08:10 AM

nvm i figured it out, this is the final code
    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);


    int amount, originalAmount,
            quarters, dimes, nickels;

        System.out.println("Enter price of item");
        System.out.println("from 25 cents to a dollar, in 5-cent increments:");





        amount = keyboard.nextInt( );

        if (amount < 25)
        {
        System.out.println("You must enter an amout over 25 cents.");
        }

        if (amount > 100)
        {
        System.out.println("You must enter an amount less than 100.");
        }

     
        
        if (amount % 5 == 1)
        {
        System.out.println("You must enter in increments of 5");
        }

        if ((amount < 25) || (amount > 100)|| (amount % 5 == 1) )
        {
        System.exit(0);
        }

        if ((amount>25) || (amount<100) || amount % 5 == 0 )
        {
        originalAmount = amount ;
        quarters = ((100-amount) / 25);
        amount= (100-amount) % 25;
        dimes = amount / 10;
        amount = amount % 10;
        nickels = amount / 5;
        amount = amount % 5;

        System.out.println("You bought an item for " + originalAmount + " cents and gave me a dollar, so your change is:");
        System.out.println(quarters + " quarters,");
        System.out.println(dimes + " dimes,");
        System.out.println(nickels + " nickels");
        }
    }
}


thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: vending machine

Posted 14 February 2011 - 08:11 AM

It will only skip the first two if statements now if you entered a number less than 25 or greater than 100. However, I think your 'increments' of 5 statement is incorrect. You want t display the error message if the number entered IS NOT an increment of 5. Therefore, you need to use the NOT symbol, which is !. Like this:

 if (!(amount % 5 == 0)) //if amount is NOT exactly divisible by 5, THEN display error message 
 {
    System.out.println("You must enter in increments of 5");
 }




Also, just a small point for the future...

...your final if statement seems redundant in that if the program gets that far without exiting, then you know that the if statement will evaluate to true, otherwise the program would have exited in the previous statement. Also, shouldn't it use 'and' (&&) not 'or' (||)?

This post has been edited by [email protected]: 14 February 2011 - 08:34 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1