Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java vending machine, minor thing.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java vending machine, minor thing.

    Hello. I have to make a vending machine in.. Java of course. The problem is, when the user is supposed to get his money back. The product costs 25.
    If you put in the exact amount, it works fine. If you put in too little, and then the exact amount it works fine. If you put in too little, and then too much, it gives you the change back perfectly.
    But.. If you put in too much at first, then the program "thinks" the user have put in the perfect amount..

    Could you look at my code? Shouldn't be a big deal, just some minor detail probably.

    Thanks

     import java.util.*;
     
    public class Vendingmachine {
     
    	public static int colaAvailable = 2;
    	public static int mineralWaterAvailable = 1;
    	public static int lemonadeAvailable = 3;
     
    	public static int canPrice = 25;
     
    	public static void main(String[] args) {
     
    		// if there are any cans left in the vending machine, it will continue. If the method cansAvailable validates to true, the program will continue.
    		while (cansAvailable())
    			choosedrink();
     
    		// else it calls the method outoforder and shuts down
    		outoforder();
    	}
     
    	public static void choosedrink() {
    	//loading scanner to get the user input
     
     
     
    	System.out.println("Welcome to the vending machine.");
    	System.out.println("Select one of the following drinks;");
     
     
     
    	if(colaAvailable > 0)
    		System.out.println("1 - Cola");
     
     
    	if(mineralWaterAvailable > 0)
    		System.out.println("2 - Mineral water");
     
     
    	if(lemonadeAvailable > 0)
    		System.out.println("3 - Lemonade");
     
    	Scanner console = new Scanner(System.in);
     
     
    	int selection = console.nextInt();
     
     
     
    	System.out.println("Now insert 25 Rupiah");
     
     
     
    	int coininput, totalAmount = canPrice;
    	coininput = console.nextInt();
    	totalAmount = totalAmount-coininput;
     
     
    	while ( totalAmount > 0) { // continues payment, until exact amount or too much is payed, in which case the machine returns the change
    		if (totalAmount > 0) { // not enough money input, put more in
    			System.out.println("Please insert " + totalAmount + " Rupiah");
    			coininput = console.nextInt();
    			totalAmount = totalAmount-coininput;
    		}
     
     
    		if (totalAmount < 0) { // too much money put in, change back
    		System.out.println("Thank you. Here is your change; " + -totalAmount+ " Rupiah");
     
    		totalAmount = totalAmount-coininput;
    		}
     
     
    	}
     
     
    	//launches the subsraction method, so the machine "knows" that there are on less can in the machine
    	cansubstraction(selection);
     
     
     
    	//when totalAmount is not less or larger than 0, it must be = 0, and then this print comes
    	System.out.println("Thank you, here is your can."); // perfect amount of money input, merchandise is given
    	System.out.println();
     
     
     
    	}
     
     
     
     
    	public static boolean cansAvailable () {
    // this metod checks if there is any cans left. if just one of these 3 variables validates to true (over 0 left)
    //then it returns the true statement and the program will run
     
    		if(colaAvailable > 0 || mineralWaterAvailable > 0 || lemonadeAvailable > 0)
    			return true;
    // if it's not true, of course then it returns false and the programs activates the method that forces the machine to stop
    		return false;
    	}
     
    	public static void cansubstraction (int selection) {
     
    		// if either, 1,2 or 3 was selected, this method substracts one from that selection.
    	if(selection == 1)
    		colaAvailable--;
    	else if(selection == 2)
    		mineralWaterAvailable--;
    	else if(selection == 3)
    		lemonadeAvailable--;
     
    	}
    // gives the following message, if there are no cans left in the machine
    	public static void outoforder() {
    	System.out.println("The machine is temporarily out of order");
     
    	}
     
    }
    Last edited by Aprok; October 5th, 2011 at 02:34 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java vending machine, minor thing.

    and sorry, it don't know how to wrap it all in code, so it's not just plain text.. How do I do that?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: Java vending machine, minor thing.

    [highlight=java]Code goes here[/highlight]

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java vending machine, minor thing.

    Thank you

Similar Threads

  1. [SOLVED] Please help with this... I think I have a minor coding error
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2011, 08:02 AM
  2. SEARCH MACHINE IN JAVA
    By charis3 in forum Java Theory & Questions
    Replies: 2
    Last Post: December 9th, 2010, 12:19 PM
  3. Moving MySql Database from one machine to another machine
    By vaishali in forum JDBC & Databases
    Replies: 5
    Last Post: July 21st, 2010, 01:21 AM
  4. vending machine submit button help
    By maybach230 in forum Java Applets
    Replies: 3
    Last Post: April 23rd, 2010, 10:16 AM
  5. need help with Vending Machine code...
    By mia_tech in forum Loops & Control Statements
    Replies: 3
    Last Post: April 20th, 2009, 05:24 AM

Tags for this Thread