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 2 of 2

Thread: Making a vending machine, trouble asking user to insert more money.

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Making a vending machine, trouble asking user to insert more money.

    So I am making a vending machine and am having trouble knowing what exactly to use or how to go about giving the user a error message depending if they initially added enough money for their choice of drink. So if the user only input 1 dollar, and the drink they select is $1.25, they need to add the $.25, but how do I implement that in this code?

    Here is my code along with the zip file just in case.





    package vending.sample;

    import java.util.Scanner;

    public class VendingSample {

    public static void main(String[] args) {

    int coke = 1, sprite = 2, DrPepper = 3, Pepsi = 4, Fanta = 5, Water = 6, selection, i;
    double change, total;
    //boolean bool = true;

    Scanner scan = new Scanner (System.in);

    do {

    total = calTotal();
    if (total > 0 && total < 20)
    System.out.println("You entered: " + total );

    else
    System.out.println("Please restart and enter a proper ammount");

    System.out.println("\nPlease make a drink selection: \n"
    + "1 = Coke ($1.25)\n2 = Sprite($1.00)\n3 = DrPepper($1.35)\n4 = Pepsi($1.25)\n5 = Fanta($.75)"
    + "\n6 = Water($1.50)");
    selection = scan.nextInt();

    //insuf();

    if (total < selection)
    System.out.println("Please enter correct ammount \n\n");

    if (selection == 1){
    System.out.println("You selected: Coke\n\n");
    if (total < 1.25)
    {
    System.out.println("Please enter more money");
    System.out.println("How much would you like to add?");
    }
    change = total - 1.25;
    System.out.println("Your change is $" + change + "\n\n");

    }

    else if (selection == 2)
    {
    System.out.println("You selected: Sprite\n\n");
    change = total - 1;
    System.out.println("Your change is $:" + change + "\n\n");
    }

    else if (selection == 3)
    {
    System.out.println("You selected: DrPepper\n\n");
    change = total - 1.35;
    System.out.println("Your change is $:" + change + "\n\n");
    }

    else if (selection == 4)
    {
    System.out.println("Your choice was Pepsi");
    change = total - 1.25;
    System.out.println("Your change is $:" + change + "\n\n");
    }

    else if (selection == 5)
    {
    System.out.println("Your choice was Fanta");
    change = total - .75;
    System.out.println("Your change is $:" + change + "\n\n");
    }
    else if (selection == 6)
    {
    System.out.println("Your choice was Water");
    change = total - 1.50;
    System.out.println("Your change is $:" + change + "\n\n");
    }
    else
    System.out.println("Please enter valid selection.");

    System.out.println("Would you like to order another drink? (1 = y / 2 = n)");
    i = scan.nextInt();
    System.out.println("\n \n \n");
    } while (i == 1);

    }
    static double calTotal()
    {
    Scanner scan = new Scanner (System.in);
    double dollar, quarters, dimes, nickles, pennies, total;
    System.out.println("Please enter ammount of Dollars, Quarters, Dimes, Nickles,"
    + " and Pennies you will be entering: ");
    dollar = scan.nextInt();
    quarters = scan.nextInt();
    dimes = scan.nextInt();
    nickles = scan.nextInt();
    pennies = scan.nextInt();

    total = (1 * dollar) + (.25 * quarters) + (.10 * dimes) + (.05 * nickles) + (.01 * pennies);
    return(total);
    }
    }
    Attached Files Attached Files


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Making a vending machine, trouble asking user to insert more money.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

Similar Threads

  1. Vending Machine
    By javaStooge in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2014, 02:56 PM
  2. Making money with java knowledge?
    By Eto Demerzel in forum The Cafe
    Replies: 1
    Last Post: October 5th, 2013, 08:06 AM
  3. Java vending machine, minor thing.
    By Aprok in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2011, 02:35 PM
  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