Question

In: Computer Science

Java: Make 3 use cases for the code bellow. Example of Use case: Leave a Message...

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;
    }
}

Solutions

Expert Solution

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

Related Solutions

UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. use OPTIONAL TYPE and NULL object design pattern.   import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map aTags = new HashMap<>(); public Student(String pName) { aName =...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map<String, String> aTags = new HashMap<>(); public Song(String pName) { aName = pName; } public String getName() { return...
Please use the link to the case study to answer the questions bellow. use the link...
Please use the link to the case study to answer the questions bellow. use the link to the case study please. this is due today. https://books.google.com/books?id=bzb3BQAAQBAJ&pg=PA198&lpg=PA198&dq=case:+cursory+exams+are+risky&source=bl&ots=W2jLZ9niMa&sig=uYHeQOv-uuQ7ISm0YAdp7JcURBc&hl=en&sa=X&ved=0ahUKEwjkhaPf7ZLZAhUl8IMKHfHnCN8Q6AEIMDAB#v=onepage&q=case%3A%20cursory%20exams%20are%20risky&f=false Case: Cursory Exams are Risky Questions The many lessons for discussion in Niles v. City of San Rafael include the following; 1.An organization can improve the quality of patient care rendered in the facility by establishing and adhering to policies, procedures and protocols that facilitate the delivery of high-quality care across all disciplines. 2....
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard (int...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation /** Class to encapsulate a tree node. */ protected static class Node implements Serializable {
Leave code as is ALL I NEED IS TO MAKE THE LIST INTO ASCENDING ORDER FROM...
Leave code as is ALL I NEED IS TO MAKE THE LIST INTO ASCENDING ORDER FROM A TO Z OF ARTISTS NAMES YOU CAN USE THIS AS REFERENCE SPOTIFY LIST TOP 50 AS REFERENCE FOR CSV FILE https://spotifycharts.com/viral/ import java.io.*; public class Spotify { public static void main(String[]args) { // Read csv Spotify file located on my desktop // Download csv file, save on desktop, and add file location in csvFile = " " String csvFile = "CSVFILE LOCATION OF...
What are the Signature lines of a Java class code? With an example. and the UML...
What are the Signature lines of a Java class code? With an example. and the UML diagram.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT