Browse Source

(feat) Implement Lab 5 exercise 7 - show user age

main
Rob Hallam 1 year ago
parent
commit
263a2b39a3
1 changed files with 33 additions and 2 deletions
  1. +33
    -2
      week1.java

+ 33
- 2
week1.java View File

@@ -5,8 +5,8 @@ import java.util.concurrent.ThreadLocalRandom;

class runLabs {
public static void main(String[] args) {
new Lab4();
// new Lab5();
// new Lab4();
new Lab5();
// new Lab6();
// new Lab7();
}
@@ -368,6 +368,37 @@ class Lab5 extends Lab {
class five7 extends Exercise {
public five7() {
super(7);
showUserAge();
}

public void showUserAge() {
Scanner userInput = new Scanner(System.in);
System.out.println(String.format("Please input your DOB"));
System.out.println(String.format("input format is YYYY M D"));
String inputString = userInput.nextLine();

Scanner processInput = new Scanner(inputString);

int dobYear = processInput.nextInt();
int dobMonth = processInput.nextInt();
int dobDay = processInput.nextInt();
// input sanitisation could be nice...

Calendar userDOB = Calendar.getInstance();
// recall months are zero-indexed!
userDOB.set(dobYear, dobMonth-1, dobDay);

Calendar today = Calendar.getInstance();

System.out.println(String.format("Today:\t%s", today.getTime()));
System.out.println(String.format(
"DOB :\t%s (difference: %d years)",
userDOB.getTime(),
Lab4.yearsOld(today, userDOB)));

userInput.close();
processInput.close();

}
}
}


Loading…
Cancel
Save