LSD and POO updates #54

[LSD] CombShiftUnit_Demo added (pratica06 - part3)

[POO] material added
[POO] aula08 ex1 part1 added
[POO] aula08 ex1 part2 added (exercise finished)
[POO] aula08 ex1 refactoring
[POO] created validations
This commit is contained in:
Tiago Garcia 2023-04-07 23:50:29 +01:00 committed by GitHub
commit 90f6092b6e
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
21 changed files with 997 additions and 0 deletions

View File

@ -0,0 +1,50 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.NUMERIC_STD.all;
entity CombShiftUnit is
port
(
clk : in std_logic;
dataIn : in std_logic_vector(7 downto 0);
loadEn, rotate, dirLeft, shArith : in std_logic;
shAmount : in std_logic_vector(2 downto 0);
dataOut : out std_logic_vector(7 downto 0)
);
end CombShiftUnit;
architecture Behavioral of CombShiftUnit is
signal s_shiftReg : std_logic_vector(7 downto 0);
begin
process (clk)
begin
if (falling_edge(clk)) then
if (loaden = '1') then
s_shiftReg <= datain;
elsif (rotate = '1') then
if (dirleft = '1') then
s_shiftReg <= std_logic_vector( rotate_left ( unsigned(s_shiftReg), to_integer(unsigned(shAmount)) ) );
else
s_shiftReg <= std_logic_vector( rotate_right( unsigned(s_shiftReg), to_integer(unsigned(shAmount)) ) );
end if;
elsif (sharith = '1') then
if (dirleft = '1') then
s_shiftReg <= std_logic_vector( shift_left ( signed(s_shiftReg), to_integer(unsigned(shAmount)) ) );
else
s_shiftReg <= std_logic_vector( shift_right( signed(s_shiftReg), to_integer(unsigned(shAmount)) ) );
end if;
else
if (dirleft = '1') then
s_shiftReg <= std_logic_vector( shift_left ( unsigned(s_shiftReg), to_integer(unsigned(shAmount)) ) );
else
s_shiftReg <= std_logic_vector( shift_right( unsigned(s_shiftReg), to_integer(unsigned(shAmount)) ) );
end if;
end if;
end if;
end process;
dataOut <= s_shiftReg;
end Behavioral;

View File

@ -0,0 +1,34 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.NUMERIC_STD.all;
entity CombShiftUnit_Demo is
port
(
CLOCK_50 : in std_logic;
SW : in std_logic_vector(17 downto 0);
KEY : in std_logic_vector(2 downto 0);
LEDR : out std_logic_vector(7 downto 0)
);
end CombShiftUnit_Demo;
architecture Shell of CombShiftUnit_Demo is
signal clk : std_logic;
begin
freq : entity work.FreqDivider(Behavioral)
generic map (divFactor => 12_500_000)
port map (clkIn => CLOCK_50, clkOut => clk);
core : entity work.CombShiftUnit(Behavioral)
port map
(
clk => clk,
dataIn => SW(7 downto 0),
loadEn => SW(8),
rotate => KEY(0),
dirLeft => KEY(1),
shArith => KEY(2),
shAmount => SW(17 downto 15),
dataOut => LEDR
);
end Shell;

View File

@ -0,0 +1,33 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.NUMERIC_STD.all;
entity FreqDivider is
generic(divFactor : positive := 10);
port
(
clkIn : in std_logic;
clkOut : out std_logic
);
end FreqDivider;
architecture Behavioral of FreqDivider is
subtype TCounter is natural range 0 to divFactor - 1;
signal s_divCounter : TCounter := 0;
begin
assert(divFactor >= 2);
process(clkIn)
begin
if (rising_edge(clkIn)) then
if (s_divCounter >= (divFactor - 1)) then
clkOut <= '0';
s_divCounter <= 0;
else
if (s_divCounter = (divFactor / 2 - 1)) then
clkOut <= '1';
end if;
s_divCounter <= s_divCounter + 1;
end if;
end if;
end process;
end Behavioral;

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,13 @@
package aula08.ex1.Enums;
public enum EngineType {
FUEL, ELECTRIC;
public static EngineType fromString(String string) {
return switch (string) {
case "FUEL", "Fuel", "fuel" -> EngineType.FUEL;
case "ELECTRIC", "Electric", "electric" -> EngineType.ELECTRIC;
default -> null;
};
}
}

