Fakultas Ilmu Komputer UI

Commit 33f59e94 authored by Bagus Prabowo's avatar Bagus Prabowo
Browse files

Merge branch 'bagus2' into 'master'

CSV addition for timetable and overall CRUD fix

See merge request !7
parents 7f50d30f 705113f1
rpl,ade,1:01:01,1:01:02
adpro,daya,5:05:05,7:07:07
daa,there,4:04:04,13:01:01
daa,there,4:04:04,13:01:01
\ No newline at end of file
st,t1,rpl,adpro,daa
......@@ -78,7 +78,7 @@ public class Student extends Role {
System.out.println(i++ + ". " + c.getCourseName());
}
timetableList.addTimetable(newTimetable);
timetableList.addTimetable(timetableName, newTimetable);
break;
case 7:
......@@ -136,17 +136,18 @@ public class Student extends Role {
}
case 10:
TimetableList delTable = new TimetableList();
System.out.println("Please enter the timetable name that you want to delete: ");
String tableDel = in.next();
if(delTable.getTimetable(tableDel, Main.currentUser) == null){
Timetable delTable = timetableList.getTimetable(tableDel, Main.currentUser);
if(delTable == null){
System.out.println("The timetable does not exist");
return;
}
delTable.deleteTimetable("tableDel");
timetableList.deleteTimetable(delTable);
System.out.println("Timetable deleted.");
break;
......
......@@ -41,19 +41,17 @@ public class CSVCourse {
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();
String[] courseWritten = row.split(",");
if(courseWritten[0].equalsIgnoreCase(name)){
courseWritten[1] = lecturer;
courseWritten[2] = start.toString();
courseWritten[3] = end.toString();
}
newList.add(courseWritten[0]);
for(int i = 1; i < courseWritten.length; i++) {
newList.add(",");
newList.add(courseWritten[i]);
}
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");
......@@ -74,15 +72,13 @@ public class CSVCourse {
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]);
String[] courseWritten = row.split(",");
if(!courseWritten[0].equalsIgnoreCase(name)){
newList.add(courseWritten[0]);
for(int i = 1; i < courseWritten.length; i++) {
newList.add(",");
newList.add(courseWritten[i]);
}
newList.add("\n");
}
} check.close();
......
package Softeng.Group3.SiJadwal.repository;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import Softeng.Group3.SiJadwal.Main;
import Softeng.Group3.SiJadwal.model.Course;
import Softeng.Group3.SiJadwal.model.Timetable;
public class CSVTimetable {
public static void writeTimetable(String name, Timetable table) {
try{
FileWriter reader = new FileWriter("Timetable.csv", true);
BufferedReader check = new BufferedReader(new FileReader("Timetable.csv"));
String row;
HashMap<String, String> usersCSV = new HashMap<>();
List<Course> courses = table.getCourses();
while((row = check.readLine()) != null){
String[] usersWritten;
usersWritten = row.split(",");
usersCSV.put(usersWritten[0], usersWritten[1]);
}
if(!usersCSV.containsKey(name)){
reader.append(Main.currentUser.getUsername()).append(",").append(name);
for (Course i : courses) {
reader.append(",").append(i.getCourseName());
}reader.append("\n");
}
reader.flush();
reader.close();
check.close();
}
catch (IOException e) {
System.out.println("File not found.");
e.printStackTrace();
}
}
public static void updateUser(String Uname, String newType){
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;
}
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 deleteTimetable(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(Main.currentUser.getUsername()) && tableWritten[1].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();
}
}
}
......@@ -45,8 +45,10 @@ public class CSVUser {
usersWritten[1] = newType;
}
newList.add(usersWritten[0]);
newList.add(",");
newList.add(usersWritten[1]);
for(int i = 1; i < usersWritten.length; i++) {
newList.add(",");
newList.add(usersWritten[i]);
}
newList.add("\n");
} check.close();
FileWriter reader = new FileWriter("Users.csv");
......@@ -70,8 +72,10 @@ public class CSVUser {
String[] usersWritten = row.split(",");
if(!usersWritten[0].equalsIgnoreCase(Uname)){
newList.add(usersWritten[0]);
newList.add(",");
newList.add(usersWritten[1]);
for(int i = 1; i < usersWritten.length; i++) {
newList.add(",");
newList.add(usersWritten[i]);
}
newList.add("\n");
}
} check.close();
......
......@@ -39,8 +39,9 @@ public class TimetableList {
return null;
}
public void addTimetable(Timetable timetable){
public void addTimetable(String tableName, Timetable timetable){
allTimetableList.add(timetable);
CSVTimetable.writeTimetable(tableName, timetable);
}
public void listToString(List<Timetable> timetables){
......@@ -51,9 +52,9 @@ public class TimetableList {
}
}
public void deleteTimetable(String table){
Timetable delTable = getTimetable(table, Main.currentUser);
allTimetableList.remove(delTable);
public void deleteTimetable(Timetable table){
allTimetableList.remove(table);
CSVTimetable.deleteTimetable(table.getTimetableName());
}
}
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