Fakultas Ilmu Komputer UI

Commit 7b8c673e authored by Bagus Prabowo's avatar Bagus Prabowo
Browse files

Merge branch 'bagus2' into 'master'

CRUD course and user finalization

See merge request !5
parents eb3d0081 c990f6e9
package Softeng.Group3.SiJadwal.actor;
import Softeng.Group3.SiJadwal.model.Course;
import Softeng.Group3.SiJadwal.repository.CourseList;
import Softeng.Group3.SiJadwal.repository.UserList;
import Softeng.Group3.SiJadwal.Main;
......@@ -21,7 +23,10 @@ public class Admin extends Role {
@Override
public void exclusiveActions() {
System.out.println("6. Add Course");
System.out.println("7. Change user role");
System.out.println("7. Update Course");
System.out.println("8. Delete Course");
System.out.println("9. Change User Role");
System.out.println("10. Delete User");
}
@Override
......@@ -33,9 +38,10 @@ public class Admin extends Role {
System.out.println("Course Name?");
courseName = in.next();
for(int i = 0; i < courseList.getCourseList().size(); i++){
if(courseName == courseList.getCourseList().get(i).getCourseName());
System.out.println("Course already exists");
return;
if(courseName.equalsIgnoreCase(courseList.getCourseList().get(i).getCourseName())){
System.out.println("Course already exists");
return;
}
}
System.out.println("Lecturer Name?");
lecturer = in.next();
......@@ -48,6 +54,41 @@ public class Admin extends Role {
break;
case 7:
System.out.println("Course you want to update: ");
courseName = in.next();
CourseList updateCourse = new CourseList();
if(updateCourse.getCourse(courseName) == null){
System.out.println("The course does not exist");
return;
}
System.out.println("Lecturer Name?");
lecturer = in.next();
System.out.println("Start Time? hour:minute:second");
startTime = Time.valueOf(in.next());
System.out.println("End Time? hour:minute:second");
endTime = Time.valueOf(in.next());
courseList.updateCourse(courseName, lecturer, startTime, endTime);
System.out.println(courseName + " Course Updated");
break;
case 8:
CourseList delCourse = new CourseList();
System.out.println("Please enter the course name that you want to delete: ");
String courseDel = in.next();
if(delCourse.getCourse(courseDel) == null){
System.out.println("The user does not exist");
return;
}
delCourse.deleteCourse(courseDel);
System.out.println("Course deleted.");
break;
case 9:
UserList userList = new UserList();
System.out.println("Please enter the username that you want to change the role: ");
......@@ -68,6 +109,22 @@ public class Admin extends Role {
userList.updateRole(user, RoleType.intToRole(newUserType));
break;
case 10:
UserList userListDel = new UserList();
System.out.println("Please enter the username that you want to delete: ");
String userDel = in.next();
if(userListDel.getUser(userDel) == null){
System.out.println("The user does not exist");
return;
}
userListDel.deleteUser(userDel);
System.out.println("User deleted.");
break;
default:
}
}
......
......@@ -3,6 +3,8 @@ package Softeng.Group3.SiJadwal.repository;
import java.io.*;
import java.sql.Time;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import Softeng.Group3.SiJadwal.actor.RoleType;
......@@ -46,6 +48,7 @@ public class CSVOperator {
if(!usersCSV.containsKey(Uname)){
reader.append(Uname).append(",").append(Type).append("\n");
}
reader.flush();
reader.close();
check.close();
}
......@@ -70,6 +73,7 @@ public class CSVOperator {
reader.append(name).append(",").append(lecturer).append(",")
.append(start.toString()).append(",").append(end.toString()).append("\n");
}
reader.flush();
reader.close();
check.close();
}
......@@ -79,4 +83,120 @@ public class CSVOperator {
}
}
public static void updateUser(String Uname, String newType){
try{
BufferedReader check = new BufferedReader(new FileReader("Users.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;
}
newList.add(usersWritten[0]);
newList.add(",");
newList.add(usersWritten[1]);
newList.add("\n");
} check.close();
FileWriter reader = new FileWriter("Users.csv");
for (String s : newList) {
reader.append(s);
}reader.flush();
reader.close();
}
catch (IOException e) {
System.out.println("File not found.");
e.printStackTrace();
}
}
public static void deleteUser(String Uname){
try{
BufferedReader check = new BufferedReader(new FileReader("Users.csv"));
String row;
List<String> newList = new ArrayList<>();
while((row = check.readLine()) != null){
String[] usersWritten = row.split(",");
if(!usersWritten[0].equalsIgnoreCase(Uname)){
newList.add(usersWritten[0]);
newList.add(",");
newList.add(usersWritten[1]);
newList.add("\n");
}
} check.close();
FileWriter reader = new FileWriter("Users.csv");
for (String s : newList) {
reader.append(s);
}reader.flush();
reader.close();
}
catch (IOException e) {
System.out.println("File not found.");
e.printStackTrace();
}
}
public static void updateCourse(String name, String lecturer, Time start, Time end){
try{
BufferedReader check = new BufferedReader(new FileReader("Courses.csv"));
String row;
List<String> newList = new ArrayList<>();
while((row = check.readLine()) != null){
String[] usersWritten = row.split(",");
if(usersWritten[0].equalsIgnoreCase(name)){
usersWritten[1] = lecturer;
usersWritten[2] = start.toString();
usersWritten[3] = end.toString();
}
newList.add(usersWritten[0]);
newList.add(",");
newList.add(usersWritten[1]);
newList.add(",");
newList.add(usersWritten[2]);
newList.add(",");
newList.add(usersWritten[3]);
newList.add("\n");
} check.close();
FileWriter reader = new FileWriter("Courses.csv");
for (String s : newList) {
reader.append(s);
}reader.flush();
reader.close();
}
catch (IOException e) {
System.out.println("File not found.");
e.printStackTrace();
}
}
public static void deleteCourse(String name){
try{
BufferedReader check = new BufferedReader(new FileReader("Courses.csv"));
String row;
List<String> newList = new ArrayList<>();
while((row = check.readLine()) != null){
String[] usersWritten = row.split(",");
if(!usersWritten[0].equalsIgnoreCase(name)){
newList.add(usersWritten[0]);
newList.add(",");
newList.add(usersWritten[1]);
newList.add(",");
newList.add(usersWritten[2]);
newList.add(",");
newList.add(usersWritten[3]);
newList.add("\n");
}
} check.close();
FileWriter reader = new FileWriter("Courses.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;
......@@ -32,6 +33,24 @@ public class CourseList {
CSVOperator.writeCourse(courseName, lecturer, startTime, endTime);
}
public void updateCourse(String courseName, String lecturer, Time startTime, Time endTime){
Course prevCourse = getCourse(courseName);
int index = CourseList.courseList.indexOf(prevCourse);
Course newCourse = new Course();
newCourse.setCourseName(courseName);
newCourse.setLecturer(lecturer);
newCourse.setStartTime(startTime);
newCourse.setEndTime(endTime);
courseList.set(index, newCourse);
CSVOperator.updateCourse(courseName, lecturer,startTime, endTime);
}
public void deleteCourse(String name){
Course delCourse = getCourse(name);
courseList.remove(delCourse);
CSVOperator.deleteCourse(name);
}
public void listToString(List<Course> courseList){
int i = 1;
for (Course course: courseList) {
......
......@@ -39,6 +39,13 @@ public class UserList {
updated.setRole(role);
updated.setUsername(username);
userList.set(index, updated);
CSVOperator.updateUser(username, RoleType.RoletoString(role));
}
public void deleteUser(String Uname){
User delUser = getUser(Uname);
userList.remove(delUser);
CSVOperator.deleteUser(Uname);
}
public void listToString(List<User> userList){
......
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