Non static variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mygolfcircle
    New Member
    • Feb 2008
    • 8

    Non static variable

    Can someone pls help ? How do I get my global variables destinationCity Code, numberOfAdults and isReturn to be populated with values from the user ?
    I will need them for calculations in to be passed to other methods later. Thanks in advance... pls help.

    Code:
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.io.*;          // Access System.out
     
    public class MyBooking
    {
    
        Scanner in = new Scanner(System.in);
        static String destinationCityCode;
        static int numberOfAdults = 0;
        static boolean isReturn = true;
        static double ticketPrice = 0.00;  
    
        public static void main(String[] args)
        {
            showMenu();
            getDestinationCityCode();
            getNumberOfAdults();
            getIsReturnTrip();
        }
    
        
    
        public static void showMenu() {
            System.out.println("Welcome to Travel World");
            System.out.println("-------------------------------------------");
            System.out.println("");
            System.out.println("Special Deals on Flights!");
            System.out.println("-------------------------");
            System.out.println("1. Los Angeles (LAX)  - $650          2. Kuala Lumpur (KLC)  - $200");
            System.out.println("3. Brunei      (BRN)  - $300          4. Philippines  (PLP)  - $410");
            System.out.println("5. Bali        (BAL)  - $380          6. Bangkok      (BKK)  - $190");
            System.out.println("");
            System.out.println("Prices shown are PER ADULT for ONE WAY journey.");
            System.out.println("");
         }  
    
         
         public static void getDestinationCityCode() {
            System.out.print("Enter Destination City Code > ");
            destinationCityCode = in.nextLine();
        }
    
        public static void getNumberOfAdults() {
          System.out.print("Enter Number of Adults Travelling > ");
          numberOfAdults = in.nextInt();
        }
    
        public static void getIsReturnTrip() {
          System.out.print("Enter 'R' for Return journey, or 'W' for One-Way > ");
            if(in.next().charAt(0) == 'R') {
                isReturn = true;
            }
            else {
                isReturn = false;
            } 
       }    
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What I see from your code is that you are *setting* your static variables given
    user supplied values in the three *get* methods. Isn't that what you want?
    btw, it sort of violates a lot of object oriented programming principles though.

    kind regards,

    Jos

    Comment

    • mygolfcircle
      New Member
      • Feb 2008
      • 8

      #3
      Any suggestions on how i should modify the code so as to not violate oo principles?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by mygolfcircle
        Any suggestions on how i should modify the code so as to not violate oo principles?
        Maybe you can start with a simple class Booking that has a single constructor
        with three arguments: the city code, the number of adults and a boolean flag
        indicating a one way or round trip. Add three getters for those values and skip
        the setters. Something like:

        [code=java]
        public class Booking {
        private String destination;
        private int nofAdults;
        private boolean round;

        Booking(String destination, int nofAdults, boolean round) {
        this.destinatio n= destination;
        this.nofAdults= nofAdults;
        this.round= round;
        }
        // three getters for the three member vars here
        }
        [/code]

        Note that the constructor isn't public (it has package scope) so something in
        the same package as the Booking class should build a Booking for you. That
        thing should ask for the values of those three variables, build a Booking object
        and return it to you.

        Such a thing is commonly called a 'factory', so a BookingFactory would be a nice
        name for it. It should ask the user for the values and build a Booking. The factory
        can be a singleton object, but it needn't be one. Automagically it can create
        Bookings in parallel if you keep those three values in local variables, local to a
        method that builds a Booking for you.

        Can you take it from here?

        kind regards,

        Jos

        Comment

        Working...