-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUndergraduateRecordSystem.java
More file actions
85 lines (82 loc) · 2.17 KB
/
UndergraduateRecordSystem.java
File metadata and controls
85 lines (82 loc) · 2.17 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
import java.io.*;
import java.util.Scanner;
public class UndergraduateRecordSystem {
public static final String DELIM = "\t"; //Delimeter
public static void recordToFile(Ugrad[] uGrads, String fileName, boolean append)
{
if (uGrads == null || fileName == null)
{
return;
}
try
{
PrintWriter fileWriter = new PrintWriter(new FileOutputStream(fileName,append));
//Print header data
fileWriter.println("Number of students"+DELIM+uGrads.length);
//print body data
for(int i = 0; i < uGrads.length; i++)
{
fileWriter.println(uGrads[i].getName()+DELIM+uGrads[i].getID()+DELIM+uGrads[i].getLevel());
}
fileWriter.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
public static Ugrad[] readFromFile(String fileName)
{
if(fileName == null)
return null;
try
{
Scanner fileScanner = new Scanner(new File(fileName));
//Read header info
String fileLine = fileScanner.nextLine();
String[] splitLines = fileLine.split(DELIM);
Ugrad[] ugrads;
if(splitLines.length == 2) //check if the file is correct
{
ugrads = new Ugrad[Integer.parseInt(splitLines[1])];
}
else
{
System.out.println("Bad file format");
return null;
}
int uGradCount = 0;
while(fileScanner.hasNextLine() && uGradCount < ugrads.length) /// could have more lines than expected/more students than reported
{
fileLine = fileScanner.nextLine(); //read line
splitLines = fileLine.split(DELIM); //split line
if (splitLines.length == 3) //check line formatting - if wrong ignore it
{
//Parse it
String name = splitLines[0];
int ID = Integer.parseInt(splitLines[1]);
int level = Integer.parseInt(splitLines[2]);
ugrads[uGradCount] = new Ugrad(name,ID,level);
uGradCount++;
}
}
fileScanner.close();
return ugrads;
}
catch(Exception e)
{
System.out.println(e);
}
return null;
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean quit = false;
System.out.println("Welcome to the undergraduate record system");
while(quit == false)
{
System.out.println("Enter 1 to create a new record\nEnter 2 to ");
}
}
}