From 29e977c1099a02486a39ac120d3fd9edafbc626c Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sat, 8 Apr 2023 13:07:08 +0100 Subject: [PATCH] [POO] aula08 ex3 added --- .../src/aula08/ex3/Interfaces/IProduct.java | 10 ++++ .../src/aula08/ex3/Interfaces/IPurchase.java | 9 ++++ .../2semestre/poo/src/aula08/ex3/Product.java | 52 +++++++++++++++++++ .../src/aula08/ex3/ProductWithDiscount.java | 23 ++++++++ .../poo/src/aula08/ex3/Purchase.java | 36 +++++++++++++ .../src/aula08/ex3/ShoppingCartTester.java | 22 ++++---- 6 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 1ano/2semestre/poo/src/aula08/ex3/Interfaces/IProduct.java create mode 100644 1ano/2semestre/poo/src/aula08/ex3/Interfaces/IPurchase.java create mode 100644 1ano/2semestre/poo/src/aula08/ex3/Product.java create mode 100644 1ano/2semestre/poo/src/aula08/ex3/ProductWithDiscount.java create mode 100644 1ano/2semestre/poo/src/aula08/ex3/Purchase.java diff --git a/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IProduct.java b/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IProduct.java new file mode 100644 index 0000000..976274b --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IProduct.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); +} diff --git a/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IPurchase.java b/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IPurchase.java new file mode 100644 index 0000000..f9a007c --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex3/Interfaces/IPurchase.java @@ -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(); +} diff --git a/1ano/2semestre/poo/src/aula08/ex3/Product.java b/1ano/2semestre/poo/src/aula08/ex3/Product.java new file mode 100644 index 0000000..03ed778 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex3/Product.java @@ -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()); + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex3/ProductWithDiscount.java b/1ano/2semestre/poo/src/aula08/ex3/ProductWithDiscount.java new file mode 100644 index 0000000..70e28e2 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex3/ProductWithDiscount.java @@ -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); + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex3/Purchase.java b/1ano/2semestre/poo/src/aula08/ex3/Purchase.java new file mode 100644 index 0000000..d6a1afb --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex3/Purchase.java @@ -0,0 +1,36 @@ +package aula08.ex3; + +import aula08.ex3.Interfaces.IPurchase; +import java.util.LinkedHashMap; + +public class Purchase implements IPurchase { + private final LinkedHashMap 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; + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex3/ShoppingCartTester.java b/1ano/2semestre/poo/src/aula08/ex3/ShoppingCartTester.java index 522e606..f8ab88f 100644 --- a/1ano/2semestre/poo/src/aula08/ex3/ShoppingCartTester.java +++ b/1ano/2semestre/poo/src/aula08/ex3/ShoppingCartTester.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()); } }