Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.io.*;
  4.  
  5. public class Project3
  6. {
  7. public static void main(String[] args) throws IOException
  8. {
  9. if(args.length ==0)
  10. {
  11. System.out.println("Usage: java Project3 <filename>");
  12. System.exit(0);
  13. }
  14. Scanner infile = new Scanner(new File(args[0]));
  15. String[] array = new String[2];
  16. int count =0;
  17. int arrayDouble = doubleArraySize(array).length;
  18. int arrayDoubledAgain = doubleArraySize(array).length *2;
  19.  
  20.  
  21. System.out.println("Doubling array size. " + "Original size: " + array.length + " New size: " + arrayDouble);
  22. System.out.println("Doubling array size. " + "Original size: " + arrayDouble + " New size: " + arrayDoubledAgain);
  23. System.out.println("Database Capacity: " + arrayDoubledAgain);
  24. array = doubleArraySizeAgain(array);
  25.  
  26. while(infile.hasNextLine())
  27. {
  28. array[count] = infile.nextLine();
  29. ++count;
  30. }
  31. showDatabase(array, count);
  32. if(args.length < 2)
  33. {
  34. System.out.print("Usage: java Project3 database [search1 search2 ALL SORTED]");
  35. System.exit(0);
  36. }
  37. else
  38. {
  39. for(int i =1; i < args.length; i++)
  40. {
  41. searchDatabase(array,count, args[i]);
  42. }
  43. }
  44.  
  45. System.out.println();
  46.  
  47. }
  48.  
  49. public static String[] doubleArraySize(String[] arr)
  50. {
  51. String[] temp = new String[arr.length*2];
  52. return temp;
  53. }
  54. public static String[] doubleArraySizeAgain(String[] arr)
  55. {
  56. String[] temp = new String[arr.length*2];
  57. String[] doubleTemp = new String[temp.length*2];
  58. return doubleTemp;
  59. }
  60. public static void showDatabase(String[] arr, int count)
  61. {
  62. int doubleSize = doubleArraySize(arr).length;
  63. System.out.println("Database has " + count + " in-use elements.");
  64.  
  65. System.out.printf("%s %20s%20s %-10s", "Name", "Department", "GPA"," Degree");
  66. System.out.println();
  67. System.out.printf("%s %20s%20s %-10s", "----", "----------", "---", " ------");
  68. for(int ii = 0; ii < arr.length; ++ii)
  69. {
  70. if(arr[ii] !=null)
  71. {
  72. showDatabaseElement(arr[ii]);
  73. }
  74. }
  75. System.out.println();
  76. System.out.println("Total number of entries: " + count);
  77.  
  78. }
  79. public static void showDatabaseElement(String elem)
  80. {
  81. String[] parts = elem.split(",");
  82.  
  83. System.out.printf("\n%-9s\t%-24s %-4s %4s", parts[0].trim(),parts[1].trim(),parts[2].trim(),parts[3].trim());
  84. }
  85.  
  86. public static void searchDatabase(String[] arr, int count, String value)
  87. {
  88. count = 0;
  89.  
  90. System.out.println("Showing results for " + value);
  91.  
  92. if(value.equals("ALL"))
  93. {
  94. showDatabase(arr, count);
  95. }
  96. else if(value.equals("SORTED"))
  97. {
  98. Arrays.sort(arr,0,count);
  99. showDatabase(arr,count);
  100. }
  101. else if(value.equals("GPA"))
  102. {
  103. double gpaSum = 0;
  104. for(int i = 0; i < count; ++i)
  105. {
  106. String[] parts = arr[i].split(",");
  107. gpaSum = Double.parseDouble(parts[2].trim());
  108. }
  109. double gpaAvg = gpaSum/count;
  110. System.out.printf("Average GPA: %2f\n\n" + gpaAvg);
  111.  
  112. }
  113. else
  114. {
  115. for(int i = 0; i < count; i++)
  116. {
  117. if(arr[i].contains(value))
  118. {
  119. showDatabaseElement(arr[i]);
  120. }
  121. }
  122. }
  123. System.out.println("Total Matches Found for " + value + "is " +
  124.  
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement