I need help making my output look this: Welcome to the day planner system Enter
ID: 3764305 • Letter: I
Question
I need help making my output look this:
Welcome to the day planner system
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
1
Enter the activity's name
class
Enter its start hour in military time
13
Enter its end hour in military time
14
class 13 14
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
1
Enter the activity's name
breakfast
Enter its start hour in military time
8
Enter its end hour in military time
9
breakfast 8 9
class 13 14
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
1
Enter the activity's name
lab exam
Enter its start hour in military time
15
Enter its end hour in military time
18
breakfast 8 9
class 13 14
lab exam 15 18
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
1
Enter the activity's name
breakfast part 2
Enter its start hour in military time
8
Enter its end hour in military time
10
The activity to be added conflicts with an existing activity. This activity will not be added.
breakfast 8 9
class 13 14
lab exam 15 18
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
2
Enter the activity's name
breakfast
class 13 14
lab exam 15 18
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
9
class 13 14
lab exam 15 18
Good bye!
My output currently looks like this:
Welcome to the day planner system
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
1
Enter the activity's name
Class
Enter its start hour in military time
13
Enter its end hour in military time
14
Class 13 14
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
1
Enter the activity's name
Breakfast
Enter its start hour in military time
8
Enter its end hour in military time
9
Breakfast 8 9
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
2
Enter the activity's name
Breakfast
Enter 1 to add an activity
Enter 2 to remove an activity
Enter 9 to quit
9
Good bye!
The program I have so far is:
import java.util.Scanner;
class DayScheduleFrontEnd
{
public static void main(String args[])
{
DaySchedule ds=new DaySchedule();
System.out.println("Welcome to the day planner system");
Scanner keyboard = new Scanner(System.in);
boolean quit = false;
String schedule = new String();
while(quit == false)
{
System.out.println("Enter 1 to add an activity Enter 2 to remove an activity Enter 9 to quit");
int choice = keyboard.nextInt();
keyboard.nextLine();//We put this because of the scanner bug
switch(choice)
{
case 1://Add activity to schedule
System.out.println("Enter the activity's name");
String name = keyboard.nextLine();
System.out.println("Enter its start hour in military time");
String start = keyboard.nextLine();
System.out.println("Enter its end hour in military time");
String end = keyboard.nextLine();
System.out.println(name + " " + start + " " + end);
break;
case 2://Remove activity from schedule
System.out.println("Enter the activity's name");
name = keyboard.nextLine();
break;
case 9://Quit
quit = true;
break;
default:
System.out.println("That's not a valid input");
break;
}
}
System.out.println("Good bye!");
}
}
Any ideas on how to make my program typed above keep track of the activities already entered would be awesome! Thanks!
Explanation / Answer
// This program passes all the test cases given in the question. it can also handle robust inputs
import java.util.Scanner;
import java.util.ArrayList;
// class Activity to store name, start hour, end hour and functions to work on them
class Activity
{
String name;
int start_hour;
int end_hour;
//constructor to create an obhect
public Activity(String name, int start_hour, int end_hour)
{
this.name = name;
this.start_hour = start_hour;
this.end_hour = end_hour;
}
// function to get name of calling activity
public String getName()
{
return this.name;
}
// function to get start hour of calling activity
public int getStartHour()
{
return this.start_hour;
}
// function to get end hour of calling activity
public int getEndHour()
{
return this.end_hour;
}
}
class DayScheduleFrontEnd
{
// ArrayList of Activities to store all entered activities
static ArrayList<Activity> activities = new ArrayList<Activity>();
// Function to display all activities present in the ArrayList
static void displayActivities()
{
// for each activity in ArrayList
for (Activity a : activities)
{
// print name, star hour and end hour
System.out.println(a.getName()+" "+a.getStartHour()+" "+a.getEndHour());
}
}
// Function to insert activity in the order of start hour without conflicting with existing activities
static int insertActivity(Activity activity)
{
int start, end;
start = activity.getStartHour();
end = activity.getEndHour();
boolean valid = true;
// Loop through each Activity present in the ArrayList
for (int i = 0; i < activities.size();i++)
{
Activity a = activities.get(i);
// Check if the start hour of new activity is in between existing Activity schedule
if(a.getStartHour() <= start && start < a.getEndHour())
{
valid = false;
break;
}
// Check if the end hour of new activity is in between existing Activity schedule
else if(a.getStartHour() < end && end <= a.getEndHour())
{
valid = false;
break;
}
// Check if the start hour of Existing activity is in between new Activity schedule
else if(start <= a.getStartHour() && a.getStartHour() < end)
{
valid = false;
break;
}
// Check if the end hour of Existing activity is in between new Activity schedule
else if(start < a.getEndHour() && a.getEndHour() <= end)
{
valid = false;
break;
}
// new Activity is not conflicting with existing activity
else
{
// insert new activity here if start hour of new activity is less than existing activity
if(start < a.getStartHour())
{
// add activity at current index and return index value
activities.add(i,activity);
return i;
}
}
}
// Control comes out of the loop either when activity is conflicting
// or start hour of new activity is bigger than all existing activities
if(valid)
{
// Add activity to the end if valid is still true and return its index
activities.add(activity);
return activities.size()-1;
}
else
{
// else print error message and return -1
System.out.println("The activity to be added conflicts with an existing activity. This activity will not be added.");
return -1;
}
}
// Function to search if an Activity is present in ArrayList by name
// and return index if present else return -1
static int searchActivity(String target_name)
{
// loop through each activity and compare name
for (Activity a : activities)
{
// if name matches return current Activity's index
if(a.getName() == target_name)
return activities.indexOf(a);
}
// if not found return -1
return -1;
}
// Main funcion
// there isn't much changed in this function
// Adding comment wherever changed
public static void main(String args[])
{
String name;
int start;
int end;
int index;
Activity a;
System.out.println("Welcome to the day planner system");
Scanner keyboard = new Scanner(System.in);
boolean quit = false;
String schedule = new String();
while(quit == false)
{
System.out.println("Enter 1 to add an activity Enter 2 to remove an activity Enter 9 to quit");
int choice = keyboard.nextInt();
keyboard.nextLine();//We put this because of the scanner bug
switch(choice)
{
case 1://Add activity to schedule
System.out.println("Enter the activity's name");
name = keyboard.nextLine();
System.out.println("Enter its start hour in military time");
start = keyboard.nextInt();
System.out.println("Enter its end hour in military time");
end = keyboard.nextInt();
// create an Activity object with given details and
// Add the Activity to ArrayList
a = new Activity(name,start,end);
index = insertActivity(a);
// Display all activities after inserting
displayActivities();
break;
case 2://Remove activity from schedule
// Display all activities and ask which activity to remove
displayActivities();
System.out.println("Enter the activity's name");
name = keyboard.nextLine();
// search for the activity with given name
index = searchActivity(name);
// remove it from ArrayList if found, else print error message
if(index>=0)
activities.remove(index);
else
System.out.println("No such Activity is present in the Schedule");
// Display remaining Activities
displayActivities();
break;
case 9://Quit
// update quit to true and display all existing activities
quit = true;
displayActivities();
break;
default:
// print error message if input is other than expected values
System.out.println("That's not a valid input");
break;
}
}
System.out.println("Good bye!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.