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: I Require some guidance :)

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I Require some guidance :)

    Hello, my name is Ryan I have been set an assignment for my procedural programming teacher to create a very simplistic and basic vending machine. I have elected to make my version in Java and have currently hit a wall in my progress. This is the code I have constructed at the moment.

    package cool;
    import java.util.Scanner;
    public class Main {

    public static void main(String[] args) {

    int Black_Tea = 1;
    int White_Tea = 2;
    int Black_Coffee = 3;
    int White_Coffee = 4;
    int Cappaccino = 5;
    int Hot_Chocolate = 6;

    System.out.println("Welcome User!");

    System.out.println("Please Select a number relative to your choice from the menu below:");


    System.out.println("1 - Black Tea");
    System.out.println("2 - White Tea");
    System.out.println("3 - Black Coffee");
    System.out.println("4 - White Coffee");
    System.out.println("5 - Cappuccino");
    System.out.println("6 - Hot Chocolate");

    Scanner scanner = new Scanner(System.in);




    As you can see I haven't really done much but I have made a start. As you can see I have tried to assign variables for each of the items I wish my vending machine to dispense. With them variables I am trying to make it so the user can simply input a number that is relative to the item the wish to purchase. For instance, If I wanted some White Coffee I would simply input the number 4.

    This is were my troubles develop. How can I make it so the scanner understands what information has been selected and save that as a variable to use throughout the process. Just like when 1 is selected the scanner then is equal to 1 and in extension 1 is equal to the variable "Black Tea". After this I am planning to create an if statement for each of the items.

    Any help and advice is welcome and would be much appreciated
    I am rather experience and don't understand everything about JAVA at the moment so there might be something very simple I am missing that will allow me to continue in the creation of this vending machine.

    Thanks.


  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: I Require some guidance :)

    Please review the Announcement topic at the top of each sub-forum to learn how to post your code correctly along with many other useful things.

    The initial int variables you've created aren't that useful and will probably not be used, but they could be used as constants, in which case their names should be all caps. Creating a menu that accepts the user's input and then repeats is a common early assignment in programming. The repeating menu functionality would typically be accomplished by putting the menu/choice logic inside a while() statement that accepts the user's input until an exit flag or menu selection is chosen.

    The Scanner object you've created to accept the user's input has to be put to work. You should review the Scanner API to see the methods available, but a common usage might be:

    String userInput = scanner.nextLine();

    And then your program would act upon the user's input stored in userInput using either if statements or possibly a switch statement. Since we don't know what you've learned or are allowed to use, you'll have to continue from here and come back when you need help.

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

    Default Re: I Require some guidance :)

    Thank you very much for the advice! Sorry about incorrectly posting code, I am unsure how to post it in the correct manner but I shall look it up when I post in the future.

    Thank you for the advice. I was unsure how to go about this, I am not really used to java as yet but I thought it would be wise to challenge myself. As you can see I am rather terrible at it but I do hope to get better. I am not great with program structure yet, I have only spent one class on Flow-chart/algorithm design and implementation and it was not very helpful for this small project. In my procedural programming class we have only really just gone over data types and simple statements - mainly If/Else, Looping (While and For). I believe at the end of this we will be using our skills to create an ATM. In extension to this we will be working in conjunction with another class I am taking - software development and design to create GUI's to present our code in a more user friendly way.

    Other than this It is only early days on the course and we haven't gone into great detail and we have only really covered the basics of programming. When my teacher set the assignment they did in fact say we shouldn't worry if we could not complete it as they couldn't really measure the level we were working at. We are all being treated as beginners at the moment which is great so their isn't a massive amount of pressure to complete this task I just thought it would be a good challenge.

    Thanks again, I shall try to fix it based on your advice. I shall also re-evaluate how I present my posts in future - sorry if I have caused any trouble.

  4. #4
    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: I Require some guidance :)

    Then I suggest you use the structure:
    while ( true )
    {
        // present the user with the menu
     
        // get the user's choice
     
        // act on the user's choice using if statements
        // like if ( userChoice == 1 ) { do this }, etc.
     
        // break out of the while loop if the exit menu item is chosen
        if ( userChoice == 10 ) // 10 is the choice to exit
        {
            break;
        }
     
    }
    Good luck!

Similar Threads

  1. Require a variable in an interface?
    By sci4me in forum Object Oriented Programming
    Replies: 2
    Last Post: February 28th, 2013, 05:05 AM
  2. [SOLVED] Does RMI require separate threads
    By mds1256 in forum Threads
    Replies: 1
    Last Post: November 17th, 2012, 09:38 AM
  3. [SOLVED] KeyListeners Require Focus...
    By snowguy13 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 6th, 2012, 08:15 AM
  4. Code require for Drag and drop
    By anujbatta in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 25th, 2011, 07:54 AM