In: Computer Science
Java: Make 3 use cases for the code bellow.
Example of Use case:
Leave a Message
1.Caller dials main number of voice mail system
2.System speaks prompt
3.User types extension number
4.System speaks
5.Caller speaks message
Variation #1
3.1 In step 3, user enters invalid extension number
3.2 Voice mail system speaks
3.3 Continue with step 2.
What the code Does: adds a new regular task, delete a task , show all tasks, and show regular tasks.
my code:
import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i); } //sorting values in array using bubble sort Task temp;//temp var to hold sorted values for (int i = 0; i < sortedList.length; i++) { for (int j = 1; j < (sortedList.length - i); j++) { if (sortedList[j].isEarlier(sortedList[j - 1])) { temp = sortedList[j - 1]; sortedList[j - 1] = sortedList[j]; sortedList[j] = temp; } } } return sortedList; }//end of getSortedList public void addTask(Task task) { this.list.add(task);//.add to add elements in the list } public void deleteTask(int taskID) { //delete tasks using task ID for (int i = 0; i < this.list.size(); i++) { if (this.list.get(i).getTaskID() == taskID) { this.list.remove(i); } } } public void deleteAllTasks() { this.list.clear(); } public int getTaskNumber() { return this.list.size(); } public void printList(Task[] tasks) { System.out.println("-------------------------"); for (Task task : tasks) { task.printTask(); } System.out.println("-------------------------"); } public static void displayMenu() { System.out.println("1)Add tasks"); System.out.println("2)List tasks"); System.out.println("3)Delete Tasks"); System.out.println("4)Delete all tasks"); System.out.println("5)Exit"); } public static void main(String[] args) { Scanner in = new Scanner(System.in); ToDoList toDoList = new ToDoList(); boolean run = true; while (run) {//will continue to loop until user chooses a valid option displayMenu(); int choice = in.nextInt(); switch (choice) { //nextLine() method advances this scanner past the current line and returns the input that was skipped case 1: in.nextLine(); System.out.print("Task Name: "); String taskTitle = in.nextLine(); System.out.print("Task Deadline (dd mm yyyy): "); int taskDay = in.nextInt(); int taskMonth = in.nextInt(); int taskYear = in.nextInt(); in.nextLine(); System.out.print("Time Deadline (hh mm): "); int taskHour = in.nextInt(); int taskMin = in.nextInt(); in.nextLine(); System.out.print("Is this task important? (yes/no): "); String taskImportant = in.nextLine(); //Task ID made with all info set up neatly Task newTask = new Task(new Time(taskHour, taskMin), new Date(taskDay, taskMonth, taskYear), taskTitle, taskImportant, toDoList.getTaskNumber() + 1); //Adds the new task to the list toDoList.addTask(newTask); //Display the sorted tasks System.out.printf("Task %d is added. The To-Do list is as follows:\n", newTask.getTaskID()); toDoList.printList(toDoList.getSortedList()); break; case 2: //List all tasks toDoList.printList(toDoList.getSortedList()); break; case 3: //Delete a task toDoList.printList(toDoList.getSortedList()); in.nextLine(); System.out.print("Enter ID number to delete task: "); int taskID = in.nextInt(); toDoList.deleteTask(taskID); System.out.printf("\nTask %d is deleted. The new list is:\n", taskID); toDoList.printList(toDoList.getSortedList()); break; case 4: //Delete all tasks toDoList.deleteAllTasks(); System.out.println("Tasks Deleted"); break; case 5: //Exit program System.out.println("Exiting program, goodbye."); run = false; break; } } } } class Date { private int day; private int month; private int year; //Default constructor for date, public Date() { this(0, 0, 0); } public Date(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } //accessors and mutators public int getDay() { return this.day; } public int getMonth() { return this.month; } public int getYear() { return this.year; } public void setDay(int day) { this.day = day; } public void setMonth(int month) { this.month = month; } public void setYear(int year) { this.year = year; } public String toString() { return String.format("%d/%d/%d", this.day, this.month, this.year); } //Returns whether this date is earlier than another date provided as parameter. If they are the same, returns false public boolean isEarlier(Date date) { //If the years are the same, narrow the comparison down the months if (this.year == date.year) { //Repeat the same thing as years, but narrowed down for months if (this.month == date.month) { if (this.day == date.day) { //Returns false because if they are the same date, then one is not earlier than the other return false; } else if (this.day < date.day) { return true; } else { return false; } } else if (this.month < date.month) { return true; } else { return false; } } else if (this.year < date.year) { return true; } else { return false; } } } class Time { private int hour; private int min; //Default constructor for time public Time() { this(0, 0); } public Time(int hour, int min) { this.hour = hour; this.min = min; } //Accessors and mutator methods for hour and min attributes public int getHour() { return this.hour; } public int getMin() { return this.min; } public void setHour(int hour) { this.hour = hour; } public void setMin(int min) { this.min = min; } //Returns time in HH:MM format public String toString() { return String.format("%d:%d", this.hour, this.min); } //Returns whether or not this time is earlier than the time provided as a parameter. Returns false if both same public boolean isEarlier(Time time) { //If they are the same hour, narrow the comparison down to the minute if (this.hour == time.hour) { //Exact same process, but for min if (this.min == time.min) { //Returns false because if they're the same time, one is not earlier than the other return false; } else if (this.min < time.min) { return true; } else { return false; } //If this hour is less than the other hour, it is earlier, so return true } else if (this.hour < time.hour) { return true; //Otherwise, the time isn't earlier } else { return false; } } } class Task { private Time time; private Date date; private String title; private String Important; private int taskID; //Default constructor leaves attributes blank public Task() { this(null, null, null, null, 0); } public Task(Time time, Date date, String title, String Important, int taskID) { this.time = time; this.date = date; this.title = title; this.Important = Important; this.taskID = taskID; } //accessors and mutators for time, date, title, Important, and taskID public Time getTime() { return this.time; } public Date getDate() { return this.date; } public String getTitle() { return this.title; } public String getImportant() { return this.Important; } public int getTaskID() { return this.taskID; } public void setTime(Time time) { this.time = time; } public void setDate(Date date) { this.date = date; } public void setTitle(String title) { this.title = title; } public void setImportant(String Important) { this.Important = Important; } //Prints task date public void printTask() { System.out.printf("Task %d: \n", this.taskID, this.title, this.date.toString(), this.time.toString(), this.Important); } //Returns true if this task is earlier than task provided as parameter. public boolean isEarlier(Task task) { if (this.date.isEarlier(task.getDate())) { return true; } else if (!this.date.isEarlier(task.getDate()) && !task.getDate().isEarlier(this.date)) { return this.time.isEarlier(task.time); } return false; } }
Use cases are used describe the different scenarios associated with a system and how it behave. Here 3 uses cases are described
Use case 1 is creating a task and the variations of step 2 is given at the end
Use case 2 is listing all tasks
Use case 3 is deleting all tasks.
Use case 1
1.Display Menu
2.User select 1 for adding task
3.Program ask for task name and wait for input
4.User input task name
5.Program ask for task deadline wait for input
6.User input dd
7.User input mm
8.User input yyyy
9.Program ask for task deadline (in hour and minute) and wait for input
10.User input hour
11.User input minute
12.Program ask if this is an important task and wait for input.
13.User enters yes/no
14.System creates a new Task and print the task ID
15.The sorted list of task is printed
16.GOTO step 1
Variation #2
2.1 User select invalid input
2.2 GOTO step 1
Use case 2
1.Display Menu
2.User select 2 for printing task
3.System sort all the tasks and print it
4.GOTO step 1
Use case 3
1.Display Menu
2.User select 4 for deleting all task
3.System delete all the tasks and print message
4.GOTO step 1