From 21c2c127f2aee3a6a3720c56b01f0be26734ca28 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Tue, 14 Mar 2023 20:02:31 +0000 Subject: [PATCH] [POO] aula05 finished --- .../2semestre/poo/src/aula05/AuctionDemo.java | 129 ++++++++++++++++ 1ano/2semestre/poo/src/aula05/DateYMD.java | 138 +++++++++--------- 2 files changed, 201 insertions(+), 66 deletions(-) create mode 100644 1ano/2semestre/poo/src/aula05/AuctionDemo.java diff --git a/1ano/2semestre/poo/src/aula05/AuctionDemo.java b/1ano/2semestre/poo/src/aula05/AuctionDemo.java new file mode 100644 index 0000000..036e920 --- /dev/null +++ b/1ano/2semestre/poo/src/aula05/AuctionDemo.java @@ -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) : ""); + } +} \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula05/DateYMD.java b/1ano/2semestre/poo/src/aula05/DateYMD.java index e9b075c..13c7f84 100644 --- a/1ano/2semestre/poo/src/aula05/DateYMD.java +++ b/1ano/2semestre/poo/src/aula05/DateYMD.java @@ -3,6 +3,76 @@ package aula05; import java.util.Scanner; public class DateYMD { + private int day; + private int month; + private int year; + + public DateYMD(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[] get() { + return new int[]{this.day, this.month, this.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 addDays(int days) { + for (int i = 0; i < days; i++) + this.increment(); + } + + 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); + } static boolean validMonth(int month) { return month >= 1 && month <= 12; } @@ -23,76 +93,12 @@ public class DateYMD { 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; + 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: