TechStore code (#1)

Technikladen-Aufgaben-Lösung hinzugefügt, weitere kleinere Änderungen werden noch folgen, aber funktional hiermit vollständig

Reviewed-on: denyskon/info-java#1
Reviewed-by: Denys Konovalov <kontakt@denyskon.de>
Co-authored-by: ibartkowski <ibartkowski@git.cantorgymnasium.de>
Co-committed-by: ibartkowski <ibartkowski@git.cantorgymnasium.de>
This commit is contained in:
Igor Bartkowski 2024-10-14 19:12:15 +02:00 committed by Denys Konovalov
parent f8e091216c
commit 89bb3a674e
8 changed files with 330 additions and 0 deletions

37
Technikladen/Client.java Executable file

@ -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<Order> clientOrders;
public Client(String clientName, String clientEmail) {
clientCounter++;
this.clientID = clientCounter;
this.clientName = clientName;
this.clientEmail = clientEmail;
this.clientOrders = new ArrayList<Order>();
}
public int getID() {
return this.clientID;
}
public String getName() {
return this.clientName;
}
public String getEmail() {
return this.clientEmail;
}
public ArrayList<Order> 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);
}
}

42
Technikladen/Order.java Executable file

@ -0,0 +1,42 @@
import java.util.ArrayList;
public class Order {
private int orderClientID;
private ArrayList<OrderEntry> orderEntries;
private String orderDate;
public Order(int clientID, String date) {
this.orderClientID = clientID;
this.orderEntries = new ArrayList<OrderEntry>();
this.orderDate = date;
}
public int getClientID() {
return this.orderClientID;
}
public String getDate() {
return this.orderDate;
}
public ArrayList<OrderEntry> 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);
}
}

14
Technikladen/OrderEntry.java Executable file

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

55
Technikladen/OrderSystem.java Executable file

@ -0,0 +1,55 @@
import java.util.ArrayList;
public class OrderSystem {
private StorageManager systemManager;
private ArrayList<Client> systemClients;
public OrderSystem(StorageManager manager, ArrayList<Client> 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<Client> 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());
}
}
}

34
Technikladen/Product.java Executable file

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

12
Technikladen/Receipt.java Executable file

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

@ -0,0 +1,25 @@
import java.util.ArrayList;
public class StorageManager {
private ArrayList<Product> storageProducts;
public StorageManager(ArrayList<Product> products) {
this.storageProducts = products;
}
public void addProduct(Product product) {
this.storageProducts.add(product);
}
public ArrayList<Product> 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);
}
}

111
Technikladen/TechStore.java Executable file

@ -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<Product>());
OrderSystem system = new OrderSystem(manager, new ArrayList<Client>());
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<Client> 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;
}
}
}
}
}