Dateien nach "/" hochladen

implementierung clear des warenkorbs und bestellung des Kunden
main
Fatima Zehra Ulu 2025-12-15 06:46:41 +01:00
parent 4a85b58c9e
commit ad85a4aeba
4 changed files with 126 additions and 60 deletions

View File

@ -3,7 +3,7 @@ import Shop.Products;
import java.util.ArrayList; import java.util.ArrayList;
public class Cart { public class Cart {
private ArrayList<Products> cartContents; private static ArrayList<Products> cartContents;
public Cart() { public Cart() {
cartContents = new ArrayList<Products>(); cartContents = new ArrayList<Products>();
@ -11,10 +11,10 @@ public class Cart {
//Adding stuff to Cart (adding stuff to cart means removing from stock) //Adding stuff to Cart (adding stuff to cart means removing from stock)
public boolean AddCart(Products prod){ public static boolean AddCart(Products prod, int amount){
if (prod.getStock() > 0) { // checking if product is in stock if (prod.getStock() > 0) { // checking if product is in stock
cartContents.add(prod); // adding to cart cartContents.add(prod); // adding to cart
prod.setStock(prod.getStock() - 1); // removing from stock prod.setStock(prod.getStock() - amount); // removing from stock
return true; return true;
} }
return false; return false;
@ -31,10 +31,10 @@ public class Cart {
} }
//removing stuff from cart (means adding back to stock) //removing stuff from cart (means adding back to stock)
public boolean RemoveCart(Products prod){ public boolean RemoveCart(Products prod, int amount){
if ( NumberOfArticels(prod) > 0) { // checking if product is in stock if ( NumberOfArticels(prod) > 0) { // checking if product is in stock
if(cartContents.remove(prod)){ // removing from cart if(cartContents.remove(prod)){ // removing from cart
prod.setStock(prod.getStock() + 1); // adding back to stock prod.setStock(prod.getStock() + amount); // adding back to stock
return true; return true;
} }
} }
@ -140,4 +140,5 @@ public class Cart {
return amount; return amount;
} }
} }

View File

@ -1,16 +1,17 @@
package Shop; package Shop;
import Shop.Cart;
import Shop.Products;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
import static Shop.Shop.cart;
public class OnlineShop { public class OnlineShop {
ArrayList<Products> warehouse; ArrayList<Products> warehouse;
public OnlineShop throws FileNotFoundException { //readFile-Methode public Object OnlineShop() throws FileNotFoundException { //readFile-Methode
warehouse = new ArrayList<>(); warehouse = new ArrayList<>();
//Einlesen //Einlesen
@ -20,63 +21,121 @@ public class OnlineShop {
while (scan.hasNextLine()) { //Solange da noch Zeilen im Dokument sind... while (scan.hasNextLine()) { //Solange da noch Zeilen im Dokument sind...
String Line = scan.nextLine().trim(); String Line = scan.nextLine().trim();
if (Line.length() == 5) { while (scan.hasNextLine()) {
String[] Parts = Line.split(","); String line = scan.nextLine().trim();
String[] Parts = line.split(",");
int productID = Integer.parseInt(Parts[0]); // ANNAHME: Die CSV-Zeile hat 6 Spalten (ID, Name, Gewicht, NetWorth, Tax, Stock)
String Name = Parts[1]; if (Parts.length == 6) {
double weight = Double.parseDouble(Parts[2]); try {
double netWorth = Double.parseDouble(Parts[3]); int productID = Integer.parseInt(Parts[0].trim()); // ID ist jetzt da
double tax = Double.parseDouble(Parts[4]); String Name = Parts[1].trim();
int stock = Integer.parseInt(Parts[5]); double weight = Double.parseDouble(Parts[2].trim());
double netWorth = Double.parseDouble(Parts[3].trim());
Products prod = new Products(Name, weight, netWorth, tax, stock, productID); double tax = Double.parseDouble(Parts[4].trim()); // z.B. 7.0 oder 19.0
warehouse.add(prod); int stock = Integer.parseInt(Parts[5].trim());
}}
Products prod = new Products(Name, weight, netWorth, tax, stock, productID);
warehouse.add(prod);
} catch (NumberFormatException e) {
System.err.println("Fehler beim Parsen numerischer Daten in Zeile: " + Line);
}
}
}
scan.close(); scan.close();
} }
public String[] ProductList() {
String[] productList = new String [warehouse.size()]; class OrderCustomer {
for (int i = 0; i < warehouse.size(); i++) { private String CustomerName;
productList[i] = warehouse.get(i).toString(); private String Adress;
private Cart cart;
private double wholeAmount;
public OrderCustomer(String Name, String Adress, Cart cart, double wholeAmount) {
this.CustomerName = Name;
this.Adress = Adress;
this.cart = cart;
this.wholeAmount = wholeAmount;
}
public String getName() {
return CustomerName;
}
public String getAdresse() {
return Adress;
}
public Cart getCart() {
return cart;
}
public double wholeAmount() {
return wholeAmount;
}
} }
return 0;
return productList;
Products getProductById(int ProductID) {
int i = 0;
while (i < warehouse.size()) {
if (warehouse.get(i).getProductID() == id) {
return warehouse.get(i);
}
i++;
} }
return null;
}
public void showProducts() {
int i = 0;
public class Order { while (i < warehouse.size()) {
private String Name; Products p = warehouse.get(i);
private String Adress; System.out.println(p.getProductID() + " | " + p.getName() + " | " +
private Cart cart; String.format("%.2f €", p.getGrossPrice()) + " | Bestand: " + p.getStock());
private double wholeAmount; i++;
public Order(String Name, String Adress, Cart cart, double wholeAmount) {
this.Name = Name;
this.Adress = Adress;
this.cart = cart;
this.wholeAmount = wholeAmount;
}
public String getName() {
return Name;
}
public String getAdresse() {
return Adress;
}
public Cart getCart() {
return cart;
}
public double wholeAmount() {
return wholeAmount;
} }
} }
OrderCustomer createOrder(String customerName, String Adress, Cart cart) {
double net = Cart.getnetWorth();
double tax = Cart.getTaxDelivery();
double gross = Cart.getGrossTotal();
Order order = new Order(name, Address, cart, net, tax, gross);
cart.clear();
return order;
}
public static class Order {
private String customerName;
private String address;
private Cart cart;
private double net;
private double tax;
private double gross;
public Order(String customerName, String address, Cart cart,
double net, double tax, double gross) {
this.customerName = customerName;
this.address = address;
this.cart = cart.copy();
this.net = net;
this.tax = tax;
this.gross = gross;
}
public void printConfirmation() {
System.out.println("===== BESTELLBESTÄTIGUNG =====");
System.out.println("Name: " + customerName);
System.out.println("Adresse: " + address);
cart.printCart();
System.out.println("Netto: " + net + " €");
System.out.println("MwSt: " + tax + " €");
System.out.println("Brutto: " + gross + " €");
}
}
}
} }

View File

@ -19,7 +19,7 @@ import java.util.*;
this.stock = stock; this.stock = stock;
this.productID = productID; this.productID = productID;
} }
public int getProductID() { public String getProductID() {
return productID; return productID;
} }
public void setProductID(int productID) { public void setProductID(int productID) {
@ -57,7 +57,7 @@ import java.util.*;
this.tax = newTax; this.tax = newTax;
} }
public int getStock() { public static int getStock() {
return this.stock; return this.stock;
} }
//neuer Bestand setzen //neuer Bestand setzen
@ -65,11 +65,16 @@ import java.util.*;
this.stock = newStock; this.stock = newStock;
} }
// Brutto Preis
public double getGrossPrice() { public double getGrossPrice() {
return this.netWorth * (1 + this.tax / 100); return this.netWorth * (1 + this.tax);
} }
public double getTaxAmount() {
return this.getGrossPrice() - this.netWorth;
}
public String toString() { public String toString() {
return return

View File

@ -11,7 +11,7 @@ import java.io.FileNotFoundException;
public class Shop { public class Shop {
private static OnlineShop shop; private static OnlineShop shop;
private static Cart cart = new Cart(); static Cart cart = new Cart();
private static Scanner sc = new Scanner(System.in); private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException { public static void main(String[] args) throws FileNotFoundException {
@ -40,7 +40,7 @@ public class Shop {
} else if (eingabe.equals("produktsuche") || eingabe.equals("suche")) { } else if (eingabe.equals("produktsuche") || eingabe.equals("suche")) {
Search(); Search();
} else if (eingabe.equals("warenkorb")) { } else if (eingabe.equals("warenkorb")) {
cart; Cart();
} else if (eingabe.equals("beenden")) { } else if (eingabe.equals("beenden")) {
System.out.println(" ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄"); System.out.println(" ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄");
System.out.println(" Auf Wiedersehen! "); System.out.println(" Auf Wiedersehen! ");
@ -143,4 +143,5 @@ public class Shop {
} }
} }
} }
}