diff --git a/week1.java b/week1.java index 17d4c59..d2c9dff 100644 --- a/week1.java +++ b/week1.java @@ -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(); + } } }