Skip to content

Commit 9dcda26

Browse files
authored
Merge pull request #1 from ProgrammingMadeEasy-Community/fix/review
Code review
2 parents f101f1e + 10c1c95 commit 9dcda26

File tree

4 files changed

+77
-58
lines changed

4 files changed

+77
-58
lines changed

Diff for: src/main/java/org/javatasks/agecomparism/AgeComparison.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,25 @@ public static void writePerson(Person person) {
4141
};
4242

4343
System.out.println("Name: " + person.name);
44-
System.out.println("Birthday: " + monthNames[person.birthday.month - 1] + " " + person.birthday.day + ", " + person.birthday.year);
44+
System.out.println("Birthday: " + monthNames[person.birthday.getMonth() - 1] + " " + person.birthday.getDay() + ", " + person.birthday.getYear());
4545
}
4646

4747
public static void whoIsOlder(Person person1, Person person2) {
48-
int yearDifference = person1.birthday.year - person2.birthday.year;
48+
int yearDifference = person1.birthday.getYear() - person2.birthday.getYear();
4949

5050
if (yearDifference < 0) {
5151
System.out.println(person1.name + " is younger than " + person2.name + " by " + (-yearDifference) + " years.");
5252
} else if (yearDifference > 0) {
5353
System.out.println(person1.name + " is older than " + person2.name + " by " + yearDifference + " years.");
5454
} else {
55-
int monthDifference = person1.birthday.month - person2.birthday.month;
55+
int monthDifference = person1.birthday.getMonth() - person2.birthday.getMonth() ;
5656

5757
if (monthDifference < 0) {
5858
System.out.println(person1.name + " is younger than " + person2.name + " by a few months.");
5959
} else if (monthDifference > 0) {
6060
System.out.println(person1.name + " is older than " + person2.name + " by a few months.");
6161
} else {
62-
int dayDifference = person1.birthday.day - person2.birthday.day;
62+
int dayDifference = person1.birthday.getDay() - person2.birthday.getDay();
6363

6464
if (dayDifference < 0) {
6565
System.out.println(person1.name + " is younger than " + person2.name + " by a few days.");

Diff for: src/main/java/org/javatasks/agecomparism/Date.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22

33

44
public class Date {
5-
int year;
6-
int month;
7-
int day;
5+
private int year;
6+
private int month;
7+
private int day;
88

99
Date(int year, int month, int day) {
1010
this.year = year;
1111
this.month = month;
1212
this.day = day;
1313
}
14+
15+
public int getYear() {
16+
return year;
17+
}
18+
19+
public int getMonth() {
20+
return month;
21+
}
22+
23+
public int getDay() {
24+
return day;
25+
}
1426
}

Diff for: src/main/java/org/javatasks/daysoftheweek/DaysOfTheWeekProgram.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void DaysOfTheWeek(int dayNumber) {
4646
// Task 3: Method to store and count user inputs
4747
public static void DaysOfTheWeek(int... dayNumbers) {
4848
try {
49-
Map<DayOfWeek, Integer> dayCountMap = new HashMap<>();
49+
Map <DayOfWeek, Integer> dayCountMap = new HashMap<>();
5050
for (int dayNumber : dayNumbers) {
5151
DayOfWeek day = DayOfWeek.values()[dayNumber - 1];
5252
dayCountMap.put(day, dayCountMap.getOrDefault(day, 0) + 1);

Diff for: src/main/java/org/javatasks/simplecalculator/SimpleCalculator.java

+57-50
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,72 @@ public static void main(String[] args) {
1010
int attempts = 5;
1111

1212
System.out.println("Simple Calculator");
13-
14-
double firstNumber = getUserInput("Enter the first number: ");
15-
double secondNumber = getUserInput("Enter the second number: ");
16-
int operationChoice;
17-
18-
while (true) {
19-
System.out.println("Choose an operation:");
20-
System.out.println("1. Addition");
21-
System.out.println("2. Subtraction");
22-
System.out.println("3. Multiplication");
23-
System.out.println("4. Division");
24-
System.out.print("Enter the number corresponding to the operation: ");
25-
26-
if (scanner.hasNextInt()) {
27-
operationChoice = scanner.nextInt();
28-
if (operationChoice >= 1 && operationChoice <= 4) {
29-
break;
13+
char recur;
14+
15+
do {
16+
double firstNumber = getUserInput("Enter the first number: ");
17+
double secondNumber = getUserInput("Enter the second number: ");
18+
int operationChoice;
19+
20+
while (true) {
21+
System.out.println("Choose an operation:");
22+
System.out.println("1. Addition");
23+
System.out.println("2. Subtraction");
24+
System.out.println("3. Multiplication");
25+
System.out.println("4. Division");
26+
System.out.print("Enter the number corresponding to the operation: ");
27+
28+
if (scanner.hasNextInt()) {
29+
operationChoice = scanner.nextInt();
30+
if (operationChoice >= 1 && operationChoice <= 4) {
31+
break;
32+
} else {
33+
System.out.println("Invalid operation choice. Please enter a number between 1 and 4.");
34+
scanner.nextLine(); // Clear the input buffer
35+
}
3036
} else {
31-
System.out.println("Invalid operation choice. Please enter a number between 1 and 4.");
37+
System.out.println("Invalid input. Please enter a valid number.");
3238
scanner.nextLine(); // Clear the input buffer
3339
}
34-
} else {
35-
System.out.println("Invalid input. Please enter a valid number.");
36-
scanner.nextLine(); // Clear the input buffer
40+
41+
attempts--;
42+
43+
if (attempts == 0) {
44+
System.out.println("You've reached the maximum number of attempts. Exiting the program.");
45+
System.exit(1);
46+
}
3747
}
3848

39-
attempts--;
49+
double result = 0.0;
4050

41-
if (attempts == 0) {
42-
System.out.println("You've reached the maximum number of attempts. Exiting the program.");
43-
System.exit(1);
51+
switch (operationChoice) {
52+
case 1:
53+
result = firstNumber + secondNumber;
54+
break;
55+
case 2:
56+
result = firstNumber - secondNumber;
57+
break;
58+
case 3:
59+
result = firstNumber * secondNumber;
60+
break;
61+
case 4:
62+
if (secondNumber != 0) {
63+
result = firstNumber / secondNumber;
64+
} else {
65+
System.out.println("Error: Division by zero is not allowed.");
66+
System.exit(1);
67+
}
68+
break;
69+
default:
70+
break;
4471
}
45-
}
4672

47-
double result = 0.0;
48-
49-
switch (operationChoice) {
50-
case 1:
51-
result = firstNumber + secondNumber;
52-
break;
53-
case 2:
54-
result = firstNumber - secondNumber;
55-
break;
56-
case 3:
57-
result = firstNumber * secondNumber;
58-
break;
59-
case 4:
60-
if (secondNumber != 0) {
61-
result = firstNumber / secondNumber;
62-
} else {
63-
System.out.println("Error: Division by zero is not allowed.");
64-
System.exit(1);
65-
}
66-
break;
67-
default:
68-
break;
69-
}
73+
System.out.println("Result: " + result);
74+
75+
System.out.println("Do you wish to perform another operation? Enter y for YES; any other key means NO\n");
76+
recur = scanner.next().charAt(0);
7077

71-
System.out.println("Result: " + result);
78+
} while (recur == 'y');
7279
}
7380

7481
private static double getUserInput(String prompt) {

0 commit comments

Comments
 (0)