Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. /**
  5. * @author Andrew Moe
  6. * Class PopMachine simulates simple functionality of a PopMachine
  7. */
  8. public class PopMachine
  9. {
  10. public static void main(String[] args) throws IOException
  11. {
  12. final int MAX_SODAS = 10;
  13. // Create a new array named popMachine that will be able
  14. // to hold MAX_SODAS Soda objects
  15. Soda[] popMachine = new Soda[MAX_SODAS];
  16. int numSodas = fillMachine(popMachine, "Sodas.txt");
  17. // Loop to reduce the quantity of each soda in the popMachine
  18. // by one. Note that the machine may not be filled. Use
  19. // the reduce method.
  20.  
  21. saveMachine(popMachine, numSodas, "SodasChanged.txt");
  22. System.out.println("Machine closed.");
  23.  
  24. }
  25. /**
  26. * fillMachine loads the machine with sodas from the specified file.
  27. * @param popMachine The array to fill with sodas
  28. * @param fileName The name of the file containing soda information
  29. * @return The number of sodas read into the machine
  30. * @throws IOException
  31. */
  32. public static int fillMachine(Soda[] popMachine,
  33. String fileName) throws IOException
  34. {
  35. int numSodas = 0;
  36. File inFile = new File("Sodas.txt");
  37. Scanner input = new Scanner(inFile);
  38. if (!inFile.exists())
  39. {
  40. System.out.println(inFile.getName() + " does not exist.");
  41. System.exit(1);
  42. }
  43. else
  44. {
  45. while(input.hasNextLine())
  46. {
  47. String name = input.nextLine();
  48. double price = Double.parseDouble(input.nextLine());
  49. int quantity = Integer.parseInt(input.nextLine());
  50. int ounces = Integer.parseInt(input.nextLine());
  51. popMachine[numSodas] = new Soda(name, price, quantity, ounces);
  52. ++numSodas;
  53. }
  54. }
  55. input.close();
  56. return numSodas;
  57.  
  58. // Check if the file exists. If not, inform the user that the
  59. // file does not exist and exit with an error code of 1.
  60. // Open the file for reading and read each Soda into the array
  61. // Stop reading when there are no more lines to process or the
  62. // array is filled with sodas.
  63. // Note: In each loop iteration, you will need to add a new Soda
  64. // object to the array, get the 4 lines of data associated with
  65. // that soda, and set the soda object with that data. You will need
  66. // to keep track of the number of sodas read, and that can be used
  67. // as an index into the array.
  68. // Close the file
  69. // Return the number of sodas read
  70. }
  71. /**
  72. * saveMachine writes the popMachine data back to file in the same format
  73. * as originally read.
  74. * @param popMachine The array that contains the sodas
  75. * @param numSodas The number of sodas in the array
  76. * @param fileName The name of the file to write the soda information
  77. * @throws IOException
  78. */
  79. public static void saveMachine(Soda[] popMachine, int numSodas,
  80. String fileName) throws IOException
  81. {
  82. try(
  83. PrintWriter output = new PrintWriter(new File("SodasChanged.txt")))
  84. {
  85. for (int i = 0; i < numSodas; ++i)
  86. {
  87. output.println(popMachine[i]);
  88. }
  89. }
  90. // Open the fileName for writing. No need to check for an error.
  91. // Create a DecimalFormat class that will output the price with
  92. // always 1 digit before the decimal point and 2 digits after.
  93. // Loop to write the soda data back to file in the same original format.
  94. // Close the output file.
  95. }
  96. }
  97.  
  98. /**
  99. * @author Andrew Moe
  100. * Class Soda sets the name, price, quantity and number of ounces of a soda
  101. * object. It also contains a method to reduce the quantity of sodas.
  102. */
  103. public class Soda
  104. {
  105. private String name;
  106. private double price;
  107. private static int quantity;
  108. private int ounces;
  109.  
  110. public Soda()
  111. {
  112. setName("");
  113. setPrice(0);
  114. setQuantity(0);
  115. setOunces(0);
  116. }
  117.  
  118. public Soda(String newName, double newPrice, int newQuantity, int newOunces)
  119. {
  120. setName(newName);
  121. setPrice(newPrice);
  122. setQuantity(newQuantity);
  123. setOunces(newOunces);
  124. }
  125.  
  126. public void setName(String newName)
  127. {
  128. name = newName;
  129. }
  130.  
  131. public String getName()
  132. {
  133. return name;
  134. }
  135.  
  136. public void setPrice(double newPrice)
  137. {
  138. if(newPrice > 0)
  139. price = newPrice;
  140. }
  141.  
  142. public double getPrice()
  143. {
  144. return price;
  145. }
  146.  
  147. public void setQuantity(int newQuantity)
  148. {
  149. if (newQuantity > 0)
  150. quantity = newQuantity;
  151. }
  152.  
  153. public int getQuantity()
  154. {
  155. return quantity;
  156. }
  157.  
  158. public void setOunces(int newOunces)
  159. {
  160. if (newOunces > 0)
  161. ounces = newOunces;
  162. }
  163.  
  164. public int getOunces()
  165. {
  166. return ounces;
  167. }
  168. /**
  169. * Method reduce takes a reduction parameter and reduces the
  170. * quantity of sodas.
  171. * @param reduction reduces quantity.
  172. */
  173. public static void reduce(int reduction)
  174. {
  175. if (reduction > 0 && quantity >= reduction)
  176. {
  177. quantity = quantity - reduction;
  178. }
  179. }
  180. @Override
  181. public String toString()
  182. {
  183. return String.format("%s%n%1.2f%n%d%n%d", getName(), getPrice(), getQuantity(), getOunces());
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement