From 263a2b39a39d6bc95192612caa627c949afb04b1 Mon Sep 17 00:00:00 2001 From: Rob Hallam <0504004h@student.gla.ac.uk> Date: Wed, 20 Sep 2023 13:30:23 +0100 Subject: [PATCH] (feat) Implement Lab 5 exercise 7 - show user age --- week1.java | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) 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(); + } } }