Last active
March 30, 2023 19:08
-
-
Save Filmaluco/7679b75e0b3d9844f49d8a1417787e7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FleetUI { | |
private final String licensePlateRegex = "[0-9]{2}[\\s-]{0,1}[0-9]{2}[\\s-]{0,1}[A-IK-PR-VZ]{2}|[0-9]{2}[\\s-]{0,1}[A-IK-PR-VZ]{2}[\\s-]{0,1}[0-9]{2}|[A-IK-PR-WYZ]{2}[\\s-]{0,1}[0-9]{2}[\\s-]{0,1}[A-IK-PR-WYZ]{2}"; | |
final IFleet fleet; | |
final Scanner scr; | |
public FleetUI(IFleet fleet) { | |
this.fleet = fleet; | |
scr = new Scanner(System.in); | |
} | |
private void addVehicle() { | |
String licencePlate = askForLicensePlate(); | |
if(licencePlate == null) { | |
return; | |
} | |
if (fleet.contains(licencePlate)) { | |
System.out.println("Vehicle already exists"); | |
return; | |
} | |
int year = PAInput.readInt("Year: "); | |
int type = PAInput.chooseOption("What type of Vehicle is this", | |
"Passenger Car", "Bus", "Cargo", | |
"Quit"); | |
Vehicle vehicle = switch (type) { | |
case 1: { | |
int numberPassengers = PAInput.readInt("Number Passengers: "); | |
yield new PassengerCar(licencePlate, year, numberPassengers); | |
} | |
case 2: { | |
int numberPassengers = PAInput.readInt("Number Passengers: "); | |
int loadCapacity = PAInput.readInt("Load capacity: "); | |
yield new Bus(licencePlate, year, numberPassengers, loadCapacity); | |
} | |
case 3: { | |
int loadCapacity = PAInput.readInt("Load capacity: "); | |
yield new Cargo(licencePlate, year, loadCapacity); | |
} | |
default: { | |
yield null; | |
} | |
}; | |
if(vehicle != null) { | |
fleet.addVehicle(vehicle); | |
} | |
} | |
private void removeVehicle() { | |
String licencePlate = askForLicensePlate(); | |
if(licencePlate == null) { | |
return; | |
} | |
if(fleet.removeVehicle(licencePlate)) { | |
System.out.println("Vehicle removed"); | |
} else { | |
System.out.println("Vehicle not found"); | |
} | |
} | |
String askForLicensePlate() { | |
String licencePlate; | |
do { | |
licencePlate = PAInput.readString("License Plate (xx-xx-xx) | Q to exit: ", false); | |
if (licencePlate.equalsIgnoreCase("Q")) { | |
return null; | |
} | |
} while (!Pattern.matches(licensePlateRegex, licencePlate.toUpperCase(Locale.ROOT))); | |
return licencePlate; | |
} | |
public void go() { | |
while (true) { | |
switch (PAInput.chooseOption("Fleet Manager - " + fleet.getName(), | |
"Add new vehicle", "remove vehicle", "list all vehicles", "list all passenger vehicles", "list all load vehicles", | |
"Quit")) { | |
case 1: | |
addVehicle(); | |
break; | |
case 2: | |
removeVehicle(); | |
break; | |
case 3: | |
System.out.println(fleet); | |
break; | |
case 4: | |
System.out.println(fleet.toStringSortByNumberOfPassengers()); | |
break; | |
case 5: | |
System.out.println(fleet.toStringSortByMaxLoad()); | |
break; | |
case 6: | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment