000000000000mekelle University EIT
000000000000mekelle University EIT
Object Oriented Programming Assignment for Computer Science second year Students
Mr. ABC recently acquired a collection of buildings containing rental apartments. Your task is to design
an advanced Apartment Management System that efficiently handles information about each apartment
owned by Mr. ABC. This system should be implemented in Java using object- oriented principles and
incorporate various advanced features.
To begin, create an Apartment class that encapsulates the details of each apartment, including the
street address, apartment number, monthly rent, number of bedrooms, and occupancy status.
Implement the necessary fields, constructors, getters, and setters. Additionally, override the toString()
method to display the apartment's information. Furthermore, implement the Comparable interface in
the Apartment class, providing a compareTo() method to compare apartments based
To demonstrate the functionality of the Apartment Management System, develop a console application
with a menu-driven interface. Utilize a dynamic data structure, such as a LinkedList or any other suitable
data structure, to store Apartment objects. The application should provide the following features:
1. Request the user to specify the number of apartments to register, and then proceed to enter the
details for each apartment and add them to the data structure.
5.
Rent apartment
6. Prompt the user for an apartment number and request confirmation before removing the
corresponding apartment from the data structure.
7. Prompt the user for an apartment number to search for. If found, display the details of the
apartment.
8. Prompt the user for an apartment number and allow them to update the values for that specific
apartment.
9. Sort the apartments in the data structure by their rent values using an efficient sorting algorithm, and
display the sorted list.
10. Calculate the monthly income generated from the apartments and display the total, using
appropriate output formatting.
11. Display the apartment with the highest monthly income, using appropriate output formatting.
12. Display the apartment with the lowest monthly income, using appropriate output formatting.
Ensure that each apartment number is unique to satisfy the constraint that no two apartments should
share the same number. Implement appropriate exception handling, providing clear instructions and
input validation to enhance the user experience.
Remember to thoroughly test your application with various user inputs to ensure it functions correctly
and handles exceptions gracefully,
Start by creating the Apartment class that encapsulates the details of each apartment.
Include fields for street address, apartment number, monthly rent, number of bedrooms, and occupancy
status.
Implement constructors, getters, setters, and override the toString() method to display apartment
information.
Implement the Comparable interface with a compareTo() method to compare apartments based on rent
value.
Create a Main class for the console application with a menu-driven interface to interact with the
Apartment Management System.
Implement Features:
Request the user to specify the number of apartments to register and add details for each apartment to
the data structure.
Sort apartments by rent values using an efficient sorting algorithm and display the sorted list.
Calculate and display the total monthly income from all apartments.
Display the apartment with the highest and lowest monthly income.
Exception Handling:
Implement appropriate exception handling to handle scenarios like duplicate apartment numbers,
invalid input, etc.
Provide clear instructions to the user and ensure input validation for a smooth user experience.
Testing:
Thoroughly test the application with various user inputs to ensure all features work correctly and
exceptions are handled gracefully.
Coding Tips:
Break down the functionality into smaller methods for better organization and readability.
import java.util.*;
this.streetAddress = streetAddress;
this.apartmentNumber = apartmentNumber;
@Override
@Override
return "Apartment #" + apartmentNumber + " - Rent: $" + monthlyRent + " - Bedrooms: " +
numBedrooms + " - Occupied: " + (isOccupied ? "Yes" : "No");
public class ApartmentManagementSystem { public static void main(String[] args) { Scanner scanner =
new Scanner(System.in); List<Apartment> apartments = new LinkedList<>(); while (true)
{ System.out.println("\nAPARTMENT MANAGEMENT SYSTEM"); System.out.println("1. Register
Apartments"); System.out.println("2. Display All Apartments"); System.out.println("3. Display Occupied
Apartments"); System.out.println("4. Display Vacant Apartments"); System.out.println("5. Rent
Apartment"); System.out.println("6. Remove Apartment"); System.out.println("7. Search for
Apartment"); System.out.println("8. Update Apartment Details"); System.out.println("9. Sort
Apartments by Rent"); System.out.println("10. Calculate Total Monthly Income");
System.out.println("11. Apartment with Highest Monthly Income"); System.out.println("12. Apartment
with Lowest Monthly Income"); System.out.println("0. Exit"); System.out.print("Enter your choice: "); int
choice = scanner.nextInt(); scanner.nextLine(); // Consume newline character switch (choice) { case 1:
System.out.print("Enter the number of apartments to register: "); int numApartments =
scanner.nextInt(); scanner.nextLine(); // Consume newline character for (int i = 0; i < numApartments; i+
+) { System.out.println("Enter details for Apartment #" + (i + 1)); System.out.print("Street Address: ");
String streetAddress = scanner.nextLine(); System.out.print("Apartment Number: "); int
apartmentNumber = scanner.nextInt(); System.out.print("Monthly Rent: "); double monthlyRent =
scanner.nextDouble(); System.out.print("Number of Bedrooms: "); int numBedrooms =
scanner.nextInt(); System.out.print("Is Occupied? (true/false): "); boolean isOccupied =
scanner.nextBoolean(); apartments.add(new Apartment(streetAddress, apartmentNumber,
monthlyRent, numBedrooms, isOccupied)); } System.out.println("Apartments registered successfully!");
break; case 2: System.out.println("All Apartments:"); for (Apartment apt : apartments)
{ System.out.println(apt); } break; // Implement other functionality as per the menu options case 0:
System.out.println("Exiting Apartment Management System. Thank you!"); System.exit(0); default:
System.out.println("Invalid choice. Please try again."); } } }
import java.util.*;
this.streetAddress = streetAddress;
this.apartmentNumber = apartmentNumber;
this.monthlyRent = monthlyRent;
this.numBedrooms = numBedrooms;
this.isOccupied = isOccupied;
}
// Getters and Setters
@Override
@Override
return "Apartment #" + apartmentNumber + " - Rent: $" + monthlyRent + " - Bedrooms: " +
numBedrooms + " - Occupied: " + (isOccupied ? "Yes" : "No");
while (true) {
System.out.println("0. Exit");
switch (choice) {
case 1:
break;
case 2:
System.out.println("All Apartments:");
System.out.println(apt);
break;
case 0:
System.out.println("Exiting Apartment Management System. Thank you!");
System.exit(0);
default:
To ensure the code runs smoothly in Netbeans and display messages correctly, let's refine the existing
code and also include missing functionalities. Here's an enhanced version of the Apartment
Management System that is clear and ready to display messages in Netbeans:
import java.util.*;
this.streetAddress = streetAddress;
this.apartmentNumber = apartmentNumber;
this.monthlyRent = monthlyRent;
this.numBedrooms = numBedrooms;
this.isOccupied = isOccupied;
}
@Override
@Override
return "Apartment #" + apartmentNumber + " - Rent: $" + monthlyRent + " - Bedrooms: " +
numBedrooms + " - Occupied: " + (isOccupied ? "Yes" : "No");
while (true) {
System.out.println("0. Exit");
switch (choice) {
case 1:
if (isApartmentNumberUnique(apartments, apartmentNumber)) {
} else {
i--;
break;
case 2:
System.out.println("All Apartments:");
break;
case 0:
System.exit(0);
default:
if (apt.getApartmentNumber() == apartmentNumber) {
return false;
return true;
}
import java.util.*; class Apartment implements Comparable<Apartment> { private String streetAddress;
private int apartmentNumber; private double monthlyRent; private int numBedrooms; private boolean
isOccupied; public Apartment(String streetAddress, int apartmentNumber, double monthlyRent, int
numBedrooms, boolean isOccupied) { this.streetAddress = streetAddress; this.apartmentNumber =
apartmentNumber; this.monthlyRent = monthlyRent; this.numBedrooms = numBedrooms;
this.isOccupied = isOccupied; } // Getters and Setters @Override public int compareTo(Apartment other)
{ return Double.compare(this.monthlyRent, other.monthlyRent); } @Override public String toString()
{ return "Apartment #" + apartmentNumber + " - Rent: $" + monthlyRent + " - Bedrooms: " +
numBedrooms + " - Occupied: " + (isOccupied ? "Yes" : "No"); }
} public class ApartmentManagementSystem { public static void main(String[] args) { Scanner scanner =
new Scanner(System.in); List<Apartment> apartments = new LinkedList<>(); while (true)
{ System.out.println("\nAPARTMENT MANAGEMENT SYSTEM"); System.out.println("1. Register
Apartments"); System.out.println("2. Display All Apartments"); System.out.println("3. Display Occupied
Apartments"); System.out.println("4. Display Vacant Apartments"); System.out.println("5. Rent
Apartment"); System.out.println("6. Remove Apartment"); System.out.println("7. Search for
Apartment"); System.out.println("8. Update Apartment Details"); System.out.println("9. Sort
Apartments by Rent"); System.out.println("10. Calculate Total Monthly Income");
System.out.println("11. Apartment with Highest Monthly Income"); System.out.println("12. Apartment
with Lowest Monthly Income"); System.out.println("0. Exit"); System.out.print("Enter your choice: "); int
choice = scanner.nextInt(); scanner.nextLine(); // Consume newline character switch (choice) { case 1:
System.out.print("Enter the number of apartments to register: "); int numApartments =
scanner.nextInt(); scanner.nextLine(); // Consume newline character for (int i = 0; i < numApartments; i+
+) { System.out.println("Enter details for Apartment #" + (i + 1)); System.out.print("Street Address: ");
String streetAddress = scanner.nextLine(); System.out.print("Apartment Number: "); int
apartmentNumber = scanner.nextInt(); if (isApartmentNumberUnique(apartments, apartmentNumber))
{ System.out.print("Monthly Rent: "); double monthlyRent = scanner.nextDouble();
System.out.print("Number of Bedrooms: "); int numBedrooms = scanner.nextInt(); System.out.print("Is
Occupied? (true/false): "); boolean isOccupied = scanner.nextBoolean(); apartments.add(new
Apartment(streetAddress, apartmentNumber, monthlyRent, numBedrooms, isOccupied)); } else
{ System.out.println("Apartment with this number already exists. Please choose a unique number.");
i--; } } System.out.println("Apartments registered successfully!"); break; case 2: System.out.println("All
Apartments:"); for (Apartment apt : apartments) { System.out.println(apt); } break; // Implement other
functionality as per the menu options case 0: System.out.println("Exiting Apartment Management
System. Thank you!"); System.exit(0); default: System.out.println("Invalid choice. Please try again."); } } }
private static boolean isApartmentNumberUnique(List<Apartment> apartments, int apartmentNumber)
{ for (Apartment apt : apartments) { if (apt.getApartmentNumber() == apartmentNumber) { return
false; } } return true; }