View File

@ -0,0 +1,6 @@
package aula08.ex1.Interfaces;
public interface IElectricVehicle {
int currentBatteryLvl();
void charge(int percentage);
}

View File

@ -0,0 +1,6 @@
package aula08.ex1.Interfaces;
public interface IFuelVehicle {
int fuelLevel();
void fillTank(int level);
}

View File

@ -0,0 +1,7 @@
package aula08.ex1.Interfaces;
public interface IKmTravelled {
void trip(int km);
int lastTrip();
int totalDistance();
}

View File

@ -0,0 +1,166 @@
package aula08.ex1;
import aula08.ex1.Enums.EngineType;
import aula08.ex1.Vehicles.*;
import java.util.Scanner;
public class Main {
private static final Scanner sin = new Scanner(System.in);
private static VehicleComp[] vehicleComps;
public static void main(String[] args) {
while (true) {
System.out.println("Choose an option\n1 - Create new company\n2 - Delete company\n3 - List companies\n4 - Manage company\n0 - Exit");
int mainOption = Integer.parseInt(sin.nextLine());
switch (mainOption) {
case 0 -> {
sin.close();
System.exit(0);
}
case 1 -> {
System.out.print("New company name: ");
String name = sin.nextLine();
VehicleComp vehicleComp = new VehicleComp(name);
if (vehicleComps == null) {
vehicleComps = new VehicleComp[1];
vehicleComps[0] = vehicleComp;
} else {
VehicleComp[] newVehicleComps = new VehicleComp[vehicleComps.length + 1];
System.arraycopy(vehicleComps, 0, newVehicleComps, 0, vehicleComps.length);
newVehicleComps[newVehicleComps.length - 1] = vehicleComp;
vehicleComps = newVehicleComps;
}
}
case 2, 3, 4 -> {
for (VehicleComp vehicleComp : vehicleComps)
System.out.println(vehicleComp);
if (mainOption == 2) {
System.out.print("Name of the company to remove: ");
String name = sin.nextLine();
VehicleComp[] newVehicleComps = new VehicleComp[vehicleComps.length - 1];
int index = 0;
for (VehicleComp vC : vehicleComps)
if (!vC.getName().equals(name))
newVehicleComps[index++] = vC;
vehicleComps = newVehicleComps;
}
if (mainOption == 4) {
System.out.print("Name of the company to manage: ");
String name = sin.nextLine();
for (VehicleComp vehicleComp : vehicleComps)
if (vehicleComp.getName().equals(name))
manageComp(vehicleComp);
}
}
default -> System.out.println("Invalid option");
}
}
}
private static void manageComp(VehicleComp vehicleComp) {
while (true) {
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());
switch (mainOption) {
case 0 -> {
return;
}
case 1 -> vehicleComp.listVehicles();
case 2 -> {
System.out.print("Vehicle plate: ");
System.out.println(vehicleComp.getVehicleByPlate(sin.nextLine()));
}
case 3 -> vehicleComp.printVehicleWithMostKm();
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 6 -> vehicleComp.removeVehicleMenu(sin);
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");
}
}
}
}

View File

