Implementierung einer Such-Funktion

main
Laura Kalkbrenner 2025-12-14 02:42:03 +01:00
parent 8be9721223
commit 9e7b077820
4 changed files with 82 additions and 9 deletions

View File

@ -10,6 +10,7 @@ import java.util.Scanner;
public class OnlineShop {
private static ArrayList<String> LagerList;
public String [][] Start() throws FileNotFoundException {
LagerList=readFile();
String [][]arr= produktArray(LagerList);
@ -18,6 +19,8 @@ public class OnlineShop {
}
public String[][] produktArray(ArrayList Lagerlist) throws FileNotFoundException {
String[] Lager = new String[LagerList.size()];
for (int i = 0; i < LagerList.size(); i++){
@ -54,6 +57,16 @@ public class OnlineShop {
return ProduktListe;
}
public ArrayList<Produkt> ÜbertrageListe() throws FileNotFoundException {
LagerList=readFile();
String [][]arr= produktArray(LagerList);
String[][] produktArray = Start();
ArrayList<Produkt>ProduktListe=erstelleListe(arr);
return ProduktListe;
}
public static ArrayList<String> readFile() throws FileNotFoundException {
Scanner sc = new Scanner(new File("Shop/resources/produkte.csv")); //Vom Sudoku übernommen
@ -66,5 +79,25 @@ public class OnlineShop {
return LagerList;
}
public boolean[] aufLager() throws FileNotFoundException { //TODO Prüfen ob ich die Methode überhaupt brauche
ArrayList<Produkt> ProduktListe= ÜbertrageListe();
boolean[] aufLager= new boolean[ProduktListe.size()];
for(int i=0;i<ProduktListe.size();i++) {
if(ProduktListe.get(i).Bestand >0)
aufLager[i]=true;
else
aufLager[i]=false;
}
return aufLager;
}
public Produkt suchProdukt(String suche) throws FileNotFoundException {
ArrayList<Produkt>ProduktListe= ÜbertrageListe();
for(Produkt p : ProduktListe){
if(p.name.equalsIgnoreCase(suche))
return p;
}
return null;
}
}

View File

@ -29,7 +29,7 @@ public class Produkt {
return preis-berechneMwst();
}
public boolean equals(Object o) {
/* public boolean equals(Object o) {
if (!(o instanceof Produkt))
return false;
@ -38,5 +38,6 @@ public class Produkt {
return false;
return true;
}
}*/
}

View File

@ -24,5 +24,11 @@ public class Warenkorb {
return preis;
}
public double berechneGesamtGewicht() {
double gewicht = 0;
for (Produkt p : inhalt)
gewicht+= p.Gewicht*1000; //für kg für den Versand
return gewicht;
}
}

View File

@ -1,20 +1,18 @@
package pack;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ShopTUI {
private static OnlineShop shop;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Willkommen zum DanDan-Shop!");
shop= new OnlineShop();
boolean cont = true;
while (cont) {
@ -33,7 +31,6 @@ public class ShopTUI {
IO.println("3.Mein Warenkorb");
IO.println("4. Bestellungen");
IO.println("5. Exit ");
Scanner sc = new Scanner(System.in);
IO.print(">>");
String eingabe = sc.nextLine();
switch (eingabe) {
@ -41,7 +38,7 @@ public class ShopTUI {
produktangebot();
break;
case "2": case "suche": case "Suche": case"produktsuche":
//Todo suchfunktiom
produktsuche();
break;
case "3": case "warenkorb": case"Warenkorb":
//Todo warenkorb
@ -61,7 +58,6 @@ public class ShopTUI {
public static void produktangebot() throws FileNotFoundException {
System.out.println("Unser Produktangebot:");
String[][] produkt = shop.Start();
IO.println();
@ -81,6 +77,43 @@ public class ShopTUI {
IO.println();
}
} public static void produktsuche() throws FileNotFoundException {
IO.print("Ich suche: ");
String suche = sc.nextLine();
int count=0;
Produkt vlt=null;
boolean[] aufLager= shop.aufLager();
ArrayList<Produkt> ProduktListe= shop.ÜbertrageListe();
IO.println();
Produkt gefunden= shop.suchProdukt(suche);
if(gefunden==null) {
/* for(int i=0;i<ProduktListe.size();i++) {
for (int j = 0; j < 5&& j<ProduktListe.get(i).name.length(); j++) {
if (suche.charAt(j) == ProduktListe.get(i).name.charAt(j)){
count++;
if(count==5){
vlt=ProduktListe.get(i);
}
}else {
return;
}
} count=0;
}
if(count==5) {
IO.println("Meinten Sie vielleicht "+ vlt.name + " " +vlt.preis+ "€ ?");
IO.println("Unser Bestand beträgt"+ vlt.Bestand);
}*/
IO.println("Das gesuchte Produkt wurde leider nicht gefunden");
} else {
IO.println("Hier ist das Produkt: " + gefunden.name + " " + gefunden.preis + " €");
if(gefunden.Bestand ==0){
IO.println("Das Produkt ist aber leider zur Zeit nicht verfügbar");
//Todo hier trimen oder splite damit der user nicht sowas wie 7l hinzufügen muss
}else
IO.println("Wollen sie das Produkt zum Warenkorb hinzufügen?");
//Todo hinzufügfen ermöglichen
}
}
}