, поэтому я выполняю это задание, и после его завершения одна часть кода не выполняется должным образом.
Склад
class Warehouse {
private final Merchandise[] merchandise;
Warehouse(Merchandise[] merchandise) {
this.merchandise = merchandise;
}
public static Warehouse fromCsv(String file) {
In.open(file);
String material = In.readFile();
In.close();
String lines[] = material.split("\n");
Merchandise[] merchandise = new Merchandise[lines.length];
for(int i=0; i<merchandise.length; i++) {
String[] parts = lines[i].split(",");
Article article = null;
if(parts[0].equals("instrument")) {
article = new Instrument(parts[1],Double.parseDouble(parts[2]),parts[3],Integer.parseInt(parts[4]));
}else{
article = new Accessory(parts[1],Double.parseDouble(parts[2]),parts[3],parts[4]);
}
merchandise[i] = new Merchandise(article,Integer.parseInt(parts[5]));
}
return new Warehouse(merchandise);
}
public Merchandise[] getMerchandise() {
return merchandise.clone();
}
public Merchandise findMerchandise(String name) {
Merchandise found = null;
for(Merchandise the : merchandise) {
if(the.getArticle().getName().equals(name)) {
found = the;
break;
}
}
return found;
}
}
Магазин
class Shop {
private final Warehouse warehouse;
Shop(Warehouse warehouse){
this.warehouse = warehouse;
}
public void printCatalog() {
Out.println();
Out.formatln("%-30s%20s%10s", "Name" , "Price","Quantity");
String format = "%-30s%20.2f%10d";
for(Merchandise the : warehouse.getMerchandise()) {
Out.formatln(format,the.getArticle().getName(),the.getArticle().getPrice(),the.getQuantity());
}
}
public void printProductDescription(String name){
Merchandise merchandise = warehouse.findMerchandise(name);
Out.println();
if(merchandise != null) {
Out.println(merchandise);
}else {
Out.println("The article does not exist");
Out.println();
}
}
}
Main
class Main {
public static void main(String[] args) {
Warehouse warehouse = Warehouse.fromCsv("warehouse.csv");
Shop shop = new Shop(warehouse);
while(true) {
Out.println("Pianos & more");
Out.println("0. Print Catalogue");
Out.println("1. Search Article");
Out.println();
Out.print("Selection:");
int selection = In.readInt();
In.read();
switch(selection) {
case 0:
shop.printCatalog();
break;
case 1:
Out.print("Article name:");
String name = In.readLine();
shop.printProductDescription(name);
break;
default:
Out.println("Invalid selection");
}
}
}
}
Итак, часть, где я распечатываю каталог, работает нормально, но когда я пытаюсь найти статью, она берет имя статьи и просто выводит «статья не существует», хотя должна. Любая помощь в том, что я делаю не так?