473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Non static variable

Can someone pls help ? How do I get my global variables destinationCityCode, 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.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.text.*;
  3. import java.math.*;
  4. import java.io.*;          // Access System.out
  5.  
  6. public class MyBooking
  7. {
  8.  
  9.     Scanner in = new Scanner(System.in);
  10.     static String destinationCityCode;
  11.     static int numberOfAdults = 0;
  12.     static boolean isReturn = true;
  13.     static double ticketPrice = 0.00;  
  14.  
  15.     public static void main(String[] args)
  16.     {
  17.         showMenu();
  18.         getDestinationCityCode();
  19.         getNumberOfAdults();
  20.         getIsReturnTrip();
  21.     }
  22.  
  23.  
  24.  
  25.     public static void showMenu() {
  26.         System.out.println("Welcome to Travel World");
  27.         System.out.println("-------------------------------------------");
  28.         System.out.println("");
  29.         System.out.println("Special Deals on Flights!");
  30.         System.out.println("-------------------------");
  31.         System.out.println("1. Los Angeles (LAX)  - $650          2. Kuala Lumpur (KLC)  - $200");
  32.         System.out.println("3. Brunei      (BRN)  - $300          4. Philippines  (PLP)  - $410");
  33.         System.out.println("5. Bali        (BAL)  - $380          6. Bangkok      (BKK)  - $190");
  34.         System.out.println("");
  35.         System.out.println("Prices shown are PER ADULT for ONE WAY journey.");
  36.         System.out.println("");
  37.      }  
  38.  
  39.  
  40.      public static void getDestinationCityCode() {
  41.         System.out.print("Enter Destination City Code > ");
  42.         destinationCityCode = in.nextLine();
  43.     }
  44.  
  45.     public static void getNumberOfAdults() {
  46.       System.out.print("Enter Number of Adults Travelling > ");
  47.       numberOfAdults = in.nextInt();
  48.     }
  49.  
  50.     public static void getIsReturnTrip() {
  51.       System.out.print("Enter 'R' for Return journey, or 'W' for One-Way > ");
  52.         if(in.next().charAt(0) == 'R') {
  53.             isReturn = true;
  54.         }
  55.         else {
  56.             isReturn = false;
  57.         } 
  58.    }    
  59. }
  60.  
Feb 24 '08 #1
3 1297
JosAH
11,448 Expert 8TB
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
Feb 24 '08 #2
Any suggestions on how i should modify the code so as to not violate oo principles?
Feb 24 '08 #3
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. public class Booking {
  2.    private String destination;
  3.    private int nofAdults;
  4.    private boolean round;
  5.  
  6.    Booking(String destination, int nofAdults, boolean round) {
  7.       this.destination= destination;
  8.       this.nofAdults= nofAdults;
  9.       this.round= round;
  10.    }
  11.    // three getters for the three member vars here
  12. }
  13.  
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
Feb 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
9
by: AnandRaj | last post by:
Hi guys, I have a few doubts in C. 1. Why static declartions are not allowed inside structs? eg struct a { static int i; }; Throws an error ..
5
by: Tom Pearson | last post by:
What is the scope of static variable when programming in ASP.NET? For example I have a control class that uses static callbacks so that another window can pass a list of items to it. The control...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
6
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.