diff --git a/data/duke.txt b/data/duke.txt index 9f42cff395..5163526ccf 100644 Binary files a/data/duke.txt and b/data/duke.txt differ diff --git a/src/main/java/AlphaNUS.java b/src/main/java/AlphaNUS.java index bdb9bf7e36..f4b8df234b 100644 --- a/src/main/java/AlphaNUS.java +++ b/src/main/java/AlphaNUS.java @@ -36,7 +36,7 @@ public AlphaNUS(String filepath) { tasklist = new TaskList(); fund = new Fund(); //TODO the fund need to be stored in the text file. managermap = new HashMap(); - commandList = new ArrayList(); + ArrayList commandList = storage.load(); projectmap = new HashMap();//To replace managermap in main class run(); } @@ -50,7 +50,7 @@ public void run() { boolean isExit = false; while (!isExit) { String input = ui.readInput(); - isExit = Parser.parse(input, tasklist, ui, fund, storage, managermap, commandList, projectmap); + isExit = Parser.parse(input, tasklist, ui, fund, storage, commandList, managermap, projectmap); } } @@ -59,7 +59,6 @@ public void run() { * @param args Unused. */ public static void main(String[] args) { - new AlphaNUS("..data/AlphaNUS.txt").run(); + new AlphaNUS("data/duke.txt").run(); } - } diff --git a/src/main/java/Launcher.java b/src/main/java/Launcher.java deleted file mode 100644 index 43d64c26fb..0000000000 --- a/src/main/java/Launcher.java +++ /dev/null @@ -1,10 +0,0 @@ -import javafx.application.Application; - -/** - * A launcher class to workaround classpath issues. - */ -public class Launcher { - public static void main(String[] args) { - Application.launch(Main.class, args); - } -} diff --git a/src/main/java/Main.java b/src/main/java/Main.java deleted file mode 100644 index 0e846862fd..0000000000 --- a/src/main/java/Main.java +++ /dev/null @@ -1,130 +0,0 @@ -import javafx.application.Application; -import javafx.scene.Scene; -import javafx.scene.control.Label; -import javafx.stage.Stage; -//import javafx.application.Application; -//import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.control.ScrollPane; -import javafx.scene.control.TextField; -import javafx.scene.layout.AnchorPane; -import javafx.scene.layout.VBox; -import javafx.scene.layout.Region; -import gui.DialogBox; -import javafx.scene.control.Label; -//import javafx.stage.Stage; - -public class Main extends Application { - private ScrollPane scrollPane; - private VBox dialogContainer; - private TextField userInput; - private Button sendButton; - private Scene scene; - - /** - * The main method of the AlphaNUS program, which instantiates a AlphaNUS instance with the filepath to the storage. - * @param args Unused. - */ - public static void main(String[] args) { - new AlphaNUS("data/AlphaNUS.txt").run(); - } - - @Override - public void start(Stage stage) { - //Step 1. Setting up required components - - //The container for the content of the chat to scroll. - scrollPane = new ScrollPane(); - dialogContainer = new VBox(); - scrollPane.setContent(dialogContainer); - - userInput = new TextField(); - sendButton = new Button("Send"); - - AnchorPane mainLayout = new AnchorPane(); - mainLayout.getChildren().addAll(scrollPane, userInput, sendButton); - - scene = new Scene(mainLayout); - - stage.setScene(scene); - stage.show(); - - // more code to be added here later - stage.setTitle("AlphaNUS"); - stage.setResizable(false); - stage.setMinHeight(500.0); - stage.setMinWidth(800.0); - - mainLayout.setPrefSize(500.0, 800.0); - - scrollPane.setPrefSize(785, 435); - scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); - scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); - - scrollPane.setVvalue(1.0); - scrollPane.setFitToWidth(true); - - // You will need to import `javafx.scene.layout.Region` for this. - dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE); - - userInput.setPrefWidth(725.0); - - sendButton.setPrefWidth(55.0); - - AnchorPane.setTopAnchor(scrollPane, 1.0); - - AnchorPane.setBottomAnchor(sendButton, 1.0); - AnchorPane.setRightAnchor(sendButton, 1.0); - - AnchorPane.setLeftAnchor(userInput, 1.0); - AnchorPane.setBottomAnchor(userInput, 1.0); - - // more code to be added here later - sendButton.setOnMouseClicked((event) -> { - handleUserInput(); - }); - - userInput.setOnAction((event) -> { - handleUserInput(); - }); - - dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0)); - } - - /** - * Iteration 1: - * Creates a label with the specified text and adds it to the dialog container. - * @param text String containing text to add - * @return a label with the specified text that has word wrap enabled. - */ - private Label getDialogLabel(String text) { - // You will need to import `javafx.scene.control.Label`. - Label textToAdd = new Label(text); - textToAdd.setWrapText(true); - - return textToAdd; - } - - /** - * Iteration 2: - * Creates two dialog boxes, one echoing user input and the other containing Duke's reply and then appends them to - * the dialog container. Clears the user input after processing. - */ - private void handleUserInput() { - Label userText = new Label(userInput.getText()); - Label dukeText = new Label(getResponse(userInput.getText())); - dialogContainer.getChildren().addAll( - new DialogBox(userText), - new DialogBox(dukeText) - ); - userInput.clear(); - } - - /** - * You should have your own function to generate a response to user input. - * Replace this stub with your completed method. - */ - private String getResponse(String input) { - return "AlphaNUS heard: " + input; - } -} diff --git a/src/main/java/command/Parser.java b/src/main/java/command/Parser.java index c570eb119f..39456606be 100644 --- a/src/main/java/command/Parser.java +++ b/src/main/java/command/Parser.java @@ -34,18 +34,23 @@ public class Parser { * @param tasklist Tasklist of the user. * @param ui Ui that interacts with the user. * @param storage Storage for the Tasklist. - * @param commandList + * @param commandList List of input commands. * @return Returns boolean variable to indicate when to stop parsing for input. * @throws AlphaNUSException if input is not valid. */ - public static boolean parse(String input, TaskList tasklist, Ui ui, Fund fund, Storage storage, HashMap managermap, ArrayList commandList, HashMap projectmap) { + + public static boolean parse(String input, TaskList tasklist, Ui ui, Fund fund, Storage storage, ArrayList commandList, + HashMap managermap, HashMap projectmap) { try { if (instr.isBye(input)) { //print bye message ui.byeMessage(); ui.getIn().close(); return true; + } else if (instr.isHistory(input)) { + process.history(ui,commandList, storage); } else if (instr.isAddProject(input)) { + process.commandHistory(input, ui, storage); if (currentProject == null) { currentProject = process.addProject(input, ui, projectmap); } else { @@ -53,38 +58,46 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Fund fund, S } } else if (instr.isDeleteProject(input)) { Project deletedProject = process.deleteProject(input, ui, projectmap); + process.commandHistory(input, ui, storage); if (currentProject == deletedProject) { currentProject = null; } } else if (instr.isGoToProject(input)) { + if (currentProject == null) { + process.noProject(ui); + } currentProject = process.goToProject(input, ui, projectmap); - } else if (currentProject == null) { - process.noProject(ui); + process.commandHistory(input, ui, storage); } else if (instr.isList(input)) { - //print out current list - process.commandHistory(input, ui, commandList,storage); ui.printList(tasklist, "list"); + process.commandHistory(input, ui, storage); } else if (instr.isDone(input)) { process.done(input, tasklist, ui); + process.commandHistory(input, ui, storage); } else if (instr.isDeadline(input)) { process.deadline(input, tasklist, ui); - storage.save(tasklist.returnArrayList()); + process.commandHistory(input, ui, storage); + //storage.save(tasklist.returnArrayList()); } else if (instr.isDoAfter(input)) { process.doAfter(input, tasklist, ui); - Storage.save(tasklist.returnArrayList()); + //Storage.save(tasklist.returnArrayList()); } else if (instr.isDeletePayment(input)) { process.deletePayment(input, managermap, ui); + process.commandHistory(input, ui, storage); //storage.save(tasklist.returnArrayList()); } else if (instr.isFind(input)) { // process.find(input, tasklist, ui); + process.commandHistory(input, ui, storage); } else if (instr.isWithinPeriodTask(input)) { process.within(input, tasklist, ui); - storage.save(tasklist.returnArrayList()); + process.commandHistory(input, ui, storage); + //storage.save(tasklist.returnArrayList()); } else if (instr.isSnooze(input)) { process.snooze(input, tasklist, ui); - storage.save(tasklist.returnArrayList()); + process.commandHistory(input, ui, storage); + //storage.save(tasklist.returnArrayList()); /* `} else if (instr.isPostpone(input)) { process.postpone(input, tasklist, ui); @@ -92,25 +105,32 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Fund fund, S */ } else if (instr.isReschedule(input)) { // process.reschedule(input, tasklist, ui); - storage.save(tasklist.returnArrayList()); + //storage.save(tasklist.returnArrayList()); } else if (instr.isViewSchedule(input)) { process.viewSchedule(input, tasklist, ui); + process.commandHistory(input, ui, storage); //storage.save(tasklist.returnArrayList()); } else if (instr.isReminder(input)) { //process.reminder(input, tasklist, ui); + process.commandHistory(input, ui, storage); } else if (instr.isEdit(input)) { // process.edit(input,tasklist,ui); + process.commandHistory(input, ui, storage); } else if (instr.isAddPayment(input)) { process.addPayment(input, managermap, ui); + process.commandHistory(input, ui, storage); } else if (instr.isgetpayee(input)) { process.findPayee(input, ui, managermap); + process.commandHistory(input, ui, storage); } else if (instr.isAddPayee(input)) { process.addPayee(input, managermap, ui); + process.commandHistory(input, ui, storage); } else if (instr.isDeletePayee(input)) { process.deletePayee(input, managermap, ui); - process.commandHistory(input, ui, commandList, storage); + process.commandHistory(input, ui, storage); } else if (instr.isInvoice(input)) { process.inVoice(input, tasklist, ui); + process.commandHistory(input, ui, storage); } else if (instr.isHistory(input)) { process.commandHistory(input, ui,commandList, storage); } else if (instr.isSetFund(input)) { @@ -120,7 +140,7 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Fund fund, S } else { throw new AlphaNUSException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } - } catch (AlphaNUSException | IOException e) { + } catch (AlphaNUSException e) { process.homePageMessage(currentProject.projectname, projectmap.size(), ui); } catch (NullPointerException e) { process.homePageMessage(null, projectmap.size(), ui); diff --git a/src/main/java/command/Process.java b/src/main/java/command/Process.java index ccd3ac1dc5..29ac3c0ca2 100644 --- a/src/main/java/command/Process.java +++ b/src/main/java/command/Process.java @@ -13,9 +13,6 @@ import task.WithinPeriodTask; import ui.Ui; -import java.io.BufferedWriter; -import java.io.FileWriter; -import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -510,22 +507,24 @@ public void inVoice(String input, TaskList tasklist, Ui ui) { ui.exceptionMessage(" ☹ OOPS!!! Please input the correct command format (refer to user guide)"); } } + /** + * processes the input command and stores it in a text file. + * @param input Input from the user. + * @param ui Ui that interacts with the user. + * @param storage Storage that stores the input commands entered by the user. + */ - public void commandHistory(String input, Ui ui, ArrayList commandList, Storage storage) throws IOException { - if (!input.equals("history")) { - Calendar cal = Calendar.getInstance(); - Date date = cal.getTime(); - DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); - String formattedDate = dateFormat.format(date); - String command_time = input + " at " + formattedDate; - //commandList.add(command_time); - FileWriter fw = new FileWriter(String.valueOf(storage)); - BufferedWriter bw = new BufferedWriter(fw); - bw.write(command_time); - } - else{ - //ui.printArrayList(commandList); - ui.printTxtfile(String.valueOf(storage)); - } - } + public void commandHistory(String input, Ui ui, Storage storage) { + Calendar cal = Calendar.getInstance(); + Date date = cal.getTime(); + DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); + String formattedDate = dateFormat.format(date); + String commandTime = input + " | " + formattedDate; + storage.save(commandTime); + } + + public void history(Ui ui, ArrayList commandList, Storage storage) { + commandList = storage.load(); + ui.printArrayList(commandList); + } } \ No newline at end of file diff --git a/src/main/java/command/Storage.java b/src/main/java/command/Storage.java index e0c84b20ba..d704c1e09b 100644 --- a/src/main/java/command/Storage.java +++ b/src/main/java/command/Storage.java @@ -1,13 +1,13 @@ package command; -import java.io.ObjectOutputStream; -import java.io.FileOutputStream; -import java.io.EOFException; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.ObjectInputStream; - import task.Task; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; /** @@ -18,6 +18,7 @@ public class Storage { /** * Creates a Storage instance with the required attributes. + * * @param filepath Filepath to the storage file. */ public Storage(String filepath) { @@ -26,21 +27,37 @@ public Storage(String filepath) { /** * Loads an ArrayList containing the Task object from the storage file. + * * @return The ArrayList containing the Task object. */ - public static ArrayList load() { + public static ArrayList load() { try { + String line; + ArrayList list = new ArrayList(); + FileReader fileReader = new FileReader(filepath); + BufferedReader bufferedReader = new BufferedReader(fileReader); + + while ((line = bufferedReader.readLine()) != null) { + list.add(line); + } - FileInputStream file = new FileInputStream(filepath); + bufferedReader.close(); + return list; + /*FileInputStream file = new FileInputStream(filepath); ObjectInputStream out = new ObjectInputStream(file); - ArrayList arraylist = (ArrayList) out.readObject(); + ArrayList commandList = (ArrayList) out.readObject(); out.close(); - file.close(); + */ - return arraylist; - } catch (EOFException e) { + } catch (FileNotFoundException ex) { + System.out.println("Unable to open file '" + filepath + "'"); + } catch (IOException ex) { + System.out.println("Error reading file '" + filepath + "'"); + } + return new ArrayList(); + /*catch (EOFException e) { System.out.println("File is empty"); } catch (IOException ioe) { ioe.printStackTrace(); @@ -48,23 +65,36 @@ public static ArrayList load() { System.out.println("Class not found"); c.printStackTrace(); } - return new ArrayList(); + return new ArrayList(); + */ } /** * Saves the tasklist of the user as an ArrayList containing the task object. - * @param tasklist Tasklist of the user. + * */ - public static void save(ArrayList tasklist) { + public static void save(String str) { try { - FileOutputStream file = new FileOutputStream(filepath); + FileWriter fileWriter = new FileWriter(filepath, true); + BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); + bufferedWriter.newLine(); + /*for (int i = 0; i < tasklist.size(); i = i + 1) { + bufferedWriter.write(tasklist.get(i)); + } + */ + bufferedWriter.write(str); + bufferedWriter.close(); + /*FileOutputStream file = new FileOutputStream(filepath); ObjectOutputStream out = new ObjectOutputStream(file); out.writeObject(tasklist); out.close(); file.close(); } catch (IOException ioe) { ioe.printStackTrace(); + } + */ + } catch (IOException ex) { + System.out.println("Error writing to file '" + filepath + "'"); } } - -} +} \ No newline at end of file diff --git a/src/main/java/gui/DialogBox.java b/src/main/java/gui/DialogBox.java deleted file mode 100644 index 2b78a8e9f7..0000000000 --- a/src/main/java/gui/DialogBox.java +++ /dev/null @@ -1,29 +0,0 @@ -package gui; - -import javafx.geometry.Pos; -import javafx.scene.control.Label; -import javafx.scene.image.ImageView; -import javafx.scene.layout.HBox; - -/** - * The style of the dialog box shown on the UI. - */ -public class DialogBox extends HBox { - - private Label text; - private ImageView displayPicture; - - /** - * Defines the style of the dialogue between the AlphaNUS and the User. - * @param l a label for the current dialog. - */ - public DialogBox(Label l) { - text = l; - - - text.setWrapText(true); - - this.setAlignment(Pos.TOP_LEFT); - this.getChildren().addAll(text); - } -} \ No newline at end of file diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index 4cc89eb2b3..d844fb82bc 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -148,20 +148,16 @@ public void printList(TaskList list, String command) { System.out.print(line); } + /** + * prints the list of input commands entered by the user. + * @param list1 list of input commands entered by the user. + */ public void printArrayList(ArrayList list1) { for (int i = 0; i < list1.size(); i = i + 1) { System.out.println(list1.get(i)); } } - public void printTxtfile(String storage) throws IOException { - BufferedReader br = new BufferedReader(new FileReader(storage)); - String line = null; - while ((line = br.readLine()) != null) { - System.out.println(line); - } - } - /** * Prints the list of payments of a payee. * @param paymentList paymentList of the payee.