[POO] aula08 ex2,3; aa1 | [C2] material update #55
[POO] AA1 added [POO] aula08 made MotorcycleType public [POO] aula08 ex3 added [POO] README update [POO] aula08 ex2 added [POO] aula08 README update [C2] material update
This commit is contained in:
commit
8610bb8397
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -12,7 +12,7 @@
|
|||
---
|
||||
## Índice
|
||||
| Aula nº | Tópicos |
|
||||
|-----------------------------------------------------------------------------------------|----------------------|
|
||||
|-----------------------------------------------------------------------------------------|-------------------------|
|
||||
| [01](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula01) | Introduction, Basics |
|
||||
| [02](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula02) | Flux Control |
|
||||
| [03](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula03) | Classes |
|
||||
|
@ -20,6 +20,7 @@
|
|||
| [05](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula05) | Classes |
|
||||
| [06](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula06) | Inheritance |
|
||||
| [07](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula07) | Polymorphism |
|
||||
| [08](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula08) | Interfaces, Collections |
|
||||
|
||||
---
|
||||
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
## Índice
|
||||
| Aula nº | Tópicos |
|
||||
|-----------------------------------------------------------------------------------------|----------------------|
|
||||
|-----------------------------------------------------------------------------------------|-------------------------|
|
||||
| [01](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula01) | Introduction, Basics |
|
||||
| [02](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula02) | Flux Control |
|
||||
| [03](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula03) | Classes |
|
||||
|
@ -12,6 +12,7 @@
|
|||
| [05](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula05) | Classes |
|
||||
| [06](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula06) | Inheritance |
|
||||
| [07](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula07) | Polymorphism |
|
||||
| [08](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula08) | Interfaces, Collections |
|
||||
|
||||
---
|
||||
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
### Tópico principal da aula: Introduction, Basics
|
||||
|
||||
* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2021-aula01.pdf)
|
||||
* [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_01_Introdução.pdf)
|
||||
|
||||
---
|
||||
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
### Tópico principal da aula: Flux Control
|
||||
|
||||
* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2021-aula02.pdf)
|
||||
* [Slides 1](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_02_ControloFluxo.pdf)
|
||||
* [Slides 2](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_02_Modularidade_MetodosEstaticos.pdf)
|
||||
|
||||
### Exercise List
|
||||
| Exercise Number | File Name | Exercise Number | File Name |
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
### Tópico principal da aula: Classes
|
||||
|
||||
* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2021-aula03.pdf)
|
||||
* [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_03_Classes.pdf)
|
||||
|
||||
### Exercise List
|
||||
| Exercise Number | File Name |
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
### Tópico principal da aula: Classes
|
||||
|
||||
* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2021-aula04.pdf)
|
||||
* [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_03_Classes.pdf)
|
||||
|
||||
### Exercise List
|
||||
| Exercise Number | File Name |
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
### Tópico principal da aula: Classes
|
||||
|
||||
* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2022-aula05.pdf)
|
||||
* [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_03_Classes.pdf)
|
||||
|
||||
### Exercise List
|
||||
| Exercise Number | File Name |
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
### Tópico principal da aula: Inheritance
|
||||
|
||||
* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2022-aula06.pdf)
|
||||
* [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_04_Herança.pdf)
|
||||
|
||||
### Exercise List
|
||||
| Exercise Number | Files Name |
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
### Tópico principal da aula: Polymorphism
|
||||
|
||||
* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2022-aula07.pdf)
|
||||
* [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_05_Polimorfismo.pdf)
|
||||
|
||||
### Exercise List
|
||||
| Exercise Number | Files Name |
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
# Programação Orientada a Objetos
|
||||
## Aula 08
|
||||
### Tópico principal da aula: Interfaces, Collections
|
||||
|
||||
* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2022-aula08.pdf)
|
||||
|
||||
### File List per Exercise
|
||||
|
||||
#### Ex1
|
||||
* [Main.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Main.java)
|
||||
* [VehicleComp.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/VehicleComp.java)
|
||||
* [Vehicles](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles)
|
||||
* [Vehicle.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java)
|
||||
* [Motorcycle.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Motorcycle.java)
|
||||
* [Car.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Car.java)
|
||||
* [Taxi.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Taxi.java)
|
||||
* [Bus.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Bus.java)
|
||||
* [Truck.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Truck.java)
|
||||
* [Interfaces](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Interfaces)
|
||||
* [IKmTravelled.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Interfaces/IKmTravelled.java)
|
||||
* [IFuelVehicle.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Interfaces/IFuelVehicle.java)
|
||||
* [IElectricVehicle.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Interfaces/IElectricVehicle.java)
|
||||
* [Enums](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums)
|
||||
* [EngineType.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/EngineType.java)
|
||||
|
||||
#### Ex2
|
||||
|
||||
* [Main.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Main.java)
|
||||
* [Menu.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Menu.java)
|
||||
* [Aliments](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments)
|
||||
* [Aliment.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Aliment.java)
|
||||
* [Cereal.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Cereal.java)
|
||||
* [Fish.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Fish.java)
|
||||
* [Meat.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Meat.java)
|
||||
* [Vegetable.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Vegetable.java)
|
||||
* [Dishes](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Dishes)
|
||||
* [Dish.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Dishes/Dish.java)
|
||||
* [VeganDish.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Dishes/VeganDish.java)
|
||||
* [DietDish.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Dishes/DietDish.java)
|
||||
* [Enums](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums)
|
||||
* [AlimentOrigin.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/AlimentOrigin.java)
|
||||
* [AlimentType.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/AlimentType.java)
|
||||
* [DishType.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/DishType.java)
|
||||
* [FishState.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/FishState.java)
|
||||
* [MeatType.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/MeatType.java)
|
||||
|
||||
#### Ex3
|
||||
|
||||
* [ShoppingCarTester.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/ShoppingCarTester.java)
|
||||
* [Purchase.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Purchase.java)
|
||||
* [Product.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Product.java)
|
||||
* [PurchaseWithDiscount.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/PurchaseWithDiscount.java)
|
||||
* [Interfaces](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Interfaces)
|
||||
* [IPurchase.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IPurchase.java)
|
||||
* [IProduct.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IProduct.java)
|
||||
|
||||
---
|
||||
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)
|
|
@ -0,0 +1,20 @@
|
|||
# Programação Orientada a Objetos
|
||||
## Aula 08 - Exercise 1
|
||||
|
||||
### File list
|
||||
|
||||
* [Main.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Main.java)
|
||||
* [VehicleComp.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/VehicleComp.java)
|
||||
* [Vehicles](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles)
|
||||
* [Vehicle.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java)
|
||||
* [Motorcycle.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Motorcycle.java)
|
||||
* [Car.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Car.java)
|
||||
* [Taxi.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Taxi.java)
|
||||
* [Bus.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Bus.java)
|
||||
* [Truck.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Truck.java)
|
||||
* [Interfaces](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Interfaces)
|
||||
* [IKmTravelled.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Interfaces/IKmTravelled.java)
|
||||
* [IFuelVehicle.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Interfaces/IFuelVehicle.java)
|
||||
* [IElectricVehicle.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Interfaces/IElectricVehicle.java)
|
||||
* [Enums](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums)
|
||||
* [EngineType.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/EngineType.java)
|
|
@ -61,7 +61,7 @@ public class Motorcycle extends Vehicle implements IFuelVehicle {
|
|||
this.fuelLevel = level;
|
||||
}
|
||||
|
||||
enum MotorcycleType {
|
||||
public enum MotorcycleType {
|
||||
SPORT, TOURING;
|
||||
|
||||
public String toString() {
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package aula08.ex2.Aliments;
|
||||
|
||||
import aula08.ex2.Enums.AlimentOrigin;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class Aliment {
|
||||
double proteins;
|
||||
double calories;
|
||||
double weight;
|
||||
final AlimentOrigin alimentOrigin;
|
||||
|
||||
public Aliment(double proteins, double calories, double weight, AlimentOrigin alimentOrigin) {
|
||||
setProteins(proteins);
|
||||
setCalories(calories);
|
||||
setCalories(weight);
|
||||
this.alimentOrigin = alimentOrigin;
|
||||
}
|
||||
|
||||
public double getProteins() {
|
||||
return this.proteins;
|
||||
};
|
||||
|
||||
public void setProteins(double proteins) {
|
||||
if (proteins <= 0)
|
||||
throw new IllegalArgumentException("Proteins must be positive");
|
||||
this.proteins = proteins;
|
||||
}
|
||||
|
||||
public double getCalories() {
|
||||
return this.calories;
|
||||
}
|
||||
|
||||
public void setCalories(double calories) {
|
||||
if (calories <= 0)
|
||||
throw new IllegalArgumentException("Calories must be positive");
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
public double getWeight() {
|
||||
return this.weight;
|
||||
}
|
||||
|
||||
public void setWeight(double weight) {
|
||||
if (weight <= 0)
|
||||
throw new IllegalArgumentException("Weight must be positive");
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public AlimentOrigin getAlimentOrigin() {
|
||||
return this.alimentOrigin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Aliment aliment = (Aliment) o;
|
||||
return Double.compare(aliment.getProteins(), getProteins()) == 0 && Double.compare(aliment.getCalories(), getCalories()) == 0 && Double.compare(aliment.getWeight(), getWeight()) == 0 && getAlimentOrigin() == aliment.getAlimentOrigin();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getProteins(), getCalories(), getWeight(), getAlimentOrigin());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package aula08.ex2.Aliments;
|
||||
|
||||
import aula08.ex2.Enums.AlimentOrigin;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Cereal extends Aliment {
|
||||
private final String name;
|
||||
|
||||
public Cereal(String name, double proteins, double calories, double weight) {
|
||||
super(proteins, calories, weight, AlimentOrigin.VEGAN);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Cereal %s, Proteins: %.1f, Calories: %.1f, Weight: %.1f", this.getName(), this.getProteins(), this.getCalories(), this.getWeight());
|
||||
}
|
||||
|
||||
@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;
|
||||
Cereal cereal = (Cereal) o;
|
||||
return getName().equals(cereal.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), getName());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package aula08.ex2.Aliments;
|
||||
|
||||
import aula08.ex2.Enums.AlimentOrigin;
|
||||
import aula08.ex2.Enums.FishState;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Fish extends Aliment {
|
||||
private FishState fishState;
|
||||
|
||||
public Fish(FishState fishState, double proteins, double calories, double weight) {
|
||||
super(proteins, calories, weight, AlimentOrigin.ANIMAL);
|
||||
this.fishState = fishState;
|
||||
}
|
||||
|
||||
public FishState getFishState() {
|
||||
return this.fishState;
|
||||
}
|
||||
|
||||
public void setFishState(FishState fishState) {
|
||||
this.fishState = fishState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Fish %s, Proteins: %.1f, Calories: %.1f, Weight: %.1f", this.getFishState().toString(), this.getProteins(), this.getCalories(), this.getWeight());
|
||||
}
|
||||
|
||||
@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;
|
||||
Fish fish = (Fish) o;
|
||||
return getFishState() == fish.getFishState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), getFishState());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package aula08.ex2.Aliments;
|
||||
|
||||
import aula08.ex2.Enums.AlimentOrigin;
|
||||
import aula08.ex2.Enums.MeatType;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Meat extends Aliment {
|
||||
private MeatType meatType;
|
||||
|
||||
public Meat(MeatType meatType, double proteins, double calories, double weight) {
|
||||
super(proteins, calories, weight, AlimentOrigin.ANIMAL);
|
||||
setMeatType(meatType);
|
||||
}
|
||||
|
||||
public MeatType getMeatType() {
|
||||
return this.meatType;
|
||||
}
|
||||
|
||||
public void setMeatType(MeatType meatType) {
|
||||
this.meatType = meatType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Meat %s, Proteins: %.1f, Calories: %.1f, Weight: %.1f", this.getMeatType().toString(), this.getProteins(), this.getCalories(), this.getWeight());
|
||||
}
|
||||
|
||||
@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;
|
||||
Meat meat = (Meat) o;
|
||||
return getMeatType() == meat.getMeatType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), getMeatType());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package aula08.ex2.Aliments;
|
||||
|
||||
import aula08.ex2.Enums.AlimentOrigin;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Vegetable extends Aliment {
|
||||
private final String name;
|
||||
|
||||
public Vegetable(String name, double proteins, double calories, double weight) {
|
||||
super(proteins, calories, weight, AlimentOrigin.VEGAN);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Vegetable %s, Proteins: %.1f, Calories: %.1f, Weight: %.1f", this.getName(), this.getProteins(), this.getCalories(), this.getWeight());
|
||||
}
|
||||
|
||||
@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;
|
||||
Vegetable vegetable = (Vegetable) o;
|
||||
return getName().equals(vegetable.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), getName());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package aula08.ex2.Dishes;
|
||||
|
||||
import aula08.ex2.Aliments.Aliment;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class DietDish extends Dish {
|
||||
private final double maxCalories;
|
||||
|
||||
public DietDish(String name, double maxCalories) {
|
||||
super(name);
|
||||
this.maxCalories = maxCalories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAliment(Aliment aliment) {
|
||||
if (aliment.getCalories() > (maxCalories - this.calculateCalories()))
|
||||
throw new IllegalArgumentException("Aliment is over calories limit.");
|
||||
super.addAliment(aliment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Dish '%s', using %d aliments - Diet (%.1f calories)", this.getName(), this.getAlimentCount(), this.calculateCalories());
|
||||
}
|
||||
|
||||
@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;
|
||||
DietDish dietDish = (DietDish) o;
|
||||
return Double.compare(dietDish.maxCalories, maxCalories) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), maxCalories);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package aula08.ex2.Dishes;
|
||||
|
||||
import aula08.ex2.Aliments.Aliment;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Dish {
|
||||
private final String name;
|
||||
private final LinkedList<Aliment> aliments;
|
||||
|
||||
public Dish(String name) {
|
||||
this.name = name;
|
||||
this.aliments = new LinkedList<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public LinkedList<Aliment> getAliments() {
|
||||
return this.aliments;
|
||||
}
|
||||
|
||||
public void addAliment(Aliment aliment) {
|
||||
this.aliments.add(aliment);
|
||||
}
|
||||
|
||||
public int getAlimentCount() {
|
||||
return this.aliments.size();
|
||||
}
|
||||
|
||||
public double calculateCalories() {
|
||||
double total = 0;
|
||||
for (Aliment aliment : this.aliments)
|
||||
total += aliment.getCalories();
|
||||
return total;
|
||||
}
|
||||
|
||||
public double calculateProteins() {
|
||||
double total = 0;
|
||||
for (Aliment aliment : this.aliments)
|
||||
total += aliment.getProteins();
|
||||
return total;
|
||||
}
|
||||
|
||||
public double calculateWeight() {
|
||||
double total = 0;
|
||||
for (Aliment aliment : this.aliments)
|
||||
total += aliment.getWeight();
|
||||
return total;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Dish '%s', using %d aliments", this.getName(), this.getAlimentCount());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Dish dish = (Dish) o;
|
||||
return getName().equals(dish.getName()) && getAliments().equals(dish.getAliments());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getName(), getAliments());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package aula08.ex2.Dishes;
|
||||
|
||||
import aula08.ex2.Aliments.Aliment;
|
||||
import aula08.ex2.Enums.AlimentOrigin;
|
||||
|
||||
public class VeganDish extends Dish {
|
||||
public VeganDish(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAliment(Aliment aliment) {
|
||||
if (aliment.getAlimentOrigin() == AlimentOrigin.ANIMAL)
|
||||
throw new IllegalArgumentException("Aliment must be vegan.");
|
||||
super.addAliment(aliment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Dish '%s', using %d aliments - Vegan dish", this.getName(), this.getAlimentCount());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package aula08.ex2.Enums;
|
||||
|
||||
public enum AlimentOrigin {
|
||||
ANIMAL, VEGAN;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return switch (this) {
|
||||
case ANIMAL -> "ANIMAL";
|
||||
case VEGAN -> "VEGAN";
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package aula08.ex2.Enums;
|
||||
|
||||
public enum AlimentType {
|
||||
MEAT, FISH, CEREAL, VEGETABLE;
|
||||
|
||||
public static AlimentType fromString(String string) {
|
||||
return switch (string) {
|
||||
case "MEAT", "Meat", "meat" -> MEAT;
|
||||
case "FISH", "Fish", "fish" -> FISH;
|
||||
case "CEREAL", "Cereal", "cereal" -> CEREAL;
|
||||
case "VEGETABLE", "Vegetable", "vegetable" -> VEGETABLE;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return switch (this) {
|
||||
case MEAT -> "Meat";
|
||||
case FISH -> "Fish";
|
||||
case CEREAL -> "Cereal";
|
||||
case VEGETABLE -> "Vegetable";
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package aula08.ex2.Enums;
|
||||
|
||||
public enum DishType {
|
||||
NORMAL, VEGAN, DIET;
|
||||
|
||||
public static DishType fromString(String string) {
|
||||
return switch (string) {
|
||||
case "NORMAL", "Normal", "normal" -> NORMAL;
|
||||
case "VEGAN", "Vegan", "vegan" -> VEGAN;
|
||||
case "DIET", "Diet", "diet" -> DIET;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return switch (this) {
|
||||
case NORMAL -> "NORMAL";
|
||||
case VEGAN -> "VEGAN";
|
||||
case DIET -> "DIET";
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package aula08.ex2.Enums;
|
||||
|
||||
public enum FishState {
|
||||
FRESH, FROZEN;
|
||||
|
||||
public static FishState fromString(String string) {
|
||||
return switch (string) {
|
||||
case "FRESH", "Fresh", "fresh" -> FRESH;
|
||||
case "FROZEN", "Frozen", "frozen" -> FROZEN;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return switch (this) {
|
||||
case FRESH -> "FRESH";
|
||||
case FROZEN -> "FROZEN";
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package aula08.ex2.Enums;
|
||||
|
||||
public enum MeatType {
|
||||
COW, PORK, TURKEY, CHICKEN;
|
||||
|
||||
public static MeatType fromString(String string) {
|
||||
return switch (string) {
|
||||
case "COW", "Cow", "cow" -> COW;
|
||||
case "PORK", "Pork", "pork" -> PORK;
|
||||
case "TURKEY", "Turkey", "turkey" -> TURKEY;
|
||||
case "CHICKEN", "Chicken", "chicken" -> CHICKEN;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return switch (this) {
|
||||
case COW -> "COW";
|
||||
case PORK -> "PORK";
|
||||
case TURKEY -> "TURKEY";
|
||||
case CHICKEN -> "CHICKEN";
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package aula08.ex2;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
private static final Scanner sin = new Scanner(System.in);
|
||||
private static LinkedList<Menu> Menus;
|
||||
|
||||
public static void main(String[] args) {
|
||||
while (true) {
|
||||
System.out.print("Choose an option:\n1 - Create menu\n2 - List menus\n0 - Exit\n\n>> ");
|
||||
int option = Integer.parseInt(sin.nextLine());
|
||||
switch (option) {
|
||||
case 0 -> {
|
||||
sin.close();
|
||||
System.exit(0);
|
||||
}
|
||||
case 1 -> Menus.add(new Menu(sin));
|
||||
case 2 -> {
|
||||
for (Menu menu : Menus)
|
||||
System.out.println(menu);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
package aula08.ex2;
|
||||
|
||||
import aula08.ex2.Aliments.*;
|
||||
import aula08.ex2.Dishes.*;
|
||||
import aula08.ex2.Enums.*;
|
||||
import utils.Enums.Weekday;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Menu {
|
||||
private final Scanner sin;
|
||||
private final LinkedHashMap<Weekday, Dish> menu;
|
||||
|
||||
public Menu(Scanner sin, LinkedHashMap<Weekday, Dish> menu) {
|
||||
this.sin = sin;
|
||||
this.menu = menu;
|
||||
while (true) {
|
||||
System.out.print("Choose an option:\n1 - Add dish\n2 - Remove dish\n3 - Print menu\n0 - Exit\n\n>> ");
|
||||
int option = Integer.parseInt(sin.nextLine());
|
||||
switch (option) {
|
||||
case 0 -> {
|
||||
return;
|
||||
}
|
||||
case 1 -> {
|
||||
System.out.print("What weekday do you want to add a dish to: ");
|
||||
Weekday weekday = Weekday.fromString(sin.nextLine());
|
||||
if (this.menu.containsKey(weekday))
|
||||
System.out.print("That weekday already has a dish assigned to it.");
|
||||
else
|
||||
this.menu.put(weekday, this.dishBuilder());
|
||||
}
|
||||
case 2 -> {
|
||||
System.out.print("What weekday dish do you want to remove: ");
|
||||
Weekday weekday = Weekday.fromString(sin.nextLine());
|
||||
try {
|
||||
this.menu.remove(weekday);
|
||||
} catch (Exception ignored) {
|
||||
System.out.println("That weekday has no dish assigned to.");
|
||||
}
|
||||
}
|
||||
case 3 -> System.out.println(this);
|
||||
default -> System.out.println("Invalid option.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Menu(Scanner sin) {
|
||||
this(sin, new LinkedHashMap<>());
|
||||
}
|
||||
|
||||
public Menu() {
|
||||
this(new Scanner(System.in), new LinkedHashMap<>());
|
||||
}
|
||||
|
||||
public Dish dishBuilder() {
|
||||
System.out.print("Dish name: ");
|
||||
String name = sin.nextLine();
|
||||
Dish dish;
|
||||
while (true) {
|
||||
System.out.print("Dish type? (NORMAL/VEGAN/DIET): ");
|
||||
DishType dishType = DishType.fromString(sin.nextLine());
|
||||
switch (dishType) {
|
||||
case NORMAL -> dish = new Dish(name);
|
||||
case VEGAN -> dish = new VeganDish(name);
|
||||
case DIET -> {
|
||||
System.out.print("Calories limit: ");
|
||||
double caloriesLimit = Double.parseDouble(sin.nextLine());
|
||||
dish = new DietDish(name, caloriesLimit);
|
||||
}
|
||||
default -> {
|
||||
System.out.print("Invalid option.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
while (true) {
|
||||
System.out.print("Choose an option:\n1 - Add aliment\n2 - Print dish\n0 - Finish dish\n\n>> ");
|
||||
int option = Integer.parseInt(sin.nextLine());
|
||||
switch (option) {
|
||||
case 0 -> {
|
||||
return dish;
|
||||
}
|
||||
case 1 -> {
|
||||
Aliment aliment = null;
|
||||
System.out.print("Aliment calories: ");
|
||||
double calories = Double.parseDouble(sin.nextLine());
|
||||
System.out.print("Aliment proteins: ");
|
||||
double proteins = Double.parseDouble(sin.nextLine());
|
||||
System.out.print("Aliment weight: ");
|
||||
double weight = Double.parseDouble(sin.nextLine());
|
||||
System.out.print("What type of aliment are u adding? (MEAT/FISH/CEREAL/VEGETABLE): ");
|
||||
AlimentType alimentType = AlimentType.fromString(sin.nextLine());
|
||||
switch (alimentType) {
|
||||
case MEAT -> {
|
||||
System.out.print(alimentType + " type? (COW/PORK/TURKEY/CHICKEN): ");
|
||||
MeatType meatType = MeatType.fromString(sin.nextLine());
|
||||
aliment = new Meat(meatType, proteins, calories, weight);
|
||||
}
|
||||
case FISH -> {
|
||||
System.out.print(alimentType + " state? (FRESH/FROZEN): ");
|
||||
FishState fishState = FishState.fromString(sin.nextLine());
|
||||
aliment = new Fish(fishState, proteins, calories, weight);
|
||||
}
|
||||
case CEREAL, VEGETABLE -> {
|
||||
System.out.print(alimentType + " name: ");
|
||||
String alimentName = sin.nextLine();
|
||||
aliment = alimentType == AlimentType.CEREAL ? new Cereal(alimentName, proteins, calories, weight) : new Vegetable(alimentName, proteins, calories, weight);
|
||||
}
|
||||
}
|
||||
dish.addAliment(aliment);
|
||||
}
|
||||
case 2 -> {
|
||||
System.out.println(dish);
|
||||
for (Aliment aliment : dish.getAliments())
|
||||
System.out.println(aliment);
|
||||
}
|
||||
default -> System.out.println("Invalid option.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder string = new StringBuilder("Menu");
|
||||
for (Weekday weekDay : this.menu.keySet())
|
||||
string.append(String.format("%s : %s", weekDay, menu.get(weekDay)));
|
||||
return string.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
# Programação Orientada a Objetos
|
||||
## Aula 08 - Exercise 2
|
||||
|
||||
### File list
|
||||
|
||||
* [Main.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Main.java)
|
||||
* [Menu.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Menu.java)
|
||||
* [Aliments](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments)
|
||||
* [Aliment.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Aliment.java)
|
||||
* [Cereal.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Cereal.java)
|
||||
* [Fish.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Fish.java)
|
||||
* [Meat.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Meat.java)
|
||||
* [Vegetable.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Aliments/Vegetable.java)
|
||||
* [Dishes](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Dishes)
|
||||
* [Dish.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Dishes/Dish.java)
|
||||
* [VeganDish.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Dishes/VeganDish.java)
|
||||
* [DietDish.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex2/Dishes/DietDish.java)
|
||||
* [Enums](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums)
|
||||
* [AlimentOrigin.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/AlimentOrigin.java)
|
||||
* [AlimentType.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/AlimentType.java)
|
||||
* [DishType.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/DishType.java)
|
||||
* [FishState.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/FishState.java)
|
||||
* [MeatType.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex1/Enums/MeatType.java)
|
|
@ -0,0 +1,10 @@
|
|||
package aula08.ex3.Interfaces;
|
||||
|
||||
public interface IProduct {
|
||||
String getName();
|
||||
void setPrice(double price);
|
||||
double getPrice();
|
||||
int stock();
|
||||
void addStock(int amount);
|
||||
void removeStock(int amount);
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package aula08.ex3.Interfaces;
|
||||
|
||||
import aula08.ex3.Product;
|
||||
|
||||
public interface IPurchase {
|
||||
void addProduct(Product product, int amount);
|
||||
void listProducts();
|
||||
double getTotal();
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package aula08.ex3;
|
||||
|
||||
import aula08.ex3.Interfaces.IProduct;
|
||||
|
||||
public class Product implements IProduct {
|
||||
private final String name;
|
||||
private double price;
|
||||
private int stock = 0;
|
||||
|
||||
public Product(String name, double price, int stock) {
|
||||
this.name = name;
|
||||
this.setPrice(price);
|
||||
this.addStock(stock);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPrice() {
|
||||
return this.price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrice(double price) {
|
||||
if (price <= 0)
|
||||
throw new IllegalArgumentException("Price must be positive.");
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int stock() {
|
||||
return this.stock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addStock(int amount) {
|
||||
this.stock += amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeStock(int amount) {
|
||||
this.stock -= amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Product: %s\n\tStock: %d\n\tPrice: %.2f\n", this.getName(), this.stock(), this.getPrice());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package aula08.ex3;
|
||||
|
||||
public class ProductWithDiscount extends Product {
|
||||
private double discountPercentage;
|
||||
|
||||
public ProductWithDiscount(String name, double price, int stock, double discountPercentage) {
|
||||
super(name, price, stock);
|
||||
this.discountPercentage = discountPercentage;
|
||||
}
|
||||
|
||||
public double getDiscountPercentage() {
|
||||
return this.discountPercentage;
|
||||
}
|
||||
|
||||
public void setDiscountPercentage(double discountPercentage) {
|
||||
this.discountPercentage = discountPercentage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPrice() {
|
||||
return super.getPrice() * (1 - discountPercentage / 100);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package aula08.ex3;
|
||||
|
||||
import aula08.ex3.Interfaces.IPurchase;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class Purchase implements IPurchase {
|
||||
private final LinkedHashMap<Product, Integer> products = new LinkedHashMap<>();
|
||||
|
||||
@Override
|
||||
public void addProduct(Product product, int amount) {
|
||||
if (product.stock() < amount)
|
||||
amount = product.stock();
|
||||
product.removeStock(amount);
|
||||
if (products.containsKey(product)) {
|
||||
int currentAmount = products.get(product);
|
||||
products.replace(product, currentAmount+amount);
|
||||
} else
|
||||
products.put(product, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listProducts() {
|
||||
for (Product product : products.keySet()) {
|
||||
int productAmount = products.get(product);
|
||||
System.out.printf("Product: %s\n\tAmount: %d\n\tPrice: %.2f\n", product.getName(), productAmount, productAmount * product.getPrice());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getTotal() {
|
||||
double total = 0;
|
||||
for (Product product : products.keySet())
|
||||
total += product.getPrice() * products.get(product);
|
||||
return total;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
# Programação Orientada a Objetos
|
||||
## Aula 08 - Exercise 3
|
||||
|
||||
### File list
|
||||
|
||||
* [ShoppingCarTester.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/ShoppingCarTester.java)
|
||||
* [Purchase.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Purchase.java)
|
||||
* [Product.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Product.java)
|
||||
* [PurchaseWithDiscount.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/PurchaseWithDiscount.java)
|
||||
* [Interfaces](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Interfaces)
|
||||
* [IPurchase.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IPurchase.java)
|
||||
* [IProduct.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IProduct.java)
|
|
@ -2,18 +2,18 @@ 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);
|
||||
Product p1 = new Product("Camisolas", 10, 3);
|
||||
Product p2 = new Product("Calças", 30, 1);
|
||||
Product p3 = new ProductWithDiscount("Sapatilhas", 50, 2, 50);
|
||||
Product p4 = new ProductWithDiscount("Casacos", 100, 1, 10);
|
||||
|
||||
CarrinhoDeCompras carrinho = new CarrinhoDeCompras();
|
||||
carrinho.adicionarProduto(p1, 1);
|
||||
carrinho.adicionarProduto(p2, 5);
|
||||
carrinho.adicionarProduto(p3, 2);
|
||||
carrinho.adicionarProduto(p4, 1);
|
||||
Purchase compra = new Purchase();
|
||||
compra.addProduct(p2, 5);
|
||||
compra.addProduct(p3, 2);
|
||||
compra.addProduct(p4, 1);
|
||||
compra.addProduct(p1, 1);
|
||||
|
||||
carrinho.listarProdutos();
|
||||
System.out.printf("Preço total da compra %.2f\n", carrinho.calcularTotal());*/
|
||||
compra.listProducts();
|
||||
System.out.printf("Preço total da compra %.2f\n", compra.getTotal());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
package aval.aa1;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Animal {
|
||||
public static int currentId = 1;
|
||||
private final int id;
|
||||
private String name;
|
||||
private int weight;
|
||||
private int age;
|
||||
private String sponsor;
|
||||
|
||||
public Animal(String name, int weight, int age, String sponsor) {
|
||||
this.id = Animal.currentId++;
|
||||
this.name = name;
|
||||
this.weight = weight;
|
||||
this.age = age;
|
||||
this.sponsor = sponsor;
|
||||
}
|
||||
|
||||
public Animal(String name, int weight, int age) {
|
||||
this(name, weight, age, null);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setWeight(int weight) {
|
||||
if (weight <= 0)
|
||||
throw new IllegalArgumentException("Weight must be positive!");
|
||||
this.weight = weight;
|
||||
}
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
if (age <= 0)
|
||||
throw new IllegalArgumentException("Age must be positive!");
|
||||
this.age = age;
|
||||
}
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setSponsor(String sponsor) {
|
||||
this.sponsor = sponsor;
|
||||
}
|
||||
public String getSponsor() {
|
||||
return sponsor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("ID: %d\nName: %s\nAge: %d\nWeight: %d\nSponsor: %s", this.id, this.name, this.age, this.weight, this.sponsor == null ? "None" : sponsor);
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Animal animal = (Animal) o;
|
||||
return id == animal.id && weight == animal.weight && age == animal.age && Objects.equals(name, animal.name) && Objects.equals(sponsor, animal.sponsor);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, weight, age, sponsor);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package aval.aa1;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Bird extends Animal {
|
||||
private String habitat;
|
||||
|
||||
public Bird(String name, int weight, int age, String habitat, String sponsor) {
|
||||
super(name, weight, age, sponsor);
|
||||
this.habitat = habitat;
|
||||
}
|
||||
|
||||
public Bird(String name, int weight, int age, String habitat) {
|
||||
super(name, weight, age);
|
||||
this.habitat = habitat;
|
||||
}
|
||||
|
||||
public String gethabitat() {
|
||||
return this.habitat;
|
||||
}
|
||||
|
||||
public void sethabitat(String habitat) {
|
||||
this.habitat = habitat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Bird of habitat " + habitat + "\n" + super.toString();
|
||||
}
|
||||
|
||||
@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;
|
||||
Bird bird = (Bird) o;
|
||||
return Objects.equals(habitat, bird.habitat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), habitat);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package aval.aa1;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Dog extends Animal {
|
||||
private final String breed;
|
||||
|
||||
public Dog(String name, int weight, int age, String breed, String sponsor) {
|
||||
super(name, weight, age, sponsor);
|
||||
this.breed = breed;
|
||||
}
|
||||
|
||||
public Dog(String name, int weight, int age, String breed) {
|
||||
super(name, weight, age);
|
||||
this.breed = breed;
|
||||
}
|
||||
|
||||
public String getBreed() {
|
||||
return this.breed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Dog of breed " + this.breed + "\n" + super.toString();
|
||||
}
|
||||
|
||||
@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;
|
||||
Dog dog = (Dog) o;
|
||||
return Objects.equals(breed, dog.breed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), breed);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package aval.aa1;
|
||||
|
||||
public interface IPetShelter {
|
||||
public void addAnimal(Animal animal);
|
||||
public void removeAnimal(Animal animal);
|
||||
public Animal searchForAnimal(String name);
|
||||
public boolean sponsorAnimal(int animalId);
|
||||
public void listAllAnimals();
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package aval.aa1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
IPetShelter shelter = new PetShelter("Patudos");
|
||||
|
||||
int choice = 0;
|
||||
do {
|
||||
System.out.println("Choose an option:");
|
||||
System.out.println("1. Add animal");
|
||||
System.out.println("2. Remove animal");
|
||||
System.out.println("3. Search for animal");
|
||||
System.out.println("4. Sponsor an animal");
|
||||
System.out.println("5. View all animals");
|
||||
System.out.println("6. Exit");
|
||||
|
||||
choice = Integer.parseInt(scanner.nextLine());
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
// adicionar animal
|
||||
System.out.println("Adding an animal.");
|
||||
System.out.print("Animal type (Dog/Rabbit/Bird): ");
|
||||
String newAnimalType = scanner.nextLine();
|
||||
System.out.print("Animal name: ");
|
||||
String newAnimalName = scanner.nextLine();
|
||||
System.out.print("Animal age: ");
|
||||
int newAnimalAge = Integer.parseInt(scanner.nextLine());
|
||||
System.out.print("Animal weight: ");
|
||||
int newAnimalWeight = Integer.parseInt(scanner.nextLine());
|
||||
System.out.print("Animal sponsor: ");
|
||||
String newAnimalSponsor = scanner.nextLine();
|
||||
Animal newAnimal;
|
||||
switch (newAnimalType) {
|
||||
case "Dog":
|
||||
System.out.print("Dog breed: ");
|
||||
String breed = scanner.nextLine();
|
||||
newAnimal = new Dog(newAnimalName, newAnimalWeight, newAnimalAge, breed, newAnimalSponsor);
|
||||
shelter.addAnimal(newAnimal);
|
||||
break;
|
||||
case "Rabbit":
|
||||
System.out.print("Rabbit fur size (small/large): ");
|
||||
String fur = scanner.nextLine();
|
||||
newAnimal = new Rabbit(newAnimalName, newAnimalWeight, newAnimalAge, fur, newAnimalSponsor);
|
||||
shelter.addAnimal(newAnimal);
|
||||
break;
|
||||
case "Bird":
|
||||
System.out.print("Bird habitat: ");
|
||||
String habitat = scanner.nextLine();
|
||||
newAnimal = new Bird(newAnimalName, newAnimalWeight, newAnimalAge, habitat, newAnimalSponsor);
|
||||
shelter.addAnimal(newAnimal);
|
||||
break;
|
||||
default:
|
||||
System.out.println("Invalid animal.");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
// remover animal
|
||||
System.out.print("Animal name: ");
|
||||
String removedAnimalName = scanner.nextLine();
|
||||
Animal removedAnimal = shelter.searchForAnimal(removedAnimalName);
|
||||
if (removedAnimal != null) {
|
||||
shelter.removeAnimal(removedAnimal);
|
||||
System.out.println("Animal removed.");
|
||||
} else {
|
||||
System.out.println("Animal not found.");
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
// procurar animal
|
||||
System.out.print("Animal name:");
|
||||
String searchAnimalName = scanner.nextLine();
|
||||
System.out.println(shelter.searchForAnimal(searchAnimalName));
|
||||
break;
|
||||
case 4:
|
||||
// apadrinhar animal
|
||||
System.out.print("Id of animal you want to sponsor: ");
|
||||
int sponsorId = Integer.parseInt(scanner.nextLine());
|
||||
if (shelter.sponsorAnimal(sponsorId))
|
||||
System.out.println("Animal of id '" + sponsorId + "' was sponsored");
|
||||
else
|
||||
System.out.println("Couldn't sponsor animal with id: " + sponsorId);
|
||||
break;
|
||||
case 5:
|
||||
// imprimir a informação de todos os animais
|
||||
shelter.listAllAnimals();
|
||||
break;
|
||||
case 6:
|
||||
System.out.println("Goodbye!");
|
||||
break;
|
||||
default:
|
||||
// imprimir mensagem de erro
|
||||
System.out.println("Invalid option.");
|
||||
break;
|
||||
}
|
||||
|
||||
} while (choice != 6);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package aval.aa1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PetShelter implements IPetShelter {
|
||||
private String shelterName;
|
||||
private Animal[] animals;
|
||||
|
||||
public PetShelter(String shelterName) {
|
||||
this.shelterName = shelterName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAnimal(Animal animal) {
|
||||
if (animals == null) {
|
||||
animals = new Animal[1];
|
||||
animals[0] = animal;
|
||||
} else {
|
||||
Animal[] newAnimals = new Animal[animals.length + 1];
|
||||
for (int i = 0; i < animals.length; i++) {
|
||||
newAnimals[i] = animals[i];
|
||||
}
|
||||
newAnimals[newAnimals.length - 1] = animal;
|
||||
animals = newAnimals;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAnimal(Animal animal) {
|
||||
Animal[] newAnimals = new Animal[animals.length - 1];
|
||||
int index = 0;
|
||||
for (Animal a : animals) {
|
||||
if (a.getId() != animal.getId()) {
|
||||
newAnimals[index] = a;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
animals = newAnimals;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Animal searchForAnimal(String name) {
|
||||
for (Animal a : animals)
|
||||
if (a.getName().equals(name))
|
||||
return a;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sponsorAnimal(int animalId) {
|
||||
System.out.println("Sponsoring animal with id " + animalId + "...");
|
||||
System.out.print("Sponsor's name: ");
|
||||
String sponsorId = new Scanner(System.in).nextLine();
|
||||
for (Animal a : animals)
|
||||
if (a.getId() == animalId) {
|
||||
a.setSponsor(sponsorId);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listAllAnimals() {
|
||||
for (Animal a : animals)
|
||||
System.out.println(a.toString()+"\n");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package aval.aa1;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Rabbit extends Animal {
|
||||
private String fur;
|
||||
|
||||
public Rabbit(String name, int weight, int age, String fur, String sponsor) {
|
||||
super(name, weight, age, sponsor);
|
||||
this.fur = fur;
|
||||
}
|
||||
|
||||
public Rabbit(String name, int weight, int age, String fur) {
|
||||
super(name, weight, age);
|
||||
this.fur = fur;
|
||||
}
|
||||
|
||||
public String getFur() {
|
||||
return this.fur;
|
||||
}
|
||||
|
||||
public void setfur(String fur) {
|
||||
this.fur = fur;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Rabbit of fur " + fur + "\n" + super.toString();
|
||||
}
|
||||
|
||||
@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;
|
||||
Rabbit rabbit = (Rabbit) o;
|
||||
return Objects.equals(fur, rabbit.fur);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), fur);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package utils.Enums;
|
||||
|
||||
public enum Weekday {
|
||||
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
|
||||
|
||||
public static Weekday fromString(String string) {
|
||||
return switch (string) {
|
||||
case "MONDAY", "Monday", "monday" -> MONDAY;
|
||||
case "TUESDAY", "Tuesday", "tuesday" -> TUESDAY;
|
||||
case "WEDNESDAY", "Wednesday", "wednesday" -> WEDNESDAY;
|
||||
case "THURSDAY", "Thursday", "thursday" -> THURSDAY;
|
||||
case "FRIDAY", "Friday", "friday" -> FRIDAY;
|
||||
case "SATURDAY", "Saturday", "saturday" -> SATURDAY;
|
||||
case "SUNDAY", "Sunday", "sunday" -> SUNDAY;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return switch (this) {
|
||||
case MONDAY -> "MONDAY";
|
||||
case TUESDAY -> "TUESDAY";
|
||||
case WEDNESDAY -> "WEDNESDAY";
|
||||
case THURSDAY -> "THURSDAY";
|
||||
case FRIDAY -> "FRIDAY";
|
||||
case SATURDAY -> "SATURDAY";
|
||||
case SUNDAY -> "SUNDAY";
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue