CSV Datei der Produkte einlesen

shopTUI zeigt Produktangebot und Suchfunktion
Trefferanzeige bei passenden Produkten, ansonsten Hinweis für keine
Treffer
main
eronahasani 2025-12-11 15:13:18 +01:00
parent a249f83566
commit 882f08524e
6 changed files with 225 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package backend;
public class Bestellung {
}

View File

@ -0,0 +1,47 @@
package backend;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
public class OnlineShop {
public ArrayList<Produkt> lager;
public OnlineShop() throws FileNotFoundException {
lager = new ArrayList<Produkt>();
Scanner sc = new Scanner(new File("././resources/produkte.csv"));
if (sc.hasNextLine()) sc.nextLine();
while(sc.hasNextLine() ) {
String zeile = sc.nextLine();
String[] teile = zeile.split(",");
int id = Integer.parseInt(teile[0]);
String name = teile[1];
double gewicht = Double.parseDouble(teile[2]);
double preis = Double.parseDouble(teile[3]);
int mwst = Integer.parseInt(teile[4]);
int bestand = Integer.parseInt(teile[5]);
Produkt p = new Produkt(id, name, gewicht, preis, mwst, bestand);
lager.add(p);
}
sc.close();
}
public String[] produktListe() {
String[] produkt = new String[lager.size()];
for(int i = 0; i < lager.size(); i++) {
produkt[i] = lager.get(i).toString();
}
return produkt;
}
}

View File

@ -0,0 +1,71 @@
package backend;
public class Produkt {
public int id;
public String name;
public double gewicht;
public double preis;
public int mwst;
public int bestand;
public Produkt(int id, String name, double gewicht, double preis, int mwst, int bestand) {
this.id = id;
this.name = name;
this.gewicht = gewicht;
this.preis = preis;
this.mwst = mwst;
this.bestand = bestand;
}
public String getName() {
return this.name;
}
public void setName(String neu) {
this.name = neu;
}
public String toString() {
return id + " | " + name + " | " + preis + "€";
}
public boolean equals(Produkt o) {
if (!(o instanceof Produkt))
return false;
if (o == null)
return false;
if (!this.name.equals(((Produkt)o).name)
|| this.preis != ((Produkt)o).preis)
return false;
return true;
}
@Override
public int hashCode() {
return Objects.hash(name, preis);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Produkt other = (Produkt) obj;
return Objects.equals(name, other.name)
&& Double.doubleToLongBits(preis) == Double.doubleToLongBits(other.preis);
}
}

View File

@ -0,0 +1,28 @@
package backend;
import java.util.ArrayList;
public class Warenkorb {
private ArrayList<Produkt> inhalt;
public Warenkorb() {
inhalt = new ArrayList<Produkt>();
}
public void produktHinzufügen(Produkt p) {
inhalt.add(p);
}
public double berechneGesamtpreis() {
double preis = 0;
for (Produkt p : inhalt)
preis+= p.preis;
return preis;
}
public void anzeigen() {
System.out.println("(。•◡•。) Hier ist Ihr aktueller Warenkorb: ");
for (Produkt p : inhalt)
System.out.println(p);
System.out.println("Betrag: " + berechneGesamtpreis() + "€ ૮˶ᵔᵕᵔ˶ა");
}
}

View File

@ -0,0 +1,5 @@
package backend;
public class WarenkorbPosition {
}

View File

@ -0,0 +1,69 @@
package tui;
import backend.OnlineShop;
import backend.Produkt;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class shopTUI {
private static OnlineShop shop;
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Willkommen bei Onami! („•֊•„)੭");
shop = new OnlineShop();
angebot();
suche();
System.out.println("Auf Wiedersehen! (づ˶•༝•˶)づ ");
}
public static void hauptmenü() {
// TODO: hier ein erstes Menü mit bspw.
// Produktangebot
// Produktsuche
// Warenkorbanzeige
// evtl. Bestellung (kann auch über Warenkorb realisiert werden)
// Exit
}
public static void angebot() {
System.out.println("૮ ྀིᴗ͈.ᴗ͈ ྀིა Was wir Ihnen anbieten:");
System.out.println();
String[] produkte = shop.produktListe();
for (int i = 0; i < produkte.length; i++)
System.out.println(produkte[i]);
}
public static void suche() {
System.out.println();
System.out.println("(˶˃ᵕ˂˶) Was suchen Sie? ");
String suchbegriff = sc.nextLine().toLowerCase();
if (suchbegriff.length() == 0) return;
boolean gefunden = false;
for (int artnr = 0; artnr < shop.lager.size(); artnr++) {
Produkt p = shop.lager.get(artnr);
for (int i = 0; i <= p.name.length() - suchbegriff.length(); i++) {
if (p.name.substring(i, i + suchbegriff.length()).toLowerCase().equals(suchbegriff)) {
if(!gefunden) {
System.out.println();
System.out.println();
System.out.println("(˶ˆᗜˆ˵) Wir haben folgenden Treffer: ");
}
System.out.println(p);
gefunden = true;
break;
}
}
}
if (!gefunden) System.out.println("Leider keine Treffer (˃̣̣̥ᯅ˂̣̣̥)");
}
}