-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateCalculator.java
More file actions
89 lines (74 loc) · 2.77 KB
/
DateCalculator.java
File metadata and controls
89 lines (74 loc) · 2.77 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
package assign2;
/* Name: Adam Mohr
* Section: 301
* Lab Teacher: Jason Mombourquette
* Purpose of Program: Calculate the difference between two given dates using classes and if statements.
* Assignment Number #2
* Date: Friday, March 16th 2018
*/
public class DateCalculator {
private OurDate firstDate; // Declare two OurDate objects.
private OurDate secondDate;
private String message; // String to store the comparison message.
public DateCalculator() { // Default constructor.
System.out.println("Date Calculator - it's all relative\n");
}
public void inputDates() { // Receive input for the two dates.
System.out.println("Enter first date");
firstDate = new OurDate();
inputDate(firstDate);
System.out.println("Enter second date");
secondDate = new OurDate();
inputDate(secondDate);
}
private void inputDate(OurDate date) { // Private method to simplify date entry.
date.inputYear();
date.inputMonth();
date.inputDay();
System.out.println("");
}
public void calculateDifference() { // Calculate the difference between the two dates and store the value in message.
int difference = firstDate.calcDays() - secondDate.calcDays(); // Instance variable to store the difference between the two dates.
String laterOrEarlier; // String will display if the firstDate is earlier or later than the secondDate.
if (difference == 0) {
message = " is the same as ";
return;
}
if (difference < 0) {
laterOrEarlier = "earlier than ";
}
else {
laterOrEarlier = "later than ";
}
difference = Math.abs(difference); // Convert negative difference to positive so the message will not display negative numbers.
if (difference >= 720) {
message = " is " + difference / 360 + " years " + laterOrEarlier; // Assume 360 days in year.
}
else if (difference >= 360) {
message = " is " + difference / 360 + " year " + laterOrEarlier;
}
else if (difference >= 60) {
message = " is " + difference / 30 + " months " + laterOrEarlier; // Assume 30 days per month.
}
else if (difference >= 30) {
message = " is " + difference / 30 + " month " + laterOrEarlier;
}
else if (difference >= 14) {
message = " is " + difference / 7 + " weeks " + laterOrEarlier;
}
else if (difference >= 7) {
message = " is " + difference / 7 + " week " + laterOrEarlier;
}
else if (difference > 1) {
message = " is " + difference + " days " + laterOrEarlier;
}
else {
message = " is " + difference + " day " + laterOrEarlier;
}
}
public void display() { // Displays whether the firstDate is earlier or later than the secondDate and by how much.
firstDate.displayDate();
System.out.print(message);
secondDate.displayDate();
}
}