Merge pull request #42 from TiagoRG/dev-tiagorg

# Commits:
- [POO] aula05 guide update
- [POO] aula05 ex2 added
- [POO] aula05 finished
This commit is contained in:
Tiago Garcia 2023-03-14 20:04:00 +00:00 committed by GitHub
commit cf3862a339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 370 additions and 66 deletions

View File

@ -0,0 +1,129 @@
package aula05;
public class AuctionDemo {
public static void main(String[] args) {
RealEstate imobiliaria = new RealEstate();
imobiliaria.newProperty("Glória", 2, 30000);
imobiliaria.newProperty("Vera Cruz", 1, 25000);
imobiliaria.newProperty("Santa Joana", 3, 32000);
imobiliaria.newProperty("Aradas", 2, 24000);
imobiliaria.newProperty("São Bernardo", 2, 20000);
imobiliaria.sell(1001);
imobiliaria.setAuction(1002, new DateYMD(21, 3, 2023), 4);
imobiliaria.setAuction(1003, new DateYMD(1, 4, 2023), 3);
imobiliaria.setAuction(1001, new DateYMD(1, 4, 2023), 4);
imobiliaria.setAuction(1010, new DateYMD(1, 4, 2023), 4);
System.out.println(imobiliaria);
}
}
class RealEstate {
private final Property[] properties;
private int currentId;
public RealEstate() {
this.properties = new Property[10];
this.currentId = 1000;
}
public void newProperty(String address, int rooms, int price) {
Property newProperty = new Property(currentId++, address, rooms, price);
for (int i = 0; i < this.properties.length; i++) {
if (this.properties[i] == null) {
this.properties[i] = newProperty;
break;
}
}
}
public void sell(int id) {
for (Property property : this.properties) {
if (property != null && property.getId() == id) {
if (!property.isAvailable()) {
System.out.printf("Imóvel %d não está disponível.\n", id);
return;
} else {
property.setAvailability(false);
System.out.printf("Imóvel %d vendido.\n", id);
return;
}
}
}
System.out.printf("Imóvel %d não existe.\n", id);
}
public void setAuction(int id, DateYMD date, int duration) {
for (Property property : this.properties) {
if (property != null && property.getId() == id) {
if (!property.isAvailable()) {
System.out.printf("Imóvel %d não está disponível.\n", id);
return;
} else {
DateYMD end = new DateYMD(date.getDay(), date.getMonth(), date.getYear());
end.addDays(duration);
property.setAuction(date, end);
return;
}
}
}
System.out.printf("Imóvel %d não existe.\n", id);
}
public String toString() {
StringBuilder result = new StringBuilder().append("Propriedades:\n");
for (Property property : this.properties)
if (property != null)
result.append(property).append("\n");
return result.toString();
}
}
class Property {
private final int id;
private final String address;
private final int rooms;
private final int price;
private boolean availability;
private DateYMD auctionBegin;
private DateYMD auctionEnd;
public Property(int id, String address, int rooms, int price) {
this.id = id;
this.address = address;
this.rooms = rooms;
this.price = price;
this.availability = true;
this.auctionBegin = null;
this.auctionEnd = null;
}
public int getId() {
return this.id;
}
public boolean isAvailable() {
return this.availability;
}
public void setAvailability(boolean availability) {
this.availability = availability;
}
public void setAuction(DateYMD begin, DateYMD end) {
this.auctionBegin = begin;
this.auctionEnd = end;
}
public DateYMD[] getAuction() {
return new DateYMD[] {this.auctionBegin, this.auctionEnd};
}
public boolean isAuction() {
return this.auctionBegin != null && this.auctionEnd != null;
}
public String toString() {
return String.format("Imóvel %d; quartos: %d; localidade: %s; preço: %d; disponível: %s%s", this.id, this.rooms, this.address, this.price, this.availability ? "sim" : "não", this.isAuction() ? String.format("; leilão: %s : %s", this.auctionBegin, this.auctionEnd) : "");
}
}

View File

@ -0,0 +1,169 @@
package aula05;
import java.util.Arrays;
import java.util.Scanner;
public class Calendar {
private final int year;
private final int firstDayOfWeek;
private final int[][] events;
public Calendar(int year, int firstDayOfWeek) {
this.year = year;
this.firstDayOfWeek = firstDayOfWeek;
this.events = new int[366][3];
}
public int getYear() {
return this.year;
}
public int getFirstDayOfWeek() {
return this.firstDayOfWeek;
}
public int firstWeekdayOfMonth(int month) {
int day = this.firstDayOfWeek;
for (int m = 1; m < month; m++) {
if (day == 7)
day = 0;
int days = DateYMD.monthDays(m, this.year);
day += days % 7 - 1;
if (day > 7)
day -= 7;
day++;
}
return day == 7 ? 7 : day%7;
}
public void printMonth(int month) {
String[] monthNames = {"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
System.out.printf("\n%15s %d\n", monthNames[month-1], year);
System.out.println("Dom Seg Ter Qua Qui Sex Sab");
int firstWeekday = this.firstWeekdayOfMonth(month);
for (int i = 1; i < firstWeekday; i++)
System.out.print(" ");
for (int monthDay = 1; monthDay <= monthDays(month, year); monthDay++) {
System.out.print(this.isEvent(monthDay, month, this.year) ? String.format("*%2d ", monthDay) : String.format("%3d ", monthDay));
if ((monthDay + firstWeekday - 1) % 7 == 0)
System.out.println();
}
System.out.println();
}
public void addEvent(int day, int month, int year) {
int[] eventToAdd = {day, month, year};
for (int i = 0; i < this.events.length; i++) {
if (Arrays.equals(this.events[i], new int[]{0, 0, 0})) {
this.events[i] = eventToAdd;
break;
}
}
}
public void removeEvent(int day, int month, int year) {
int[] eventToRemove = {day, month, year};
for (int i = 0; i < this.events.length; i++) {
if (this.events[i] == eventToRemove) {
this.events[i] = new int[]{0, 0, 0};
break;
}
}
}
private boolean isEvent(int day, int month, int year) {
int[] event = {day, month, year};
for (int[] ints : this.events) {
if (Arrays.equals(ints, event))
return true;
}
return false;
}
private int monthDays(int month, int year) {
switch (month) {
case 2 -> {
if (year % 100 == 0 ? year % 400 == 0 : year % 4 == 0)
return 29;
else
return 28;
}
case 4, 6, 9, 11 -> {
return 30;
}
default -> {
return 31;
}
}
}
}
class Tester {
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
Calendar calendar = null;
while (true) {
System.out.print("Calendar operations:\n1 - create new calendar\n2 - print calendar month\n3 - print calendar\n4 - add event\n5 - remove event\n6 - exit\n> ");
int op = sin.nextInt();
switch (op) {
case 1 -> {
System.out.print("Year: ");
int year = sin.nextInt();
System.out.print("First day of week: ");
int firstDayOfWeek = sin.nextInt();
calendar = new Calendar(year, firstDayOfWeek);
}
case 2 -> {
if (calendar == null) {
System.out.println("Create a calendar first");
break;
}
System.out.print("Month: ");
int month = sin.nextInt();
calendar.printMonth(month);
}
case 3 -> {
if (calendar == null) {
System.out.println("Create a calendar first");
break;
}
for (int month = 1; month <= 12; month++)
calendar.printMonth(month);
}
case 4 -> {
if (calendar == null) {
System.out.println("Create a calendar first");
break;
}
System.out.print("Day: ");
int day = sin.nextInt();
System.out.print("Month: ");
int month = sin.nextInt();
System.out.print("Year: ");
int year = sin.nextInt();
calendar.addEvent(day, month, year);
}
case 5 -> {
if (calendar == null) {
System.out.println("Create a calendar first");
break;
}
System.out.print("Day: ");
int day = sin.nextInt();
System.out.print("Month: ");
int month = sin.nextInt();
System.out.print("Year: ");
int year = sin.nextInt();
calendar.removeEvent(day, month, year);
}
case 6 -> {
sin.close();
System.exit(0);
}
default -> System.out.println("Invalid option");
}
}
}
}

View File

@ -3,33 +3,11 @@ package aula05;
import java.util.Scanner; import java.util.Scanner;
public class DateYMD { 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 day;
private int month; private int month;
private int year; private int year;
public Date(int day, int month, int year) { public DateYMD(int day, int month, int year) {
if (!validDate(day, month, year)) if (!validDate(day, month, year))
throw new IllegalArgumentException("Invalid date"); throw new IllegalArgumentException("Invalid date");
this.day = day; this.day = day;
@ -45,6 +23,10 @@ public class DateYMD {
this.year = year; this.year = year;
} }
public int[] get() {
return new int[]{this.day, this.month, this.year};
}
public int getDay() { public int getDay() {
return day; return day;
} }
@ -70,6 +52,11 @@ public class DateYMD {
} }
} }
public void addDays(int days) {
for (int i = 0; i < days; i++)
this.increment();
}
public void decrement() { public void decrement() {
if (this.day > 1) if (this.day > 1)
this.day--; this.day--;
@ -86,13 +73,32 @@ public class DateYMD {
public String toString() { public String toString() {
return String.format("%04d-%02d-%02d", year, month, day); return String.format("%04d-%02d-%02d", year, month, day);
} }
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 TestDateYMD { class TestDateYMD {
public static void main(String[] args) { public static void main(String[] args) {
Scanner sin = new Scanner(System.in); Scanner sin = new Scanner(System.in);
DateYMD.Date date = null; DateYMD date = null;
while (true) { while (true) {
System.out.println("Date operations:"); System.out.println("Date operations:");
System.out.println("1 - Create date"); System.out.println("1 - Create date");
@ -112,7 +118,7 @@ class TestDateYMD {
int month = sin.nextInt(); int month = sin.nextInt();
System.out.print("Year: "); System.out.print("Year: ");
int year = sin.nextInt(); int year = sin.nextInt();
date = new DateYMD().new Date(day, month, year); date = new DateYMD(day, month, year);
System.out.println("Date created: " + date); System.out.println("Date created: " + date);
break; break;
case 2: case 2: