develop #1
+13
-11
@@ -13,21 +13,23 @@ public class Main {
|
|||||||
if (input == null) {
|
if (input == null) {
|
||||||
throw new IOException("products.csv not found");
|
throw new IOException("products.csv not found");
|
||||||
}
|
}
|
||||||
Scanner scanner = new Scanner(input);
|
|
||||||
|
|
||||||
// TODO:
|
try (Scanner scanner = new Scanner(input)) {
|
||||||
// - Read each line from products.csv
|
while (scanner.hasNextLine()) {
|
||||||
// - For each line, parse productId, name, and price
|
String line = scanner.nextLine();
|
||||||
// - The format of the file is like this:
|
if (line.isBlank()) continue;
|
||||||
// [productId],[name],[price]
|
|
||||||
// - Store Product objects in the productCatalog ArrayList
|
|
||||||
|
|
||||||
// NOTE
|
String[] parts = line.split(",");
|
||||||
// - The data in products.csv is guaranteed to be valid.
|
int id = Integer.parseInt(parts[0].trim());
|
||||||
|
String name = parts[1].trim();
|
||||||
|
double price = Double.parseDouble(parts[2].trim());
|
||||||
|
|
||||||
|
productCatalog.add(new Product(id, name, price));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args) throws IOException {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
loadProducts();
|
loadProducts();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user