Advertisement
bbbbas

Product.java

May 16th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. package problem10;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8.  
  9. /**
  10. *
  11. * @author bas
  12. */
  13.  
  14. public class Product {
  15.  
  16. private String name;
  17. private double price;
  18.  
  19. public Product(String name, double price) {
  20. setName(name);
  21. setPrice(price);
  22. }
  23.  
  24. public String getName() {
  25. return name;
  26. }
  27.  
  28. public void setName(String name) {
  29. if (name.equals(" ")) {
  30. this.name = "not entered";
  31. } else {
  32. this.name = name;
  33. }
  34. }
  35.  
  36. public double getPrice() {
  37. return price;
  38. }
  39.  
  40. public void setPrice(double price) {
  41. if (price < 0) {
  42. this.price = 0;
  43. } else {
  44. this.price = price;
  45. }
  46. }
  47.  
  48. public static void writeBill() {
  49. try {
  50. BufferedWriter out = new BufferedWriter(new FileWriter(
  51. "Output.txt"));
  52. out.write(String.format("%.2f", calculateTotalBill()));
  53. out.close();
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. }
  58.  
  59. public static Product[] extractProducts() {
  60. FileReader fr;
  61.  
  62. try {
  63. fr = new FileReader("Products.txt");
  64.  
  65. BufferedReader br = new BufferedReader(fr);
  66.  
  67. int numLines = countLines("Products.txt");
  68. Product[] products = new Product[numLines];
  69. String[] info = new String[2];
  70. for (int i = 0; i < numLines; i++) {
  71. String current = br.readLine();
  72. info = current.split(" ");
  73.  
  74. products[i] = new Product(" ", 0);
  75. products[i].setName(info[0].toString());
  76. products[i].setPrice(Double.parseDouble(info[1]));
  77. }
  78. fr.close();
  79. return products;
  80. } catch (Exception e) {
  81.  
  82. e.printStackTrace();
  83. }
  84. return null;
  85. }
  86.  
  87. public static double calculateTotalBill() {
  88. FileReader fr;
  89. double bill = 0;
  90. try {
  91. fr = new FileReader("Order.txt");
  92. BufferedReader br = new BufferedReader(fr);
  93. int numLines = countLines("Order.txt");
  94.  
  95. Product[] products = extractProducts();
  96.  
  97. for (int i = 0; i < numLines; i++) {
  98. String[] orderInfo = br.readLine().split(" ");
  99. for (int j = 0; j < products.length; j++) {
  100. if (orderInfo[1].equals(products[j].getName())) {
  101. bill += products[j].getPrice()
  102. * Double.parseDouble(orderInfo[0]);
  103. }
  104. }
  105. }
  106.  
  107. fr.close();
  108. } catch (Exception e) {
  109.  
  110. e.printStackTrace();
  111. }
  112.  
  113. return bill;
  114. }
  115.  
  116. public static int countLines(String file) throws IOException {
  117. FileReader fr = new FileReader(file);
  118. BufferedReader br = new BufferedReader(fr);
  119.  
  120. String aLine = br.readLine();
  121. int numberOfLines = 0;
  122.  
  123. while ((aLine) != null) {
  124. numberOfLines++;
  125. aLine = br.readLine();
  126. }
  127. br.close();
  128. return numberOfLines;
  129. }
  130.  
  131. public static void main(String[] args) {
  132. writeBill();
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement