package ShopManagement; import java.io.*; import java.util.ArrayList; public class ProductList { ArrayList list; public ProductList(){ this.list = new ArrayList(); } /*public void InputData() { ArrayList products = new ArrayList<>(); products.add(new Product(1, "Thuc an cho meo", 50000, 0)); products.add(new Product(2, "Cat ve sinh", 150000, 0)); products.add(new Product(3, "Ao cho cho", 40000, 0)); products.add(new Product(4, "Thuc an cho cho", 50000, 0)); products.add(new Product(5, "Chau cat ve sinh cho meo", 170000, 0)); products.add(new Product(6, "Vong co cho meo", 55000, 0)); products.add(new Product(7, "Chen an cho meo 2 cai", 50000, 0)); products.add(new Product(8, "Sua tam cho meo", 98000, 0)); products.add(new Product(9, "Cay meo leo", 900000, 0)); products.add(new Product(10, "Luoc chai long meo", 70000, 0)); SaveToFile("ProductList.txt",products); }*/ public void SaveToFile() { try { File f = new File("ProductList.dat"); FileOutputStream fos = new FileOutputStream(f); ObjectOutputStream ous= new ObjectOutputStream(fos); ous.writeObject( new Product(1, "Thuc an cho meo", 50000, 0)); ous.writeObject( new Product(2, "Cat ve sinh", 150000, 0)); ous.writeObject( new Product(3, "Ao cho cho", 40000, 0)); ous.writeObject( new Product(4, "Thuc an cho cho", 50000, 0)); ous.writeObject( new Product(5, "Chau cat ve sinh cho meo", 170000, 0)); ous.writeObject( new Product(6, "Vong co cho meo", 55000, 0)); ous.writeObject( new Product(7, "Chen an cho meo 2 cai", 50000, 0)); ous.writeObject( new Product(8, "Sua tam cho meo", 98000, 0)); ous.writeObject( new Product(9, "Cay meo leo", 900000, 0)); ous.writeObject( new Product(10, "Luoc chai long meo", 70000, 0)); fos.close(); ous.close(); System.out.println("Done"); } catch (Exception e) { System.out.println("Failed: " +e.getMessage()); } // File f = new File(fileName); // { // try // { // FileWriter fw = new FileWriter(f); // PrintWriter pw = new PrintWriter(fw); // for(Product stud:products) // { // pw.println(stud); // } // pw.close(); // fw.close(); // } // catch (IOException e) // { // System.out.println(e); // } // } } public void readFile(){ try{ FileInputStream fis = new FileInputStream("ProductList.dat"); ObjectInputStream ois = new ObjectInputStream(fis); Product p = (Product)ois.readObject(); while (p != null){ this.list.add(p); try{ p = (Product)ois.readObject(); }catch (Exception e){ p = null; } } }catch (Exception e) { } } public void Output() { for(Product st:list) { System.out.println(st.toString()); } } public Product getItemById(int id){ int pos = getPosById(id); if (pos < 0 ) return null; return list.get(pos); } public int getPosById(int id) { for (int i=0; i