Fakultas Ilmu Komputer UI
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
G4-SoftEng
Group4-SoftEng
Commits
426926fe
Commit
426926fe
authored
Jun 03, 2021
by
saddamonpc
Browse files
Refactor Main.java such that it uses ListIO
parent
b0977895
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/softeng/g4/Main.java
View file @
426926fe
...
...
@@ -2,6 +2,7 @@ package softeng.g4;
import
jdk.swing.interop.SwingInterOpUtils
;
import
softeng.g4.appointment.Appointment
;
import
softeng.g4.cli.ListIO
;
import
softeng.g4.course.Course
;
import
softeng.g4.course.CourseList
;
import
softeng.g4.user.*
;
...
...
@@ -87,24 +88,13 @@ public class Main {
// new Course("Fisika", "PHYS101", "Aku Dosen Fisika", 4, courseList);
//
// // Save the course
// saveList(courseList, userList);
//
ListIO.
saveList(courseList, userList);
//
// // ---------------- END OF "HARDCODED" VERSION
// PRODUCTION BUILD: Open all persistent storage
courseList
=
openCourseList
();
userList
=
openUserList
();
// // Store userList test
// try {
// FileOutputStream fos = new FileOutputStream("src/softeng/g4/userlist.txt");
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// oos.writeObject(userList);
//
// } catch (Exception ex) {
// System.out.println("Exception is caught");
// }
courseList
=
ListIO
.
openCourseList
();
userList
=
ListIO
.
openUserList
();
while
(
true
)
{
...
...
@@ -114,7 +104,7 @@ public class Main {
// EXITS THE PROGRAM AND SERIALIZE ALL LIST, IF USER COMMAND IS "EXIT"
if
(
q
.
equals
(
"EXIT"
))
{
System
.
out
.
println
(
"Thank you for using our program! Have a good day!"
);
saveList
(
courseList
,
userList
);
ListIO
.
saveList
(
courseList
,
userList
);
break
;
}
...
...
@@ -144,7 +134,7 @@ public class Main {
introStudent
(
currentStudent
);
// If we reach here, that means we found the user, and we are now logged in.
while
(!
q
.
equals
(
"LOG OUT"
))
{
saveList
(
courseList
,
userList
);
// Saves the list everytime the user is in the "Main menu"
ListIO
.
saveList
(
courseList
,
userList
);
// Saves the list everytime the user is in the "Main menu"
System
.
out
.
print
(
"Input: "
);
q
=
sc
.
nextLine
();
switch
(
q
)
{
...
...
@@ -200,7 +190,7 @@ public class Main {
introTA
(
currentTA
);
// If we reach here, that means we found the user, and we are now logged in.
while
(!
q
.
equals
(
"LOG OUT"
))
{
saveList
(
courseList
,
userList
);
// Saves the list everytime the user is in the "Main menu"
ListIO
.
saveList
(
courseList
,
userList
);
// Saves the list everytime the user is in the "Main menu"
System
.
out
.
print
(
"Input: "
);
q
=
sc
.
nextLine
();
switch
(
q
)
{
...
...
@@ -264,7 +254,7 @@ public class Main {
introAdmin
(
currentAdmin
);
// If we reach here, that means we found the user, and we are now logged in.
while
(!
q
.
equals
(
"LOG OUT"
))
{
saveList
(
courseList
,
userList
);
// Saves the list everytime the user is in the "Main menu"
ListIO
.
saveList
(
courseList
,
userList
);
// Saves the list everytime the user is in the "Main menu"
System
.
out
.
print
(
"Input: "
);
q
=
sc
.
nextLine
();
switch
(
q
)
{
...
...
@@ -375,73 +365,4 @@ public class Main {
System
.
out
.
println
();
}
// Open courselist.txt for persistent storage of list of courses
public
static
CourseList
openCourseList
()
{
try
{
// Reading the object from a file
FileInputStream
file
=
new
FileInputStream
(
"src/main/java/softeng/g4/courselist.txt"
);
ObjectInputStream
in
=
new
ObjectInputStream
(
file
);
// Method for deserialization of object
CourseList
courseList
=
(
CourseList
)
in
.
readObject
();
in
.
close
();
file
.
close
();
return
courseList
;
}
catch
(
IOException
ex
)
{
System
.
out
.
println
(
"IOException is caught"
);
}
catch
(
ClassNotFoundException
ex
)
{
System
.
out
.
println
(
"ClassNotFoundException is caught"
);
}
return
null
;
}
// Open userlist.txt for persistent storage of list of users
public
static
UserList
openUserList
()
{
try
{
// Reading the object from a file
FileInputStream
file
=
new
FileInputStream
(
"src/main/java/softeng/g4/userlist.txt"
);
ObjectInputStream
in
=
new
ObjectInputStream
(
file
);
// Method for deserialization of object
UserList
userList
=
(
UserList
)
in
.
readObject
();
in
.
close
();
file
.
close
();
return
userList
;
}
catch
(
IOException
ex
)
{
System
.
out
.
println
(
"IOException is caught"
);
}
catch
(
ClassNotFoundException
ex
)
{
System
.
out
.
println
(
"ClassNotFoundException is caught"
);
}
return
null
;
}
// Store all list
public
static
void
saveList
(
CourseList
courseList
,
UserList
userList
)
{
// Store courseList
try
{
FileOutputStream
fos
=
new
FileOutputStream
(
"src/main/java/softeng/g4/courselist.txt"
);
ObjectOutputStream
oos
=
new
ObjectOutputStream
(
fos
);
oos
.
writeObject
(
courseList
);
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
"Exception is caught"
);
}
// Store userList
try
{
FileOutputStream
fos
=
new
FileOutputStream
(
"src/main/java/softeng/g4/userlist.txt"
);
ObjectOutputStream
oos
=
new
ObjectOutputStream
(
fos
);
oos
.
writeObject
(
userList
);
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
"Exception is caught"
);
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment