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 2: Aircraft Abstraction and Duplicate Code Removal. #20

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
18 changes: 8 additions & 10 deletions src/main/java/Runner.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import flight.reservation.Airport;
import flight.reservation.flight.Schedule;
import flight.reservation.flight.Flight;
import flight.reservation.plane.Helicopter;
import flight.reservation.plane.PassengerDrone;
import flight.reservation.plane.PassengerPlane;
import flight.reservation.plane.*;

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

static List<Object> aircrafts = Arrays.asList(
new PassengerPlane("A380"),
new PassengerPlane("A350"),
new PassengerPlane("Embraer 190"),
new PassengerPlane("Antonov AN2"),
new Helicopter("H1"),
new PassengerDrone("HypaHype")
static List<IAircraft> aircrafts = Arrays.asList(
new A380Plane(),
new A350Plane(),
new Embraer190Plane(),
new AntonovAN2Plane(),
new H1Helicopter(),
new HypaHypePassengerDrone()
);

static List<Flight> flights = Arrays.asList(
Expand Down
22 changes: 4 additions & 18 deletions src/main/java/flight/reservation/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public Customer(String name, String email) {
}

public FlightOrder createOrder(List<String> passengerNames, List<ScheduledFlight> flights, double price) {
if (!isOrderValid(passengerNames, flights)) {
throw new IllegalStateException("Order is not valid");
}
FlightOrder order = new FlightOrder(flights);
order.setCustomer(this);
order.setPrice(price);
Expand All @@ -33,25 +30,14 @@ public FlightOrder createOrder(List<String> passengerNames, List<ScheduledFlight
.collect(Collectors.toList());
order.setPassengers(passengers);
order.getScheduledFlights().forEach(scheduledFlight -> scheduledFlight.addPassengers(passengers));
if (!order.isValid()) {
throw new IllegalStateException("Order is not valid");
}

orders.add(order);
return order;
}

private boolean isOrderValid(List<String> passengerNames, List<ScheduledFlight> flights) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good decision to remove logic that belongs to a specific order

boolean valid = true;
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 valid;
}

public String getEmail() {
return email;
}
Expand Down
24 changes: 6 additions & 18 deletions src/main/java/flight/reservation/flight/Flight.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package flight.reservation.flight;

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

import java.util.Arrays;

Expand All @@ -12,9 +10,9 @@ public class Flight {
private int number;
private Airport departure;
private Airport arrival;
protected Object aircraft;
protected IAircraft aircraft;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good decision to remove object casting with base class


public Flight(int number, Airport departure, Airport arrival, Object aircraft) throws IllegalArgumentException {
public Flight(int number, Airport departure, Airport arrival, IAircraft aircraft) throws IllegalArgumentException {
this.number = number;
this.departure = departure;
this.arrival = arrival;
Expand All @@ -29,22 +27,12 @@ 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 Arrays.stream(airport.getAllowedAircrafts()).anyMatch(allowedModel -> {
return allowedModel.equals(aircraft.getModel());
});
}

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

Expand Down
31 changes: 6 additions & 25 deletions src/main/java/flight/reservation/flight/ScheduledFlight.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import flight.reservation.Airport;
import flight.reservation.Passenger;
import flight.reservation.plane.Helicopter;
import flight.reservation.plane.PassengerDrone;
import flight.reservation.plane.PassengerPlane;
import flight.reservation.plane.IAircraft;
import flight.reservation.plane.HypaHypePassengerDrone;

import java.util.ArrayList;
import java.util.Date;
Expand All @@ -16,30 +15,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, IAircraft 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, IAircraft 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");
return aircraft.getCrewCapacity();
}

public void addPassengers(List<Passenger> passengers) {
Expand All @@ -51,16 +41,7 @@ public void removePassengers(List<Passenger> 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");
return this.aircraft.getPassengerCapacity();
}

public int getAvailableCapacity() throws NoSuchFieldException {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/flight/reservation/order/FlightOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public List<ScheduledFlight> getScheduledFlights() {
return flights;
}

private boolean isOrderValid(Customer customer, List<String> passengerNames, List<ScheduledFlight> flights) {
public boolean isValid() {
boolean valid = true;
valid = valid && !noFlyList.contains(customer.getName());
valid = valid && passengerNames.stream().noneMatch(passenger -> noFlyList.contains(passenger));
valid = valid && passengers.stream().noneMatch(passenger -> noFlyList.contains(passenger.getName()));
valid = valid && flights.stream().allMatch(scheduledFlight -> {
try {
return scheduledFlight.getAvailableCapacity() >= passengerNames.size();
return scheduledFlight.getAvailableCapacity() >= passengers.size();
} catch (NoSuchFieldException e) {
e.printStackTrace();
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/flight/reservation/order/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class Order {
private final UUID id;
private double price;
private boolean isClosed = false;
private Customer customer;
private List<Passenger> passengers;
protected Customer customer;
protected List<Passenger> passengers;

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

public class A350Plane implements IAircraft {

@Override
public String getModel() {
return "A350";
}

@Override
public int getPassengerCapacity() {
return 320;
}

@Override
public int getCrewCapacity() {
return 40;
}
}
19 changes: 19 additions & 0 deletions src/main/java/flight/reservation/plane/A380Plane.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package flight.reservation.plane;

public class A380Plane implements IAircraft {

@Override
public String getModel() {
return "A380";
}

@Override
public int getPassengerCapacity() {
return 500;
}

@Override
public int getCrewCapacity() {
return 42;
}
}
19 changes: 19 additions & 0 deletions src/main/java/flight/reservation/plane/AntonovAN2Plane.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package flight.reservation.plane;

public class AntonovAN2Plane implements IAircraft {

@Override
public String getModel() {
return "Antonov AN2";
}

@Override
public int getPassengerCapacity() {
return 15;
}

@Override
public int getCrewCapacity() {
return 3;
}
}
19 changes: 19 additions & 0 deletions src/main/java/flight/reservation/plane/Embraer190Plane.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package flight.reservation.plane;

public class Embraer190Plane implements IAircraft {

@Override
public String getModel() {
return "Embraer 190";
}

@Override
public int getPassengerCapacity() {
return 25;
}

@Override
public int getCrewCapacity() {
return 5;
}
}
14 changes: 14 additions & 0 deletions src/main/java/flight/reservation/plane/H1Helicopter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package flight.reservation.plane;

public class H1Helicopter extends Helicopter {

@Override
public String getModel() {
return "H1";
}

@Override
public int getPassengerCapacity() {
return 4;
}
}
14 changes: 14 additions & 0 deletions src/main/java/flight/reservation/plane/H2Helicopter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package flight.reservation.plane;

public class H2Helicopter extends Helicopter {

@Override
public String getModel() {
return "H2";
}

@Override
public int getPassengerCapacity() {
return 6;
}
}
25 changes: 3 additions & 22 deletions src/main/java/flight/reservation/plane/Helicopter.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
package flight.reservation.plane;

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

public Helicopter(String model) {
this.model = model;
if (model.equals("H1")) {
passengerCapacity = 4;
} else if (model.equals("H2")) {
passengerCapacity = 6;
} else {
throw new IllegalArgumentException(String.format("Model type '%s' is not recognized", model));
}
}

public String getModel() {
return model;
}

public int getPassengerCapacity() {
return passengerCapacity;
}
public abstract class Helicopter implements IAircraft {
liedtkeInTUM marked this conversation as resolved.
Show resolved Hide resolved
@Override
public int getCrewCapacity() { return 2; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it is not ideal to enforce each helicopter with 2 crew member (although this setup allows it)

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

public class HypaHypePassengerDrone implements IAircraft {

@Override
public String getModel() {
return "HypyHype";
}


@Override
public int getPassengerCapacity() {
return 4;
}

@Override
public int getCrewCapacity() {
return 0;
}
}
7 changes: 7 additions & 0 deletions src/main/java/flight/reservation/plane/IAircraft.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package flight.reservation.plane;

public interface IAircraft {
String getModel();
int getPassengerCapacity();
int getCrewCapacity();
}
13 changes: 0 additions & 13 deletions src/main/java/flight/reservation/plane/PassengerDrone.java

This file was deleted.

Loading