Fehler ausbesserung implementierung der fehlenden methoden und classen im Cart hinzugefügt
main
Fatima Zehra Ulu 2025-12-15 08:56:17 +01:00
parent ad85a4aeba
commit 4c06b681bf
4 changed files with 66 additions and 50 deletions

View File

@ -9,7 +9,6 @@ public class Cart {
cartContents = new ArrayList<Products>(); cartContents = new ArrayList<Products>();
} }
//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 static boolean AddCart(Products prod, int amount){ 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
@ -45,7 +44,7 @@ public class Cart {
return cartContents.size(); return cartContents.size();
} }
public double GrossAmountProd (){ public static double GrossAmountProd(){
double gross = 0.0; double gross = 0.0;
for(Products prod: cartContents) { for(Products prod: cartContents) {
gross += prod.getGrossPrice(); gross += prod.getGrossPrice();
@ -53,7 +52,7 @@ public class Cart {
return gross; return gross;
} }
public double ContentsMass() { public static double ContentsMass() {
double weights = 0.0; double weights = 0.0;
for(Products prod: cartContents) { for(Products prod: cartContents) {
weights += prod.getWeight(); weights += prod.getWeight();
@ -70,7 +69,7 @@ public class Cart {
} }
public double deliveryCost () { public static double deliveryCost() {
double weight = ContentsMass(); double weight = ContentsMass();
double delivery = 0; double delivery = 0;
@ -97,7 +96,7 @@ public class Cart {
} }
public double taxDelivery(){ public static double taxDelivery(){
double delivery = deliveryCost (); double delivery = deliveryCost ();
double tax7perc = 0; // grosstax is 7% which means: 1/3rd of 19,95 double tax7perc = 0; // grosstax is 7% which means: 1/3rd of 19,95
@ -141,4 +140,13 @@ public class Cart {
} }
public void clear() {
for (int i = 0; !cartContents.isEmpty(); i++) {
cartContents.remove(1);
}
}
public void printCart() {
}
} }

View File

@ -6,44 +6,51 @@ 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 Object OnlineShop() throws FileNotFoundException { //readFile-Methode public Object OnlineShop() throws FileNotFoundException {
warehouse = new ArrayList<>(); warehouse = new ArrayList<>();
//Einlesen //Einlesen
Scanner scan = new Scanner(new File("resources/produkte.csv")); //Scanner erstellen Scanner scan = new Scanner(new File("resources/produkte.csv")); //Scanner erstellen
scan.nextLine(); scan.nextLine();
while (scan.hasNextLine()) { //Solange da noch Zeilen im Dokument sind... while (scan.hasNextLine()) {
String Line = scan.nextLine().trim(); String line = scan.nextLine().trim();
String[] Parts = line.split(",");
while (scan.hasNextLine()) {
String line = scan.nextLine().trim();
String[] Parts = line.split(",");
// ANNAHME: Die CSV-Zeile hat 6 Spalten (ID, Name, Gewicht, NetWorth, Tax, Stock) if (Parts.length == 6) {
if (Parts.length == 6) { int productID = Integer.parseInt(Parts[0].trim()); // ID ist jetzt da
try { String Name = Parts[1].trim();
int productID = Integer.parseInt(Parts[0].trim()); // ID ist jetzt da double weight = Double.parseDouble(Parts[2].trim());
String Name = Parts[1].trim(); double netWorth = Double.parseDouble(Parts[3].trim());
double weight = Double.parseDouble(Parts[2].trim()); double tax = Double.parseDouble(Parts[4].trim()); // z.B. 7.0 oder 19.0
double netWorth = Double.parseDouble(Parts[3].trim()); int stock = Integer.parseInt(Parts[5].trim());
double tax = Double.parseDouble(Parts[4].trim()); // z.B. 7.0 oder 19.0
int stock = Integer.parseInt(Parts[5].trim());
Products prod = new Products(Name, weight, netWorth, tax, stock, productID); Products prod = new Products(Name, weight, netWorth, tax, stock, productID);
warehouse.add(prod); warehouse.add(prod);
} catch (NumberFormatException e) { } else {
System.err.println("Fehler beim Parsen numerischer Daten in Zeile: " + Line); System.err.println("Fehler beim Parsen numerischer Daten in Zeile: " + line);
}
}
} }
scan.close();
} }
scan.close();
return null;
}
static String[] ProductList(){
ArrayList<Products> warehouse;
warehouse = new ArrayList<>();
String[] productList = new String[warehouse.size()];
for (int i = 0; i < warehouse.size(); i++) {
productList[i] = warehouse.get(i).toString();
}
return productList;
}
class OrderCustomer { class OrderCustomer {
private String CustomerName; private String CustomerName;
@ -74,13 +81,13 @@ public class OnlineShop {
return wholeAmount; return wholeAmount;
} }
} }
return 0;
Products getProductById(int ProductID) { Products getProductById(int ProductID) {
int i = 0; int i = 0;
while (i < warehouse.size()) { while (i < warehouse.size()) {
if (warehouse.get(i).getProductID() == id) { if (ProductID == warehouse.get(i).getProductID()) {
return warehouse.get(i); return warehouse.get(i);
} }
i++; i++;
@ -98,12 +105,12 @@ public class OnlineShop {
} }
} }
OrderCustomer createOrder(String customerName, String Adress, Cart cart) { OnlineShop.Order createOrder(String customerName, String Adress, Cart cart) {
double net = Cart.getnetWorth(); double net = Products.getNetWorth();
double tax = Cart.getTaxDelivery(); double tax = Cart.taxDelivery();
double gross = Cart.getGrossTotal(); double gross = Cart.GrossAmountProd();
Order order = new Order(name, Address, cart, net, tax, gross); Order order = new Order(customerName, Adress, cart, net, tax, gross);
cart.clear(); cart.clear();
return order; return order;
} }
@ -117,11 +124,10 @@ public class OnlineShop {
private double tax; private double tax;
private double gross; private double gross;
public Order(String customerName, String address, Cart cart, public Order(String customerName, String address, Cart cart, double net, double tax, double gross) {
double net, double tax, double gross) {
this.customerName = customerName; this.customerName = customerName;
this.address = address; this.address = address;
this.cart = cart.copy(); this.cart = cart;
this.net = net; this.net = net;
this.tax = tax; this.tax = tax;
this.gross = gross; this.gross = gross;
@ -138,4 +144,4 @@ public class OnlineShop {
} }
} }
} }
}

View File

@ -5,7 +5,7 @@ import java.util.*;
private double grossWorth; private double grossWorth;
private String Name; private String Name;
private double weight; private double weight;
private double netWorth; private static double netWorth;
private double tax; private double tax;
private int productID; private int productID;
private int stock; private int stock;
@ -19,7 +19,7 @@ import java.util.*;
this.stock = stock; this.stock = stock;
this.productID = productID; this.productID = productID;
} }
public String getProductID() { public int getProductID() {
return productID; return productID;
} }
public void setProductID(int productID) { public void setProductID(int productID) {
@ -42,7 +42,7 @@ import java.util.*;
return weight; return weight;
} }
public double getNetWorth() { public static double getNetWorth() {
return netWorth; return netWorth;
} }
public void setNetWorth(double netWorth) { public void setNetWorth(double netWorth) {
@ -57,8 +57,8 @@ import java.util.*;
this.tax = newTax; this.tax = newTax;
} }
public static int getStock() { public int getStock() {
return this.stock; return this.stock;
} }
//neuer Bestand setzen //neuer Bestand setzen
public void setStock(int newStock) { public void setStock(int newStock) {

View File

@ -40,7 +40,10 @@ 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(); System.out.println(" ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄");
System.out.println(" Auf Wiedersehen! ");
System.out.println("____________________________");
} 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! ");
@ -56,11 +59,11 @@ public class Shop {
public static void Sale() { public static void Sale() {
System.out.println("\n\n\n Produkte: \n"); System.out.println("\n\n\n Produkte: \n");
Products[] produkte = shop.ProductList(); String[] products = OnlineShop.ProductList();
for (int i = 0; i < produkte.length; i++) { for (int i = 0; i < products.length;i++) {
Products prod = shop.warehouse.get(i); Products prod = shop.warehouse.get(i);
System.out.printf(prod.getProductID(), prod.getName(), prod.getGrossPrice()); System.out.printf(prod.getProductID() +" " + prod.getName()+""+ prod.getGrossPrice());
} }
System.out.println("\nWählen Sie ein Produkt anhand der Artikelnummer für Ihren Warenkorb aus \noder geben Sie 'Hauptmenü' an, wenn Sie zurück wollen: "); System.out.println("\nWählen Sie ein Produkt anhand der Artikelnummer für Ihren Warenkorb aus \noder geben Sie 'Hauptmenü' an, wenn Sie zurück wollen: ");
@ -132,7 +135,6 @@ public class Shop {
mainMenu(); mainMenu();
} }
} }
} }
} }
} }