[POO] aula08 ex1 part2 added (exercise finished)
This commit is contained in:
parent
0f6e498cb3
commit
63c9a7732d
|
@ -2,16 +2,21 @@ package aula08.ex1;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Bus extends Vehicle {
|
public class Bus extends Vehicle implements IFuelVehicle, IEletricVehicle {
|
||||||
private final int boardNumber;
|
private final int boardNumber;
|
||||||
private final int weight;
|
private final int weight;
|
||||||
private final int maxPassengers;
|
private final int maxPassengers;
|
||||||
|
private final EngineType engineType;
|
||||||
|
|
||||||
public Bus(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxPassengers) {
|
private int fuelLevel;
|
||||||
|
private int battery;
|
||||||
|
|
||||||
|
public Bus(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxPassengers, EngineType engineType) {
|
||||||
super(plate, brand, model, potency);
|
super(plate, brand, model, potency);
|
||||||
this.boardNumber = boardNumber;
|
this.boardNumber = boardNumber;
|
||||||
this.weight = weight;
|
this.weight = weight;
|
||||||
this.maxPassengers = maxPassengers;
|
this.maxPassengers = maxPassengers;
|
||||||
|
this.engineType = engineType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBoardNumber() {
|
public int getBoardNumber() {
|
||||||
|
@ -26,6 +31,10 @@ public class Bus extends Vehicle {
|
||||||
return this.maxPassengers;
|
return this.maxPassengers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EngineType getEngineType() {
|
||||||
|
return this.engineType;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Bus {" +
|
return "Bus {" +
|
||||||
|
@ -38,6 +47,7 @@ public class Bus extends Vehicle {
|
||||||
",\n\tmaxPassengers=" + this.getMaxPassengers() +
|
",\n\tmaxPassengers=" + this.getMaxPassengers() +
|
||||||
",\n\tlastTripKm=" + this.lastTrip() +
|
",\n\tlastTripKm=" + this.lastTrip() +
|
||||||
",\n\tkm=" + this.totalDistance() +
|
",\n\tkm=" + this.totalDistance() +
|
||||||
|
String.format(",\n\t%s=%d", this.engineType == EngineType.FUEL ? "fuelLevel" : "battery", this.engineType == EngineType.FUEL ? this.fuelLevel : this.battery) +
|
||||||
"\n}";
|
"\n}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,11 +57,35 @@ public class Bus extends Vehicle {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
if (!super.equals(o)) return false;
|
if (!super.equals(o)) return false;
|
||||||
Bus bus = (Bus) o;
|
Bus bus = (Bus) o;
|
||||||
return this.getBoardNumber() == bus.getBoardNumber() && this.getWeight() == bus.getWeight() && this.getMaxPassengers() == bus.getMaxPassengers();
|
return this.getBoardNumber() == bus.getBoardNumber() && this.getWeight() == bus.getWeight() && this.getMaxPassengers() == bus.getMaxPassengers() && this.getEngineType().equals(bus.getEngineType());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getWeight(), this.getMaxPassengers());
|
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getWeight(), this.getMaxPassengers());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int currentBatteryLvl() {
|
||||||
|
return this.battery;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void charge(int percentage) {
|
||||||
|
if (this.engineType == EngineType.FUEL)
|
||||||
|
return;
|
||||||
|
this.battery = percentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int fuelLevel() {
|
||||||
|
return this.fuelLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillTank(int level) {
|
||||||
|
if (this.engineType == EngineType.ELETRIC)
|
||||||
|
return;
|
||||||
|
this.fuelLevel = level;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,18 +2,25 @@ package aula08.ex1;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Car extends Vehicle {
|
public class Car extends Vehicle implements IEletricVehicle, IFuelVehicle {
|
||||||
private final int boardNumber;
|
private final int boardNumber;
|
||||||
private final int trunkSize;
|
private final int trunkSize;
|
||||||
|
private final EngineType engineType;
|
||||||
|
|
||||||
public Car(String plate, String brand, String model, int potency, int boardNumber, int trunkSize) {
|
private int fuelLevel;
|
||||||
|
private int battery;
|
||||||
|
|
||||||
|
public Car(String plate, String brand, String model, int potency, int boardNumber, int trunkSize, EngineType engineType) {
|
||||||
super(plate, brand, model, potency);
|
super(plate, brand, model, potency);
|
||||||
this.boardNumber = boardNumber;
|
this.boardNumber = boardNumber;
|
||||||
this.trunkSize = trunkSize;
|
this.trunkSize = trunkSize;
|
||||||
|
this.engineType = engineType;
|
||||||
|
this.fuelLevel = 0;
|
||||||
|
this.battery = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Car(Car car) {
|
public Car(Car car) {
|
||||||
this(car.getPlate(), car.getBrand(), car.getModel(), car.getPotency(), car.getBoardNumber(), car.getTrunkSize());
|
this(car.getPlate(), car.getBrand(), car.getModel(), car.getPotency(), car.getBoardNumber(), car.getTrunkSize(), car.getEngineType());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBoardNumber() {
|
public int getBoardNumber() {
|
||||||
|
@ -24,6 +31,10 @@ public class Car extends Vehicle {
|
||||||
return this.trunkSize;
|
return this.trunkSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EngineType getEngineType() {
|
||||||
|
return this.engineType;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Car {" +
|
return "Car {" +
|
||||||
|
@ -35,6 +46,7 @@ public class Car extends Vehicle {
|
||||||
",\n\ttrunkSize=" + this.getTrunkSize() +
|
",\n\ttrunkSize=" + this.getTrunkSize() +
|
||||||
",\n\tlastTripKm=" + this.lastTrip() +
|
",\n\tlastTripKm=" + this.lastTrip() +
|
||||||
",\n\tkm=" + this.totalDistance() +
|
",\n\tkm=" + this.totalDistance() +
|
||||||
|
String.format(",\n\t%s=%d", this.engineType == EngineType.FUEL ? "fuelLevel" : "battery", this.engineType == EngineType.FUEL ? this.fuelLevel : this.battery) +
|
||||||
"\n}";
|
"\n}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,11 +57,35 @@ public class Car extends Vehicle {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
if (!super.equals(o)) return false;
|
if (!super.equals(o)) return false;
|
||||||
Car car = (Car) o;
|
Car car = (Car) o;
|
||||||
return this.getBoardNumber() == car.getBoardNumber() && this.getTrunkSize() == car.getTrunkSize();
|
return this.getBoardNumber() == car.getBoardNumber() && this.getTrunkSize() == car.getTrunkSize() && this.getEngineType().equals(car.getEngineType());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getTrunkSize());
|
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getTrunkSize(), this.getEngineType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int currentBatteryLvl() {
|
||||||
|
return this.battery;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void charge(int percentage) {
|
||||||
|
if (this.engineType == EngineType.FUEL)
|
||||||
|
return;
|
||||||
|
this.battery = percentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int fuelLevel() {
|
||||||
|
return this.fuelLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillTank(int level) {
|
||||||
|
if (this.engineType == EngineType.ELETRIC)
|
||||||
|
return;
|
||||||
|
this.fuelLevel = level;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package aula08.ex1;
|
||||||
|
|
||||||
|
public enum EngineType {
|
||||||
|
FUEL, ELETRIC;
|
||||||
|
|
||||||
|
public static EngineType fromString(String string) {
|
||||||
|
return switch (string) {
|
||||||
|
case "FUEL", "Fuel", "fuel" -> EngineType.FUEL;
|
||||||
|
case "ELETRIC", "Eletric", "eletric" -> EngineType.ELETRIC;
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package aula08.ex1;
|
||||||
|
|
||||||
|
public interface IEletricVehicle {
|
||||||
|
int currentBatteryLvl();
|
||||||
|
void charge(int percentage);
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package aula08.ex1;
|
||||||
|
|
||||||
|
public interface IFuelVehicle {
|
||||||
|
int fuelLevel();
|
||||||
|
void fillTank(int level);
|
||||||
|
}
|
|
@ -58,7 +58,7 @@ public class Main {
|
||||||
|
|
||||||
private static void manageComp(VehicleComp vehicleComp) {
|
private static void manageComp(VehicleComp vehicleComp) {
|
||||||
while (true) {
|
while (true) {
|
||||||
System.out.println("Choose an option\n1 - List vehicle\n2 - Get vehicle info\n3 - Vehicle with most Km\n4 - Send vehicle on a trip\n5 - Add Vehicle\n6 - Remove vehicle\n0 - Exit");
|
System.out.println("Choose an option\n1 - List vehicle\n2 - Get vehicle info\n3 - Vehicle with most Km\n4 - Manage vehicle\n5 - Add Vehicle\n6 - Remove vehicle\n0 - Exit");
|
||||||
int mainOption = Integer.parseInt(sin.nextLine());
|
int mainOption = Integer.parseInt(sin.nextLine());
|
||||||
switch (mainOption) {
|
switch (mainOption) {
|
||||||
case 0 -> {
|
case 0 -> {
|
||||||
|
@ -70,11 +70,94 @@ public class Main {
|
||||||
System.out.println(vehicleComp.getVehicleByPlate(sin.nextLine()));
|
System.out.println(vehicleComp.getVehicleByPlate(sin.nextLine()));
|
||||||
}
|
}
|
||||||
case 3 -> vehicleComp.printVehicleWithMostKm();
|
case 3 -> vehicleComp.printVehicleWithMostKm();
|
||||||
case 4 -> vehicleComp.tripMenu(sin);
|
case 4 -> {
|
||||||
|
System.out.print("Plate of the vehicle to manage: ");
|
||||||
|
String plate = sin.nextLine();
|
||||||
|
Vehicle vehicle = vehicleComp.getVehicleByPlate(plate);
|
||||||
|
manageVehicle(vehicle);
|
||||||
|
}
|
||||||
case 5 -> vehicleComp.addVehicleMenu(sin);
|
case 5 -> vehicleComp.addVehicleMenu(sin);
|
||||||
case 6 -> vehicleComp.removeVehicleMenu(sin);
|
case 6 -> vehicleComp.removeVehicleMenu(sin);
|
||||||
default -> System.out.println("Invalid option");
|
default -> System.out.println("Invalid option");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void manageVehicle(Vehicle vehicle) {
|
||||||
|
while (true) {
|
||||||
|
System.out.println("Choose an option\n1 - Send vehicle on a trip\n2 - Refuel/Recharge\n0 - Exit");
|
||||||
|
int mainOption = Integer.parseInt(sin.nextLine());
|
||||||
|
switch (mainOption) {
|
||||||
|
case 0 -> {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1 -> {
|
||||||
|
System.out.print("Km the vehicle is going to travel: ");
|
||||||
|
int km = Integer.parseInt(sin.nextLine());
|
||||||
|
vehicle.trip(km);
|
||||||
|
if (vehicle instanceof Car car) {
|
||||||
|
if (car.getEngineType() == EngineType.FUEL) {
|
||||||
|
System.out.print("What fuel level is the car going to be left at: ");
|
||||||
|
int level = Integer.parseInt(sin.nextLine());
|
||||||
|
((Car) vehicle).fillTank(level);
|
||||||
|
} else {
|
||||||
|
System.out.print("What battery percentage is the car going to be left at: ");
|
||||||
|
int battery = Integer.parseInt(sin.nextLine());
|
||||||
|
((Car) vehicle).charge(battery);
|
||||||
|
}
|
||||||
|
} else if (vehicle instanceof Bus bus) {
|
||||||
|
if (bus.getEngineType() == EngineType.FUEL) {
|
||||||
|
System.out.print("What fuel level is the bus going to be left at: ");
|
||||||
|
int level = Integer.parseInt(sin.nextLine());
|
||||||
|
((Bus) vehicle).fillTank(level);
|
||||||
|
} else {
|
||||||
|
System.out.print("What battery percentage is the bus going to be left at: ");
|
||||||
|
int battery = Integer.parseInt(sin.nextLine());
|
||||||
|
((Bus) vehicle).charge(battery);
|
||||||
|
}
|
||||||
|
} else if (vehicle instanceof Motorcycle) {
|
||||||
|
System.out.print("What fuel level is the motorcycle going to be left at: ");
|
||||||
|
int level = Integer.parseInt(sin.nextLine());
|
||||||
|
((Motorcycle) vehicle).fillTank(level);
|
||||||
|
} else if (vehicle instanceof Truck) {
|
||||||
|
System.out.print("What fuel level is the truck going to be left at: ");
|
||||||
|
int level = Integer.parseInt(sin.nextLine());
|
||||||
|
((Truck) vehicle).fillTank(level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 2 -> {
|
||||||
|
if (vehicle instanceof Car car) {
|
||||||
|
if (car.getEngineType() == EngineType.FUEL) {
|
||||||
|
System.out.print("What fuel level are you refueling the car to: ");
|
||||||
|
int level = Integer.parseInt(sin.nextLine());
|
||||||
|
((Car) vehicle).fillTank(level);
|
||||||
|
} else {
|
||||||
|
System.out.print("What battery percentage are you charging the car to: ");
|
||||||
|
int battery = Integer.parseInt(sin.nextLine());
|
||||||
|
((Car) vehicle).charge(battery);
|
||||||
|
}
|
||||||
|
} else if (vehicle instanceof Bus bus) {
|
||||||
|
if (bus.getEngineType() == EngineType.FUEL) {
|
||||||
|
System.out.print("What fuel level are you refueling the bus to: ");
|
||||||
|
int level = Integer.parseInt(sin.nextLine());
|
||||||
|
((Bus) vehicle).fillTank(level);
|
||||||
|
} else {
|
||||||
|
System.out.print("What battery percentage are you charging the bus to: ");
|
||||||
|
int battery = Integer.parseInt(sin.nextLine());
|
||||||
|
((Bus) vehicle).charge(battery);
|
||||||
|
}
|
||||||
|
} else if (vehicle instanceof Motorcycle) {
|
||||||
|
System.out.print("What fuel level are you refueling the motorcycle to: ");
|
||||||
|
int level = Integer.parseInt(sin.nextLine());
|
||||||
|
((Motorcycle) vehicle).fillTank(level);
|
||||||
|
} else if (vehicle instanceof Truck) {
|
||||||
|
System.out.print("What fuel level are you refueling the truck to: ");
|
||||||
|
int level = Integer.parseInt(sin.nextLine());
|
||||||
|
((Truck) vehicle).fillTank(level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default -> System.out.println("Invalid option");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,25 +2,28 @@ package aula08.ex1;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Motorcicle extends Vehicle {
|
public class Motorcycle extends Vehicle implements IFuelVehicle {
|
||||||
private final MotorcicleType type;
|
private final MotorcycleType type;
|
||||||
|
|
||||||
public Motorcicle(String plate, String brand, String model, int potency, MotorcicleType type) {
|
private int fuelLevel;
|
||||||
|
|
||||||
|
public Motorcycle(String plate, String brand, String model, int potency, MotorcycleType type) {
|
||||||
super(plate, brand, model, potency);
|
super(plate, brand, model, potency);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
this.fuelLevel = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Motorcicle(Motorcicle motorcicle) {
|
public Motorcycle(Motorcycle motorcycle) {
|
||||||
this(motorcicle.getPlate(), motorcicle.getBrand(), motorcicle.getModel(), motorcicle.getPotency(), motorcicle.getType());
|
this(motorcycle.getPlate(), motorcycle.getBrand(), motorcycle.getModel(), motorcycle.getPotency(), motorcycle.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
public MotorcicleType getType() {
|
public MotorcycleType getType() {
|
||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Motorcicle {" +
|
return "Motorcycle {" +
|
||||||
"\n\tplate='" + this.getPlate() + '\'' +
|
"\n\tplate='" + this.getPlate() + '\'' +
|
||||||
",\n\tbrand='" + this.getBrand() + '\'' +
|
",\n\tbrand='" + this.getBrand() + '\'' +
|
||||||
",\n\tmodel='" + this.getModel() + '\'' +
|
",\n\tmodel='" + this.getModel() + '\'' +
|
||||||
|
@ -28,6 +31,7 @@ public class Motorcicle extends Vehicle {
|
||||||
",\n\ttype=" + this.getType().toString() +
|
",\n\ttype=" + this.getType().toString() +
|
||||||
",\n\tlastTripKm=" + this.lastTrip() +
|
",\n\tlastTripKm=" + this.lastTrip() +
|
||||||
",\n\tkm=" + this.totalDistance() +
|
",\n\tkm=" + this.totalDistance() +
|
||||||
|
",\n\tfuelLevel=" + this.fuelLevel() +
|
||||||
"\n}";
|
"\n}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +40,7 @@ public class Motorcicle extends Vehicle {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
if (!super.equals(o)) return false;
|
if (!super.equals(o)) return false;
|
||||||
Motorcicle that = (Motorcicle) o;
|
Motorcycle that = (Motorcycle) o;
|
||||||
return this.getType() == that.getType();
|
return this.getType() == that.getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +49,17 @@ public class Motorcicle extends Vehicle {
|
||||||
return Objects.hash(super.hashCode(), this.getType());
|
return Objects.hash(super.hashCode(), this.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
enum MotorcicleType {
|
@Override
|
||||||
|
public int fuelLevel() {
|
||||||
|
return this.fuelLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillTank(int level) {
|
||||||
|
this.fuelLevel = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MotorcycleType {
|
||||||
SPORT, TOURING;
|
SPORT, TOURING;
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -55,11 +69,11 @@ public class Motorcicle extends Vehicle {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MotorcicleType fromString(String s) {
|
public static MotorcycleType fromString(String s) {
|
||||||
return switch (s) {
|
return switch (s) {
|
||||||
case "SPORT", "Sport", "sport" -> SPORT;
|
case "SPORT", "Sport", "sport" -> SPORT;
|
||||||
case "TOURING", "Touring", "touring" -> TOURING;
|
case "TOURING", "Touring", "touring" -> TOURING;
|
||||||
default -> throw new IllegalArgumentException("Invalid MotorcicleType: " + s);
|
default -> throw new IllegalArgumentException("Invalid MotorcycleType: " + s);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -15,7 +15,7 @@ public class Taxi extends Car {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Car getCar() {
|
public Car getCar() {
|
||||||
return new Car(this.getPlate(), this.getBrand(), this.getModel(), this.getPotency(), this.getBoardNumber(), this.getTrunkSize());
|
return new Car(this.getPlate(), this.getBrand(), this.getModel(), this.getPotency(), this.getBoardNumber(), this.getTrunkSize(), this.getEngineType());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getLicenseNumber() {
|
public int getLicenseNumber() {
|
||||||
|
@ -34,6 +34,7 @@ public class Taxi extends Car {
|
||||||
",\n\ttrunkSize=" + this.getTrunkSize() +
|
",\n\ttrunkSize=" + this.getTrunkSize() +
|
||||||
",\n\tlastTripKm=" + this.lastTrip() +
|
",\n\tlastTripKm=" + this.lastTrip() +
|
||||||
",\n\tkm=" + this.totalDistance() +
|
",\n\tkm=" + this.totalDistance() +
|
||||||
|
String.format(",\n\t%s=%d", this.getEngineType() == EngineType.FUEL ? "fuelLevel" : "battery", this.getEngineType() == EngineType.FUEL ? this.fuelLevel() : this.currentBatteryLvl()) +
|
||||||
"\n}";
|
"\n}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,16 +2,18 @@ package aula08.ex1;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Truck extends Vehicle {
|
public class Truck extends Vehicle implements IFuelVehicle {
|
||||||
private final int boardNumber;
|
private final int boardNumber;
|
||||||
private final int weight;
|
private final int weight;
|
||||||
private final int maxWeight;
|
private final int maxWeight;
|
||||||
|
private int fuelLevel;
|
||||||
|
|
||||||
public Truck(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxWeight) {
|
public Truck(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxWeight) {
|
||||||
super(plate, brand, model, potency);
|
super(plate, brand, model, potency);
|
||||||
this.boardNumber = boardNumber;
|
this.boardNumber = boardNumber;
|
||||||
this.weight = weight;
|
this.weight = weight;
|
||||||
this.maxWeight = maxWeight;
|
this.maxWeight = maxWeight;
|
||||||
|
this.fuelLevel = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBoardNumber() {
|
public int getBoardNumber() {
|
||||||
|
@ -38,6 +40,7 @@ public class Truck extends Vehicle {
|
||||||
",\n\tmaxWeight=" + this.getMaxWeight() +
|
",\n\tmaxWeight=" + this.getMaxWeight() +
|
||||||
",\n\tlastTripKm=" + this.lastTrip() +
|
",\n\tlastTripKm=" + this.lastTrip() +
|
||||||
",\n\tkm=" + this.totalDistance() +
|
",\n\tkm=" + this.totalDistance() +
|
||||||
|
",\n\tfuelLevel=" + this.fuelLevel() +
|
||||||
"\n}";
|
"\n}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,4 +58,14 @@ public class Truck extends Vehicle {
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getWeight(), this.getMaxWeight());
|
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getWeight(), this.getMaxWeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int fuelLevel() {
|
||||||
|
return this.fuelLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillTank(int level) {
|
||||||
|
this.fuelLevel = level;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ public class VehicleComp {
|
||||||
System.out.print("Km to travel: ");
|
System.out.print("Km to travel: ");
|
||||||
int km = Integer.parseInt(sin.nextLine());
|
int km = Integer.parseInt(sin.nextLine());
|
||||||
Vehicle vehicle = this.getVehicleByPlate(plate);
|
Vehicle vehicle = this.getVehicleByPlate(plate);
|
||||||
|
|
||||||
vehicle.trip(km);
|
vehicle.trip(km);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,13 +70,13 @@ public class VehicleComp {
|
||||||
String model = sin.nextLine();
|
String model = sin.nextLine();
|
||||||
System.out.print("Vehicle potency: ");
|
System.out.print("Vehicle potency: ");
|
||||||
int potency = Integer.parseInt(sin.nextLine());
|
int potency = Integer.parseInt(sin.nextLine());
|
||||||
System.out.print("What vehicle are you adding?\n1 - Motorcicle\n2 - Car\n3 - Taxi\n4 - Bus\n5 - Truck\n>> ");
|
System.out.print("What vehicle are you adding?\n1 - Motorcycle\n2 - Car\n3 - Taxi\n4 - Bus\n5 - Truck\n>> ");
|
||||||
int vehicleType = Integer.parseInt(sin.nextLine());
|
int vehicleType = Integer.parseInt(sin.nextLine());
|
||||||
switch (vehicleType) {
|
switch (vehicleType) {
|
||||||
case 1 -> {
|
case 1 -> {
|
||||||
System.out.print("What's the motorcicle type? (SPORT/TOURING): ");
|
System.out.print("What's the motorcycle type? (SPORT/TOURING): ");
|
||||||
Motorcicle.MotorcicleType motorcicleType = Motorcicle.MotorcicleType.fromString(sin.nextLine());
|
Motorcycle.MotorcycleType motorcycleType = Motorcycle.MotorcycleType.fromString(sin.nextLine());
|
||||||
addVehicle(new Motorcicle(plate, brand, model, potency, motorcicleType));
|
addVehicle(new Motorcycle(plate, brand, model, potency, motorcycleType));
|
||||||
}
|
}
|
||||||
case 2, 3, 4, 5 -> {
|
case 2, 3, 4, 5 -> {
|
||||||
System.out.print("Vehicle's board number: ");
|
System.out.print("Vehicle's board number: ");
|
||||||
|
@ -84,7 +85,9 @@ public class VehicleComp {
|
||||||
case 2, 3 -> {
|
case 2, 3 -> {
|
||||||
System.out.print("Car's trunk size: ");
|
System.out.print("Car's trunk size: ");
|
||||||
int trunkSize = Integer.parseInt(sin.nextLine());
|
int trunkSize = Integer.parseInt(sin.nextLine());
|
||||||
Car car = new Car(plate, brand, model, potency, boardNumber, trunkSize);
|
System.out.print("What's the engine type? (FUEL/ELETRIC): ");
|
||||||
|
EngineType engineType = EngineType.fromString(sin.nextLine());
|
||||||
|
Car car = new Car(plate, brand, model, potency, boardNumber, trunkSize, engineType);
|
||||||
if (vehicleType == 2)
|
if (vehicleType == 2)
|
||||||
addVehicle(car);
|
addVehicle(car);
|
||||||
else {
|
else {
|
||||||
|
@ -99,7 +102,9 @@ public class VehicleComp {
|
||||||
if (vehicleType == 4) {
|
if (vehicleType == 4) {
|
||||||
System.out.print("Bus passenger limit: ");
|
System.out.print("Bus passenger limit: ");
|
||||||
int maxPassengers = Integer.parseInt(sin.nextLine());
|
int maxPassengers = Integer.parseInt(sin.nextLine());
|
||||||
addVehicle(new Bus(plate, brand, model, potency, boardNumber, weight, maxPassengers));
|
System.out.print("What's the engine type? (FUEL/ELETRIC): ");
|
||||||
|
EngineType engineType = EngineType.fromString(sin.nextLine());
|
||||||
|
addVehicle(new Bus(plate, brand, model, potency, boardNumber, weight, maxPassengers, engineType));
|
||||||
} else {
|
} else {
|
||||||
System.out.print("Truck weight limit: ");
|
System.out.print("Truck weight limit: ");
|
||||||
int maxWeight = Integer.parseInt(sin.nextLine());
|
int maxWeight = Integer.parseInt(sin.nextLine());
|
||||||
|
|
Loading…
Reference in New Issue