Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum itemType{Chair, Table, Lamp}
- public class Purchase {
- private String line = "";
- private itemType itemType;
- private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
- private Date testDate;
- private String itemPrice;
- private Vector<Purchase> purchaseList;
- public Purchase(String s) throws ParseException{
- String[] purchaseLine = s.split(";");
- setItemType(purchaseLine[0]);
- this.testDate = sdf.parse(purchaseLine[1]);
- setItemPrice(purchaseLine[2]);
- }
- public void createList(){
- purchaseList = new Vector<Purchase>();
- }
- public void setItemType(String item){
- try{
- this.itemType = task2Test.itemType.valueOf(item);
- }
- catch(IllegalArgumentException e){
- e.getMessage();
- }
- }
- public void setItemPrice(String s){
- int itemPrice = Integer.parseInt(s);
- if(itemPrice >= 1 && itemPrice <= 10){
- this.itemPrice = String.valueOf(itemPrice);
- }
- }
- public void printPurchase(){
- System.out.println(itemPrice);
- System.out.println(itemType);
- System.out.println(testDate);
- }
- public String printAll(){
- String str="";
- for(int i = 0; i < purchaseList.size(); i++){
- String s;
- //System.out.println(this.purchaseList.get(i).itemPrice);
- //System.out.println(this.purchaseList.get(i).itemType);
- //System.out.println(this.purchaseList.get(i).testDate);
- s = purchaseList.get(i).itemType + " " + purchaseList.get(i).itemPrice + " " + this.testDate + "\n";
- str = str + s ;
- }
- return str;
- }
- public void readFromFile(String file) throws ParseException, IOException{
- String line;
- BufferedReader bf = new BufferedReader(new FileReader(file));
- while((line = bf.readLine()) != null){
- Purchase p = new Purchase(line);
- addItem(p);
- }
- bf.close();
- }
- public void saveData() throws IOException{
- BufferedWriter bw = new BufferedWriter(new FileWriter("foo.txt"));
- bw.write(printAll());
- bw.newLine();
- bw.close();
- }
- public void saveData2(String p) throws IOException{
- FileOutputStream fileOut =
- new FileOutputStream("employee.txt");
- ObjectOutputStream out = new ObjectOutputStream(fileOut);
- out.writeObject(p);
- out.close();
- fileOut.close();
- }
- public void readData2(String file) throws IOException, ClassNotFoundException{
- //FileReader f = new FileReader(file);
- FileInputStream input = new FileInputStream(file);
- ObjectInputStream in = new ObjectInputStream(input);
- System.out.println(in.readObject());
- in.close();
- input.close();
- }
- public void addItem(Purchase p){
- this.purchaseList.add(p);
- }
- public static void main(String[] args) throws ParseException, IOException, ClassNotFoundException{
- Purchase p = new Purchase("Chair;21.09.2012;9");
- Purchase d = new Purchase("Table;21.09.2012;9");
- p.createList();
- p.addItem(p);
- p.addItem(d);
- //p.readFromFile("test.txt");
- //p.printPurchase();
- String str = p.printAll();
- //System.out.println(str);
- p.saveData();
- p.saveData2(str);
- p.readData2("employee.txt");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement