[POO] aula07 ex2 menu created

This commit is contained in:
TiagoRG 2023-03-28 19:03:55 +01:00
parent d336440300
commit 4098cf1156
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 127 additions and 42 deletions

View File

@ -77,11 +77,8 @@ public class DateND extends Date {
@Override @Override
public void removeDays(int days) { public void removeDays(int days) {
if (days > this.absDay) for (int i = 0; i < days; i++)
this.absDay = 0; this.decrement();
else
for (int i = 0; i < days; i++)
this.decrement();
} }
@Override @Override

View File

@ -6,48 +6,136 @@ public class DateTest {
public static void main(String[] args) { public static void main(String[] args) {
Scanner sin = new Scanner(System.in); Scanner sin = new Scanner(System.in);
while (true) { mainLoop: while (true) {
System.out.print("Class to test (0-Quit;1-DateYMD;2-DateND): "); System.out.print("Class to test (0-Quit;1-DateYMD;2-DateND): ");
String option = sin.nextLine(); int classoption = sin.nextInt();
switch (option) { switch (classoption) {
case "0" -> { case 0 -> {
sin.close(); sin.close();
System.exit(0); break mainLoop;
} }
case "1" -> { case 1 -> {
System.out.print("Day: "); DateYMD date = null;
int day = Integer.parseInt(sin.nextLine()); class1Loop: while (true) {
System.out.print("Month: "); System.out.println("Date operations:");
int month = Integer.parseInt(sin.nextLine()); System.out.println("1 - Create date");
System.out.print("Year: "); System.out.println("2 - Show current date");
int year = Integer.parseInt(sin.nextLine()); System.out.println("3 - Show current date (absolute day format)");
System.out.println("4 - Increment date");
DateYMD date = new DateYMD(day, month, year); System.out.println("5 - Decrement date");
System.out.printf("Initial date: %s%n%n", date); System.out.println("0 - Exit");
System.out.printf("Absolute day: %d%n", date.getAbsDay()); System.out.print("Option: ");
System.out.println("Adding 7 days..."); int option = sin.nextInt();
date.addDays(7); if (option == 0)
System.out.printf("New absolute day: %d%n", date.getAbsDay()); break class1Loop;
System.out.println("Removing 534 days..."); class1Switch: switch (option) {
date.removeDays(534); case 1 -> {
System.out.printf("New absolute day: %d%n", date.getAbsDay()); System.out.print("Day: ");
System.out.println("Final date: " + date); int day = sin.nextInt();
System.out.print("Month: ");
int month = sin.nextInt();
System.out.print("Year: ");
int year = sin.nextInt();
date = new DateYMD(day, month, year);
System.out.println("Date created: " + date);
}
case 2 -> {
if (date == null) {
System.out.println("Date not created");
break class1Switch;
}
System.out.println("Current date: " + date);
}
case 3 -> {
if (date == null) {
System.out.println("Date not created");
break class1Switch;
}
System.out.println("Current date: " + new DateND(date.getAbsDay()));
}
case 4 -> {
if (date == null) {
System.out.println("Date not created");
break class1Switch;
}
System.out.print("Number of days to increment date by: ");
int daysToIncrement = sin.nextInt();
date.addDays(daysToIncrement);
System.out.println("Date incremented: " + date);
}
case 5 -> {
if (date == null) {
System.out.println("Date not created");
break class1Switch;
}
System.out.print("Number of days to decremente date by: ");
int daysToDecrement = sin.nextInt();
date.removeDays(daysToDecrement);
System.out.println("Date decremented: " + date);
}
default -> System.out.println("Invalid option");
}
}
} }
case "2" -> { case 2 -> {
System.out.print("Days past past 01-01-2000: "); DateND date = null;
int days = Integer.parseInt(sin.nextLine()); class2Loop: while (true) {
System.out.println("Date operations:");
DateND date = new DateND(days); System.out.println("1 - Create date");
System.out.printf("Initial date: %s%n%n", date); System.out.println("2 - Show current date");
System.out.printf("YMD date: %s%n", new DateYMD(date.getDay(), date.getMonth(), date.getYear())); System.out.println("3 - Show current date (YMD days format)");
System.out.println("Adding 74 days..."); System.out.println("4 - Increment date");
date.addDays(74); System.out.println("5 - Decrement date");
System.out.printf("New YMD date: %s%n", new DateYMD(date.getDay(), date.getMonth(), date.getYear())); System.out.println("0 - Exit");
System.out.println("Removing 612 days..."); System.out.print("Option: ");
date.removeDays(612); int option = sin.nextInt();
System.out.printf("New YMD date: %s%n", new DateYMD(date.getDay(), date.getMonth(), date.getYear())); if (option == 0)
System.out.println("Final date: " + date); break class2Loop;
class2Switch: switch (option) {
case 1 -> {
System.out.print("Day: ");
int day = sin.nextInt();
date = new DateND(day);
System.out.println("Date created: " + date);
}
case 2 -> {
if (date == null) {
System.out.println("Date not created");
break class2Switch;
}
System.out.println("Current date: " + date);
}
case 3 -> {
if (date == null) {
System.out.println("Date not created");
break class2Switch;
}
System.out.println("Current date: " + new DateYMD(date.getDay(), date.getMonth(), date.getYear()));
}
case 4 -> {
if (date == null) {
System.out.println("Date not created");
break class2Switch;
}
System.out.print("Number of days to increment date by: ");
int daysToIncrement = sin.nextInt();
date.addDays(daysToIncrement);
System.out.println("Date incremented: " + date);
}
case 5 -> {
if (date == null) {
System.out.println("Date not created");
break class2Switch;
}
System.out.print("Number of days to decremente date by: ");
int daysToDecrement = sin.nextInt();
date.removeDays(daysToDecrement);
System.out.println("Date decremented: " + date);
}
default -> System.out.println("Invalid option");
}
}
} }
default -> System.out.println("Invalid option."); default -> System.out.println("Invalid option.");
} }