Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Write a client program for MyTime which loops around getting strings from
the user representing times and responding with a greeting such as “good
morning” as appropriate. The user should enter the string “quit” to exit the
program.

 

EXAMPLE run of client:
USER: 2:07 pm
PROGRAM: good afternoon
USER: 10:71 am
PROGRAM: that’s not a valid time, please re-enter
USER: 14:30am
PROGRAM: that’s not a valid time, please re-enter
USER: 10:21am
PROGRAM: good morning
USER: 7:00 pm
PROGRAM: good evening

 

the set method for MyTime should detect input
strings which do not represent valid times. An exception should be thrown in
such a case. Clients will have
to handle (or catch) the exceptions. Thus you will have to also write your own exception class which can be used by MyTime and the client class.

Here are my code:

public class MyTime {


private String time;

public String getTime() {

return time;

}

public void setTime() throws InvalidTime {

String time = askTime();

if (time.equalsIgnoreCase("quit")) {

System.exit(0);

} else {

try {

getHours(time);

getMinutes(time);

AmOrPM(time);

greetUser(time);

} catch (InvalidTime e) {

System.out.println("that's not a valid time, please re-enter");

setTime();

}

}

}

public static int getHours(String time) throws InvalidTime {

String[] separatedHourAndMin = time.split(":");

int hour = Integer.parseInt(separatedHourAndMin[0]);

if (hour >= 0 && hour <= 12) {

return hour;

} else {

throw new InvalidTime("that's not a valid time, please re-enter");

}

}

public static int getMinutes(String time) {

MyTime mt = new MyTime();

String[] separatedHourAndMin = time.split(":");

String[] separatedMinandAM_PM = separatedHourAndMin[1].split(" ");

int min = Integer.parseInt(separatedMinandAM_PM[0]);

if (min >= 0 && min <= 60) {

return min;

} else {

System.out.println("that's not a valid time, please re-enter");

try {

mt.setTime();

} catch (InvalidTime e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return min;

}

public static boolean isAm(String time) throws InvalidTime {

String[] separatedHourAndMin = time.split(":");

String[] separatedMinandAM_PM = separatedHourAndMin[1].split(" ");

if (separatedMinandAM_PM[1].equalsIgnoreCase("am")) {

return true;

} else {

throw new InvalidTime("that's not a valid time, please re-enter");

}

}

public String AmOrPM(String time) throws InvalidTime {

String[] separatedHourAndMin = time.split(":");

String[] separatedMinandAM_PM = separatedHourAndMin[1].split(" ");

if (separatedMinandAM_PM[1].equalsIgnoreCase("am")) {

return "am";

} else {

if (separatedMinandAM_PM[1].equalsIgnoreCase("pm")) {

return "pm";

} else {

throw new InvalidTime("that's not a valid time, please re-enter");

}

}

}

public void greetUser(String timeAmOrPM) throws InvalidTime {

MyTime mt = new MyTime();

if (getHours(timeAmOrPM) >= 6 && getHours(timeAmOrPM) < 12 && AmOrPM(timeAmOrPM).equalsIgnoreCase("pm")) {

System.out.println("good evening");

mt.setTime();

;

} else {

if (getHours(timeAmOrPM) >= 0 && getHours(timeAmOrPM) < 3 && AmOrPM(timeAmOrPM).equalsIgnoreCase("am")) {

System.out.println("good night");

mt.setTime();

} else {

if (getHours(timeAmOrPM) >= 3 && getHours(timeAmOrPM) < 11

&& AmOrPM(timeAmOrPM).equalsIgnoreCase("am")) {

System.out.println("good morning");

mt.setTime();

;

} else {

if ((getHours(timeAmOrPM) >= 12 || getHours(timeAmOrPM) < 6)

&& AmOrPM(timeAmOrPM).equalsIgnoreCase("pm")) {

System.out.println("good afternoon");

mt.setTime();

} else {

System.out.println("that's not a valid time, please re-enter");

mt.setTime();

}

}

}

}

}

public static String askTime() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter time: ");

return sc.nextLine();

}

}

 

public class InvalidTime extends Exception{

InvalidTime(String s){

super(s);

}

}

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education