-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.java
More file actions
108 lines (104 loc) · 3 KB
/
Copy pathTime.java
File metadata and controls
108 lines (104 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
public class Time {
private int hour;
private int minute;
/**
* Sets the local variables to their passed in parameters
* @param hour for the time
* @param minute for the time
*/
public Time(int hour, int minute) {
this.hour = hour;
this.minute = minute;
}
/**
* Sets the local variables to their passed in parameters
* @param hour for the time
* @param minute set to 0
*/
public Time(int hour) {
this.hour = hour;
this.minute = 0;
}
/**
* Alternative Constructor for Time
* @param time in the format of ("hh:mm am")
* or ("hh:mm pm")
*/
public Time(String time) {
//If we get "9:34 am", get {"9", "34 am"}
String[] firstCut = time.split(":");
//In our example this will have {"34", "am"}
String[] secondCut = firstCut[1].split(" ");
this.hour = Integer.parseInt(firstCut[0]);
this.minute = Integer.parseInt(secondCut[0]);
if (secondCut[1].equalsIgnoreCase("pm") && hour < 12 ||
(hour == 12 && secondCut[1].equalsIgnoreCase("am"))) {
this.hour += 12;
}
}
//Getters
public int getHour() {
return hour;
}
public int getMinute(){
return minute;
}
//Setters
public void setHour(int hour) {
this.hour = hour;
}
public void setMinute(int minute) {
this.minute = minute;
}
//Other Methods
public String toString() {
//If AM Time
if (checkAMHour(hour)) {
return makeTimeString(hour, minute) + " am";
}
else {
return makeTimeString(hour, minute) + " pm";
}
}
/**
* Check which period of the day the hour falls into
* @param hour the hour of time
* @return Whether the time falls in am (T) or pm (F)
*/
static boolean checkAMHour(int hour) {
return hour < 12 || hour == 24;
}
/**
* For the sake of printing to the terminal:
* figure out if a number will need a 0 before it
* @param number the hour or minute in question
* @return Whether a 0 should be added before the number in the print string
*/
static boolean checkBelow10(int number) {
return number < 10;
}
/**
* Express Time as a string to be printed
* @param hour the hour from Time
* @param minute the minute from Time
* @return a string representation of the Time object
*/
static String makeTimeString(int hour, int minute) {
hour %= 12;
if (hour == 0) {
hour = 12;
}
if (checkBelow10(hour) && checkBelow10(minute)){
return "0" + hour + ":0" + minute;
}
else if (checkBelow10(hour) && !checkBelow10(minute)) {
return "0" + hour + ":" + minute;
}
else if (!checkBelow10(hour) && checkBelow10(minute)) {
return hour + ":0" + minute;
}
else {
return hour + ":" + minute;
}
}
}