Add main program loop
parent
68d378d9ac
commit
d3127da4a5
|
|
@ -1,7 +1,60 @@
|
|||
package org.example;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
HashMap<Product, Integer> products = loadProductsFromFile("resources/produkte.csv");
|
||||
Shop shop = new Shop(products);
|
||||
TUI tui = new TUI(shop);
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (true) {
|
||||
System.out.print("> ");
|
||||
String input = scanner.nextLine();
|
||||
tui.processInput(input);
|
||||
if (tui.exit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static HashMap<Product, Integer> loadProductsFromFile(String path) {
|
||||
HashMap<Product, Integer> products = new HashMap<>();
|
||||
ArrayList<String> lines;
|
||||
try {
|
||||
lines = readFile(path);
|
||||
} catch (FileNotFoundException e) {
|
||||
lines = new ArrayList<>();
|
||||
}
|
||||
for (String line : lines) {
|
||||
String[] tokens = line.split(",");
|
||||
int id = Integer.parseInt(tokens[0]);
|
||||
String name = tokens[1];
|
||||
float weight = Float.parseFloat(tokens[2]);
|
||||
float price = Float.parseFloat(tokens[3]);
|
||||
float vatPortion = Float.parseFloat(tokens[4]);
|
||||
int stock = Integer.parseInt(tokens[5]);
|
||||
products.put(new Product(id, name, weight, price, vatPortion), stock);
|
||||
}
|
||||
return products;
|
||||
}
|
||||
|
||||
public static ArrayList<String> readFile(String path) throws FileNotFoundException {
|
||||
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
Scanner sc = new Scanner(new File(path));
|
||||
|
||||
while (sc.hasNextLine()) {
|
||||
lines.add(sc.nextLine());
|
||||
}
|
||||
|
||||
sc.close();
|
||||
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue