From 4142a450716d75d8cc7a865b9ec4694c5ba2b456 Mon Sep 17 00:00:00 2001 From: Tiago Garcia Date: Sat, 27 May 2023 15:16:38 +0100 Subject: [PATCH] [POO] AA2 added (#68) * [POO] aa2 added --> Solution for the aa I had. --> Added only after all student finished their aa. * .gitignore update --> Made quartus gitignore lines affect all directories inside the repository, not just /1ano/2semestre/lsd/ --- .gitignore | 18 ++-- .../poo/src/aval/aa2/Classes/Item.java | 23 +++++ .../poo/src/aval/aa2/Classes/Order.java | 80 +++++++++++++++ .../src/aval/aa2/Classes/OrderManager.java | 45 +++++++++ .../Classes/StandardOrderCostCalculator.java | 14 +++ .../aa2/Interfaces/OrderCostCalculator.java | 7 ++ .../poo/src/aval/aa2/OrderTester.java | 99 +++++++++++++++++++ 1ano/2semestre/poo/src/aval/aa2/output.txt | 31 ++++++ 1ano/2semestre/poo/src/aval/aa2/pedidos.txt | 30 ++++++ 9 files changed, 338 insertions(+), 9 deletions(-) create mode 100644 1ano/2semestre/poo/src/aval/aa2/Classes/Item.java create mode 100644 1ano/2semestre/poo/src/aval/aa2/Classes/Order.java create mode 100644 1ano/2semestre/poo/src/aval/aa2/Classes/OrderManager.java create mode 100644 1ano/2semestre/poo/src/aval/aa2/Classes/StandardOrderCostCalculator.java create mode 100644 1ano/2semestre/poo/src/aval/aa2/Interfaces/OrderCostCalculator.java create mode 100644 1ano/2semestre/poo/src/aval/aa2/OrderTester.java create mode 100644 1ano/2semestre/poo/src/aval/aa2/output.txt create mode 100644 1ano/2semestre/poo/src/aval/aa2/pedidos.txt diff --git a/.gitignore b/.gitignore index fb5a953..857bb80 100644 --- a/.gitignore +++ b/.gitignore @@ -10,13 +10,13 @@ /**/__pycache__/ # Quartus Output/Cache files -/1ano/2semestre/lsd/**/db/ -/1ano/2semestre/lsd/**/incremental_db/ -/1ano/2semestre/lsd/**/output_files/* -!/1ano/2semestre/lsd/**/output_files/*.sof -/1ano/2semestre/lsd/**/simulation/ -/1ano/2semestre/lsd/**/*.bak -/1ano/2semestre/lsd/**/*.qws -/1ano/2semestre/lsd/**/*.qpf -/1ano/2semestre/lsd/**/*.qsf +/**/db/ +/**/incremental_db/ +/**/output_files/* +!/**/output_files/*.sof +/**/simulation/ +/**/*.bak +/**/*.qws +/**/*.qpf +/**/*.qsf !/1ano/2semestre/lsd/master.qsf diff --git a/1ano/2semestre/poo/src/aval/aa2/Classes/Item.java b/1ano/2semestre/poo/src/aval/aa2/Classes/Item.java new file mode 100644 index 0000000..d77d5b7 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa2/Classes/Item.java @@ -0,0 +1,23 @@ +package aval.aa2.Classes; + +import java.util.Objects; + +public record Item(String name, double price) { + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Item item)) return false; + return Double.compare(item.price(), price()) == 0 && Objects.equals(name(), item.name()); + } + + @Override + public int hashCode() { + return Objects.hash(name(), price()); + } + + @Override + public String toString() { + return String.format("%s: %.2f", name, price); + } +} diff --git a/1ano/2semestre/poo/src/aval/aa2/Classes/Order.java b/1ano/2semestre/poo/src/aval/aa2/Classes/Order.java new file mode 100644 index 0000000..6424ed1 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa2/Classes/Order.java @@ -0,0 +1,80 @@ +package aval.aa2.Classes; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Objects; + +public class Order { + public static int currentId = 0; + private final int id; + private final List items; + private final String storeId; + private final String clientId; + private final LocalDateTime orderDateTime; + private final boolean expressOrder; + + private double price; + + public Order(List items, String storeId, String clientId, LocalDateTime orderDateTime, boolean expressOrder) { + this.id = ++currentId; + this.items = items; + this.storeId = storeId; + this.clientId = clientId; + this.orderDateTime = orderDateTime; + this.expressOrder = expressOrder; + } + + public int getId() { + return id; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public List getItems() { + return items; + } + + public String getStoreId() { + return storeId; + } + + public String getClientId() { + return clientId; + } + + public LocalDateTime getOrderDateTime() { + return orderDateTime; + } + + public boolean isExpressOrder() { + return expressOrder; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Order order)) return false; + return getId() == order.getId() && isExpressOrder() == order.isExpressOrder() && Double.compare(order.getPrice(), getPrice()) == 0 && Objects.equals(getItems(), order.getItems()) && Objects.equals(getStoreId(), order.getStoreId()) && Objects.equals(getClientId(), order.getClientId()) && Objects.equals(getOrderDateTime(), order.getOrderDateTime()); + } + + @Override + public int hashCode() { + return Objects.hash(getId(), getItems(), getStoreId(), getClientId(), getOrderDateTime(), isExpressOrder(), getPrice()); + } + + @Override + public String toString() { + StringBuilder itemsStringBuilder = new StringBuilder(); + items.forEach(item -> itemsStringBuilder.append(item).append(", ")); + return String.format("Order #%d: Client ID: %s, Store ID: %s, Order Datetime: %s, Express order: %b, Total cost: %.2f, Items: {%s};", + getId(), getClientId(), getStoreId(), getOrderDateTime().toString().replace('T', ' '), isExpressOrder(), + getPrice(), itemsStringBuilder.substring(0, itemsStringBuilder.length()-2)); + } +} diff --git a/1ano/2semestre/poo/src/aval/aa2/Classes/OrderManager.java b/1ano/2semestre/poo/src/aval/aa2/Classes/OrderManager.java new file mode 100644 index 0000000..b956c08 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa2/Classes/OrderManager.java @@ -0,0 +1,45 @@ +package aval.aa2.Classes; + +import java.util.ArrayList; +import java.util.List; + +public class OrderManager { + List orders = new ArrayList<>(); + + public void addOrder(Order order) { + orders.add(order); + } + + public void removeOrder(int id) { + orders.removeIf(order -> order.getId() == id); + } + + public Order searchOrder(int id) { + return orders.stream().filter(order -> order.getId() == id).findFirst().orElse(null); + } + + public double calculateOrderCost(int id) { + StandardOrderCostCalculator calculator = new StandardOrderCostCalculator(); + double cost = searchOrder(id) != null ? calculator.calculateOrderCost(searchOrder(id)): -1; + searchOrder(id).setPrice(cost); + return cost; + } + + public void calculateOrdersCostInMonth(int month) { + StandardOrderCostCalculator calculator = new StandardOrderCostCalculator(); + orders.stream().filter(order -> order.getOrderDateTime().getMonth().getValue() == month).forEach(order -> order.setPrice(calculator.calculateOrderCost(searchOrder(order.getId())))); + } + + public void printAllOrders() { + for (Order order : orders) + System.out.println(order); + System.out.println(); + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + for (Order order : orders) + sb.append(order).append("\n"); + return sb.toString(); + } +} diff --git a/1ano/2semestre/poo/src/aval/aa2/Classes/StandardOrderCostCalculator.java b/1ano/2semestre/poo/src/aval/aa2/Classes/StandardOrderCostCalculator.java new file mode 100644 index 0000000..66b047e --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa2/Classes/StandardOrderCostCalculator.java @@ -0,0 +1,14 @@ +package aval.aa2.Classes; + +import aval.aa2.Interfaces.OrderCostCalculator; + +public class StandardOrderCostCalculator implements OrderCostCalculator { + @Override + public double calculateOrderCost(Order order) { + double cost = 0; + for (Item item : order.getItems()) { + cost += item.price(); + } + return order.isExpressOrder() ? cost * 1.3 : cost; + } +} diff --git a/1ano/2semestre/poo/src/aval/aa2/Interfaces/OrderCostCalculator.java b/1ano/2semestre/poo/src/aval/aa2/Interfaces/OrderCostCalculator.java new file mode 100644 index 0000000..4ccc161 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa2/Interfaces/OrderCostCalculator.java @@ -0,0 +1,7 @@ +package aval.aa2.Interfaces; + +import aval.aa2.Classes.Order; + +public interface OrderCostCalculator { + double calculateOrderCost(Order order); +} diff --git a/1ano/2semestre/poo/src/aval/aa2/OrderTester.java b/1ano/2semestre/poo/src/aval/aa2/OrderTester.java new file mode 100644 index 0000000..7a04dbd --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa2/OrderTester.java @@ -0,0 +1,99 @@ +package aval.aa2; + +import aval.aa2.Classes.Item; +import aval.aa2.Classes.Order; +import aval.aa2.Classes.OrderManager; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class OrderTester { + public static void main(String[] args) { + OrderManager orderManager = new OrderManager(); + + orderManager.addOrder(new Order(new ArrayList<>(List.of(new Item[]{ + new Item("Chocolate", 2.43) + })), + "104", "23", LocalDateTime.of(2023, 5, 2, 14, 5), false)); + + orderManager.addOrder(new Order(new ArrayList<>(List.of(new Item[]{ + new Item("Pastel de nata", 7.73), + new Item("Sumo de laranja", 1.20) + })), + "114", "27", LocalDateTime.of(2023, 5, 6, 12, 4), true)); + + orderManager.addOrder(new Order(new ArrayList<>(List.of(new Item[]{ + new Item("Galão", 1.20), + new Item("Torrada", 1.00) + })), + "124", "1", LocalDateTime.of(2023, 5, 12, 7, 4), false)); + + orderManager.printAllOrders(); + + orderManager.removeOrder(3); + + orderManager.printAllOrders(); + + try { + loadOrdersFile(orderManager, "src/aval/aa2/pedidos.txt"); + } catch (FileNotFoundException e) { + System.out.println("Couldn't find that file!"); + } + + System.out.println(orderManager.searchOrder(10)); + + orderManager.printAllOrders(); + + System.out.println(orderManager.calculateOrderCost(5)); + + orderManager.calculateOrdersCostInMonth(5); + + orderManager.printAllOrders(); + + saveToFile(orderManager, "src/aval/aa2/output.txt"); + } + + private static void loadOrdersFile(OrderManager orderManager, String filename) throws FileNotFoundException { + Scanner sin = new Scanner(new FileReader(filename)); + if (!sin.hasNextLine()) return; + + sin.nextLine(); + while (sin.hasNextLine()) { + try { + String orderString = sin.nextLine(); + String[] orderData = orderString.split(";"); + String[] localDate = orderData[3].split(" ")[0].split("-"); + String[] localTime = orderData[3].split(" ")[1].split(":"); + LocalDateTime localDateTime = LocalDateTime.of(Integer.parseInt(localDate[0]), Integer.parseInt(localDate[1]), Integer.parseInt(localDate[2]), Integer.parseInt(localTime[0]), Integer.parseInt(localTime[1])); + String[] itemsStrings = orderData[2].split("\\|"); + List items = new ArrayList<>(); + Arrays.stream(itemsStrings).toList().forEach(item -> items.add(new Item(item.split(":")[0], Double.parseDouble(item.split(":")[1])))); + orderManager.addOrder(new Order(items, orderData[1], orderData[0], localDateTime, orderData[4].equals("express"))); + } catch (ArrayIndexOutOfBoundsException ignored) { + } + } + } + + public static void saveToFile(OrderManager orderManager, String filename) { + Path path = Path.of(filename); + try { + Files.writeString(path, orderManager.toString()); + } catch (IOException e) { + try { + Files.createFile(path); + saveToFile(orderManager, filename); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } +} diff --git a/1ano/2semestre/poo/src/aval/aa2/output.txt b/1ano/2semestre/poo/src/aval/aa2/output.txt new file mode 100644 index 0000000..7784039 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa2/output.txt @@ -0,0 +1,31 @@ +Order #1: Client ID: 23, Store ID: 104, Order Datetime: 2023-05-02 14:05, Express order: false, Total cost: 2.43, Items: {Chocolate: 2.43}; +Order #2: Client ID: 27, Store ID: 114, Order Datetime: 2023-05-06 12:04, Express order: true, Total cost: 11.61, Items: {Pastel de nata: 7.73, Sumo de laranja: 1.20}; +Order #4: Client ID: 1, Store ID: 101, Order Datetime: 2023-05-01 09:15, Express order: false, Total cost: 5.50, Items: {Café: 2.50, Sandes de Queijo: 3.00}; +Order #5: Client ID: 2, Store ID: 102, Order Datetime: 2023-05-01 09:30, Express order: false, Total cost: 10.00, Items: {Cappuccino: 4.50, Bolo de Chocolate: 5.50}; +Order #6: Client ID: 3, Store ID: 103, Order Datetime: 2023-05-01 10:00, Express order: false, Total cost: 2.50, Items: {Água Mineral: 1.50, Pão Francês: 1.00}; +Order #7: Client ID: 4, Store ID: 104, Order Datetime: 2023-05-02 08:45, Express order: false, Total cost: 7.50, Items: {Café com Leite: 3.50, Sandes de Presunto: 4.00}; +Order #8: Client ID: 5, Store ID: 105, Order Datetime: 2023-05-02 10:30, Express order: false, Total cost: 8.00, Items: {Chá de Erva-Cidreira: 2.00, Torta de Frango: 6.00}; +Order #9: Client ID: 6, Store ID: 101, Order Datetime: 2023-05-03 12:15, Express order: false, Total cost: 8.50, Items: {Tosta Mista: 5.00, Coca-Cola: 3.50}; +Order #10: Client ID: 7, Store ID: 103, Order Datetime: 2023-05-03 14:00, Express order: false, Total cost: 5.50, Items: {Café Expresso: 2.50, Sandes de Queijo: 3.00}; +Order #11: Client ID: 8, Store ID: 102, Order Datetime: 2023-05-04 09:45, Express order: false, Total cost: 9.50, Items: {Cappuccino: 4.50, Bolo de Cenoura: 5.00}; +Order #12: Client ID: 9, Store ID: 104, Order Datetime: 2023-05-04 11:30, Express order: false, Total cost: 7.50, Items: {Sumo de Laranja: 3.00, Salada de Frutas: 4.50}; +Order #13: Client ID: 10, Store ID: 105, Order Datetime: 2023-05-05 13:00, Express order: false, Total cost: 9.50, Items: {Sandes de Frango: 6.50, Refrigerante Guaraná: 3.00}; +Order #14: Client ID: 11, Store ID: 101, Order Datetime: 2023-05-05 15:45, Express order: false, Total cost: 6.50, Items: {Café com Leite: 3.50, Sandes de Queijo: 3.00}; +Order #15: Client ID: 12, Store ID: 103, Order Datetime: 2023-05-06 10:15, Express order: false, Total cost: 5.00, Items: {Café Expresso: 2.50, Croissant: 2.50}; +Order #16: Client ID: 13, Store ID: 102, Order Datetime: 2023-05-06 11:45, Express order: false, Total cost: 7.50, Items: {Chá de Hortelã: 2.00, Bolo de Chocolate: 5.50}; +Order #17: Client ID: 14, Store ID: 104, Order Datetime: 2023-05-07 09:30, Express order: false, Total cost: 2.50, Items: {Água Mineral: 1.50, Pão Francês: 1.00}; +Order #18: Client ID: 15, Store ID: 105, Order Datetime: 2023-05-07 12:00, Express order: false, Total cost: 6.50, Items: {Café: 2.50, Sandes de Presunto: 4.00}; +Order #19: Client ID: 16, Store ID: 101, Order Datetime: 2023-05-08 14:30, Express order: false, Total cost: 4.50, Items: {Chá de Camomila: 2.00, Salgado de Queijo: 2.50}; +Order #20: Client ID: 17, Store ID: 103, Order Datetime: 2023-05-08 16:15, Express order: false, Total cost: 8.50, Items: {Tosta Mista: 5.00, Refrigerante Coca-Cola: 3.50}; +Order #21: Client ID: 18, Store ID: 102, Order Datetime: 2023-05-09 08:45, Express order: false, Total cost: 8.50, Items: {Café com Leite: 3.50, Bolo de Cenoura: 5.00}; +Order #22: Client ID: 19, Store ID: 104, Order Datetime: 2023-05-09 10:30, Express order: false, Total cost: 5.50, Items: {Café Expresso: 2.50, Sumo de Laranja: 3.00}; +Order #23: Client ID: 21, Store ID: 101, Order Datetime: 2023-05-10 12:15, Express order: false, Total cost: 14.50, Items: {Café com Leite: 3.50, Sandes de Queijo: 3.00, Tosta Mista: 5.00, Refrigerante Guaraná: 3.00}; +Order #24: Client ID: 22, Store ID: 103, Order Datetime: 2023-05-10 14:00, Express order: false, Total cost: 7.50, Items: {Café Expresso: 2.50, Croissant: 2.50, Água Mineral: 1.50, Pão Francês: 1.00}; +Order #25: Client ID: 23, Store ID: 102, Order Datetime: 2023-05-11 09:45, Express order: false, Total cost: 11.50, Items: {Cappuccino: 4.50, Bolo de Cenoura: 5.00, Chá de Hortelã: 2.00}; +Order #26: Client ID: 24, Store ID: 104, Order Datetime: 2023-05-11 11:30, Express order: false, Total cost: 14.00, Items: {Sumo de Laranja: 3.00, Salada de Frutas: 4.50, Sandes de Frango: 6.50}; +Order #27: Client ID: 25, Store ID: 105, Order Datetime: 2023-05-12 13:00, Express order: false, Total cost: 6.50, Items: {Café: 2.50, Sandes de Presunto: 4.00}; +Order #28: Client ID: 26, Store ID: 101, Order Datetime: 2023-05-12 15:45, Express order: false, Total cost: 14.50, Items: {Café com Leite: 3.50, Sandes de Queijo: 3.00, Tosta Mista: 5.00, Refrigerante Guaraná: 3.00}; +Order #29: Client ID: 27, Store ID: 103, Order Datetime: 2023-05-13 10:15, Express order: false, Total cost: 7.50, Items: {Café Expresso: 2.50, Croissant: 2.50, Água Mineral: 1.50, Pão Francês: 1.00}; +Order #30: Client ID: 28, Store ID: 102, Order Datetime: 2023-05-13 11:45, Express order: false, Total cost: 11.50, Items: {Cappuccino: 4.50, Bolo de Cenoura: 5.00, Chá de Hortelã: 2.00}; +Order #31: Client ID: 29, Store ID: 104, Order Datetime: 2023-05-14 09:30, Express order: false, Total cost: 14.00, Items: {Sumo de Laranja: 3.00, Salada de Frutas: 4.50, Sandes de Frango: 6.50}; +Order #32: Client ID: 30, Store ID: 105, Order Datetime: 2023-05-14 12:00, Express order: false, Total cost: 14.50, Items: {Café: 2.50, Sandes de Presunto: 4.00, Chá de Erva-Cidreira: 2.00, Torta de Frango: 6.00}; diff --git a/1ano/2semestre/poo/src/aval/aa2/pedidos.txt b/1ano/2semestre/poo/src/aval/aa2/pedidos.txt new file mode 100644 index 0000000..9d6a540 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa2/pedidos.txt @@ -0,0 +1,30 @@ +ID Cliente;ID loja;Composição;Data e hora;Tipo +1;101;Café:2.50|Sandes de Queijo:3.00;2023-05-01 09:15;expresso +2;102;Cappuccino:4.50|Bolo de Chocolate:5.50;2023-05-01 09:30;normal +3;103;Água Mineral:1.50|Pão Francês:1.00;2023-05-01 10:00;expresso +4;104;Café com Leite:3.50|Sandes de Presunto:4.00;2023-05-02 08:45;normal +5;105;Chá de Erva-Cidreira:2.00|Torta de Frango:6.00;2023-05-02 10:30;normal +6;101;Tosta Mista:5.00|Coca-Cola:3.50;2023-05-03 12:15;expresso +7;103;Café Expresso:2.50|Sandes de Queijo:3.00;2023-05-03 14:00;normal +8;102;Cappuccino:4.50|Bolo de Cenoura:5.00;2023-05-04 09:45;expresso +9;104;Sumo de Laranja:3.00|Salada de Frutas:4.50;2023-05-04 11:30;normal +10;105;Sandes de Frango:6.50|Refrigerante Guaraná:3.00;2023-05-05 13:00;normal +11;101;Café com Leite:3.50|Sandes de Queijo:3.00;2023-05-05 15:45;expresso +12;103;Café Expresso:2.50|Croissant:2.50;2023-05-06 10:15;normal +13;102;Chá de Hortelã:2.00|Bolo de Chocolate:5.50;2023-05-06 11:45;expresso +14;104;Água Mineral:1.50|Pão Francês:1.00;2023-05-07 09:30;normal +15;105;Café:2.50|Sandes de Presunto:4.00;2023-05-07 12:00;expresso +16;101;Chá de Camomila:2.00|Salgado de Queijo:2.50;2023-05-08 14:30;normal +17;103;Tosta Mista:5.00|Refrigerante Coca-Cola:3.50;2023-05-08 16:15;normal +18;102;Café com Leite:3.50|Bolo de Cenoura:5.00;2023-05-09 08:45;expresso +19;104;Café Expresso:2.50|Sumo de Laranja:3.00;2023-05-09 10:30;expresso +21;101;Café com Leite:3.50|Sandes de Queijo:3.00|Tosta Mista:5.00|Refrigerante Guaraná:3.00;2023-05-10 12:15;normal +22;103;Café Expresso:2.50|Croissant:2.50|Água Mineral:1.50|Pão Francês:1.00;2023-05-10 14:00;expresso +23;102;Cappuccino:4.50|Bolo de Cenoura:5.00|Chá de Hortelã:2.00;2023-05-11 09:45;expresso +24;104;Sumo de Laranja:3.00|Salada de Frutas:4.50|Sandes de Frango:6.50;2023-05-11 11:30;normal +25;105;Café:2.50|Sandes de Presunto:4.00;2023-05-12 13:00;expresso +26;101;Café com Leite:3.50|Sandes de Queijo:3.00|Tosta Mista:5.00|Refrigerante Guaraná:3.00;2023-05-12 15:45;normal +27;103;Café Expresso:2.50|Croissant:2.50|Água Mineral:1.50|Pão Francês:1.00;2023-05-13 10:15;expresso +28;102;Cappuccino:4.50|Bolo de Cenoura:5.00|Chá de Hortelã:2.00;2023-05-13 11:45;expresso +29;104;Sumo de Laranja:3.00|Salada de Frutas:4.50|Sandes de Frango:6.50;2023-05-14 09:30;normal +30;105;Café:2.50|Sandes de Presunto:4.00|Chá de Erva-Cidreira:2.00|Torta de Frango:6.00;2023-05-14 12:00;expresso