From 89bb3a674e4304644c37109ed5a608b97926deb9 Mon Sep 17 00:00:00 2001 From: ibartkowski Date: Mon, 14 Oct 2024 19:12:15 +0200 Subject: [PATCH] TechStore code (#1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Technikladen-Aufgaben-Lösung hinzugefügt, weitere kleinere Änderungen werden noch folgen, aber funktional hiermit vollständig Reviewed-on: https://git.cantorgymnasium.de/denyskon/info-java/pulls/1 Reviewed-by: Denys Konovalov Co-authored-by: ibartkowski Co-committed-by: ibartkowski --- Technikladen/Client.java | 37 +++++++++++ Technikladen/Order.java | 42 ++++++++++++ Technikladen/OrderEntry.java | 14 ++++ Technikladen/OrderSystem.java | 55 +++++++++++++++ Technikladen/Product.java | 34 ++++++++++ Technikladen/Receipt.java | 12 ++++ Technikladen/StorageManager.java | 25 +++++++ Technikladen/TechStore.java | 111 +++++++++++++++++++++++++++++++ 8 files changed, 330 insertions(+) create mode 100755 Technikladen/Client.java create mode 100755 Technikladen/Order.java create mode 100755 Technikladen/OrderEntry.java create mode 100755 Technikladen/OrderSystem.java create mode 100755 Technikladen/Product.java create mode 100755 Technikladen/Receipt.java create mode 100755 Technikladen/StorageManager.java create mode 100755 Technikladen/TechStore.java diff --git a/Technikladen/Client.java b/Technikladen/Client.java new file mode 100755 index 0000000..3b32333 --- /dev/null +++ b/Technikladen/Client.java @@ -0,0 +1,37 @@ +import java.util.ArrayList; + +public class Client { + private static int clientCounter; + private int clientID; + private String clientName; + private String clientEmail; + private ArrayList clientOrders; + public Client(String clientName, String clientEmail) { + clientCounter++; + this.clientID = clientCounter; + this.clientName = clientName; + this.clientEmail = clientEmail; + this.clientOrders = new ArrayList(); + } + public int getID() { + return this.clientID; + } + public String getName() { + return this.clientName; + } + public String getEmail() { + return this.clientEmail; + } + public ArrayList getOrders() { + return this.clientOrders; + } + public void addOrder(Order order) { + this.clientOrders.add(order); + } + public boolean hasOrder() { + return !this.clientOrders.isEmpty(); + } + public String toString() { + return ("ID: " + this.clientID + ", Name: " + this.clientName + ", Email: " + this.clientEmail); + } +} diff --git a/Technikladen/Order.java b/Technikladen/Order.java new file mode 100755 index 0000000..dc08e7c --- /dev/null +++ b/Technikladen/Order.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; + +public class Order { + private int orderClientID; + private ArrayList orderEntries; + private String orderDate; + public Order(int clientID, String date) { + this.orderClientID = clientID; + this.orderEntries = new ArrayList(); + this.orderDate = date; + } + public int getClientID() { + return this.orderClientID; + } + public String getDate() { + return this.orderDate; + } + public ArrayList getEntries() { + return this.orderEntries; + } + public void addEntry(OrderEntry entry) { + this.orderEntries.add(entry); + } + public int calcPrice() { + int price = 0; + for (OrderEntry entry : this.orderEntries) { + price += entry.calcPrice(); + } + return price; + } + public String createReceipt() { + String output = "\n"; + for (OrderEntry entry : this.orderEntries) { + output += entry.toString() + ", Price: " + entry.calcPrice() + "\n"; + } + output += "=====" + this.calcPrice() + "====="; + return output; + } + public String toString() { + return ("Client: " + this.orderClientID + ", Date: " + this.orderDate); + } +} diff --git a/Technikladen/OrderEntry.java b/Technikladen/OrderEntry.java new file mode 100755 index 0000000..9d47921 --- /dev/null +++ b/Technikladen/OrderEntry.java @@ -0,0 +1,14 @@ +public class OrderEntry { + private Product orderProduct; + private int entryAmount; + public OrderEntry(Product product, int amt) { + this.orderProduct = product; + this.entryAmount = amt; + } + public int calcPrice() { + return this.entryAmount * this.orderProduct.getPrice(); + } + public String toString() { + return ("Product: " + this.orderProduct.getName() + ", Amount: " + this.entryAmount); + } +} diff --git a/Technikladen/OrderSystem.java b/Technikladen/OrderSystem.java new file mode 100755 index 0000000..c111eac --- /dev/null +++ b/Technikladen/OrderSystem.java @@ -0,0 +1,55 @@ +import java.util.ArrayList; + +public class OrderSystem { + private StorageManager systemManager; + private ArrayList systemClients; + public OrderSystem(StorageManager manager, ArrayList clients) { + this.systemManager = manager; + this.systemClients = clients; + } + public void addProduct(Product product) { + this.systemManager.addProduct(product); + } + public void addClient(Client client) { + this.systemClients.add(client); + } + public Client getClientByID(int id) { + for (Client client : this.systemClients) { + if (client.getID() == id) { + return client; + } + } + return new Client("", ""); + } + public void createOrder(int id, Order order) { + this.getClientByID(id).addOrder(order); + } + public ArrayList getClients() { + return this.systemClients; + } + public Product getProductByID(int id) { + for (Product product : this.systemManager.getStorage()) { + if (product.getID() == id) { + return product; + } + } + return new Product(-1, "", 0, 0); + } + public StorageManager getManager() { + return this.systemManager; + } + public void showClients() { + for (Client client : this.systemClients) { + System.out.println(client.toString()); + } + } + public void showProducts() { + if (this.systemManager.getStorage().isEmpty()) { + System.out.println("No products exist"); + return; + } + for (Product product : this.systemManager.getStorage()) { + System.out.println("ID: " + product.getID() + ", Name: " + product.getName() + ", Price: " + product.getPrice() + ", Amount: " + product.getAmount()); + } + } +} \ No newline at end of file diff --git a/Technikladen/Product.java b/Technikladen/Product.java new file mode 100755 index 0000000..3471cd3 --- /dev/null +++ b/Technikladen/Product.java @@ -0,0 +1,34 @@ +public class Product { + private int productID; + private String productName; + private int productPrice; + private int productAmount; + public Product(int id, String name, int price, int amt) { + this.productID = id; + this.productName = name; + this.productPrice = price; + this.productAmount = amt; + } + public int getID() { + return this.productID; + } + public String getName() { + return this.productName; + } + public int getPrice() { + return this.productPrice; + } + public int getAmount() { + return this.productAmount; + } + public void reduceAmount(int amt) { + if (this.productAmount < amt) { + System.out.println("Not enough of product " + this.productID + "(" + this.productName + ") available!"); + return; + } + this.productAmount -= amt; + } + public void increaseAmount(int amt) { + this.productAmount += amt; + } +} diff --git a/Technikladen/Receipt.java b/Technikladen/Receipt.java new file mode 100755 index 0000000..95ddeb5 --- /dev/null +++ b/Technikladen/Receipt.java @@ -0,0 +1,12 @@ +public class Receipt { + private Order receiptOrder; + private Client receiptClient; + public Receipt(Order order, Client client) { + this.receiptOrder = order; + this.receiptClient = client; + } + public void printReceipt() { + System.out.println("Client:\n" + this.receiptClient.toString()); + System.out.println(this.receiptOrder.createReceipt()); + } +} diff --git a/Technikladen/StorageManager.java b/Technikladen/StorageManager.java new file mode 100755 index 0000000..9771cad --- /dev/null +++ b/Technikladen/StorageManager.java @@ -0,0 +1,25 @@ +import java.util.ArrayList; + +public class StorageManager { + private ArrayList storageProducts; + public StorageManager(ArrayList products) { + this.storageProducts = products; + } + public void addProduct(Product product) { + this.storageProducts.add(product); + } + public ArrayList getStorage() { + return this.storageProducts; + } + public void increaseStorage(int id, int amt) { + this.getProductByID(id).increaseAmount(amt); + } + public Product getProductByID(int id) { + for (Product product : this.storageProducts) { + if (product.getID() == id) { + return product; + } + } + return new Product(-1, "", 0, 0); + } +} diff --git a/Technikladen/TechStore.java b/Technikladen/TechStore.java new file mode 100755 index 0000000..a11e42a --- /dev/null +++ b/Technikladen/TechStore.java @@ -0,0 +1,111 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class TechStore { + public static void p(String input) { + System.out.print(input); + } + public static void main(String[] args) { + StorageManager manager = new StorageManager(new ArrayList()); + OrderSystem system = new OrderSystem(manager, new ArrayList()); + Scanner scnr = new Scanner(System.in); + while (true) { + p("\nInput mode: "); + String input = scnr.nextLine(); + if (input.equals("8")) { + break; + } + switch (Integer.parseInt(input)) { + case 1: { + p("Name: "); + String name = scnr.nextLine(); + p("E-Mail: "); + String email = scnr.nextLine(); + system.addClient(new Client(name, email)); + break; + } + case 2: { + ArrayList clients = system.getClients(); + if (clients.isEmpty()) { + System.out.println("No clients found"); + break; + } + for (Client client : clients) { + System.out.println(client.toString()); + } + break; + } + case 3: { + p("ID: "); + int id = Integer.parseInt(scnr.nextLine()); + p("Name: "); + String name = scnr.nextLine(); + p("Price: "); + int price = Integer.parseInt(scnr.nextLine()); + p("Amount: "); + int amt = Integer.parseInt(scnr.nextLine()); + system.addProduct(new Product(id, name, price, amt)); + break; + } + case 4: { + system.showProducts(); + break; + } + case 5: { + system.showProducts(); + p("ID: "); + int id = Integer.parseInt(scnr.nextLine()); + p("Amount: "); + int amt = Integer.parseInt(scnr.nextLine()); + manager.increaseStorage(id, amt); + break; + } + case 6: { + system.showClients(); + p("ID: "); + int clientID = Integer.parseInt(scnr.nextLine()); + p("Date: "); + String date = scnr.nextLine(); + Order order = new Order(clientID, date); + system.showProducts(); + while (true) { + p("ID: "); + String id_str = scnr.nextLine(); + if (id_str.equals("exit")) { + break; + } + int id = Integer.parseInt(id_str); + p("Amount: "); + int amt = Integer.parseInt(scnr.nextLine()); + if (manager.getProductByID(id).getAmount() < amt) { + System.out.println("Amount chosen too high"); + continue; + } + order.addEntry(new OrderEntry(system.getProductByID(id), amt)); + } + system.createOrder(clientID, order); + break; + } + case 7: { + system.showClients(); + p("ID: "); + int id = Integer.parseInt(scnr.nextLine()); + int n = 0; + for (Order order : system.getClientByID(id).getOrders()) { + System.out.println((n++) + order.toString()); + } + p("ID: "); + int order_id = Integer.parseInt(scnr.nextLine()); + System.out.println(system.getClientByID(id).getOrders().get(order_id).createReceipt()); + break; + } + case 8: { + return; + } + } + } + } +} + + +