Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. enum itemType{Chair, Table, Lamp}
  2.  
  3. public class Purchase {
  4. private String line = "";
  5. private itemType itemType;
  6. private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
  7. private Date testDate;
  8. private String itemPrice;
  9. private Vector<Purchase> purchaseList;
  10.  
  11.  
  12. public Purchase(String s) throws ParseException{
  13. String[] purchaseLine = s.split(";");
  14. setItemType(purchaseLine[0]);
  15. this.testDate = sdf.parse(purchaseLine[1]);
  16. setItemPrice(purchaseLine[2]);
  17.  
  18.  
  19. }
  20.  
  21. public void createList(){
  22. purchaseList = new Vector<Purchase>();
  23. }
  24.  
  25. public void setItemType(String item){
  26. try{
  27. this.itemType = task2Test.itemType.valueOf(item);
  28. }
  29. catch(IllegalArgumentException e){
  30. e.getMessage();
  31. }
  32. }
  33. public void setItemPrice(String s){
  34. int itemPrice = Integer.parseInt(s);
  35. if(itemPrice >= 1 && itemPrice <= 10){
  36. this.itemPrice = String.valueOf(itemPrice);
  37. }
  38. }
  39.  
  40. public void printPurchase(){
  41. System.out.println(itemPrice);
  42. System.out.println(itemType);
  43. System.out.println(testDate);
  44. }
  45. public String printAll(){
  46. String str="";
  47. for(int i = 0; i < purchaseList.size(); i++){
  48. String s;
  49. //System.out.println(this.purchaseList.get(i).itemPrice);
  50. //System.out.println(this.purchaseList.get(i).itemType);
  51. //System.out.println(this.purchaseList.get(i).testDate);
  52. s = purchaseList.get(i).itemType + " " + purchaseList.get(i).itemPrice + " " + this.testDate + "\n";
  53. str = str + s ;
  54. }
  55. return str;
  56. }
  57. public void readFromFile(String file) throws ParseException, IOException{
  58. String line;
  59.  
  60. BufferedReader bf = new BufferedReader(new FileReader(file));
  61.  
  62. while((line = bf.readLine()) != null){
  63. Purchase p = new Purchase(line);
  64. addItem(p);
  65. }
  66. bf.close();
  67. }
  68. public void saveData() throws IOException{
  69. BufferedWriter bw = new BufferedWriter(new FileWriter("foo.txt"));
  70.  
  71. bw.write(printAll());
  72. bw.newLine();
  73. bw.close();
  74. }
  75.  
  76. public void saveData2(String p) throws IOException{
  77. FileOutputStream fileOut =
  78. new FileOutputStream("employee.txt");
  79. ObjectOutputStream out = new ObjectOutputStream(fileOut);
  80. out.writeObject(p);
  81. out.close();
  82. fileOut.close();
  83. }
  84. public void readData2(String file) throws IOException, ClassNotFoundException{
  85. //FileReader f = new FileReader(file);
  86. FileInputStream input = new FileInputStream(file);
  87. ObjectInputStream in = new ObjectInputStream(input);
  88. System.out.println(in.readObject());
  89. in.close();
  90. input.close();
  91. }
  92. public void addItem(Purchase p){
  93. this.purchaseList.add(p);
  94. }
  95. public static void main(String[] args) throws ParseException, IOException, ClassNotFoundException{
  96. Purchase p = new Purchase("Chair;21.09.2012;9");
  97. Purchase d = new Purchase("Table;21.09.2012;9");
  98.  
  99. p.createList();
  100. p.addItem(p);
  101. p.addItem(d);
  102. //p.readFromFile("test.txt");
  103. //p.printPurchase();
  104. String str = p.printAll();
  105. //System.out.println(str);
  106. p.saveData();
  107. p.saveData2(str);
  108. p.readData2("employee.txt");
  109. }
  110.  
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement