Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- import java.io.*;
- public class Project3
- {
- public static void main(String[] args) throws IOException
- {
- if(args.length ==0)
- {
- System.out.println("Usage: java Project3 <filename>");
- System.exit(0);
- }
- Scanner infile = new Scanner(new File(args[0]));
- String[] array = new String[2];
- int count =0;
- int arrayDouble = doubleArraySize(array).length;
- int arrayDoubledAgain = doubleArraySize(array).length *2;
- System.out.println("Doubling array size. " + "Original size: " + array.length + " New size: " + arrayDouble);
- System.out.println("Doubling array size. " + "Original size: " + arrayDouble + " New size: " + arrayDoubledAgain);
- System.out.println("Database Capacity: " + arrayDoubledAgain);
- array = doubleArraySizeAgain(array);
- while(infile.hasNextLine())
- {
- array[count] = infile.nextLine();
- ++count;
- }
- showDatabase(array, count);
- if(args.length < 2)
- {
- System.out.print("Usage: java Project3 database [search1 search2 ALL SORTED]");
- System.exit(0);
- }
- else
- {
- for(int i =1; i < args.length; i++)
- {
- searchDatabase(array,count, args[i]);
- }
- }
- System.out.println();
- }
- public static String[] doubleArraySize(String[] arr)
- {
- String[] temp = new String[arr.length*2];
- return temp;
- }
- public static String[] doubleArraySizeAgain(String[] arr)
- {
- String[] temp = new String[arr.length*2];
- String[] doubleTemp = new String[temp.length*2];
- return doubleTemp;
- }
- public static void showDatabase(String[] arr, int count)
- {
- int doubleSize = doubleArraySize(arr).length;
- System.out.println("Database has " + count + " in-use elements.");
- System.out.printf("%s %20s%20s %-10s", "Name", "Department", "GPA"," Degree");
- System.out.println();
- System.out.printf("%s %20s%20s %-10s", "----", "----------", "---", " ------");
- for(int ii = 0; ii < arr.length; ++ii)
- {
- if(arr[ii] !=null)
- {
- showDatabaseElement(arr[ii]);
- }
- }
- System.out.println();
- System.out.println("Total number of entries: " + count);
- }
- public static void showDatabaseElement(String elem)
- {
- String[] parts = elem.split(",");
- System.out.printf("\n%-9s\t%-24s %-4s %4s", parts[0].trim(),parts[1].trim(),parts[2].trim(),parts[3].trim());
- }
- public static void searchDatabase(String[] arr, int count, String value)
- {
- count = 0;
- System.out.println("Showing results for " + value);
- if(value.equals("ALL"))
- {
- showDatabase(arr, count);
- }
- else if(value.equals("SORTED"))
- {
- Arrays.sort(arr,0,count);
- showDatabase(arr,count);
- }
- else if(value.equals("GPA"))
- {
- double gpaSum = 0;
- for(int i = 0; i < count; ++i)
- {
- String[] parts = arr[i].split(",");
- gpaSum = Double.parseDouble(parts[2].trim());
- }
- double gpaAvg = gpaSum/count;
- System.out.printf("Average GPA: %2f\n\n" + gpaAvg);
- }
- else
- {
- for(int i = 0; i < count; i++)
- {
- if(arr[i].contains(value))
- {
- showDatabaseElement(arr[i]);
- }
- }
- }
- System.out.println("Total Matches Found for " + value + "is " +
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement