Advertisement
chrisenoch

ProductIO.java

Oct 3rd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. package murach.data;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. import murach.business.*;
  7.  
  8. public class ProductIO {
  9.  
  10. public static Product getProduct(String code, String filepath) {
  11. try {
  12. File file = new File(filepath);
  13. BufferedReader in
  14. = new BufferedReader(
  15. new FileReader(file));
  16.  
  17. String line = in.readLine();
  18. while (line != null) {
  19. StringTokenizer t = new StringTokenizer(line, "|");
  20. String productCode = t.nextToken();
  21. if (code.equalsIgnoreCase(productCode)) {
  22. String description = t.nextToken();
  23. double price = Double.parseDouble(t.nextToken());
  24. Product p = new Product();
  25. p.setCode(code);
  26. p.setDescription(description);
  27. p.setPrice(price);
  28. in.close();
  29. return p;
  30. }
  31. line = in.readLine();
  32. }
  33. in.close();
  34. return null;
  35. } catch (IOException e) {
  36. System.err.println(e);
  37. return null;
  38. }
  39. }
  40.  
  41. public static ArrayList<Product> getProducts(String filepath) {
  42. ArrayList<Product> products = new ArrayList<Product>();
  43. File file = new File(filepath);
  44. try {
  45. BufferedReader in
  46. = new BufferedReader(
  47. new FileReader(file));
  48.  
  49. String line = in.readLine();
  50. while (line != null) {
  51. StringTokenizer t = new StringTokenizer(line, "|");
  52. String code = t.nextToken();
  53. String description = t.nextToken();
  54. String priceAsString = t.nextToken();
  55. double price = Double.parseDouble(priceAsString);
  56. Product p = new Product();
  57. p.setCode(code);
  58. p.setDescription(description);
  59. p.setPrice(price);
  60. products.add(p);
  61. line = in.readLine();
  62. }
  63. in.close();
  64. return products;
  65. } catch (IOException e) {
  66. System.err.println(e);
  67. return null;
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement