From f228f0b3f87a9da9327c4fa90037dbcf74996a3d Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Fri, 18 Feb 2022 15:23:27 +0800 Subject: [PATCH] add save function --- data/task-file | 1 + src/main/java/IllegalShapeException.java | 2 - src/main/java/controller/TaskManager.java | 154 +++++++++++++++++++++- src/main/java/task/Deadline.java | 6 + src/main/java/task/Event.java | 4 + src/main/java/task/Task.java | 9 ++ src/main/java/task/Todo.java | 3 + 7 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 data/task-file delete mode 100644 src/main/java/IllegalShapeException.java diff --git a/data/task-file b/data/task-file new file mode 100644 index 000000000..61a8f34a5 --- /dev/null +++ b/data/task-file @@ -0,0 +1 @@ +T | | read diff --git a/src/main/java/IllegalShapeException.java b/src/main/java/IllegalShapeException.java deleted file mode 100644 index 957599a81..000000000 --- a/src/main/java/IllegalShapeException.java +++ /dev/null @@ -1,2 +0,0 @@ -package PACKAGE_NAME;public class IllegalShapeException { -} diff --git a/src/main/java/controller/TaskManager.java b/src/main/java/controller/TaskManager.java index 1b7de5b2d..a53e81df8 100644 --- a/src/main/java/controller/TaskManager.java +++ b/src/main/java/controller/TaskManager.java @@ -5,13 +5,16 @@ import task.Task; import task.Todo; -import java.util.Scanner; +import java.io.*; +import java.util.*; public class TaskManager { private static final String INDENT = " "; private static final String LINE="-------------------------------------------"; public static final String SPACE = " "; private static final int MAX_TASK_COUNT = 100; + private static final String DIR = "data/task-file"; + private static final String FILE_SEPARATOR = " | "; private static int taskCount=0; @@ -19,7 +22,7 @@ public class TaskManager { private static Scanner sc = new Scanner(System.in); - public static void main(String[] args) { + public static void main(String[] args) { greet(); String input = getInput(); String action = getAction(input); @@ -53,6 +56,7 @@ public static void main(String[] args) { } private static void bye() { + save(DIR, new ArrayList<>(Arrays.asList(taskList))); System.out.println(INDENT + "Bye. Hope to see you again soon!"); printLine(); } @@ -145,8 +149,152 @@ private static void addTaskByMessage(String action, String input){ } addTask(newTask); } - + +// private static void retrieveData() { +// File f = new File(DIR); +// if(f.exists()){ +// taskList = load(f); +// }else{ +// f.getParentFile().mkdir(); +// f.createNewFile(); +// taskList = new ArrayList(); +// save(DIR, taskList); +// } +// } + + private static void save(String filename, ArrayList al){ + StringBuilder sb = new StringBuilder(); + List newList = new ArrayList(); + for(int i = 0; i < al.size(); i++){ + Task task = al.get(i); + if(task == null) break; + String taskType = task.getTypeIcon(); + String taskStatus = task.getStatusIcon(); + String taskDetails = task.getDescription(); + sb.append(taskType); + sb.append(FILE_SEPARATOR); + sb.append(taskStatus); + sb.append(FILE_SEPARATOR); + sb.append(taskDetails); + if(taskType.equals("D") || taskType.equals("E")){ + sb.append(FILE_SEPARATOR); + sb.append(task.getTime()); + } + sb.append(System.lineSeparator()); + } + try { + writeToFile(filename, sb.toString()); + } catch (IOException e) { + System.out.println("Something went wrong:" + e.getMessage()); + } + } + + public static void writeToFile(String fileName, String data) throws IOException { + FileWriter fw = new FileWriter(fileName); + fw.write(data); + fw.close(); + } + + +// +// public static List read(String filename) throws IOException{ +// File f = new File(filename); +// Scanner s = new Scanner(f); +// while(s.hasNext()) { +// String newLine = s.nextLine(); +// Task newTask; +// StringTokenizer st = new StringTokenizer(newLine, FILE_SEPARATOR); +// +// String taskType = st.nextToken(); +// String taskStatus = st.nextToken(); +// String description = st.nextToken(); +// if(taskType.equals("D")) { +// String time = st.nextToken(); +// newTask = new Deadline(description,time); +// newTask. +// } +// +// +// +// } +// try{ +// List data = new ArrayList(); +// Scanner scanner = new Scanner(new FileInputStream(filename)); +// try { +// while (scanner.hasNextLine()) { +// data.add(scanner.nextLine()); +// } +// } finally { +// scanner.close(); +// } +// return data; +// }catch(IOException e){ +// System.out.println("reading file unsuccessfully"); +// e.printStackTrace(); +// return null; +// } +// } + + + + +// try { +// File file = new File(dir); +// if (file.exists()) { +// orders = load(dir); +// } else { +// file.getParentFile().mkdir(); +// file.createNewFile(); +// orders = new ArrayList(); +// save(dir, orders); +// } +// } catch (IOException e) { +// e.printStackTrace(); +// } } + + +// +// /** +// * This method is to load orders from external files +// * @param filename +// * specifies where the external files stored +// * @return all reservations read from the file +// */ +// @Override +// public ArrayList load(String filename) { +// ArrayList stringArray = (ArrayList) read(filename); +// ArrayList alr = new ArrayList(); +// +// for (int i = 0; i < stringArray.size(); i++) { +// String st = (String) stringArray.get(i); +// StringTokenizer star = new StringTokenizer(st, "|"); +// +// int orderId = Integer.parseInt(star.nextToken().trim()); +// int staffId = Integer.parseInt(star.nextToken().trim()); +// int tableId = Integer.parseInt(star.nextToken().trim()); +// int numberOfPax = Integer.parseInt(star.nextToken().trim()); +// int orderSize = Integer.parseInt(star.nextToken().trim()); // write the orderSize in the file in order to read different size of order items +// boolean isActive = Boolean.parseBoolean(star.nextToken().trim()); +// +// //create order with no order item +// Order order = new Order(orderId, staffId, tableId, numberOfPax, isActive); +// //add order item in order +// for (int j = 0; j < orderSize; j++) { +// int itemId = Integer.parseInt(star.nextToken().trim()); +// String name = star.nextToken().trim(); +// int quantity = Integer.parseInt(star.nextToken().trim()); +// double price = Double.parseDouble(star.nextToken().trim()); +// order.addOrderItem(itemId, quantity, name, price); +// } +// //add order to order list +// alr.add(order); +// } +// return alr; +// +// } + + // //public class Cup{ // private T item; diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index e8b85bb85..69983a58a 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -21,4 +21,10 @@ public String toString(){ return "[D]" + super.toString() + "(by: " + by + ")"; } + public String getTypeIcon(){return "D";} + + public String getTime(){ + return by; + } + } diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index c817bad1a..1694ad2bb 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -11,6 +11,10 @@ public Event(String description, String at) { public String toString(){ return "[E]" + super.toString() + "(at: " + at + ")"; } + public String getTypeIcon(){return "E";} + public String getTime(){ + return at; + } diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index 76cd5b776..23aab17ef 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -13,6 +13,10 @@ public String getStatusIcon(){ return (isDone ? "X" : " "); } + public String getDescription() { + return description; + } + public void markAsDone(){ this.isDone=true; } @@ -25,4 +29,9 @@ public String toString(){ return "[" + getStatusIcon() + "] " + description; } + public String getTypeIcon(){return null;} + + public String getTime(){return null;} + + } diff --git a/src/main/java/task/Todo.java b/src/main/java/task/Todo.java index 869d894d3..c3f982cd3 100644 --- a/src/main/java/task/Todo.java +++ b/src/main/java/task/Todo.java @@ -9,4 +9,7 @@ public Todo(String description){ public String toString(){ return "[T]"+super.toString(); } + + public String getTypeIcon(){return "T";} + }