Reputation: 1
here is what i have to do, and what I have already did
After a quick meeting with the head of the company, you got the following information:
It is required to store the whole data in one collection
Each flight has a number, a pilot and a specific date. In addition, passengers could be added or removed within a given limit for the maximum number of passengers who could be in the same flight. Furthermore, there are other attributes (add at least 3 attributes from your choice). Of course, flight number is unique within the same date.
Each pilot has a unique ID, a name and other attributes (add at least 2 attributes from your choice).
Each passenger has a unique passport number, a name and other attributes (add at least 2 attributes from your choice but one of them should be common with pilots).
Moreover, you have been informed that the following operations happen frequently:
Offering a new flight
Adding a passenger to a specified flight
Removing a passenger from a specified flight Retrieving the average number of passengers per flight of a specified date
Displaying all available flights in a format similar to the following: date1: flightNo1 flightNo2 flightNo3 … date2: flightNo1 flightNo2 flightNo3 … … where dates and flights are sorted in ascending order
Saving all the data into a text file
this is what did
I am starting the test class and when I started I for some reason couldn't get the date so I tried a shitty way and I don't think I am right
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class Testclass {
public static void main(String[] args) {
Date date1 = new Date("11/12/2020");
Flights a = new Flight(1, date1, 50, 5, "saudi air", "complimentary");
}
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Flight {
final List<Passenger> passengers = new ArrayList<>();
int number;
Pilot pilot;
Date date;
int maxPassengers;
int minPassengers;
String airline;
String food;
public Flight() {
}
public Flight(int number, Date date, int maxPassengers, int minPassengers, String airline, String food) {
this.number = number;
this.date = date;
this.maxPassengers = maxPassengers;
this.minPassengers = minPassengers;
this.airline = airline;
this.food = food;
}
public List<Passenger> getPassengers() {
return passengers;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Pilot getPilot() {
return pilot;
}
public void setPilot(Pilot pilot) {
if (!pilotExistance(pilot)) {
this.pilot = pilot;
System.out.println("Pilot has been added.");
}
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getMaxPassengers() {
return maxPassengers;
}
public void setMaxPassengers(int maxPassengers) {
this.maxPassengers = maxPassengers;
}
public int getMinPassengers() {
return minPassengers;
}
public void setMinPassengers(int minPassengers) {
this.minPassengers = minPassengers;
}
public String getAirline() {
return airline;
}
public void setAirline(String airline) {
this.airline = airline;
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public boolean pilotExistance(Pilot newPilot) {
boolean pilotExist = false;
for (Flight flight : Airport.flights) {
if (flight.getPilot() != null && flight.getPilot().getId().equals(newPilot.getId())) {
pilotExist = true;
System.out.println("Pilot id: " + newPilot.getId() + " pilots in plane of flight number: " + flight.getNumber());
break;
}
}
return pilotExist;
}
public boolean passengerExistance(Passenger newPassenger) {
boolean passengerExist = false;
for (Flight flight : Airport.flights) {
final List<Passenger> passengers = flight.getPassengers();
for (Passenger passenger : passengers) {
if (passenger.passportNumber.equals(newPassenger.passportNumber)) {
passengerExist = true;
System.out.println("Passenger with passport number: " + newPassenger.getPassportNumber() + " are on the flight with the flgit number: " + flight.getNumber());
break;
}
}
if (passengerExist) {
break;
}
}
return passengerExist;
}
public void addPassenger(Passenger passenger) {
if (passengers.size() < maxPassengers) {
if (!passengerExistance(passenger)) {
passengers.add(passenger);
System.out.println(" Included.");
}
} else {
System.out.println("The Max number of passengers has already been reached.");
}
}
public void removePassenger(Passenger passenger) {
if (passengers.size() > minPassengers) {
if (passengerExistance(passenger)) {
passengers.remove(passenger);
System.out.println("Passnger has been taken out of flight.");
} else {
System.out.println("Passnger with passport number: " + passenger.getPassportNumber() + " doesn't exist.");
}
} else {
System.out.println("The lowest amount of passengers have been reached.");
}
}
public void passengersInformation() {
if (passengers.isEmpty()) {
System.out.println("There are no passengers.");
} else {
for (Passenger passenger : passengers) {
System.out.println(passenger.toString());
}
}
}
@Override
public String toString() {
return "Number: " + number + ", Pilot: " + pilot != null ? pilot.toString() : "No pilot yet" + ", Date: " + date + ", airline: " + airline + ", food: " + food + ", Number of passengers: " + passengers.size();
}
}
public class Passenger {
String passportNumber;
String name;
String bloodtype;
String agecatagory;
public Passenger() {
}
public Passenger(String id, String name, String bloodtype, String agecatagory) {
this.passportNumber = id;
this.name = name;
this.bloodtype = bloodtype;
this.agecatagory = agecatagory;
}
public String getPassportNumber() {
return passportNumber;
}
public void setPassportNumber(String passportNumber) {
this.passportNumber = passportNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBloodtype() {
return bloodtype;
}
public void setBloodtype(String bloodtype) {
this.bloodtype = bloodtype;
}
public String getAgecatagory() {
return agecatagory;
}
public void setAgecatagory(String agecatagory) {
this.agecatagory = agecatagory;
}
@Override
public String toString() {
return "Passport Number: " + passportNumber + ", Name: " + name + ", Bloodtype: " + bloodtype + ", Age catagory: " + agecatagory;
}
}
public class Pilot {
String id;
String name;
String experiancelevel;
String pilotcatagory;
public Pilot() {
}
public Pilot(String id, String name, String experiancelevel, String pilotcatagory) {
this.id = id;
this.name = name;
this.experiancelevel = experiancelevel;
this.pilotcatagory = pilotcatagory;
}
public String getId() {
return id;
}
//This function to get pilot id
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getExperiancelevel() {
return experiancelevel;
}
public void setExperiancelevel(String experiancelevel) {
this.experiancelevel = experiancelevel;
}
public String getPilotcatagory() {
return pilotcatagory;
}
public void setPilotcatagory(String pilotcatagory) {
this.pilotcatagory = pilotcatagory;
}
@Override
public String toString() {
return "Id: " + id + ", Name: " + name + ", Experiance level: " + experiancelevel + ", Pilot catagory: " + pilotcatagory;
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class Airport {
public static final List<Flight> flights = new ArrayList<>();
public Airport() {
}
public List<Flight> getFlights() {
return flights;
}
public boolean flightExistance(Flight newFlight) {
boolean flightExist = false;
if (!flights.isEmpty()) {
for (Flight flight : flights) {
if (flight.date.equals(newFlight.date) && flight.number == newFlight.number) {
flightExist = true;
break;
}
}
}
return flightExist;
}
public void addFlight(Flight flight) {
if (!flightExistance(flight)) {
flights.add(flight);
System.out.println("Flight Included.");
} else {
System.out.println("Number of flight: " + flight.getNumber() + " already in place.");
}
}
public void averageNumberOfPassengersPerFlight(Date date) {
int passengersCount = 0;
int flightsCount = 0;
double average = 0.0;
for (Flight flight : flights) {
if (flight.getDate().equals(date)) {
flightsCount++;
passengersCount += flight.getPassengers().size();
}
}
System.out.println("Total number of passengers in the flight: " + passengersCount);
if (passengersCount > 0 && flightsCount > 0) {
average = passengersCount / flightsCount;
System.out.println("Average number of passengers per flight for the specific date(" + date + "): " + average);
} else {
System.out.println("Average number of passengers per flight for the specific date(" + date + "): " + 0.0);
}
}
public void display() {
final List<Date> dates = new ArrayList<>();
for (Flight flight : flights) {
if (!dates.contains(flight.getDate())) {
dates.add(flight.getDate());
}
}
Collections.sort(dates);
for (Date date : dates) {
System.out.print(date + ": ");
final List<Integer> numbers = new ArrayList<>();
for (Flight flight : flights) {
if (flight.getDate().equals(date)) {
numbers.add(flight.getNumber());
}
}
Collections.sort(numbers);
for (int number : numbers) {
System.out.print("flightNo." + number + " ");
}
System.out.println("---------");
}
}
public void saveInTextFile(String filePath) {
try {
final FileWriter writer = new FileWriter(filePath);
for (Flight flight : flights) {
writer.write(flight.toString());
writer.write("\n");
writer.write("---------");
}
writer.close();
System.out.println("Saved successfully");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
@Override
public String toString() {
return "Number of flights: " + flights.size();
}
}
Upvotes: -1
Views: 145
Reputation: 1
i'm here to learn but i had a time limit so with the case of using Local time, I don't want to use code that I still don't understand yet so I just continued with what I did, it kinda sucked how I had to make a new object for each time but it's wasn't too bad and I finished the main method.
here it is(of course this isn't the last version as i added many more variables and commands but i just wanted to give you the final draft before i finished it completely.
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
// public Flight(int number, Date date, int maxPassengers, int minPassengers, String airline, String food) {
Date d = new Date("11/12/2020");
Pilot h = new Pilot("qw2563", "zaid", "epxerianced", "main pilot");
Pilot h1 = new Pilot("qw2563", "zaid", "epxerianced", "main pilot");
Pilot h2 = new Pilot("qw2563", "zaid", "epxerianced", "main pilot");
Flight f = new Flight(1, d, 50, 5, "saudi air", "complimentary",h);
Passenger one = new Passenger("23546785", "ahmad", "B+", "adult");
Airport hq = new Airport();
hq.addFlight(f);
f.addPassenger(one);
f.addPassenger(one);
}
}
Upvotes: 0