complete all parts
This commit is contained in:
+20
-9
@@ -15,15 +15,26 @@ public class Main {
|
||||
}
|
||||
Scanner scanner = new Scanner(input);
|
||||
|
||||
// TODO:
|
||||
// - Read each line from products.csv
|
||||
// - For each line, parse productId, name, and price
|
||||
// - The format of the file is like this:
|
||||
// [productId],[name],[price]
|
||||
// - Store Product objects in the productCatalog ArrayList
|
||||
|
||||
// NOTE
|
||||
// - The data in products.csv is guaranteed to be valid.
|
||||
while (scanner.hasNextLine())
|
||||
{
|
||||
String line = scanner.nextLine();
|
||||
String[] parts = line.split(",");
|
||||
if(parts.length == 3)
|
||||
{
|
||||
try
|
||||
{
|
||||
int id = Integer.parseInt(parts[0]);
|
||||
String name = parts[1];
|
||||
double price = Double.parseDouble(parts[2]);
|
||||
productCatalog.add(new Product(id, name, price));
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
System.err.println("Invalid format in products.csv: " + line);
|
||||
}
|
||||
}
|
||||
}
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
|
||||
Reference in New Issue
Block a user