Commit 8fbb0f5d authored by Wichit Sombat's avatar Wichit Sombat

finish PointOfSaleApp

parent c33e2796
package edu.ubu.csapp;
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class PointOfSaleApp {
......@@ -11,6 +12,7 @@ public class PointOfSaleApp {
int n = s.nextInt();
s.nextLine();
ArrayList<Product> products = new ArrayList<>();
for (int i=0; i<n; i++) {
System.out.printf("ชื่อสินค้า #%d: ", i+1);
String desc = s.nextLine();
......@@ -19,6 +21,28 @@ public class PointOfSaleApp {
System.out.printf("จำนวนชิ้นของสินค้า #%d: ", i+1);
int unit = s.nextInt();
s.nextLine();
products.add(new Product(desc, price, unit));
}
saveLongFile(products);
ArrayList<Product> db = loadFromFile();
}
private static ArrayList<Product> loadFromFile() {
String fname = "products.db";
ArrayList<Product> products = new ArrayList<>();
try (ObjectInputStream oos = new ObjectInputStream(new FileInputStream(fname))) {
products = (ArrayList<Product>)oos.readObject();
return products;
} catch (Exception e) {
}
return products;
}
private static void saveLongFile(ArrayList<Product> products) {
String fname = "products.db";
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fname))) {
oos.writeObject(products);
} catch (Exception e) {
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment