-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUndergraduateRecordSystem.java
More file actions
104 lines (88 loc) · 3.39 KB
/
UndergraduateRecordSystem.java
File metadata and controls
104 lines (88 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.*;
import java.util.Scanner;
public class UndergraduateRecordSystem {
public static final String DELIM = "\t"; //Delimiter
//Record ugrads as file from array of objects
public static void recordToFile(Ugrad[] uGrads, String fileName, boolean append) {
if(uGrads == null || fileName == null) return;
try {//If failure, goes to catch
PrintWriter fileWriter =
new PrintWriter(new FileOutputStream(fileName, append));
//Print header Data
//This line is a problem, if append is true, then we're just adding new data, and we would just need to add new uGrads, and fix the header
fileWriter.println("Number of students" + DELIM + uGrads.length);
//Print body data
for(int i = 0; i < uGrads.length; i++) { //cycle through uGrads
fileWriter.println( uGrads[i].getName() + DELIM +
uGrads[i].getID() + DELIM +
uGrads[i].getLevel());//write into file
}//for
fileWriter.close();
} catch (Exception e) {//Catch all
System.out.println(e);//Let user know
}//catch
}//recordToFile
//Return ugrads as array of objects from file
public static Ugrad[] readFromFile(String fileName) {
//Classic data parse
if(fileName == null) return null;
try {
Scanner fileScanner = new Scanner(new file(fileName)); //creates a file scanner
//TECHNIQUE
//Read the line, parse the line, do the data with the line
//Read header info
String fileLine = fileScanner.nextLine(); //Get first line
String[] splitLines = fileLine.split(DELIM); //Split line by delimiter
Ugrad[] uGrads;
//Verify file is not corrupted
//Read the header
if(splitLines.length == 2) { //Make sure header is uncorrupted
uGrads = new Ugrad[Integer.parseInt(splitLines[1])];
} else {
System.out.println("Bad file format");
return null;
}//if else
//Read the Body
int uGradCount = 0;
String name;
int id;
while(fileScanner.hasNextLine() && uGradCount < ugrads.length) { //Check to make sure correct number of ugrads
fileLine = fileScanner.nextLine(); //read line
splitLines = fileLine.split(DELIM); //split line
if(splitLines.length ==3) {//check right number of columns
//Parse it
name = splitLines[0];
id = Integer.parseInt(splitLines[1]);
uGrads[uGradCount] = new Ugrad(name,id,level);
uGradCount++;
}//if check lines
}//while
fileScanner.close();//always close
return uGrads; //return ugrads
} catch (Exception e) {//Catch all
System.out.println(e);//let user know
}//catch
return null;
}//readFromFile
//Main loop
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean quit = false;
System.out.println("Welcome to the Undergraduate Record System");
int input;
while(quit == false) {
System.out.println( "Enter 1 to create a new record\n" +
"Enter 2 to read from a file\n"+
"Enter 0 to quit.");
input = keyboard.nextInt();
}//while
}//main
}//UndergraduateRecordSystem
//File format for our system
//Use tab to delimit (\t)
//Use new line for new data (\n)
/*
//Header Data
[Number of Students \t <<# of students>>]\n
[<<name>>\t<<ID>>\t<<level>>\n]
*/