Guest User

Untitled

a guest
Jun 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. ## InventoryProgram class
  2. package inventory;
  3.  
  4. import java.util.*;
  5. import java.io.*;
  6.  
  7.  
  8.  
  9. public class InventoryProgram {
  10. static ArrayList<String> lines = new ArrayList<String>(100);
  11. static ArrayList<InventoryItem> items = new ArrayList<InventoryItem>(100);
  12.  
  13.  
  14. public static void main(String args[]) {
  15. int totalItems = 0;
  16. double totalValue = 0.0;
  17. try{readFile("..\\InventoryIn.txt");}
  18. catch(IOException ioe){System.err.println("File Not Found");}
  19. setUpInventory(lines);
  20. for (int i = 0; i<items.size(); i++) {
  21. totalItems += items.get(i).getQuantity();
  22. totalValue += items.get(i).getTotalValue();
  23. }
  24. for (int i = 0; i<items.size(); i++) {
  25. if(items.get(i).isOverStocked(totalItems) && items.get(i).isOverValued(totalValue)){
  26. items.get(i).markDown(.5);
  27. System.out.println (items.get(i));
  28. }
  29. else if(items.get(i).isOverStocked(totalItems) || items.get(i).isOverValued(totalValue)){
  30. items.get(i).markDown(.25);
  31. System.out.println (items.get(i));
  32. }
  33. else
  34. System.out.println (items.get(i));
  35. }
  36.  
  37. }
  38.  
  39. public static void readFile(String fileName) throws IOException{
  40. File inputFile = new File(fileName);
  41. Scanner sc = new Scanner(inputFile);
  42. while(sc.hasNextLine())
  43. lines.add(sc.nextLine());
  44. }
  45.  
  46. public static void setUpInventory(ArrayList<String> info){
  47. for (int i = 0; i<info.size(); i++) {
  48. StringTokenizer st = new StringTokenizer(info.get(i));
  49. InventoryItem item = new InventoryItem(
  50. Double.parseDouble(st.nextToken().substring(1)), st.nextToken(), Integer.parseInt(st.nextToken()), st.nextToken());
  51. items.add(item);
  52. }
  53. }
  54. }
  55. ## InventoryItem Object Class
  56. /**
  57. * This class represents a single item in the
  58. * store. Based on the 2007 SJCNY Programming
  59. * Competition.
  60. */
  61. package inventory;
  62. import java.text.*;
  63.  
  64. public class InventoryItem {
  65. //fields:
  66. double price, salePrice, totalValue;
  67. int quantity;
  68. String manufacturer, name;
  69. boolean onSale;
  70. DecimalFormat formPrice = new DecimalFormat("#########.00");
  71.  
  72. //constructor:
  73. public InventoryItem(double p, String m,
  74. int q, String name) {
  75. price = p;
  76. quantity = q;
  77. manufacturer = m;
  78. this.name = name;
  79. onSale = false;
  80. }
  81.  
  82. /**
  83. * Produces a one line string that gives the data
  84. * for this class. If the item is on sale, then
  85. * this will be indicated by ****
  86. * For Example:
  87. * "Abit Motherboard 75.95 200 **** Sale item, price: 37.98 ****"
  88. * "Canon Camera 299.99 1"
  89. */
  90. public String toString() {
  91. String output = this.manufacturer + " " + this.name + " " + this.price + " " + this.quantity;
  92. if(!this.onSale)
  93. return output;
  94. else return output.concat(" **** Sale item, price: " + formPrice.format(salePrice) + " ****");
  95. }
  96.  
  97. /**
  98. * Reduces the price by the given percentage.
  99. * For example: if the initial price is 100
  100. * and percent is 10, then the new price is 90.
  101. * If initial price is 500, percent is 50, then
  102. * the new price will be 250.
  103. * The new price is stored in salePrice (field).
  104. * percent is in the decimal format
  105. * for example: if the percent mark down is 25
  106. * double percent = .75
  107. */
  108. public void markDown(double percent) {
  109. this.salePrice = (1 - percent) * price;
  110. this.onSale = true;
  111. }
  112. public int getQuantity(){
  113. return quantity;
  114. }
  115.  
  116. public double getTotalValue(){
  117. totalValue = price * quantity;
  118. return totalValue;
  119. }
  120.  
  121. public boolean isOverStocked(double totalQuantity){
  122. double percent = 100.0 * (this.quantity / totalQuantity);
  123. if (percent > 10){
  124. return percent > 10;
  125. }
  126. return false;
  127. }
  128.  
  129. public boolean isOverValued(double totalValue){
  130. double percent = 100.0 * (this.price / totalValue);
  131. if (percent > 5){
  132. return percent > 5;
  133. }
  134. return false;
  135. }
  136. //testing only
  137. public static void main(String args[]) {
  138. InventoryItem box = new InventoryItem(75.92, "Abit", 500, "Mobo");
  139. box.markDown(.25);
  140. System.out.println (box);
  141. }
  142.  
  143.  
  144. }
Add Comment
Please, Sign In to add comment