Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group 1: Minor refactoring #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import flight.reservation.plane.Helicopter;
import flight.reservation.plane.PassengerDrone;
import flight.reservation.plane.PassengerPlane;
import flight.reservation.plane.Vehicle;

import java.util.Arrays;
import java.util.List;
Expand All @@ -20,7 +21,7 @@ public class Runner {
new Airport("Chengdu Shuangliu International Airport", "CTU", "Shuangliu-Wuhou, Chengdu, Sichuan")
);

static List<Object> aircrafts = Arrays.asList(
static List<Vehicle> aircrafts = Arrays.asList(
new PassengerPlane("A380"),
new PassengerPlane("A350"),
new PassengerPlane("Embraer 190"),
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/flight/reservation/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import java.util.List;
import java.util.stream.Collectors;

public class Customer {
// TODO: introduce passenger for name? or Person as parent class
public class Customer extends Person {

private String email;
private String name;
private List<Order> orders;

public Customer(String name, String email) {
this.name = name;
super(name);
this.email = email;
this.orders = new ArrayList<>();
}
Expand All @@ -42,12 +42,7 @@ private boolean isOrderValid(List<String> passengerNames, List<ScheduledFlight>
valid = valid && !FlightOrder.getNoFlyList().contains(this.getName());
valid = valid && passengerNames.stream().noneMatch(passenger -> FlightOrder.getNoFlyList().contains(passenger));
valid = valid && flights.stream().allMatch(scheduledFlight -> {
try {
return scheduledFlight.getAvailableCapacity() >= passengerNames.size();
} catch (NoSuchFieldException e) {
e.printStackTrace();
return false;
}
return scheduledFlight.getAvailableCapacity() >= passengerNames.size();
});
return valid;
}
Expand All @@ -60,10 +55,6 @@ public void setEmail(String email) {
this.email = email;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
Expand All @@ -77,3 +68,4 @@ public void setOrders(List<Order> orders) {
}

}

12 changes: 3 additions & 9 deletions src/main/java/flight/reservation/Passenger.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package flight.reservation;

public class Passenger {

private final String name;
import javax.xml.namespace.QName;

public class Passenger extends Person {
Copy link

@SandroWeber SandroWeber Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introducing Person as a class, the Passenger class seems a bit "empty". Person class itself also seems empty, is the idea to distinguish passengers and crew? Maybe just have either Person or Customer with all the info and a ScheduledFlight could then have a List<Customer> passengers?

public Passenger(String name) {
this.name = name;
}

public String getName() {
return name;
super(name);
}

}
13 changes: 13 additions & 0 deletions src/main/java/flight/reservation/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package flight.reservation;

public class Person {
protected String name;

public Person(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
19 changes: 5 additions & 14 deletions src/main/java/flight/reservation/flight/Flight.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import flight.reservation.plane.Helicopter;
import flight.reservation.plane.PassengerDrone;
import flight.reservation.plane.PassengerPlane;
import flight.reservation.plane.Vehicle;

import java.util.Arrays;

Expand All @@ -12,9 +13,9 @@ public class Flight {
private int number;
private Airport departure;
private Airport arrival;
protected Object aircraft;
protected Vehicle aircraft;

public Flight(int number, Airport departure, Airport arrival, Object aircraft) throws IllegalArgumentException {
public Flight(int number, Airport departure, Airport arrival, Vehicle aircraft) throws IllegalArgumentException {
this.number = number;
this.departure = departure;
this.arrival = arrival;
Expand All @@ -30,21 +31,11 @@ private void checkValidity() throws IllegalArgumentException {

private boolean isAircraftValid(Airport airport) {
return Arrays.stream(airport.getAllowedAircrafts()).anyMatch(x -> {
String model;
if (this.aircraft instanceof PassengerPlane) {
model = ((PassengerPlane) this.aircraft).model;
} else if (this.aircraft instanceof Helicopter) {
model = ((Helicopter) this.aircraft).getModel();
} else if (this.aircraft instanceof PassengerDrone) {
model = "HypaHype";
} else {
throw new IllegalArgumentException(String.format("Aircraft is not recognized"));
}
return x.equals(model);
return x.equals(this.aircraft.getModel());
});
}

public Object getAircraft() {
public Vehicle getAircraft() {
return aircraft;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/flight/reservation/flight/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public List<ScheduledFlight> getScheduledFlights() {
}

public void scheduleFlight(Flight flight, Date date) {
ScheduledFlight scheduledFlight = new ScheduledFlight(flight.getNumber(), flight.getDeparture(), flight.getArrival(), flight.getAircraft(), date);
ScheduledFlight scheduledFlight = new ScheduledFlight(flight, date);
scheduledFlights.add(scheduledFlight);
}

Expand Down
35 changes: 9 additions & 26 deletions src/main/java/flight/reservation/flight/ScheduledFlight.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import flight.reservation.plane.Helicopter;
import flight.reservation.plane.PassengerDrone;
import flight.reservation.plane.PassengerPlane;
import flight.reservation.plane.Vehicle;

import java.util.ArrayList;
import java.util.Date;
Expand All @@ -16,30 +17,21 @@ public class ScheduledFlight extends Flight {
private final Date departureTime;
private double currentPrice = 100;

public ScheduledFlight(int number, Airport departure, Airport arrival, Object aircraft, Date departureTime) {
super(number, departure, arrival, aircraft);
public ScheduledFlight(Flight flight, Date departureTime) {
super(flight.getNumber(), flight.getDeparture(), flight.getArrival(), flight.getAircraft());
this.departureTime = departureTime;
this.passengers = new ArrayList<>();
}

public ScheduledFlight(int number, Airport departure, Airport arrival, Object aircraft, Date departureTime, double currentPrice) {
public ScheduledFlight(int number, Airport departure, Airport arrival, Vehicle aircraft, Date departureTime, double currentPrice) {
super(number, departure, arrival, aircraft);
this.departureTime = departureTime;
this.passengers = new ArrayList<>();
this.currentPrice = currentPrice;
}

public int getCrewMemberCapacity() throws NoSuchFieldException {
if (this.aircraft instanceof PassengerPlane) {
return ((PassengerPlane) this.aircraft).crewCapacity;
}
if (this.aircraft instanceof Helicopter) {
return 2;
}
if (this.aircraft instanceof PassengerDrone) {
return 0;
}
throw new NoSuchFieldException("this aircraft has no information about its crew capacity");
public int getCrewMemberCapacity() {
return this.aircraft.getCrewCapacity();
}

public void addPassengers(List<Passenger> passengers) {
Expand All @@ -50,20 +42,11 @@ public void removePassengers(List<Passenger> passengers) {
this.passengers.removeAll(passengers);
}

public int getCapacity() throws NoSuchFieldException {
if (this.aircraft instanceof PassengerPlane) {
return ((PassengerPlane) this.aircraft).passengerCapacity;
}
if (this.aircraft instanceof Helicopter) {
return ((Helicopter) this.aircraft).getPassengerCapacity();
}
if (this.aircraft instanceof PassengerDrone) {
return 4;
}
throw new NoSuchFieldException("this aircraft has no information about its capacity");
public int getCapacity() {
return this.aircraft.getPassengerCapacity();
}

public int getAvailableCapacity() throws NoSuchFieldException {
public int getAvailableCapacity() {
return this.getCapacity() - this.passengers.size();
}

Expand Down
8 changes: 2 additions & 6 deletions src/main/java/flight/reservation/order/FlightOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ private boolean isOrderValid(Customer customer, List<String> passengerNames, Lis
valid = valid && !noFlyList.contains(customer.getName());
valid = valid && passengerNames.stream().noneMatch(passenger -> noFlyList.contains(passenger));
valid = valid && flights.stream().allMatch(scheduledFlight -> {
try {
return scheduledFlight.getAvailableCapacity() >= passengerNames.size();
} catch (NoSuchFieldException e) {
e.printStackTrace();
return false;
}
return scheduledFlight.getAvailableCapacity() >= passengerNames.size();

});
return valid;
}
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/flight/reservation/plane/Helicopter.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package flight.reservation.plane;

public class Helicopter {
private final String model;
private final int passengerCapacity;

public class Helicopter extends Vehicle {
public Helicopter(String model) {
this.model = model;
this.crewCapacity = 2;
if (model.equals("H1")) {
passengerCapacity = 4;
} else if (model.equals("H2")) {
Expand All @@ -14,12 +12,4 @@ public Helicopter(String model) {
throw new IllegalArgumentException(String.format("Model type '%s' is not recognized", model));
}
}

public String getModel() {
return model;
}

public int getPassengerCapacity() {
return passengerCapacity;
}
}
5 changes: 2 additions & 3 deletions src/main/java/flight/reservation/plane/PassengerDrone.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package flight.reservation.plane;

public class PassengerDrone {
private final String model;

public class PassengerDrone extends Vehicle {
public PassengerDrone(String model) {
if (model.equals("HypaHype")) {
this.model = model;
this.passengerCapacity = 4;
} else {
throw new IllegalArgumentException(String.format("Model type '%s' is not recognized", model));
}
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/flight/reservation/plane/PassengerPlane.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package flight.reservation.plane;

public class PassengerPlane {

public String model;
public int passengerCapacity;
public int crewCapacity;
public class PassengerPlane extends Vehicle {

public PassengerPlane(String model) {
this.model = model;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/flight/reservation/plane/Vehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package flight.reservation.plane;

public class Vehicle {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vehicles can also be on wheels. Aircraft would be a better name.

protected String model;
protected int crewCapacity = 0;
protected int passengerCapacity = 0;

public String getModel() {
return model;
}

public int getCrewCapacity() {
return crewCapacity;
}

public int getPassengerCapacity() {
return passengerCapacity;
}
}