From 9e54056f89f9ab5b4de92b550dbb904046f8f06d Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Wed, 29 Mar 2023 23:31:05 +0200 Subject: [PATCH] u06 --- u06/src/com/mymart/CartItem.java | 48 ++++++++++++++++++++++++++ u06/src/com/mymart/ShoppingCart.java | 50 ++++++++++++++++++++++++++++ u06/src/com/mymart/Test.java | 11 ++++++ 3 files changed, 109 insertions(+) create mode 100644 u06/src/com/mymart/CartItem.java create mode 100644 u06/src/com/mymart/ShoppingCart.java create mode 100644 u06/src/com/mymart/Test.java diff --git a/u06/src/com/mymart/CartItem.java b/u06/src/com/mymart/CartItem.java new file mode 100644 index 0000000..e9d4187 --- /dev/null +++ b/u06/src/com/mymart/CartItem.java @@ -0,0 +1,48 @@ +package com.mymart; + +/** + * Klasse zum abspeichern eines Artikels + * @author p8q-yhw-u1 + */ +public class CartItem { + private String name; + private int quantity; + private double pricePerUnit; + + /** + * initialisiert einen einzukaufenden Artikel mit den gegebenen Daten + * @param name der Name des Artikels + * @param quantity die Anzahl der gekauften Artikel + * @param pricePerUnit der Preis pro Artikel + */ + public CartItem(String name, int quantity, double pricePerUnit) { + this.name = name; + setQuantity(quantity); + this.pricePerUnit = pricePerUnit; + } + + /** + * Liefert den Gesamtpreis des Artikels. + */ + public double getCost() { + return this.quantity * this.pricePerUnit; + } + + /** + * Setzt die Anzahl der Artikel + * @param quantity Anzahl der Artikel (muss mindestens 1 sein) + */ + public void setQuantity(int quantity) { + if (quantity <= 0) { + throw new IllegalArgumentException("Die Anzahl (quantity) eines Artikels muss stehts >= 1 sein"); + } + this.quantity = quantity; + } + + /** + * Gibt das Item in String Form zurück. + */ + public String toString() { + return String.format("%3d x %-30s %10.2f %10.2f%n", this.quantity, this.name, this.pricePerUnit, this.getCost()); + } +} diff --git a/u06/src/com/mymart/ShoppingCart.java b/u06/src/com/mymart/ShoppingCart.java new file mode 100644 index 0000000..11c6266 --- /dev/null +++ b/u06/src/com/mymart/ShoppingCart.java @@ -0,0 +1,50 @@ +package com.mymart; + +import java.util.ArrayList; + +/** + * Klasse zum abspeichern eines Einkaufswagens mit inhalt + * @author p8q-yhw-u1 + */ +public class ShoppingCart { + private ArrayList items; + + /** + * initialisiert einen leeren Einkaufswagen + */ + public ShoppingCart() { + this.items = new ArrayList(); + } + + /** + * Fügt einen Artikel hinzu + * @param item der hinzuzufügende Artikel + */ + public void addItem(CartItem item) { + this.items.add(item); + } + + /** + * Liefert den Gesamtpreis aller Artikel im Einkaufswagen + * @return + */ + public double getTotalCost() { + double cost = 0; + for (CartItem item : items) { + cost += item.getCost(); + } + return cost; + } + + /** + * Gibt den Kassenbon für den Einkaufswagen aus + */ + public String toString() { + StringBuilder sb = new StringBuilder(); + for (CartItem item : items) { + sb.append(item); + } + sb.append("%nSumme:%55.2f%n".formatted(this.getTotalCost())); + return sb.toString(); + } +} diff --git a/u06/src/com/mymart/Test.java b/u06/src/com/mymart/Test.java new file mode 100644 index 0000000..1946bc8 --- /dev/null +++ b/u06/src/com/mymart/Test.java @@ -0,0 +1,11 @@ +package com.mymart; + +public class Test { + + public static void main(String[] args) { + ShoppingCart sc = new ShoppingCart(); + sc.addItem(new CartItem("Erdbeeren", 15, 0.5)); + sc.addItem(new CartItem("Vollkornbrot", 2, 2.7)); + System.out.println(sc); + } +}