From 0bd456bbc1b6dcab1188b578174b24a3798fb1f7 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sat, 11 Mar 2023 17:59:41 +0000 Subject: [PATCH] [POO] aula05 ex1 added --- 1ano/2semestre/poo/src/aula05/DateYMD.java | 147 +++++++++++++++++++++ 1ano/2semestre/poo/src/aula05/README.md | 6 +- 2 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 1ano/2semestre/poo/src/aula05/DateYMD.java diff --git a/1ano/2semestre/poo/src/aula05/DateYMD.java b/1ano/2semestre/poo/src/aula05/DateYMD.java new file mode 100644 index 0000000..e9b075c --- /dev/null +++ b/1ano/2semestre/poo/src/aula05/DateYMD.java @@ -0,0 +1,147 @@ +package aula05; + +import java.util.Scanner; + +public class DateYMD { + static boolean validMonth(int month) { + return month >= 1 && month <= 12; + } + + static int monthDays(int month, int year) { + if (!validMonth(month)) + return -1; + int[] daysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if (month == 2 && isLeapYear(year)) + return 29; + return daysPerMonth[month - 1]; + } + + static boolean isLeapYear(int year) { + return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0; + } + + static boolean validDate(int day, int month, int year) { + return day >= 1 && day <= monthDays(month, year); + } + + class Date { + private int day; + private int month; + private int year; + + public Date(int day, int month, int year) { + if (!validDate(day, month, year)) + throw new IllegalArgumentException("Invalid date"); + this.day = day; + this.month = month; + this.year = year; + } + + public void set(int day, int month, int year) { + if (!validDate(day, month, year)) + throw new IllegalArgumentException("Invalid date"); + this.day = day; + this.month = month; + this.year = year; + } + + public int getDay() { + return day; + } + + public int getMonth() { + return month; + } + + public int getYear() { + return year; + } + + public void increment() { + if (this.day < monthDays(this.month, this.year)) + this.day++; + else if (this.month < 12) { + this.day = 1; + this.month++; + } else { + this.day = 1; + this.month = 1; + this.year++; + } + } + + public void decrement() { + if (this.day > 1) + this.day--; + else if (this.month > 1) { + this.day = monthDays(this.month - 1, this.year); + this.month--; + } else { + this.day = 31; + this.month = 12; + this.year--; + } + } + + public String toString() { + return String.format("%04d-%02d-%02d", year, month, day); + } + } +} + +class TestDateYMD { + public static void main(String[] args) { + Scanner sin = new Scanner(System.in); + DateYMD.Date date = null; + while (true) { + System.out.println("Date operations:"); + System.out.println("1 - Create date"); + System.out.println("2 - Show current date"); + System.out.println("3 - Increment date"); + System.out.println("4 - Decrement date"); + System.out.println("0 - Exit"); + System.out.print("Option: "); + int option = sin.nextInt(); + if (option == 0) + break; + switch (option) { + case 1: + System.out.print("Day: "); + int day = sin.nextInt(); + System.out.print("Month: "); + int month = sin.nextInt(); + System.out.print("Year: "); + int year = sin.nextInt(); + date = new DateYMD().new Date(day, month, year); + System.out.println("Date created: " + date); + break; + case 2: + if (date == null) { + System.out.println("Date not created"); + break; + } + System.out.println("Current date: " + date); + break; + case 3: + if (date == null) { + System.out.println("Date not created"); + break; + } + date.increment(); + System.out.println("Date incremented: " + date); + break; + case 4: + if (date == null) { + System.out.println("Date not created"); + break; + } + date.decrement(); + System.out.println("Date decremented: " + date); + break; + default: + System.out.println("Invalid option"); + } + } + sin.close(); + } +} diff --git a/1ano/2semestre/poo/src/aula05/README.md b/1ano/2semestre/poo/src/aula05/README.md index 1a55736..9274d24 100755 --- a/1ano/2semestre/poo/src/aula05/README.md +++ b/1ano/2semestre/poo/src/aula05/README.md @@ -6,9 +6,9 @@ * [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_04_Herança.pdf) ### Exercise List -| Exercise Number | File Name | -|-----------------|----------------------------------------------------------------------------------------------------------------------| -| | | +| Exercise Number | File Name | +|-----------------|----------------------------------------------------------------------------------------------------------------| +| 1 | [DateYMD.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula05/DateYMD.java) | --- *Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)