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

Thread: Newbie needing help with some variables(?) help

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Location
    Melbourne, Australia
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Newbie needing help with some variables(?) help

    Hi all just joined the forum and am new to everything Java and programming in general

    I was just looking for some help with an issue i have if anyone would be able to help?

    I am trying to do a ticketing system, very basic, and need to add/multiply the cost of the ticket by the amount of the ticket requested.

    e.g.
    User offered different kinds of tickets
    user types '1'
    User offered to choose how many tickets they want.
    user types '3'

    What i need to be able to do is assign a value to the first input and the multiply it by the second.

    I have put the code for my current project below

    I am looking to do what i said above toward the end of the piece eg in the output of public static void ConfirmPurchase()

    I need to assign the values 1,2,3 a dollar value to get a grand total in the System.out.println("Total Amount for "+tickets+": $");

    Just struging on how to do it.


    Thanks in advance for anything and for letting me be apart of your community.


    package movie;
     
    // Import Utils
    import java.io.InputStream;
    import java.util.Scanner;
     
    /**
     *
     * @author Mark
     */
    public class Movie {
     
        static Scanner scan = new Scanner(System.in);
        //static String name;
        static String [] list = {"1) Child","2) Adult","3) Senior"};
        static int number;
        static int tickets;
        static int confirm;
     
        // Might put this in
        //public static void getName(){
        //    System.out.println("Please enter you name");
        //    name=scan.nextLine();
        //    System.out.println("Hello "+name);
        //    getOption();
        //}
     
        public static void getOption(){
     
            System.out.println("Enter you option:");
            //for(int i=0;i<list.length;i++){
            //    System.out.println(list[i]);
            //}
            number=scan.nextInt();
     
            if (number > 3)
            {
                System.out.println("Please enter a valid number from 1 - 3");
                getOption();
            }
            if (number < 1)
            {
                System.out.println("Please enter a valid number from 1 - 3");
                getOption();
            }
     
            System.out.println("You selected "+list[number-1].substring(3, list[number-1].length()));
     
            getTickets();
        }   
     
        public static void getTickets(){
            System.out.println("Please Enter total number of Tickets");
            tickets=scan.nextInt();
            System.out.println("Thank You, \nYou Have selected "+tickets+" ticket(s) for "+list[number-1].substring(3, list[number-1].length()));
     
            ConfirmPurchase();
        }    
     
        public static void ConfirmPurchase(){
     
            //System.out.println("You are purchasing "+tickets+" at $ each \nPress 1 for confirmation of purchase:");
            System.out.print("You are purchasing "+tickets+" "+list[number-1].substring(3, list[number-1].length()));
            System.out.println(" at TOTAL $$$");
            System.out.println("CONFIRM PURCHASE:");
            confirm=scan.nextInt();
            if (confirm < 1){
                getOption();
            }
            if (confirm > 1){
                getOption();
            }
     
            else{
            System.out.println("Total Amount for "+tickets+": $");
            }
     
        }    
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            System.out.println("        @@@@@  Welcome To Zoos Victoria @@@@@");
            System.out.println("                   M A I N  M E N U ");
            System.out.println("        Zoo Has the following ticketing options:");
            System.out.println("             1 =  Child (4-5 yrs)");
            System.out.println("             2 =  Adult (18+ yrs)");
            System.out.println("             3 =  Senior (60+ yrs)"); 
     
            System.out.println("");
            getOption();
        }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,691
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Newbie needing help with some variables(?) help

    There are problems with the structure of the program. For example the getOption method should use a loop that continues until the user has entered the correct value and then return that correct value to the caller. The getOption method should not call itself.

    The getTickets method should not call the ConfirmPurchase method. It should get the number of tickets and return that value to the caller.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Keropy (December 8th, 2018)

  4. #3
    Junior Member
    Join Date
    Dec 2018
    Location
    Melbourne, Australia
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Newbie needing help with some variables(?) help

    Thanks for that Norm
    Sorry but this is still a bit Greek to me.

    I can only assume that i should merge the ConfirmPurchase and GetTickets in to the same method?
    And then try and get the variable to be assigne and then multiplied in that

    The final out put i am looking for is along the lines of.
    run:
    @@@@@ Welcome To Zoos Victoria @@@@@
    M A I N M E N U
    Zoo Has the following ticketing options:
    1 = Child (4-5 yrs) has a value of $18.00
    2 = Adult (18+ yrs) has a value of $36.00
    3 = Senior (60+ yrs) has a value of $32.50

    Enter you option:
    1 <option from above
    You selected Child
    Please Enter total number of Tickets
    2 <user input
    Thank You,
    You Have selected 2 ticket(s) for Child
    You are purchasing 2 Child at TOTAL $18.00
    CONFIRM PURCHASE:
    1 <user input
    Total Amount for 1: $32.00 <sum of the amount wanted and the option selected

    Main question was how do i get this calculation?

    I have kind of hit a wall with what i am doing

    Sorry and Thanks

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,691
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Newbie needing help with some variables(?) help

    I suggest that you start over on the design of the program.
    First make a list of the steps the program must take.
    Then work on writing the code for each step.
    When the code for a step has been written, test it and make sure it works.
    When the code for a step works as expected, move to writing the code for the next step.

    A method should do just one simple thing.
    All get... methods should return the value that was gotten.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Keropy (December 8th, 2018)

  7. #5
    Junior Member
    Join Date
    Dec 2018
    Location
    Melbourne, Australia
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Newbie needing help with some variables(?) help

    Bummer....

    Thanks for the advice. I will give it a go
    Might see me here again soon

    Thanks again Norm ^ ^
    Cheers

  8. #6
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    81
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Newbie needing help with some variables(?) help

    Thank you for your input. That means this program is not integrated. getTickets( ) should not call another class or method. So what your saying all the variables must be contained in one program that executes that specific function. And cannot be written to call another method.

Similar Threads

  1. Replies: 3
    Last Post: October 20th, 2013, 01:39 PM
  2. Newbie needing a little help
    By Bgriesh in forum Loops & Control Statements
    Replies: 3
    Last Post: September 24th, 2012, 08:19 PM
  3. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  4. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 07:33 AM
  5. Java and programming newbie needing help
    By speedmaster in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 8th, 2010, 07:57 AM

Tags for this Thread