Online-Shop/OnlineShop.java

141 lines
4.8 KiB
Java

package Shop;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import static Shop.Shop.cart;
public class OnlineShop {
ArrayList<Products> warehouse;
public Object OnlineShop() throws FileNotFoundException { //readFile-Methode
warehouse = new ArrayList<>();
//Einlesen
Scanner scan = new Scanner(new File("resources/produkte.csv")); //Scanner erstellen
scan.nextLine();
while (scan.hasNextLine()) { //Solange da noch Zeilen im Dokument sind...
String Line = scan.nextLine().trim();
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) {
try {
int productID = Integer.parseInt(Parts[0].trim()); // ID ist jetzt da
String Name = Parts[1].trim();
double weight = Double.parseDouble(Parts[2].trim());
double netWorth = Double.parseDouble(Parts[3].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);
warehouse.add(prod);
} catch (NumberFormatException e) {
System.err.println("Fehler beim Parsen numerischer Daten in Zeile: " + Line);
}
}
}
scan.close();
}
class OrderCustomer {
private String CustomerName;
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;
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;
while (i < warehouse.size()) {
Products p = warehouse.get(i);
System.out.println(p.getProductID() + " | " + p.getName() + " | " +
String.format("%.2f €", p.getGrossPrice()) + " | Bestand: " + p.getStock());
i++;
}
}
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 + " €");
}
}
}
}