[POO] aula05 finished

This commit is contained in:
TiagoRG 2023-03-14 20:02:31 +00:00
parent dbb7dba377
commit 21c2c127f2
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 201 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

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