-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndividualCourse.java
More file actions
156 lines (109 loc) · 4.05 KB
/
Copy pathIndividualCourse.java
File metadata and controls
156 lines (109 loc) · 4.05 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* IndividualCourse.java
* GradeBook v.1.0 by Adam Grabowski
* 25 January 2009
* Purpose: Contains list of students (Student class objects)
* Requires: Student.java
*/
import java.io.*;
import java.util.List;
import java.util.ArrayList;
class IndividualCourse
{
private String courseName, studName;
private List <Student> students;
public IndividualCourse ( String theName ) throws IOException
{
this.courseName = theName;
students = new ArrayList <Student>();
}
public void createStudentList() throws IOException {
//Reads file and assign each student to the course
BufferedReader studFile = new BufferedReader(new FileReader (courseName + "/students.txt"));
while(true)
{
studName = studFile.readLine();
if(studName==null){break;}
Student m = new Student(studName);
students.add(m);
}
studFile.close(); //Close File
}
public String getCourseName(){
return this.courseName;
}
public void setCourseName(String n){
this.courseName = n;
}
/////////////////////////////// STUDENTS OF THE COURSE///////////////////////////
//SHOW STUDENTS LIST
public void showStudents(){
for(int i=1; i < students.size();i++){
Output.println("[" + i + "]" + students.get(i).getStudentName());
}
}
//CREATE NEW STUDENT
public void createStudent(Student k) throws IOException
{
//Add student to file of students
PrintWriter courseFile = new PrintWriter(new FileWriter (courseName + "/students.txt",true));
courseFile.println(k.getStudentName());
courseFile.close();
//Create student file in course folder
File f;
f=new File(courseName + "/" + k.getStudentName() + ".txt");
if(!f.exists()){
f.createNewFile();
}
//Add first entry
PrintWriter stFile = new PrintWriter(new FileWriter (courseName + "/" + k.getStudentName() + ".txt",true));
stFile.println("null");
stFile.println("-1");
stFile.println("-1");
stFile.close();
students.add(k);//add student at the end of the list
} // END CREATE NEW STUDENT
//DELETE STUDENT
public void deleteStudent(int index) throws IOException
{
//Delete student file
boolean success = (new File(courseName + "/" + students.get(index).getStudentName() + ".txt")).delete();
if (!success) {
Output.println("Student file cannot be deleted!");
}
students.remove(index); //removes student object form the list
//Put new array to the file
PrintWriter delStudentFile = new PrintWriter(new FileWriter (courseName + "/students.txt"));
for(int j = 0; j < students.size(); j++)
{
delStudentFile.println(students.get(j).getStudentName());
}
delStudentFile.close();
}
//START OF EDIT STUDENT NAME
public void editStudentName(int nm, String st) throws IOException
{
// Rename file name
File file = new File(courseName + "/" + students.get(nm).getStudentName() + ".txt");
File file2 = new File(courseName + "/" + st + ".txt");
boolean success = file.renameTo(file2);
if (!success) {
Output.println("Student file cannot be renamed!");
}
students.get(nm).setStudentName(st); //Edit element of array
PrintWriter editStudentFile = new PrintWriter(new FileWriter (courseName + "/students.txt"));
for(int j = 0; j < students.size(); j++)
{
editStudentFile.println(students.get(j).getStudentName());
}
editStudentFile.close();
} //ENDOF EDIT STUDENT NAME
//GETS SELECTED OBJECT FROM THE LIST
public Student getStudentObject(int no) throws IOException {
students.get(no).createAssignmentsList(courseName);
return students.get(no);
}
//GET STUDENT LIST SIZE
public int getSize(){
return students.size();
}
} //end class IndividualCourse