From 8eb50df5155917eb0cbd7ad24e9cc9bb8855a5fa Mon Sep 17 00:00:00 2001 From: Sven Liedtke Date: Tue, 23 Nov 2021 10:30:01 +0100 Subject: [PATCH 1/2] Introduce vehicle super class --- src/main/java/Runner.java | 3 +- .../java/flight/reservation/Customer.java | 9 ++--- .../flight/reservation/flight/Flight.java | 19 +++-------- .../reservation/flight/ScheduledFlight.java | 33 +++++-------------- .../flight/reservation/order/FlightOrder.java | 8 ++--- .../flight/reservation/plane/Helicopter.java | 14 ++------ .../reservation/plane/PassengerDrone.java | 5 ++- .../reservation/plane/PassengerPlane.java | 6 +--- .../flight/reservation/plane/Vehicle.java | 19 +++++++++++ 9 files changed, 44 insertions(+), 72 deletions(-) create mode 100644 src/main/java/flight/reservation/plane/Vehicle.java diff --git a/src/main/java/Runner.java b/src/main/java/Runner.java index ddea7fb..3ff33f8 100644 --- a/src/main/java/Runner.java +++ b/src/main/java/Runner.java @@ -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; @@ -20,7 +21,7 @@ public class Runner { new Airport("Chengdu Shuangliu International Airport", "CTU", "Shuangliu-Wuhou, Chengdu, Sichuan") ); - static List aircrafts = Arrays.asList( + static List aircrafts = Arrays.asList( new PassengerPlane("A380"), new PassengerPlane("A350"), new PassengerPlane("Embraer 190"), diff --git a/src/main/java/flight/reservation/Customer.java b/src/main/java/flight/reservation/Customer.java index 2e2c12b..ccabab2 100644 --- a/src/main/java/flight/reservation/Customer.java +++ b/src/main/java/flight/reservation/Customer.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.stream.Collectors; +// TODO: introduce passenger for name? or Person as parent class public class Customer { private String email; @@ -42,12 +43,7 @@ private boolean isOrderValid(List passengerNames, List 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; } @@ -77,3 +73,4 @@ public void setOrders(List orders) { } } + diff --git a/src/main/java/flight/reservation/flight/Flight.java b/src/main/java/flight/reservation/flight/Flight.java index b674511..029c6a1 100644 --- a/src/main/java/flight/reservation/flight/Flight.java +++ b/src/main/java/flight/reservation/flight/Flight.java @@ -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; @@ -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; @@ -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; } diff --git a/src/main/java/flight/reservation/flight/ScheduledFlight.java b/src/main/java/flight/reservation/flight/ScheduledFlight.java index 7c49d21..e758dfc 100644 --- a/src/main/java/flight/reservation/flight/ScheduledFlight.java +++ b/src/main/java/flight/reservation/flight/ScheduledFlight.java @@ -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; @@ -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) { + public ScheduledFlight(int number, Airport departure, Airport arrival, Vehicle aircraft, Date departureTime) { super(number, departure, arrival, aircraft); 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 passengers) { @@ -50,20 +42,11 @@ public void removePassengers(List 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(); } diff --git a/src/main/java/flight/reservation/order/FlightOrder.java b/src/main/java/flight/reservation/order/FlightOrder.java index b2bdb56..ec0c8ef 100644 --- a/src/main/java/flight/reservation/order/FlightOrder.java +++ b/src/main/java/flight/reservation/order/FlightOrder.java @@ -30,12 +30,8 @@ private boolean isOrderValid(Customer customer, List 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; } diff --git a/src/main/java/flight/reservation/plane/Helicopter.java b/src/main/java/flight/reservation/plane/Helicopter.java index 4add92c..fec6588 100644 --- a/src/main/java/flight/reservation/plane/Helicopter.java +++ b/src/main/java/flight/reservation/plane/Helicopter.java @@ -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")) { @@ -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; - } } diff --git a/src/main/java/flight/reservation/plane/PassengerDrone.java b/src/main/java/flight/reservation/plane/PassengerDrone.java index 2f75861..b0059fe 100644 --- a/src/main/java/flight/reservation/plane/PassengerDrone.java +++ b/src/main/java/flight/reservation/plane/PassengerDrone.java @@ -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)); } diff --git a/src/main/java/flight/reservation/plane/PassengerPlane.java b/src/main/java/flight/reservation/plane/PassengerPlane.java index aa4b738..3f02df4 100644 --- a/src/main/java/flight/reservation/plane/PassengerPlane.java +++ b/src/main/java/flight/reservation/plane/PassengerPlane.java @@ -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; diff --git a/src/main/java/flight/reservation/plane/Vehicle.java b/src/main/java/flight/reservation/plane/Vehicle.java new file mode 100644 index 0000000..7b90519 --- /dev/null +++ b/src/main/java/flight/reservation/plane/Vehicle.java @@ -0,0 +1,19 @@ +package flight.reservation.plane; + +public class Vehicle { + 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; + } +} From c3d8ecac042da235d0b31227a666887321eb7828 Mon Sep 17 00:00:00 2001 From: Sven Liedtke Date: Tue, 23 Nov 2021 11:00:01 +0100 Subject: [PATCH 2/2] Introduce new person class for Customer and Passenger --- src/main/java/flight/reservation/Customer.java | 9 ++------- src/main/java/flight/reservation/Passenger.java | 12 +++--------- src/main/java/flight/reservation/Person.java | 13 +++++++++++++ .../java/flight/reservation/flight/Schedule.java | 2 +- .../flight/reservation/flight/ScheduledFlight.java | 4 ++-- 5 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 src/main/java/flight/reservation/Person.java diff --git a/src/main/java/flight/reservation/Customer.java b/src/main/java/flight/reservation/Customer.java index ccabab2..cc7ee1a 100644 --- a/src/main/java/flight/reservation/Customer.java +++ b/src/main/java/flight/reservation/Customer.java @@ -9,14 +9,13 @@ import java.util.stream.Collectors; // TODO: introduce passenger for name? or Person as parent class -public class Customer { +public class Customer extends Person { private String email; - private String name; private List orders; public Customer(String name, String email) { - this.name = name; + super(name); this.email = email; this.orders = new ArrayList<>(); } @@ -56,10 +55,6 @@ public void setEmail(String email) { this.email = email; } - public String getName() { - return name; - } - public void setName(String name) { this.name = name; } diff --git a/src/main/java/flight/reservation/Passenger.java b/src/main/java/flight/reservation/Passenger.java index 70223a5..3780c0d 100644 --- a/src/main/java/flight/reservation/Passenger.java +++ b/src/main/java/flight/reservation/Passenger.java @@ -1,15 +1,9 @@ package flight.reservation; -public class Passenger { - - private final String name; +import javax.xml.namespace.QName; +public class Passenger extends Person { public Passenger(String name) { - this.name = name; - } - - public String getName() { - return name; + super(name); } - } diff --git a/src/main/java/flight/reservation/Person.java b/src/main/java/flight/reservation/Person.java new file mode 100644 index 0000000..70632c2 --- /dev/null +++ b/src/main/java/flight/reservation/Person.java @@ -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; + } +} diff --git a/src/main/java/flight/reservation/flight/Schedule.java b/src/main/java/flight/reservation/flight/Schedule.java index 55244e9..786070d 100644 --- a/src/main/java/flight/reservation/flight/Schedule.java +++ b/src/main/java/flight/reservation/flight/Schedule.java @@ -18,7 +18,7 @@ public List 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); } diff --git a/src/main/java/flight/reservation/flight/ScheduledFlight.java b/src/main/java/flight/reservation/flight/ScheduledFlight.java index e758dfc..97170b9 100644 --- a/src/main/java/flight/reservation/flight/ScheduledFlight.java +++ b/src/main/java/flight/reservation/flight/ScheduledFlight.java @@ -17,8 +17,8 @@ public class ScheduledFlight extends Flight { private final Date departureTime; private double currentPrice = 100; - public ScheduledFlight(int number, Airport departure, Airport arrival, Vehicle 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<>(); }