You will implement a TA office hour management program as described below. Your
ID: 3619796 • Letter: Y
Question
You will implement a TA office hour management program as described below. Your program should be robust and able to cope with deviant user input.Function Requirements
Your program will be driven by a menu prompt. The menu item is add TA, All input in this entire program is case sensitive. The main menu should re-prompt on invalid input.
TA Office Hour Program
1. Add TA
Add TA
Prompts for first name, last name, number of courses (up to 5), course ID for each course, number of office hours (up to 10), day and start time for each office hour. Your program should be able to handle up to 200 TAs. We assume that, regardless of start time, an office hour last for exactly 1 hour. For example:
First Name: John
Last Name: Doe
Number of courses: 2
ID for course 1: CS201
ID for course 2: CS251
Number of office hour intervals: 3
Day (MTWRFS) and time for office hour 1: M:08:00
Day (MTWRFS) and time for office hour 2: M:13:30
Day (MTWRFS) and time for office hour 3: W:11:00
Valid day/time inputs must be in the form <day code>:<2 digit hour>:<2 digit minute>. After all values are entered, if any of the data is invalid (bad time, bad day code) the program prints an error message and discards the input. Otherwise it adds the new TA to the TA array. In either case, the main menu is displayed immediately afterwards.
Explanation / Answer
please rate - thanks each of these 3 modules are a separate fileimport java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Time {
private int hour;
private int min;
private int day;
private String[] dayNames = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//creates time class object from string in format d:hh:mm such as M:10:30
public static Time parseTime(String s) {
Time result = null;
if (s != null) {
String tokens[] = s.split(":");
Integer hour = null, min = null, day = null;
if (tokens.length < 3) {
System.out.println("Invalid time format!");
} else {
if (tokens[0].length() != 1 || s.indexOf(tokens[0]) < 0) {
System.out.println("Invalid day of week!");
} else {
day = ("MTWRFS".indexOf(tokens[0].toUpperCase()));
if (day < 0) {
System.out.println("Invalid day!");
day = null;
}
}
if (tokens[1].length() != 2) {
System.out.println("Invalid hour format!");
} else {
try {
hour = Integer.parseInt(tokens[1]);
if (hour < 0 || hour > 23) {
throw new Exception();
}
} catch (Exception ex) {
System.out.println("Invalid hour format!");
}
}
if (tokens[2].length() != 2) {
System.out.println("Invalid minutes format!");
} else {
try {
min = Integer.parseInt(tokens[2]);
if (min < 0 || min > 59) {
throw new Exception();
}
} catch (Exception ex) {
System.out.println("Invalid minutes format!");
}
}
if (hour != null && min != null && day != null) {
result = new Time();
result.setDay(day);
result.setHour(hour);
result.setMin(min);
}
}
}
return result;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public void printInfo() {
System.out.print(String.format("%s, %02d:%02d", dayNames[day], hour, min));
}
public boolean isInPeriod(Time that) {
if (this.getDay() != that.getDay()) {
return false;
}
Calendar c1 = new GregorianCalendar();
Calendar c2 = new GregorianCalendar();
//Set date/time to now
c1.setTime(new Date());
c2.setTime(c1.getTime());
//Set hours/minutes to objects values
c1.set(Calendar.HOUR_OF_DAY, this.getHour());
c1.set(Calendar.MINUTE, this.getMin());
c2.set(Calendar.HOUR_OF_DAY, that.getHour());
c2.set(Calendar.MINUTE, that.getMin());
if (c2.getTime().getTime() < c1.getTime().getTime()) {
return false;
}
if (c2.getTime().getTime() - c1.getTime().getTime() > 1000 * 3600) {
return false;
}
return true;
}
}
----------------------------------------------------
import java.util.*;
import java.io.*;
public class TA {
private String firstName;
private String lastName;
private String[] courseIds = new String[5];
private int coursesCount;
private Time[] times = new Time[10];
private int timesCount;
public TA(String firstName, String lastName, String[] courseIds, int coursesCount, Time[] times, int timesCount) {
this.firstName = firstName;
this.lastName = lastName;
this.courseIds = courseIds;
this.coursesCount = coursesCount;
this.times = times;
this.timesCount = timesCount;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setCourseIds(String[] courseIds) {
this.courseIds = courseIds;
}
public void setCoursesCount(int coursesCount) {
this.coursesCount = coursesCount;
}
public void setTimes(Time[] times) {
this.times = times;
}
public void setTimesCount(int timesCount) {
this.timesCount = timesCount;
}
}
-------------------------------------------------
import java.util.*;
import java.io.*;
public class TA {
private String firstName;
private String lastName;
private String[] courseIds = new String[5];
private int coursesCount;
private Time[] times = new Time[10];
private int timesCount;
public TA(String firstName, String lastName, String[] courseIds, int coursesCount, Time[] times, int timesCount) {
this.firstName = firstName;
this.lastName = lastName;
this.courseIds = courseIds;
this.coursesCount = coursesCount;
this.times = times;
this.timesCount = timesCount;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setCourseIds(String[] courseIds) {
this.courseIds = courseIds;
}
public void setCoursesCount(int coursesCount) {
this.coursesCount = coursesCount;
}
public void setTimes(Time[] times) {
this.times = times;
}
public void setTimesCount(int timesCount) {
this.timesCount = timesCount;
}
} import java.util.Scanner;
public class TADriver {
private static TA[] listOfTA = new TA[200];
private static int taCount = 0;
public static void main(String[] args) {
int choice;
Scanner in = new Scanner(System.in);
choice = menu(in);
while (choice != 5) {
switch (choice) {
case 1:
if (taCount < 200)
addTA();
else
System.out.println("Max of 200 TA's reached ");
break;
case 5:
System.exit(0);
}
System.out.println();
choice = menu(in);
}
}
private static int menu(Scanner in) {
String choice;
do {
System.out.println("TA Office Hour Program");
System.out.println("1. Add TA");
System.out.println("5. Quit");
choice = in.next();
if (choice.compareTo("1") < 0 || choice.compareTo("5") > 0)
System.out.println("Illegal entry-try again! ");
} while (choice.compareTo("1") < 0 || choice.compareTo("5") > 0);
return Integer.parseInt(choice);
}
private static void addTA() {
Scanner in = new Scanner(System.in);
System.out.print("First Name: ");
String firstName = in.next();
System.out.print("Last Name: ");
String lastName = in.next();
Integer coursesNum = enterInt(1, 5, "Number of courses");
String[] courseIds = new String[coursesNum];
for (int i = 0; i < coursesNum; i++) {
System.out.print("ID for course " + (i + 1) + ": ");
courseIds[i] = in.next();
}
Integer timesNum = enterInt(1, 10, "Number of office hour intervals");
Time[] times = new Time[timesNum];
for (int i = 0; i < timesNum; i++) {
Time t = null;
do {
System.out.print("Day (MTWRFS) and time for office hour :" + (i + 1) + ": ");
t = Time.parseTime(in.next());
} while (t == null);
times[i] = t;
}
listOfTA[taCount] = new TA(firstName, lastName, courseIds, coursesNum, times, timesNum);
taCount++;
}
private static int enterInt(Integer from, Integer to, String msg) {
Integer result = null;
do {
System.out.print(msg + ": ");
Scanner in = new Scanner(System.in);
String coursesNumStr = in.next();
try {
result = Integer.parseInt(coursesNumStr);
} catch (Exception ex) {
System.out.println("Invalid number!");
continue;
}
if ((from != null && result < from) || (to != null && result > to)) {
System.out.println(msg + " should be positive integer between " + from + " and " + to);
result = null;
}
} while (result == null);
return result;
}
}
import java.util.Scanner;
public class TADriver {
private static TA[] listOfTA = new TA[200];
private static int taCount = 0;
public static void main(String[] args) {
int choice;
Scanner in = new Scanner(System.in);
choice = menu(in);
while (choice != 5) {
switch (choice) {
case 1:
if (taCount < 200)
addTA();
else
System.out.println("Max of 200 TA's reached ");
break;
case 5:
System.exit(0);
}
System.out.println();
choice = menu(in);
}
}
private static int menu(Scanner in) {
String choice;
do {
System.out.println("TA Office Hour Program");
System.out.println("1. Add TA");
System.out.println("5. Quit");
choice = in.next();
if (choice.compareTo("1") < 0 || choice.compareTo("5") > 0)
System.out.println("Illegal entry-try again! ");
} while (choice.compareTo("1") < 0 || choice.compareTo("5") > 0);
return Integer.parseInt(choice);
}
private static void addTA() {
Scanner in = new Scanner(System.in);
System.out.print("First Name: ");
String firstName = in.next();
System.out.print("Last Name: ");
String lastName = in.next();
Integer coursesNum = enterInt(1, 5, "Number of courses");
String[] courseIds = new String[coursesNum];
for (int i = 0; i < coursesNum; i++) {
System.out.print("ID for course " + (i + 1) + ": ");
courseIds[i] = in.next();
}
Integer timesNum = enterInt(1, 10, "Number of office hour intervals");
Time[] times = new Time[timesNum];
for (int i = 0; i < timesNum; i++) {
Time t = null;
do {
System.out.print("Day (MTWRFS) and time for office hour :" + (i + 1) + ": ");
t = Time.parseTime(in.next());
} while (t == null);
times[i] = t;
}
listOfTA[taCount] = new TA(firstName, lastName, courseIds, coursesNum, times, timesNum);
taCount++;
}
private static int enterInt(Integer from, Integer to, String msg) {
Integer result = null;
do {
System.out.print(msg + ": ");
Scanner in = new Scanner(System.in);
String coursesNumStr = in.next();
try {
result = Integer.parseInt(coursesNumStr);
} catch (Exception ex) {
System.out.println("Invalid number!");
continue;
}
if ((from != null && result < from) || (to != null && result > to)) {
System.out.println(msg + " should be positive integer between " + from + " and " + to);
result = null;
}
} while (result == null);
return result;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.