Advertisement
bbbbas

Product.java

May 16th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. package problem9;
  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 toFile() {
  49. try {
  50. BufferedWriter out = new BufferedWriter(new FileWriter(
  51. "Output-Problem9.txt"));
  52. Product[] products = sortArray();
  53. for (int i = 0; i < products.length; i++) {
  54. out.write(String.format("%.2f %s", products[i].getPrice(),
  55. products[i].getName()));
  56. out.newLine();
  57. }
  58.  
  59. out.close();
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. }
  64.  
  65. public static Product[] extractProducts() {
  66. FileReader fr;
  67.  
  68. try {
  69. fr = new FileReader("Input.txt");
  70.  
  71. BufferedReader br = new BufferedReader(fr);
  72.  
  73. int numLines = countLines("Input.txt");
  74. Product[] products = new Product[numLines];
  75. String[] info = new String[2];
  76. for (int i = 0; i < numLines; i++) {
  77. String current = br.readLine();
  78. info = current.split(" ");
  79.  
  80. products[i] = new Product(" ", 0);
  81. products[i].setName(info[0].toString());
  82. products[i].setPrice(Double.parseDouble(info[1]));
  83. }
  84. fr.close();
  85. return products;
  86. } catch (Exception e) {
  87.  
  88. e.printStackTrace();
  89. }
  90. return null;
  91. }
  92.  
  93. public static Product[] sortArray() {
  94.  
  95. Product[] toSort = extractProducts();
  96.  
  97. // using bubbleSort algorithm to sort the array
  98. int n = toSort.length;
  99. boolean swapped = true;
  100. while (swapped) {
  101. swapped = false;
  102. for (int i = 1; i < n; i++) {
  103. if (toSort[i].getPrice() < toSort[i - 1].getPrice()) {
  104. swap(toSort, i, i - 1);
  105. swapped = true;
  106. }
  107. }
  108. n = n - 1;
  109. }
  110. return toSort;
  111. }
  112.  
  113. public static void swap(Product[] x, int pos1, int pos2) {
  114.  
  115. Product temp = x[pos1];
  116. x[pos1] = x[pos2];
  117. x[pos2] = temp;
  118. }
  119.  
  120. public static int countLines(String file) throws IOException {
  121. FileReader fr = new FileReader(file);
  122. BufferedReader br = new BufferedReader(fr);
  123.  
  124. String aLine = br.readLine();
  125. int numberOfLines = 0;
  126.  
  127. while ((aLine) != null) {
  128. numberOfLines++;
  129. aLine = br.readLine();
  130. }
  131. br.close();
  132. return numberOfLines;
  133. }
  134.  
  135. public static void main(String[] args) {
  136. toFile();
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement