atanasovetr

Apartment

Apr 14th, 2021
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. public class Apartment {
  2.     private String city;
  3.     private String district;
  4.     private String address;
  5.     private double area;
  6.     private double price;
  7.     private double priceBySquareM;
  8.     private boolean akt16;
  9.  
  10.  
  11.  
  12.     public Apartment(String city, String district, String address, double area, double price, boolean akt16) {
  13.         this.city = city;
  14.         this.district = district;
  15.         this.address = address;
  16.         this.area = area;
  17.         this.price = price;
  18.         this.akt16 = akt16;
  19.         this.priceBySquareM = price / area;
  20.     }
  21.  
  22.     public String getCity() {
  23.         return city;
  24.     }
  25.  
  26.     public void setCity(String city) {
  27.         this.city = city;
  28.     }
  29.  
  30.     public String getDistrict() {
  31.         return district;
  32.     }
  33.  
  34.     public void setDistrict(String district) {
  35.         this.district = district;
  36.     }
  37.  
  38.     public String getAddress() {
  39.         return address;
  40.     }
  41.  
  42.     public void setAddress(String address) {
  43.         this.address = address;
  44.     }
  45.  
  46.     public double getArea() {
  47.         return area;
  48.     }
  49.  
  50.     public void setArea(double area) {
  51.         this.area = area;
  52.     }
  53.  
  54.     public double getPrice() {
  55.         return price;
  56.     }
  57.  
  58.     public void setPrice(double price) {
  59.         this.price = price;
  60.     }
  61.  
  62.     public double getPriceBySquareM() {
  63.         return priceBySquareM;
  64.     }
  65.  
  66.     public void setPriceBySquareM(double priceBySquareM) {
  67.         this.priceBySquareM = priceBySquareM;
  68.     }
  69.  
  70.     public boolean isAkt16() {
  71.         return akt16;
  72.     }
  73.  
  74.     public void setAkt16(boolean akt16) {
  75.         this.akt16 = akt16;
  76.     }
  77.  
  78.     @Override
  79.     public String toString() {
  80.         return "Apartment{" +
  81.                 "city='" + city + '\'' +
  82.                 ", district='" + district + '\'' +
  83.                 ", address='" + address + '\'' +
  84.                 ", area=" + area +
  85.                 ", price=" + price +
  86.                 ", priceBySquareM=" + String.format("%.2f", priceBySquareM) +
  87.                 ", akt16=" + akt16 +
  88.                 '}';
  89.     }
  90. }
  91.  
  92.  
  93.  
  94. ////////////////////////////////////////////////////////////////
  95.  
  96.  
  97. import java.util.ArrayList;
  98.  
  99. public class ApartmentMain {
  100.     public static void main(String[] args) {
  101.         Apartment apt1 = new Apartment("Sofia", "Lulin", "Zahari Stoqnov 16", 123, 200000, true);
  102.         Apartment apt2 = new Apartment("Sofia", "Vitosha", "Sofronii Vrachanski 12", 212, 423197, true);
  103.         Apartment apt3 = new Apartment("Varna", "Morski", "Hristo Botev 1", 78, 78000, false);
  104.         Apartment apt4 = new Apartment("Burgas", "Morska gradina", "Hristofor Kolumb", 326, 303784, true);
  105.         Apartment apt5 = new Apartment("Botevgrad", "Iztok", "Kolio Ficheto", 122, 70678, true);
  106.  
  107.         ArrayList<Apartment> apartments = new ArrayList<>();
  108.         apartments.add(apt1);
  109.         apartments.add(apt2);
  110.         apartments.add(apt3);
  111.         apartments.add(apt4);
  112.         apartments.add(apt5);
  113.  
  114.         bubbleSort(apartments);
  115.  
  116.         for(Apartment apt: apartments){
  117.             System.out.println(apt);
  118.         }
  119.  
  120.  
  121.     }
  122.     public static void bubbleSort(ArrayList<Apartment> apartments) {
  123.         for (int a = 1; a < apartments.size(); a++) {
  124.             for (int b = 0; b < apartments.size() - a; b++) {
  125.                 if (((apartments.get(b).getPriceBySquareM())
  126.                         > ((apartments.get(b + 1).getPriceBySquareM())))) {
  127.                     Apartment temp = apartments.get(b);
  128.                     apartments.set(b, apartments.get(b + 1));
  129.                     apartments.set(b + 1, temp);
  130.                 }
  131.             }
  132.         }
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment