-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuration.java
More file actions
100 lines (86 loc) · 3.34 KB
/
Duration.java
File metadata and controls
100 lines (86 loc) · 3.34 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
import java.text.DecimalFormat;
public class Duration {
private int hours;
private int minutes;
private int seconds;
public static void main(String[] args) {
//TEST HARNESS
Duration d1 = new Duration();
Duration d2 = new Duration(0,3,24);
Duration d3 = new Duration("00:03:45");
Duration d4 = new Duration("00:03:20");
d2.getTotal();
d2.getSeconds();
d2.getMinutes();
d2.getHours();
d2.toString();
d2.add(d3);
//100% Method coverage
}
//Constructor that takes integer arguments to build Duration with.
public Duration(int hours, int minutes, int seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
//Default Constructor for Duration
public Duration() {
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
//Constructor that accepts String input and parses as Integer
public Duration(String duration) {
String[] time = duration.split(":");
String stringHours = time[0];
String stringMinutes = time[1];
String stringSeconds = time[2];
hours = Integer.parseInt(stringHours);
minutes = Integer.parseInt(stringMinutes);
seconds = Integer.parseInt(stringSeconds);
}
//toString method that converts a Duration object into a String object
public String toString() {
DecimalFormat df = new DecimalFormat("00");
String stringHours = df.format(this.hours);
String stringMinutes = df.format(this.minutes);
String stringSeconds = df.format(this.seconds);
return stringHours + ":" + stringMinutes + ":" + stringSeconds;
}
//Add method that accepts another Duration object as a parameter to add to the original object
public void add(Duration d2) {
if (seconds + d2.seconds >= 60) {
minutes += ((seconds + d2.seconds) / 60); //Finds out the amount of extra minutes and adds that result to the minutes variable
seconds = ((seconds + d2.seconds) % 60); //Finds out the remainder to set the new seconds
} else {
seconds += d2.seconds;
}
if (minutes + d2.minutes >= 60) {
hours += ((minutes + d2.minutes) / 60); //Finds out the amount of extra hours and adds that result to the hours variable
minutes = ((minutes + d2.minutes) % 60); //Finds out the remainder to set the new minutes
} else {
minutes += d2.minutes;
}
hours += d2.hours;
}
//Getter methods for each variable
public int getHours() {
return hours;
}
public int getMinutes() {
return minutes;
}
public int getSeconds() {
return seconds;
}
//The getTotal method creates a concatenated String of a Tracks attributes and is then parsed as an int
//This is so that a comparison can be made easily
public int getTotal() {
//This String object tot creates a String with the int values of a Track object
//No arithmetic is carried out as the int values are concatenated
String tot = "" + hours + minutes + seconds;
int total = Integer.parseInt(tot);
//The String is parsed back to an int value to act as the final comparison value and returned as total
return total;
}
}