简体   繁体   中英

Returning my object into a HashSet

I'm almost completely blind, and I'm trying to learn Java. I'm telling you this because I've had a difficult time understanding concepts well, because of my disability.

My problem : In this particular class, I'm returning an object that I'd like to be stored somewhere to later be called. I'm unfamiliar with Collections , but I believe (I think?) a HashSet is the type of Set I'd like to implement here. Would I be able to do this, and call this collection of objects from a different class once its created?

I'm very new to Java so I'm not quite sure if I've explained my problem effectively. Please don't hesitate to ask for clarification!

Important: I'm not to allow any duplicates of airCode , would HashSet handle that in some way?

public class A8AirlineAircraftData {


private String airName; //these are all showing a warning saying 'Field ____ can be final'
private String airCode;
private String airCraft;
private int firstClass;
private int busiClass;
private int econClass;
//public String toStringAirLine(){return "airname is:" + airName + ", aircode is: "+airCode;}


public A8AirlineAircraftData(String airName, String airCode, String airCraft, int firstClass, int busiClass, int econClass) {
    this.airName = airName;
    this.airCode = airCode;
    this.airCraft = airCraft;
    this.firstClass = firstClass;
    this.busiClass = busiClass;
    this.econClass = econClass;
} 

public static A8AirlineAircraftData AddAirline(Scanner sc) {
    sc.nextLine();
    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);
    //Splitting the first word from the rest of the string
    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);
    //Airline object
    A8AirlineAircraftData airline = new A8AirlineAircraftData(airName, airCode, airCraft, firstClass, busiClass, econClass);
    System.out.println(airName + " successfully added. Press Enter to continue.");

    sc.nextLine();
    sc.nextLine();//Press Enter to continue 
    return airline;

}   
}

Thank you for any guidance provided. Again, I'm sorry if I'm not being clear or my submission was grammatically incorrect in any way. I try to be concise!

I will now show the changes EDIT:

public class A8AirlineAircraftData {

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((airCode == null) ? 0 : airCode.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof A8AirlineAircraftData)) {
        return false;
    }
    A8AirlineAircraftData other = (A8AirlineAircraftData) obj;
    return Objects.equals(airCode, other.airCode);
}

private String airName; //these are all showing a warning saying 'Field ____ can be final'
private String airCode;
private String airCraft;
private int firstClass;
private int busiClass;
private int econClass;
//public String toStringAirLine(){return "airname is:" + airName + ", aircode is: "+airCode;}

public A8AirlineAircraftData(String airName, String airCode, String airCraft, int firstClass, int busiClass, int econClass) {
    this.airName = airName;
    this.airCode = airCode;
    this.airCraft = airCraft;
    this.firstClass = firstClass;
    this.busiClass = busiClass;
    this.econClass = econClass;
}

public static A8AirlineAircraftData AddAirline(Scanner sc) {
    sc.nextLine();
    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);
    //Splitting the first word from the rest of the string
    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);
    //Airline object
    A8AirlineAircraftData airline = new A8AirlineAircraftData(airName, airCode, airCraft, firstClass, busiClass, econClass);
    Set<A8AirlineAircraftData> aircrafts = new HashSet<>();

    /*for (int i = 0; i < 3; i++) {
        aircrafts.add(A8AirlineAircraftData.AddAirline(sc));
    } */

    System.out.println(airName + " successfully added. Press Enter to continue.");

    sc.nextLine();
    sc.nextLine();//Press Enter to continue 
    return airline;

You could invoke your factory method and store the created object in a Set such as.
For example to create 3 elements, you could write :

Set<A8AirlineAircraftData> aircrafts = new HashSet<>();
for (int i=0; i<3; i++){
   aircrafts.add(A8AirlineAircraftData.AddAirline());
}

I'm not to allow any duplicates of airCode, would HashSet handle that in some way?

Uniqueness of elements in a Set relies on the equals() / hashCode() methods of elements that it contains.
You should so override them in A8AirlineAircraftData to rely on the class field that identify in a unique way an instance : airCode .

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((airCode == null) ? 0 : airCode.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (!(obj instanceof A8AirlineAircraftData))
        return false;
    A8AirlineAircraftData other = (A8AirlineAircraftData) obj;
    return Objects.equal(airCode, other.airCode);
}

You are correct, Set is an interface for implementations of Set Objects, that do not allow duplicates. In your code airCode is a String, so no duplicate Strings will be allowed in it. In order to access the variable or method outside of class, you have to set up a correct access modifier for the variable. ie public will let this variable be accessible from any other class.

public static Set<String> setOfAirCodes = new HashSet<>();

regarding other modifiers, read here: access modifiers in java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM