[POO] aula08 ex3 added

This commit is contained in:
TiagoRG 2023-04-08 13:07:08 +01:00
parent 916941ce39
commit 3c5c840f03
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
6 changed files with 141 additions and 11 deletions

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}