Guest User

Untitled

a guest
Jun 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. import java.io.*;
  2. import javax.swing.JOptionPane;
  3. import java.util.*;
  4. import java.util.StringTokenizer;
  5.  
  6. public class Main {
  7. public static void main(String[] args) throws IOException {
  8. String fname = "data.txt"; //Read in the data file for use in the array
  9. String pass= JOptionPane.showInputDialog("Please enter the " +
  10. "password to continue:"); /*Have the user enter the password to
  11. access the file. */
  12.  
  13. checkPass(pass); // Verify that the password is correct before continuing.
  14. readData (fname); // Read data, print output and save output file.
  15.  
  16. }
  17.  
  18.  
  19. private static void checkPass (String pass)
  20. {
  21. String password= "INF260";
  22. int passCount= 0;
  23. if (pass.equals(password)) {
  24. System.out.println("The password is correct. Continuing...");
  25. }
  26. else {
  27. do {
  28. pass= JOptionPane.showInputDialog("Please re-enter the" +
  29. "password:");
  30. passCount++;
  31. } while (!pass.equals(password) && passCount < 2);
  32. if (!pass.equals(password)) {
  33. System.out.println("You have tried to enter the " +
  34. "password too many times. Exiting...");
  35. System.exit(0);
  36. }
  37. else {
  38. System.out.println("The password is correct. Continuing...");
  39. }
  40. }
  41. }
  42. public static void readData (String data) throws IOException{
  43. FileReader inputData= new FileReader (data);
  44. BufferedReader findNum= new BufferedReader (inputData);
  45. String str= findNum.readLine ();
  46.  
  47. int count=-1;
  48. int countNum= 0;
  49. double total= 0;
  50. double min= 0;
  51. double max= 0;
  52. double average= 0;
  53.  
  54. FileWriter writeFile = new FileWriter("sales.txt");
  55. PrintWriter printFile = new PrintWriter(writeFile);
  56.  
  57. while (str != null)
  58. {
  59. double num= Double.parseDouble (str);
  60. if (count == 0){
  61. countNum++; // counter of Reciepts to use
  62. }
  63. str = findNum.readLine();
  64. }
  65. double [][] input = new double [countNum][10];
  66. total= getCurrentTotal(input); /*This will get the total
  67. from the method getCurrentTotal.*/
  68. min= getCurrentMin(input); /*This will get the minimum value from
  69. the method getCurrentMin.*/
  70. max= getCurrentMax (input); /*This will get the maximum value from
  71. the method getCurrentMax.*/
  72.  
  73. average= (total / countNum); //Calculate the average.
  74. System.out.println("The List of Today's Sales:");
  75. for (int row = 0; row < input.length; row++){
  76. System.out.println ();
  77. System.out.println("Customer " + row + "t");
  78. for (int column = 0; column < input[row].length; column++){
  79. if (input [row].length < 10){
  80. System.out.println(input[row][column] + "t");
  81. str = findNum.readLine();
  82. }
  83. else{
  84. System.out.println ("There are too many receipts" +
  85. " for one Customer.n");
  86. System.exit (0);
  87. }
  88. }
  89.  
  90. }
  91.  
  92. System.out.println ("There are " + countNum + "receipts in the list.");
  93. /*This will print the total of receipts in the list.*/
  94. System.out.println ("The total of today's sales is $" + total); /*
  95. This will print the total of the sales for the day.*/
  96. System.out.println ("The average of today's sales is $" + average); /*
  97. This will print the average sale total.*/
  98. System.out.println ("The highest receipt is $" + max); /* This will print
  99. the highest sale.*/
  100. System.out.println ("The lowest receipt is $" + min); /* This will print
  101. the lowest sale.*/
  102. Date date = new Date();
  103. System.out.println ("n The current time is:" + date.toString()); /* This
  104. will print the current date and time */
  105.  
  106. }
  107.  
  108.  
  109.  
  110. public static double getCurrentTotal (double [][] input){
  111. double totalAmount = 0;
  112. for (int row = 0; row < input.length; row++){
  113. for (int column = 0; column < input [row].length; column++){
  114. totalAmount += input [row][column];
  115. }
  116. }
  117. return totalAmount;
  118. }
  119.  
  120. public static double getCurrentMin (double [][] input) {
  121. double currentMin = input[0][0];
  122. for (int row = 0; row < input.length; row++){
  123. for (int column = 0; column < input [row].length; column++){
  124. if (currentMin > input[row][column])
  125. currentMin = input[row][column];
  126. }
  127. }
  128. return currentMin;
  129. }
  130.  
  131. public static double getCurrentMax (double [][] input){
  132. double currentMax = input[0][0];
  133. for (int row = 0; row < input.length; row++){
  134. for (int column = 0; column < input [row].length; column++){
  135. if (currentMax < input[row][column]){
  136. currentMax = input[row][column];
  137. }
  138. }
  139. }
  140. return currentMax;
  141. }
  142. }
  143.  
  144. // from your main method
  145. String fname = "data.txt";
  146. readData (fname);
  147.  
  148. // the method being called
  149. public static void readData (String data[][]){
  150. BufferedReader br = new BufferedReader(new FileReader(data));
Add Comment
Please, Sign In to add comment