@ -0,0 +1,168 @@
package aula08.ex1;
import aula08.ex1.Enums.EngineType;
import aula08.ex1.Vehicles.*;
import java.util.Scanner;
public class VehicleComp {
private final String name;
private Vehicle[] vehicles;
public VehicleComp(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void listVehicles() {
System.out.println("List of vehicles in company " + this.name);
if (this.vehicles == null) {
System.out.println("You must add a vehicle first");
return;
}
for (Vehicle v : this.vehicles)
System.out.println(v);
}
public Vehicle getVehicleByPlate(String plate) {
for (Vehicle v : this.vehicles)
if (v.getPlate().equals(plate))
return v;
return null;
}
public void tripMenu(Scanner sin) {
if (this.vehicles == null) {
System.out.println("You must add a vehicle first");
return;
}
this.listVehicles();
System.out.print("Plate of the vehicle: ");
String plate = sin.nextLine();
System.out.print("Km to travel: ");
int km = Integer.parseInt(sin.nextLine());
Vehicle vehicle = this.getVehicleByPlate(plate);
vehicle.trip(km);
}
public void printVehicleWithMostKm() {
if (this.vehicles == null) {
System.out.println("You must add a vehicle first");
return;
}
Vehicle max = this.vehicles[0];
for (Vehicle vehicle : this.vehicles)
if (vehicle.totalDistance() > max.totalDistance())
max = vehicle;
System.out.println(max);
}
// Add Vehicle methods
public void addVehicleMenu(Scanner sin) {
System.out.print("Vehicle plate: ");
String plate = sin.nextLine();
System.out.print("Vehicle brand: ");
String brand = sin.nextLine();
System.out.print("Vehicle model: ");
String model = sin.nextLine();
System.out.print("Vehicle potency: ");
int potency = Integer.parseInt(sin.nextLine());
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());
switch (vehicleType) {
case 1 -> {
System.out.print("What's the motorcycle type? (SPORT/TOURING): ");
Motorcycle.MotorcycleType motorcycleType = Motorcycle.MotorcycleType.fromString(sin.nextLine());
addVehicle(new Motorcycle(plate, brand, model, potency, motorcycleType));
}
case 2, 3, 4, 5 -> {
System.out.print("Vehicle's board number: ");
int boardNumber = Integer.parseInt(sin.nextLine());
switch (vehicleType) {
case 2, 3 -> {
System.out.print("Car's trunk size: ");
int trunkSize = Integer.parseInt(sin.nextLine());
System.out.print("What's the engine type? (FUEL/ELECTRIC): ");
EngineType engineType = EngineType.fromString(sin.nextLine());
Car car = new Car(plate, brand, model, potency, boardNumber, trunkSize, engineType);
if (vehicleType == 2)
addVehicle(car);
else {
System.out.print("Taxi's license number: ");
int licenseNumber = Integer.parseInt(sin.nextLine());
addVehicle(new Taxi(car, licenseNumber));
}
}
case 4, 5 -> {
System.out.print("Vehicle's weight: ");
int weight = Integer.parseInt(sin.nextLine());
if (vehicleType == 4) {
System.out.print("Bus passenger limit: ");
int maxPassengers = Integer.parseInt(sin.nextLine());
System.out.print("What's the engine type? (FUEL/ELECTRIC): ");
EngineType engineType = EngineType.fromString(sin.nextLine());
addVehicle(new Bus(plate, brand, model, potency, boardNumber, weight, maxPassengers, engineType));
} else {
System.out.print("Truck weight limit: ");
int maxWeight = Integer.parseInt(sin.nextLine());
addVehicle(new Truck(plate, brand, model, potency, boardNumber, weight, maxWeight));
}
}
}
}
default -> throw new IllegalStateException("Unexpected value: " + vehicleType);
}
}
private void addVehicle(Vehicle vehicle) {
if (this.vehicles == null) {
this.vehicles = new Vehicle[1];
this.vehicles[0] = vehicle;
} else {
Vehicle[] newVehicles = new Vehicle[this.vehicles.length + 1];
System.arraycopy(this.vehicles, 0, newVehicles, 0, this.vehicles.length);
newVehicles[newVehicles.length - 1] = vehicle;
this.vehicles = newVehicles;
}
}
// Remove Vehicle methods
public void removeVehicleMenu(Scanner sin) {
this.listVehicles();
System.out.print("Plate of the vehicle to remove: ");
String plate = sin.nextLine();
Vehicle vehicle = this.getVehicleByPlate(plate);
removeVehicle(vehicle);
}
private void removeVehicle(Vehicle vehicle) {
Vehicle[] newVehicles = new Vehicle[this.vehicles.length - 1];
int index = 0;
for (Vehicle v : this.vehicles)
if (!v.getPlate().equals(vehicle.getPlate()))
newVehicles[index++] = v;
this.vehicles = newVehicles;
}
private int totalDistance() {
int total = 0;
for (Vehicle vehicle : this.vehicles)
total += vehicle.totalDistance();
return total;
}
@Override
public String toString() {
return String.format("Company %s%nNumber of vehicles: %d%nTotal distance: %d", this.name, this.vehicles.length, this.totalDistance());
}
}

View File

@ -0,0 +1,99 @@
package aula08.ex1.Vehicles;
import aula08.ex1.Enums.EngineType;
import aula08.ex1.Interfaces.IElectricVehicle;
import aula08.ex1.Interfaces.IFuelVehicle;
import java.util.Objects;
public class Bus extends Vehicle implements IFuelVehicle, IElectricVehicle {
private final int boardNumber;
private final int weight;
private final int maxPassengers;
private final EngineType engineType;
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);
if (weight <= 0)
throw new IllegalArgumentException("Weight must be positive.");
if (maxPassengers <= 0)
throw new IllegalArgumentException("Max passengers must be positive.");
this.boardNumber = boardNumber;
this.weight = weight;
this.maxPassengers = maxPassengers;
this.engineType = engineType;
}
public int getBoardNumber() {
return this.boardNumber;
}
public int getWeight() {
return this.weight;
}
public int getMaxPassengers() {
return this.maxPassengers;
}
public EngineType getEngineType() {
return this.engineType;
}
@Override
public String toString() {
return "Bus {" +
"\n\tplate='" + this.getPlate() + '\'' +
",\n\tbrand='" + this.getBrand() + '\'' +
",\n\tmodel='" + this.getModel() + '\'' +
",\n\tpotency=" + this.getPotency() +
",\n\tboardNumber=" + this.getBoardNumber() +
",\n\tweight=" + this.getWeight() +
",\n\tmaxPassengers=" + this.getMaxPassengers() +
",\n\tlastTripKm=" + this.lastTrip() +
",\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}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Bus bus = (Bus) o;
return this.getBoardNumber() == bus.getBoardNumber() && this.getWeight() == bus.getWeight() && this.getMaxPassengers() == bus.getMaxPassengers() && this.getEngineType().equals(bus.getEngineType());
}
@Override
public int hashCode() {
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.ELECTRIC)
return;
this.fuelLevel = level;
}
}

View File

@ -0,0 +1,97 @@
package aula08.ex1.Vehicles;
import aula08.ex1.Enums.EngineType;
import aula08.ex1.Interfaces.IElectricVehicle;
import aula08.ex1.Interfaces.IFuelVehicle;
import java.util.Objects;
public class Car extends Vehicle implements IElectricVehicle, IFuelVehicle {
private final int boardNumber;
private final int trunkSize;
private final EngineType engineType;
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);
if (trunkSize <= 0)
throw new IllegalArgumentException("Trunk size must be positive.");
this.boardNumber = boardNumber;
this.trunkSize = trunkSize;
this.engineType = engineType;
this.fuelLevel = 0;
this.battery = 0;
}
public Car(Car car) {
this(car.getPlate(), car.getBrand(), car.getModel(), car.getPotency(), car.getBoardNumber(), car.getTrunkSize(), car.getEngineType());
}
public int getBoardNumber() {
return this.boardNumber;
}
public int getTrunkSize() {
return this.trunkSize;
}
public EngineType getEngineType() {
return this.engineType;
}
@Override
public String toString() {
return "Car {" +
"\n\tplate='" + this.getPlate() + '\'' +
",\n\tbrand='" + this.getBrand() + '\'' +
",\n\tmodel='" + this.getModel() + '\'' +
",\n\tpotency=" + this.getPotency() +
",\n\tboardNumber=" + this.getBoardNumber() +
",\n\ttrunkSize=" + this.getTrunkSize() +
",\n\tlastTripKm=" + this.lastTrip() +
",\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}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Car car = (Car) o;
return this.getBoardNumber() == car.getBoardNumber() && this.getTrunkSize() == car.getTrunkSize() && this.getEngineType().equals(car.getEngineType());
}
@Override
public int hashCode() {
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.ELECTRIC)
return;
this.fuelLevel = level;
}
}

View File

@ -0,0 +1,82 @@
package aula08.ex1.Vehicles;
import aula08.ex1.Interfaces.IFuelVehicle;
import java.util.Objects;
public class Motorcycle extends Vehicle implements IFuelVehicle {
private final MotorcycleType type;
private int fuelLevel;
public Motorcycle(String plate, String brand, String model, int potency, MotorcycleType type) {
super(plate, brand, model, potency);
this.type = type;
this.fuelLevel = 0;
}
public Motorcycle(Motorcycle motorcycle) {
this(motorcycle.getPlate(), motorcycle.getBrand(), motorcycle.getModel(), motorcycle.getPotency(), motorcycle.getType());
}
public MotorcycleType getType() {
return this.type;
}
@Override
public String toString() {
return "Motorcycle {" +
"\n\tplate='" + this.getPlate() + '\'' +
",\n\tbrand='" + this.getBrand() + '\'' +
",\n\tmodel='" + this.getModel() + '\'' +
",\n\tpotency=" + this.getPotency() +
",\n\ttype=" + this.getType().toString() +
",\n\tlastTripKm=" + this.lastTrip() +
",\n\tkm=" + this.totalDistance() +
",\n\tfuelLevel=" + this.fuelLevel() +
"\n}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Motorcycle that = (Motorcycle) o;
return this.getType() == that.getType();
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), this.getType());
}
@Override
public int fuelLevel() {
return this.fuelLevel;
}
@Override
public void fillTank(int level) {
this.fuelLevel = level;
}
enum MotorcycleType {
SPORT, TOURING;
public String toString() {
return switch (this) {
case SPORT -> "Sport";
case TOURING -> "Touring";
};
}
public static MotorcycleType fromString(String s) {
return switch (s) {
case "SPORT", "Sport", "sport" -> SPORT;
case "TOURING", "Touring", "touring" -> TOURING;
default -> throw new IllegalArgumentException("Invalid MotorcycleType: " + s);
};
}
}
}

View File

@ -0,0 +1,57 @@
package aula08.ex1.Vehicles;
import aula08.ex1.Enums.EngineType;
import java.util.Objects;
public class Taxi extends Car {
private final int licenseNumber;
public Taxi(Car car, int licenseNumber) {
super(car);
this.licenseNumber = licenseNumber;
}
public Taxi(Taxi taxi) {
this(taxi.getCar(), taxi.getLicenseNumber());
}
public Car getCar() {
return new Car(this.getPlate(), this.getBrand(), this.getModel(), this.getPotency(), this.getBoardNumber(), this.getTrunkSize(), this.getEngineType());
}
public int getLicenseNumber() {
return this.licenseNumber;
}
@Override
public String toString() {
return "Taxi {" +
"\n\tlicenseNumber=" + this.licenseNumber +
",\n\tplate='" + this.getPlate() + '\'' +
",\n\tbrand='" + this.getBrand() + '\'' +
",\n\tmodel='" + this.getModel() + '\'' +
",\n\tpotency=" + this.getPotency() +
",\n\tboardNumber=" + this.getBoardNumber() +
",\n\ttrunkSize=" + this.getTrunkSize() +
",\n\tlastTripKm=" + this.lastTrip() +
",\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}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Taxi taxi = (Taxi) o;
return this.getLicenseNumber() == taxi.getLicenseNumber();
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getLicenseNumber());
}
}

View File

@ -0,0 +1,77 @@
package aula08.ex1.Vehicles;
import aula08.ex1.Interfaces.IFuelVehicle;
import java.util.Objects;
public class Truck extends Vehicle implements IFuelVehicle {
private final int boardNumber;
private final int weight;
private final int maxWeight;
private int fuelLevel;
public Truck(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxWeight) {
super(plate, brand, model, potency);
if (weight <= 0)
throw new IllegalArgumentException("Weight must be positive.");
if (maxWeight <= 0)
throw new IllegalArgumentException("Max weight must be positive.");
this.boardNumber = boardNumber;
this.weight = weight;
this.maxWeight = maxWeight;
this.fuelLevel = 0;
}
public int getBoardNumber() {
return this.boardNumber;
}
public int getWeight() {
return this.weight;
}
public int getMaxWeight() {
return this.maxWeight;
}
@Override
public String toString() {
return "Truck {" +
"\n\tplate='" + this.getPlate() + '\'' +
",\n\tbrand='" + this.getBrand() + '\'' +
",\n\tmodel='" + this.getModel() + '\'' +
",\n\tpotency=" + this.getPotency() +
",\n\tboardNumber=" + this.getBoardNumber() +
",\n\tweight=" + this.getWeight() +
",\n\tmaxWeight=" + this.getMaxWeight() +
",\n\tlastTripKm=" + this.lastTrip() +
",\n\tkm=" + this.totalDistance() +
",\n\tfuelLevel=" + this.fuelLevel() +
"\n}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Truck truck = (Truck) o;
return this.getBoardNumber() == truck.getBoardNumber() && this.getWeight() == truck.getWeight() && this.getMaxWeight() == truck.getMaxWeight();
}
@Override
public int hashCode() {
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;
}
}

View File

@ -0,0 +1,74 @@
package aula08.ex1.Vehicles;
import aula08.ex1.Interfaces.IKmTravelled;
import utils.Validations;
import java.util.Objects;
public abstract class Vehicle implements IKmTravelled {
private final String plate;
private final String brand;
private final String model;
private final int potency;
private int lastTripKm;
private int km;
public Vehicle(String plate, String brand, String model, int potency) {
if (!Validations.validateVehiclePlate(plate))
throw new IllegalArgumentException("Invalid plate!");
if (potency <= 0)
throw new IllegalArgumentException("Potency must be positive");
this.plate = plate;
this.brand = brand;
this.model = model;
this.potency = potency;
this.lastTripKm = 0;
this.km = 0;
}
public String getPlate() {
return this.plate;
}
public String getBrand() {
return this.brand;
}
public String getModel() {
return this.model;
}
public int getPotency() {
return this.potency;
}
@Override
public void trip(int km) {
this.lastTripKm = km;
this.km += km;
}
@Override
public int lastTrip() {
return lastTripKm;
}
@Override
public int totalDistance() {
return km;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vehicle vehicle = (Vehicle) o;
return this.getPotency() == vehicle.getPotency() && this.lastTripKm == vehicle.lastTripKm && this.km == vehicle.km && this.getPlate().equals(vehicle.getPlate()) && this.getBrand().equals(vehicle.getBrand()) && this.getModel().equals(vehicle.getModel());
}
@Override
public int hashCode() {
return Objects.hash(this.getPlate(), this.getBrand(), this.getModel(), this.getPotency(), this.lastTripKm, this.km);
}
}

View File

@ -0,0 +1,19 @@
package aula08.ex3;
public class ShoppingCartTester {
public static void main(String[] args) {
/*Produto p1 = new ProdutoGenerico("Camisolas", 10, 3);
Produto p2 = new ProdutoGenerico("Calças", 30, 1);
Produto p3 = new ProdutoComDesconto("Sapatilhas", 50, 2, 50);
Produto p4 = new ProdutoComDesconto("Casacos", 100, 1, 10);
CarrinhoDeCompras carrinho = new CarrinhoDeCompras();
carrinho.adicionarProduto(p1, 1);
carrinho.adicionarProduto(p2, 5);
carrinho.adicionarProduto(p3, 2);
carrinho.adicionarProduto(p4, 1);
carrinho.listarProdutos();
System.out.printf("Preço total da compra %.2f\n", carrinho.calcularTotal());*/
}
}

View File

@ -0,0 +1,9 @@
package utils;
import java.util.regex.Pattern;
public class Validations {
public static boolean validateVehiclePlate(String plate) {
return Pattern.matches("^[A-Z]{2}-\\d{2}-\\d{2}|\\d{2}-[A-Z]{2}-\\d{2}|\\d{2}-\\d{2}-[A-Z]{2}|[A-Z]{2}-\\d{2}-[A-Z]{2}|[A-Z]{2}-[A-Z]{2}-\\d{2}|\\d{2}-[A-Z]{2}-[A-Z]{2}$", plate);
}
}