#Issue anfragen behoben.

backend logik aus TUI entfernt
Dummy Check entfernt
cart preislogik hinzugefügt
checkout in TUI modifiziert
loadProductsAndParse ist jetzt private
ArrayList non static
Frontendlogik aus Order entfernt
main
Daniel Zikol 2026-01-11 20:17:43 +01:00
parent 4d105abbf8
commit 51785b8f84
5 changed files with 89 additions and 36 deletions

View File

@ -15,17 +15,22 @@ public Cart(){
}
public void addProduct(Product prod, int quantity){
public boolean addProduct(Product prod, int quantity){
if (quantity > prod.getStock()){
return false;
}
//Ich überprüfe damit ob es das Produkt gibt
for(CartPosition pos : positions){
//Ist es das Produkt? Hat es dieselbe ID? ---> Ja ---> Quantity erhöhen---->break
if(pos.getProduct().getProdID() == prod.getProdID()){
pos.setQuantity(pos.getQuantity() + quantity); //Damit erhöhe ich quantity
break;
return true;
}
}
positions.add(new CartPosition(prod, quantity));
return true;
}
public void updateProductQuantity(int prodID,int quantity) {
@ -78,6 +83,21 @@ public void removeProduct(int productID){
public void clear(){
positions.clear();
}
public double getTotalNet() {
double sum = 0;
for (CartPosition pos : positions) {
sum += pos.getProduct().getNetPrice() * pos.getQuantity();
}
return sum;
}
public double getTotalGross() {
double sum = 0;
for (CartPosition pos : positions) {
sum += pos.getProduct().getBruttoPrice() * pos.getQuantity();
}
return sum;
}

View File

@ -43,5 +43,6 @@ public class CartPosition {
}
}

View File

@ -24,7 +24,7 @@ public class OnlineShop {
lager = new ArrayList<Product>();
}
public void loadProductsAndParse(ArrayList<String> lines){
private void loadProductsAndParse(ArrayList<String> lines){
for (int i = 1; i < lines.size(); i++){
String line = lines.get(i);
String[] partsOfList = line.split(",");
@ -80,7 +80,7 @@ public class OnlineShop {
// aus Sud
public static ArrayList<String> readFile(String path) throws FileNotFoundException {
public ArrayList<String> readFile(String path) throws FileNotFoundException {
ArrayList<String> lines = new ArrayList<>();
Scanner sc = new Scanner(new File(path));
@ -97,4 +97,29 @@ public class OnlineShop {
}
/*Ich habe erst später verstanden, was genau business logik ist im ganzen.
Zuweisungen sind keine Business logik... aber! wenn zustände verändert werden, dann schon.
weshalb ich in TUI diesen Process checkoutlogik veränderen müsste bzw checkout
*/
public Order checkOutLogic(Cart cart, String name, String addresse) {
//Hier schaue ich auch ob es die bestimme menge gibt, wenn nicht --> Nullpointer
for (CartPosition pos : cart.getPositions()) {
Product product = pos.getProduct();
if(product.getStock() < pos.getQuantity()){
return null;
}
}
//Habe genug deer Menge, kann reduziert werden
for (CartPosition pos : cart.getPositions()) {
reduceStock(pos.getProduct(), pos.getQuantity());
}
//Bestellung wird aufgegeben
Order order = new Order(name,addresse,cart.getPositions());
return order;
}
}

View File

@ -54,6 +54,7 @@ public class Order {
}
}
public void printConfirmation() {
IO.println("----------------------------------------");
IO.println(" ORDER CONFIRMATION ");
@ -95,4 +96,5 @@ public class Order {
return finalTotal;
}
}

View File

@ -14,6 +14,7 @@ public class ShopTUI {
private static OnlineShop onlineShop;
private static Cart cart;
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
IO.println("\n+--------------------------------------+");
IO.println("| WELCOME TO THE ONLINE SHOP |");
@ -110,13 +111,13 @@ public class ShopTUI {
String strQuantity = IO.readln("Quantity: ");
int quantity = Integer.parseInt(strQuantity);
boolean success = cart.addProduct(p, quantity);
if (success) {
IO.println("Added: " + p.getName() + " x " + quantity);
} else {
if (quantity > p.getStock()) {
IO.println("Not enough in stock!");
IO.println("Available: " + p.getStock());
} else {
cart.addProduct(p, quantity);
IO.println("Added: " + p.getName() + " x " + quantity);
}
}
@ -126,7 +127,7 @@ public class ShopTUI {
return;
}
Order tempOrder = new Order("", "", cart.getPositions());
IO.println("\nYour shopping cart:");
IO.println("----------------------------------------");
@ -134,8 +135,8 @@ public class ShopTUI {
IO.println(pos.getProduct());
}
IO.println("----------------------------------------");
IO.println(String.format("Product value (net): %6.2f €", tempOrder.getTotalNet()));
IO.println(String.format("TOTAL: %6.2f €", tempOrder.getFinalTotal()));
IO.println(String.format("Product value (net): %6.2f €", cart.getTotalNet()));
IO.println(String.format("TOTAL: %6.2f €", cart.getTotalGross()));
IO.println("----------------------------------------");
}
@ -159,6 +160,27 @@ public class ShopTUI {
}
}
public static void printConfirmation(Order order) {
IO.println("----------------------------------------");
IO.println(" ORDER CONFIRMATION ");
IO.println("----------------------------------------");
IO.println("Customer: " + order.getCustomerName());
IO.println("Delivery address: " + order.getAddress());
IO.println("----------------------------------------");
IO.println("Your order:");
for (CartPosition pos : order.getPositions()) {
IO.println(" " + pos.getProduct().getName() + " x " + pos.getQuantity());
}
IO.println("----------------------------------------");
IO.println(String.format("Product net value: %6.2f €", cart.getTotalNet()));
IO.println(String.format("Final total product value: %6.2f €", cart.getTotalGross()));
IO.println("----------------------------------------");
IO.println("Thank you for your order, lil bro!");
IO.println("----------------------------------------");
}
public static void checkout() {
if (cart.isEmpty()) {
IO.println("Cart is empty!");
@ -169,30 +191,13 @@ public class ShopTUI {
String name = IO.readln("Name: ");
String address = IO.readln("Delivery address: ");
for (CartPosition pos : cart.getPositions()) {
Product prod = pos.getProduct();
int quantity = pos.getQuantity();
if (prod.getStock() < quantity) {
IO.println("Error: " + prod.getName() + " not enough in stock!");
IO.println("Available: " + prod.getStock() + ", Requested: " + quantity);
IO.println("Order cannot be completed!");
return;
}
}
for (CartPosition pos : cart.getPositions()) {
Product prod = pos.getProduct();
int quantity = pos.getQuantity();
onlineShop.reduceStock(prod, quantity);
}
Order order = new Order(name, address, cart.getPositions());
order.printConfirmation();
Order successOrder = onlineShop.checkOutLogic(cart, name,address);
if(successOrder == null){
IO.println("Order not successful! -> not enough in stock...");
}else {
printConfirmation(successOrder);
cart.clear();
IO.println("Cart has been cleared. You can continue shopping!");
}
}
}