-
Notifications
You must be signed in to change notification settings - Fork 223
[Chanell Ng] iP #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[Chanell Ng] iP #244
Changes from 34 commits
3b19ba1
a75fcee
407ffa5
06f0689
9b617be
4006726
5487996
b5c3290
27b1dca
2ebb099
575b392
ce02ad4
38ed607
45deb86
ae29bdd
ec45d14
8dfff5e
5557dcb
cabc8e2
ff30251
73a64ba
8a94ea4
c942d11
4f0a9d5
add4617
1c760e8
d10b9a1
743980d
26ef9ce
b7970f1
c72addf
a929bc3
d3c2107
11af796
97c5ab0
2b307f3
7fd1218
98b7b46
4851440
498e37f
17312a9
8d0acb9
ed4a16f
aeb15f7
d5e2d9a
bd46e79
446ca55
194b0bc
6f462ad
925acea
28aa931
7bebc15
77eec58
b231f90
b2572af
87ed919
6cd61be
b993914
17cda08
30702c2
fffce7c
b65e6ea
d1837f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Manifest-Version: 1.0 | ||
| Main-Class: ip.src.main.java.A_MoreOOP | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package ip.src.main.java; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Scanner; | ||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.FileWriter; | ||
|
|
||
| /** A-More OOP class that demonstrates OOP by having classes such as Task, Event etc. | ||
| * | ||
| */ | ||
|
|
||
| public class A_MoreOOP { | ||
|
|
||
| /** Returns a Duke bot that has loaded the tasks from the file given. | ||
| * | ||
| * @param filePath Location of the file with the tasks information. | ||
| * @param bot The Duke bot to be updated with the tasks from the file. | ||
| * @throws FileNotFoundException | ||
| */ | ||
|
|
||
| private static void createBot(String filePath, Duke bot) throws FileNotFoundException { | ||
| File f = new File(filePath); // create a File for the given file path | ||
| Scanner s = new Scanner(f); // create a Scanner using the File as the source | ||
| Storage storage = new Storage(filePath, bot); | ||
| Task newTask = new Task(""); | ||
|
|
||
| while (s.hasNext()) { | ||
| storage.loadTask(s.nextLine()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Overwrites the file with the updated tasks everytime a user gives a command/input. | ||
| * | ||
| * @param filePath The location of the file to be updated. | ||
| * @param bot The bot with the new tasks to be written to the file. | ||
| * @throws IOException | ||
| */ | ||
| private static void updateFile(String filePath, Duke bot) throws IOException { | ||
| Storage storage = new Storage(filePath, bot); | ||
| storage.updateFile(); | ||
| } | ||
|
|
||
| public static void main(String[] args) throws DukeException { | ||
| System.out.println("Hello! I'm Duke"); | ||
| System.out.println("What can I do for you?"); | ||
| Duke bot = new Duke(); | ||
|
|
||
| try{ | ||
| File f = new File("data/duke.txt"); | ||
| f.getParentFile().mkdirs(); | ||
| if(f.createNewFile()){ | ||
| System.out.println("File created"); | ||
| }else { | ||
| createBot("data/duke.txt", bot); | ||
| } | ||
| }catch (IOException e){ | ||
| System.out.println("Error in creating file"); | ||
|
|
||
| } | ||
|
|
||
| Scanner sc = new Scanner(System.in); | ||
| String input = sc.nextLine(); | ||
| Parser parser = new Parser(); | ||
| Ui ui = new Ui(bot); | ||
|
|
||
| while (!(input.equals("bye"))) { | ||
| if (input.equals("list")) { | ||
| bot.printList(); | ||
| } else { | ||
| String command = parser.getCommand(input); | ||
| try { | ||
| if (command.equals("done")) { | ||
| int id = parser.getId(input); | ||
| ui.doneCommand(id); | ||
|
|
||
| } else if (command.equals("todo")) { | ||
| try { | ||
| input = parser.toDoTask(input); | ||
| ui.toDoCommand(input); | ||
|
|
||
| } catch (Exception e) { | ||
| throw new DukeException(("OOPS!!! The description cannot be empty.")); | ||
| } | ||
| } else if (command.equals("event")) { | ||
| String content = parser.eventTaskContent(input); | ||
| String at = parser.eventTaskAt(input); | ||
| ui.eventCommand(content, at); | ||
|
|
||
| } else if (command.equals("deadline")) { | ||
| String content = parser.deadlineTaskContent(input); | ||
| String by = parser.deadlineTaskBy(input); | ||
| ui.deadlineCommand(content, by); | ||
|
|
||
| }else if(command.equals("delete")){ | ||
| int id = parser.getId(input); | ||
| ui.deleteCommand(id); | ||
|
|
||
| } else { | ||
| throw new DukeException("OOPS!!! I'm sorry, but I don't know what that means :-{"); | ||
| } | ||
| } catch (DukeException e1) { | ||
| System.out.println(e1); | ||
|
|
||
| } | ||
| } | ||
| try { | ||
| updateFile("data/duke.txt", bot); | ||
| }catch (IOException e) { | ||
| System.out.println("Unable to update file"); | ||
| } | ||
| input = sc.nextLine(); | ||
| } | ||
|
|
||
|
|
||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package ip.src.main.java; | ||
|
|
||
| import java.text.ParseException; | ||
| import java.util.Date; | ||
| import java.text.DateFormat; | ||
| import java.text.SimpleDateFormat; | ||
|
|
||
| /** | ||
| * Deadline is a type of Task with attributes String content and a Date stored as a Date object. | ||
| * | ||
| */ | ||
|
|
||
| public class Deadline extends Task{ | ||
| protected Date by; | ||
|
|
||
| /** | ||
| * Constructor for Deadline Class. | ||
| * | ||
| * @param content String content given by user. | ||
| * @param by_str Deadline date given by user, parsed into a Date object with the format dd/MM/yyyy HH:mm. | ||
| */ | ||
|
|
||
| public Deadline(String content,String by_str) { | ||
| super(content); | ||
| DateFormat sourceFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); | ||
| try { | ||
| Date by = sourceFormat.parse(by_str); | ||
| this.by = by; | ||
| } catch (ParseException e) { | ||
| System.out.println("Date and Time is not in correct format DD/MM/YYYY HH:MM"); | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * toString() of Deadline Class. | ||
| * | ||
| * @return toString() representation of a Deadline object with its done status, content and date. | ||
| */ | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| DateFormat sourceFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); | ||
| String strDate = sourceFormat.format(this.by); | ||
| if(!this.done){ | ||
| return "D | 0 | " + super.toString() + " | " + strDate; | ||
| }else { | ||
| return "D | 1 | " + super.toString() + " | " + strDate; | ||
|
|
||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package ip.src.main.java; | ||
|
|
||
| /** | ||
| * Returns a Duke bot that stores the tasks given by the user. | ||
| */ | ||
| public class Duke { | ||
| /** Tasks stored in this task list object */ | ||
| TaskList list; | ||
|
|
||
| public Duke(){ | ||
|
|
||
| this.list = new TaskList(); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a greeting to the user. | ||
| */ | ||
| public void greet(){ | ||
| System.out.println("Hello! I'm Duke"); | ||
| System.out.println("What can I do for you?"); | ||
| } | ||
|
|
||
| /** | ||
| * Echos the user input. | ||
| * | ||
| * @param input User input given. | ||
| */ | ||
|
|
||
| public void echo(String input){ | ||
| System.out.println(input); | ||
|
|
||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe have consistent line breaks within the body of a method? (Compare this with lines 52~55) |
||
|
|
||
| /** | ||
| * Adds the given task to the Duke bot list, informs the user and prints the number of tasks in the bot list. | ||
| * | ||
| * @param task Task object created from user input. | ||
| */ | ||
|
|
||
| public void addToList(Task task){ | ||
| this.list.addTask(task); | ||
| System.out.println("Got it. I've added this task: "); | ||
| System.out.println(" " + task); | ||
| System.out.println("Now you have " + String.valueOf(this.list.size()) + " tasks in the list."); | ||
| } | ||
|
|
||
| /** | ||
| * Adds the given task to the Duke bot list quietly. | ||
| * | ||
| * @param task Task object created from user input. | ||
| */ | ||
|
|
||
| public void addToBot(Task task){ | ||
|
|
||
| this.list.addTask(task); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe have consistent line breaks within the body of a method? (Compare this with lines 28~31) |
||
|
|
||
| /** | ||
| * Prints the tasks in the bot list. | ||
| */ | ||
| public void printList(){ | ||
|
|
||
| if(this.list.Empty()){ | ||
| System.out.println("There are no tasks!"); | ||
| } | ||
| this.list.printTasks(); | ||
| } | ||
|
|
||
| /** | ||
| * Marks a task specified in the bot list as done. | ||
| * | ||
| * @param id The position ,1-th based, of the task to be marked done in the bot list. | ||
| */ | ||
| public void markTaskAsDone(int id){ | ||
| Task task = this.list.getTask(id-1); | ||
| task.markDone(); | ||
| System.out.println("Nice! I've marked this task as done:"); | ||
| System.out.println(" [X] "+ task.content); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a task specified in the bot list. | ||
| * | ||
| * @param id The position ,1-th based, of the task to be marked done in the bot list. | ||
| */ | ||
| public void deleteTask(int id){ | ||
| Task task = this.list.getTask(id-1); | ||
| this.list.remove(id-1); | ||
| System.out.println("Noted. I've removed this task: "); | ||
| System.out.println(" " + task); | ||
| System.out.println("Now you have " + String.valueOf(this.list.size()) + " tasks in the list."); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Prints all the tasks with content that contain the keyword. | ||
| * | ||
| * @param keyword The keyword given by the user. | ||
| */ | ||
| public void findMatchingTasks(String keyword){ | ||
| TaskList matchingTasks = this.list.findTasks(keyword); | ||
| matchingTasks.printTasks(); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ip.src.main.java; | ||
|
|
||
| /** | ||
| * Customized Exception Class for Duke. | ||
| */ | ||
|
|
||
| public class DukeException extends Exception{ | ||
| public DukeException(String message){ | ||
| super(message); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(){ | ||
| return this.getMessage(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package ip.src.main.java; | ||
| import java.text.ParseException; | ||
| import java.util.Date; | ||
| import java.text.DateFormat; | ||
| import java.text.SimpleDateFormat; | ||
|
|
||
| /** | ||
| * Event is a type of Task with attributes String content and a Date stored as a Date object. | ||
| * | ||
| */ | ||
| public class Event extends Task{ | ||
| protected Date at; | ||
|
|
||
| /** | ||
| * Constructor for Event Class. | ||
| * | ||
| * @param content String content given by user. | ||
| * @param at_str Event date given by user, parsed into a Date object with the format dd/MM/yyyy HH:mm. | ||
| */ | ||
|
|
||
| public Event(String content,String at_str) { | ||
| super(content); | ||
| DateFormat sourceFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); | ||
| try { | ||
| Date at = sourceFormat.parse(at_str); | ||
| this.at = at; | ||
| } catch (ParseException e) { | ||
| System.out.println("Date and Time is not in correct format DD/MM/YYYY HH:MM"); | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * toString() of Event Class. | ||
| * | ||
| * @return toString() representation of a Event object with its done status, content and date. | ||
| */ | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| DateFormat sourceFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); | ||
| String strDate = sourceFormat.format(this.at); | ||
| if(!this.done){ | ||
| return "E | 0 | " + super.toString() + " | " + strDate; | ||
| }else { | ||
| return "E | 1 | " + super.toString() + " | " + strDate; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package ip.src.main.java; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Level_1 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe consider deleting dead code? So that your codebase can be simpler and easier to understand. |
||
| public static void main(String[] args) { | ||
| System.out.println("Hello! I'm Duke"); | ||
| System.out.println("What can I do for you?"); | ||
| Scanner sc = new Scanner(System.in); | ||
| String input = sc.next(); | ||
| Duke bot = new Duke(); | ||
|
|
||
| while(!(input.equals("bye"))){ | ||
| bot.echo(input); | ||
| input = sc.next(); | ||
| } | ||
|
|
||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a whitespace between Java reserved words and curly brackets?