-1

This class is part of a larger application that will take a number of variables from the user, and expect to use them later, even after this particular class is left.

How can I grab the data that this class takes (after multiple iterations), and call it back from a different section?

public class A8AirlineAircraftData {
public static void AddAirline(){
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter the Airline name: ");
    String airName = sc.nextLine();
    System.out.println("Please enter the Airline code: ");
    String airCode = sc.nextLine();
    System.out.println("Please enter the Delta Aircraft: ");
    String airCraft = sc.nextLine();
    System.out.println("Please enter the first class seat capacity: ");
    int firstClass = sc.nextInt();
    System.out.println("Please enter the business class seat capacity: ");
    int busiClass = sc.nextInt();
    System.out.println("Please enter the economy class seat capacity: ");
    int econClass = sc.nextInt();
    System.out.println("Airline name: " + airName);
    System.out.println("Airline code: " + airCode);
    System.out.println("Delta Aircraft: " + airCraft);
    String arr[] = airCraft.split(" ", 2);
    String firstWord = arr[0];
    System.out.println(firstWord + " first class seat capacity: " + firstClass);
    System.out.println(firstWord + " business class seat capacity: " + busiClass);
    System.out.println(firstWord + " economy class seat capacity: " + econClass);

    System.out.println( airName + " successfully added. Press Enter to continue.");
    sc.nextLine();//Press Enter to continue
    sc.nextLine();
    A8MainMenu.mainMenu(); //return to main menu after Enter. 
} //AddAirline

My output now:

Airline name: Qatar Airlines
Airline code: QA
Delta Aircraft: Boeing 787 Dreamliner
Boeing first class seat capacity: 16
Boeing business class seat capacity: 25
Boeing economy class seat capacity: 199
Qatar Airlines successfully added. Press Enter to continue.

I need to just store this information somewhere in memory until I can call it later.

halfer
  • 18,701
  • 13
  • 79
  • 158
J Ben
  • 127
  • 9
  • 1
    Why make the method `void` if you want to extract information that it gains? Create an AirLine class, and have this method return an instance of this class. Also, don't create multiple Scanner objects on System.in. Create one only and use it throughout the program. – Hovercraft Full Of Eels Dec 17 '17 at 21:00
  • Oh, I'm not exactly sure how to do that. And use one 'public' Scanner object? – J Ben Dec 17 '17 at 21:06
  • Not sure how to do what? Return something from a method? That's basic Java. As for the Scanner, have your method accept a Scanner parameter and pass it in from the calling code. I'm starting to thing that your question may be best answered by reading some basic Java tutorials. Keep working at it. – Hovercraft Full Of Eels Dec 17 '17 at 21:08
  • Please understand that this isn't a tutoring site or a help site but rather a question and answer site, and you've just now completely changed your question and thereby have invalidated all answers that have been already given. Please don't do this. Instead roll your question back and accept the answer that helped you the most, and consider this question closed. If you have a new problem then search this site for similar questions, such as [this one](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo), ....\ – Hovercraft Full Of Eels Dec 17 '17 at 23:38
  • ... and consider asking a new question, but if you do so, try to make it as high quality as possible, including showing [mcve] code, including showing how your searches did not solve your problem. – Hovercraft Full Of Eels Dec 17 '17 at 23:38
  • @HovercraftFullOfEels I apologize, i forgot to close this. – J Ben Dec 17 '17 at 23:40
  • (This question has attracted a close vote, as someone thought it did not meet the posting guidelines. In order to give it a chance to escape this fate, I have rolled back to the last good version, minus the significant change. As Hovercraft says, if you can get into the habit of asking new questions in a new post, that will allow the old question to sync with the answers that were already given.) – halfer Dec 18 '17 at 00:05

2 Answers2

1

Define a class

Create a class to represent each airplane.

Learn about constructors.

Rough code, untested…

public class Airplane {
    private String airlineName, airlineCode ;
    private Integer seatCapacityFirstClass ;

    // Constructor
    public Airplane( String airlineNameArg , String airlineCodeArg , Integer seatCapacityFirstClassArg ) {
        this.airlineName = airlineNameArg ;
        this.airlineCode = airlineCodeArg ;
        this.seatCapacityFirstClass = seatCapacityFirstClassArg ;
    }

}

Instantiate these objects after collecting input.

Airplane a = new Airplane ( "United" , "42X937" , 42 ) ;

Make a list

Put each instance of Airplane into a collection.

List<Airplane> airplanes = new ArrayList<>() ; 
airplanes.add( a ) ;

To retrieve data, add some “getter” methods to your class. Loop the list, and on each object from the list, call the getter method. For debugging, consider overriding the Object::toString method in your class to generate descriptive piece of text. All of this is covered in the Oracle Java Tutorials, and discussed in many many many Stack Overflow Questions and Answers. DuckDuckGo/Google/Bing is your friend and tutor.

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
1

First create a Airline class

public class Airline {
    private String name;
    private String code;

    // ..... other fields
    public Airline(String name, String code /* , .... other parameters */) {
        this.name = name;
        this.code = code;
        // .... other set fields
    }

    // getters and setters...

    // equals and hashCode...

}

Next your UI (user interface) code should

  • should get a Scanner object as a parameter. Don't re-make a Scanner object based on System.in multiple times as that is a dangerous thing to do as you risk corrupting the System.in if you close this object early. Make it once and use it throughout.
  • The method should create and return an Airline object,
  • This method should have one responsibility and that is to get data from the user and use it to create an Airline object, nothing less and nothing more. It should not make calls to show the menu, otherwise known as "side effects" -- that's the calling code's responsibility, meaning the code that calls this method will then get the Airline object from the method, and the calling code will decide what should next be done.

e.g.,

// use a Scanner parameter
public static Airline AddAirline(Scanner sc) {
    // don't re-create the Scanner object
    // Scanner sc = new Scanner(System.in);
    System.out.println("Please enter the Airline name: ");
    String airName = sc.nextLine();
    System.out.println("Please enter the Airline code: ");
    String airCode = sc.nextLine();

    // ..... other code

    // create your Airline object
    Airline airline = new Airline(airName, airCode); // plus other parameters in the complete class
    return airline;  // and return it

    // !! the code below should be part of the calling code
    // System.out.println(airName + " successfully added. Press Enter to continue.");
    // sc.nextLine();// Press Enter to continue
    // sc.nextLine();
    // !! A8MainMenu.mainMenu(); // NO!! Don't do this
} 
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
  • I've updated my post to better fit your guidance. I apologize for being such a rookie. Would you explain why you put "NO!! Don't do this" after I return to the main menu once the program is complete? Would it terminate the objects we've created? I truly appreciate the help you've provided... – J Ben Dec 17 '17 at 22:16
  • @JBen: the NO! part was because you're giving this method too much responsibility. It should create and return an Airline object and do nothing more. The calling code can then do what it wants with that object, such as put it into an ArrayList, but it is not this methods responsibility to do anything else, nor should it be. Also calling the mainMenu() from this method looks to make this code recursive -- having a method call itself, since this code is surely called from the main menu, and that's a dangerous thing to do. It's just bad coding. – Hovercraft Full Of Eels Dec 17 '17 at 22:29