Fakultas Ilmu Komputer UI

Commit 8e201394 authored by Bagus Prabowo's avatar Bagus Prabowo
Browse files

Add course to timetable CSV implementation

parent 5b30711f
......@@ -5,6 +5,7 @@ import Softeng.Group3.SiJadwal.model.Timetable;
import Softeng.Group3.SiJadwal.repository.CourseList;
import Softeng.Group3.SiJadwal.Main;
import Softeng.Group3.SiJadwal.repository.TimetableList;
import Softeng.Group3.SiJadwal.repository.CSVTimetable;
import java.util.HashMap;
import java.util.List;
......@@ -22,8 +23,9 @@ public class Student extends Role {
System.out.println("6. Create a timetable");
System.out.println("7. See the list of timetable");
System.out.println("8. Add Course to a timetable");
System.out.println("9. Find a timetable");
System.out.println("10. Delete a timetable");
System.out.println("9. Delete a Course in a timetable");
System.out.println("10. Find a timetable");
System.out.println("11. Delete a timetable");
}
@Override
......@@ -44,7 +46,7 @@ public class Student extends Role {
timetableName = in.next();
if(timetableList.getTimetable(timetableName, Main.currentUser) != null){
System.out.println("The user does not exist");
System.out.println("The table already exists.");
return;
}
......@@ -99,17 +101,22 @@ public class Student extends Role {
coursesPicked = in.next().split(",");
System.out.println("Picked: ");
for (String s: coursesPicked) {
System.out.println(s + " " + courseList.getCourseList().get(Integer.parseInt(s) - 1).getCourseName());
System.out.println(s + ". " + courseList.getCourseList().get(Integer.parseInt(s) - 1).getCourseName());
timetableList.getTimetable(timetableName, Main.currentUser).addCourse(
courseList.getCourseList().get(Integer.parseInt(s) - 1));
}
System.out.println("Timetable created: ");
System.out.println(timetableList.getTimetable(timetableName, Main.currentUser).getTimetableName());
List<Course> addCourse = timetableList.getTimetable(timetableName, Main.currentUser).getCourses();
int j = 1;
for (Course c: timetableList.getTimetable(timetableName, Main.currentUser).getCourses()) {
System.out.println(j++ + " " + c.getCourseName());
for (Course c: addCourse) {
System.out.println(j++ + ". " + c.getCourseName());
}
CSVTimetable.addCourseTimetable(Main.currentUser.getUsername(), timetableName, addCourse);
break;
} catch (NullPointerException e) {
......@@ -117,8 +124,51 @@ public class Student extends Role {
break;
}
case 9:
try {
System.out.println("Delete a course in a timetable");
System.out.println("===============================");
System.out.println("Timetable Name?");
timetableName = in.next();
Timetable table = timetableList.getTimetable(timetableName, Main.currentUser);
if(table == null){
System.out.println("The table doesn't exists");
return;
}
System.out.println("Pick one or more courses to delete (Comma separated): ");
courseList.listToString(courseList.getCourseList());
coursesPicked = in.next().split(",");
System.out.println("Picked: ");
for (String s: coursesPicked) {
System.out.println(s + ". " + courseList.getCourseList().get(Integer.parseInt(s) - 1).getCourseName());
timetableList.getTimetable(timetableName, Main.currentUser).deleteCourse(
courseList.getCourseList().get(Integer.parseInt(s) - 1));
}
System.out.println("Timetable updated: ");
System.out.println(timetableList.getTimetable(timetableName, Main.currentUser).getTimetableName());
List<Course> delCourse = timetableList.getTimetable(timetableName, Main.currentUser).getCourses();
int j = 1;
for (Course c: delCourse) {
System.out.println(j++ + ". " + c.getCourseName());
}
break;
} catch (NullPointerException e) {
System.out.println("Timetable not found");
break;
}
case 10:
System.out.println("Find timetable by name: ");
try {
String timetableName = in.next();
......@@ -131,7 +181,7 @@ public class Student extends Role {
break;
}
case 10:
case 11:
System.out.println("Please enter the timetable name that you want to delete: ");
String tableDel = in.next();
......
......@@ -44,10 +44,8 @@ public class Timetable {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
public void addCourse(Course course) { courses.add(course); }
public void deleteCourse(Course course){ courses.remove(course); }
}
......@@ -38,20 +38,43 @@ public class CSVTimetable {
}
}
public static void updateTimetable(String Uname, String newType){
public static void addCourseTimetable(String name, String table, List<Course> newCourse){
try{
BufferedReader check = new BufferedReader(new FileReader("Timetable.csv"));
String row;
List<String> newList = new ArrayList<>();
while((row = check.readLine()) != null){
String[] usersWritten = row.split(",");
if(usersWritten[0].equalsIgnoreCase(Uname)){
usersWritten[1] = newType;
String[] tableWritten = row.split(",");
int counter = 0;
if(tableWritten[0].equalsIgnoreCase(name) && tableWritten[1].equalsIgnoreCase(table)){
for(int i = 2; i < tableWritten.length; i++, counter++){
tableWritten[i] = newCourse.get(i-2).getCourseName();
}if(counter <= newCourse.size()-1){
newList.add(tableWritten[0]);
for(int i = 1; i < tableWritten.length; i++) {
newList.add(",");
newList.add(tableWritten[i]);
}for(int i = counter; i < newCourse.size(); i++){
newList.add(",");
newList.add(newCourse.get(i).getCourseName());
}
newList.add("\n");
}else{
newList.add(tableWritten[0]);
for(int i = 1; i < tableWritten.length; i++) {
newList.add(",");
newList.add(tableWritten[i]);
}
newList.add("\n");
}
}else{
newList.add(tableWritten[0]);
for(int i = 1; i < tableWritten.length; i++) {
newList.add(",");
newList.add(tableWritten[i]);
}
newList.add("\n");
}
newList.add(usersWritten[0]);
newList.add(",");
newList.add(usersWritten[1]);
newList.add("\n");
} check.close();
FileWriter reader = new FileWriter("Timetable.csv");
for (String s : newList) {
......@@ -92,4 +115,32 @@ public class CSVTimetable {
e.printStackTrace();
}
}
public static void deleteUserTimetable(String name){
try{
BufferedReader check = new BufferedReader(new FileReader("Timetable.csv"));
String row;
List<String> newList = new ArrayList<>();
while((row = check.readLine()) != null){
String[] tableWritten = row.split(",");
if(!tableWritten[0].equalsIgnoreCase(name)){
newList.add(tableWritten[0]);
for(int i = 1; i < tableWritten.length; i++) {
newList.add(",");
newList.add(tableWritten[i]);
}
newList.add("\n");
}
} check.close();
FileWriter reader = new FileWriter("Timetable.csv");
for (String s : newList) {
reader.append(s);
}reader.flush();
reader.close();
}
catch (IOException e) {
System.out.println("File not found.");
e.printStackTrace();
}
}
}
package Softeng.Group3.SiJadwal.repository;
import Softeng.Group3.SiJadwal.actor.User;
import Softeng.Group3.SiJadwal.model.Course;
import java.sql.Time;
......
......@@ -34,6 +34,9 @@ public class UserList {
public void updateRole(String username, RoleType role){
User newUser = getUser(username);
if((newUser.getRole().getRoleName() == RoleType.STUDENT) && (role != RoleType.STUDENT)){
CSVTimetable.deleteUserTimetable(newUser.getUsername());
}
int index = userList.indexOf(newUser);
User updated = new User();
updated.setRole(role);
......@@ -44,6 +47,9 @@ public class UserList {
public void deleteUser(String Uname){
User delUser = getUser(Uname);
if(delUser.getRole().getRoleName() == RoleType.STUDENT){
CSVTimetable.deleteUserTimetable(delUser.getUsername());
}
userList.remove(delUser);
CSVUser.deleteUser(Uname);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment