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 5: Refactoring #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -2,6 +2,7 @@
import flight.reservation.flight.Schedule;
import flight.reservation.flight.Flight;
import flight.reservation.plane.Helicopter;
import flight.reservation.plane.IdentifiedFlightObject;
import flight.reservation.plane.PassengerDrone;
import flight.reservation.plane.PassengerPlane;

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<IdentifiedFlightObject> aircrafts = Arrays.asList(
new PassengerPlane("A380"),
new PassengerPlane("A350"),
new PassengerPlane("Embraer 190"),
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/flight/reservation/flight/Flight.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import flight.reservation.Airport;
import flight.reservation.plane.Helicopter;
import flight.reservation.plane.IdentifiedFlightObject;
import flight.reservation.plane.PassengerDrone;
import flight.reservation.plane.PassengerPlane;

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

public Flight(int number, Airport departure, Airport arrival, Object aircraft) throws IllegalArgumentException {
public Flight(int number, Airport departure, Airport arrival, IdentifiedFlightObject aircraft) throws IllegalArgumentException {
this.number = number;
this.departure = departure;
this.arrival = arrival;
Expand All @@ -32,19 +33,19 @@ private boolean isAircraftValid(Airport airport) {
return Arrays.stream(airport.getAllowedAircrafts()).anyMatch(x -> {
String model;
if (this.aircraft instanceof PassengerPlane) {
model = ((PassengerPlane) this.aircraft).model;
model = ((PassengerPlane) this.aircraft).getModel();
} else if (this.aircraft instanceof Helicopter) {
model = ((Helicopter) this.aircraft).getModel();
} else if (this.aircraft instanceof PassengerDrone) {
model = "HypaHype";
model = ((PassengerDrone) this.aircraft).getModel();
} else {
throw new IllegalArgumentException(String.format("Aircraft is not recognized"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

this could be a separate commit

throw new IllegalArgumentException("Aircraft is not recognized");
}
return x.equals(model);
});
}

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

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/flight/reservation/flight/ScheduledFlight.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import flight.reservation.Airport;
import flight.reservation.Passenger;
import flight.reservation.plane.Helicopter;
import flight.reservation.plane.IdentifiedFlightObject;
import flight.reservation.plane.PassengerDrone;
import flight.reservation.plane.PassengerPlane;

Expand All @@ -16,13 +17,13 @@ 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, IdentifiedFlightObject 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, IdentifiedFlightObject aircraft, Date departureTime, double currentPrice) {
super(number, departure, arrival, aircraft);
this.departureTime = departureTime;
this.passengers = new ArrayList<>();
Expand All @@ -31,7 +32,7 @@ public ScheduledFlight(int number, Airport departure, Airport arrival, Object ai

public int getCrewMemberCapacity() throws NoSuchFieldException {
Copy link
Collaborator

Choose a reason for hiding this comment

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

i would keep the function but call the method of aircraft

if (this.aircraft instanceof PassengerPlane) {
return ((PassengerPlane) this.aircraft).crewCapacity;
return ((PassengerPlane) this.aircraft).getCrewCapacity();
}
if (this.aircraft instanceof Helicopter) {
return 2;
Expand All @@ -52,7 +53,7 @@ public void removePassengers(List<Passenger> passengers) {

public int getCapacity() throws NoSuchFieldException {
if (this.aircraft instanceof PassengerPlane) {
return ((PassengerPlane) this.aircraft).passengerCapacity;
return ((PassengerPlane) this.aircraft).getPassengerCapacity();
}
if (this.aircraft instanceof Helicopter) {
return ((Helicopter) this.aircraft).getPassengerCapacity();
Expand All @@ -64,7 +65,7 @@ public int getCapacity() throws NoSuchFieldException {
}

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

public Date getDepartureTime() {
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/flight/reservation/plane/Helicopter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package flight.reservation.plane;

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

public Helicopter(String model) {
this.model = model;
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;
}
}
19 changes: 19 additions & 0 deletions src/main/java/flight/reservation/plane/IdentifiedFlightObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package flight.reservation.plane;

public abstract class IdentifiedFlightObject {
String model;
int passengerCapacity;
int crewCapacity;

public String getModel() {
return model;
}

public int getPassengerCapacity() {
return passengerCapacity;
}

public int getCrewCapacity() {
return crewCapacity;
}
}
3 changes: 1 addition & 2 deletions src/main/java/flight/reservation/plane/PassengerDrone.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package flight.reservation.plane;

public class PassengerDrone {
private final String model;
public class PassengerDrone extends IdentifiedFlightObject{

public PassengerDrone(String model) {
if (model.equals("HypaHype")) {
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 IdentifiedFlightObject{

public PassengerPlane(String model) {
this.model = model;
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/flight.reservation/ScenarioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void thenTheBookingShouldBeStopped() throws NoSuchFieldException {
ScheduledFlight scheduledFlight = schedule.searchScheduledFlight(flight.getNumber());
assertThrows(IllegalStateException.class, () -> customer.createOrder(Arrays.asList("Amanda", "Max"), Arrays.asList(scheduledFlight), 180));
assertEquals(3, scheduledFlight.getPassengers().size());
assertEquals(4, scheduledFlight.getCapacity());
Copy link
Collaborator

Choose a reason for hiding this comment

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

after the presentation today, I would prefer the old solution here :)

assertEquals(4, scheduledFlight.getAircraft().getPassengerCapacity());
assertEquals(1, scheduledFlight.getAvailableCapacity());
assertTrue(scheduledFlight.getPassengers().stream().noneMatch(passenger -> passenger.getName().equals("Max")));
assertTrue(scheduledFlight.getPassengers().stream().noneMatch(passenger -> passenger.getName().equals("Amanda")));
Expand All @@ -113,7 +113,7 @@ void thenTheBookingShouldSucceed() throws NoSuchFieldException {
FlightOrder order = customer.createOrder(Arrays.asList("Amanda", "Max"), Arrays.asList(scheduledFlight), 180);

assertEquals(2, scheduledFlight.getPassengers().size());
assertEquals(4, scheduledFlight.getCapacity());
assertEquals(4, scheduledFlight.getAircraft().getPassengerCapacity());
assertEquals(2, scheduledFlight.getAvailableCapacity());
assertTrue(scheduledFlight.getPassengers().stream().anyMatch(passenger -> passenger.getName().equals("Max")));
assertTrue(scheduledFlight.getPassengers().stream().anyMatch(passenger -> passenger.getName().equals("Amanda")));
Expand Down Expand Up @@ -207,7 +207,7 @@ void thenTheBookingShouldSucceed() throws NoSuchFieldException {
assertEquals(900, creditCard.getAmount());
assertEquals(1, scheduledFlight.getPassengers().size());
assertEquals("Max", scheduledFlight.getPassengers().get(0).getName());
assertEquals(500, scheduledFlight.getCapacity());
assertEquals(500, scheduledFlight.getAircraft().getPassengerCapacity());
assertEquals(499, scheduledFlight.getAvailableCapacity());

}
Expand Down