0% found this document useful (0 votes)
20 views14 pages

000000000000mekelle University EIT

The document describes an assignment to design an advanced apartment management system in Java. It provides details on creating an Apartment class and developing a console application with features like registering apartments, displaying apartments by status, renting apartments, removing apartments, searching for apartments, updating apartment details, sorting by rent, and calculating total monthly income.

Uploaded by

tesewaka3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

000000000000mekelle University EIT

The document describes an assignment to design an advanced apartment management system in Java. It provides details on creating an Apartment class and developing a console application with features like registering apartments, displaying apartments by status, renting apartments, removing apartments, searching for apartments, updating apartment details, sorting by rent, and calculating total monthly income.

Uploaded by

tesewaka3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Mekelle University EIT-M

School of Computing, Software Engineering Chair

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

on their rent value.

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.

2. Display all apartments stored in the system.

3. Display occupied apartments

4. Display vacant apartments

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,

Submission date: Friday May 17, 2024

Create the Apartment Class:

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.

Develop the Console Application:

Create a Main class for the console application with a menu-driven interface to interact with the
Apartment Management System.

Use a dynamic data structure like a LinkedList to store Apartment objects.

Implement Features:

Request the user to specify the number of apartments to register and add details for each apartment to
the data structure.

Implement functions to:

Display all apartments.

Display occupied and vacant apartments.

Rent an apartment by updating its occupancy status.


Remove an apartment by confirming the apartment number.

Search for an apartment by apartment number.

Update information for a specific apartment based on apartment number.

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:

Use meaningful variable names and follow Java naming conventions.

Break down the functionality into smaller methods for better organization and readability.

Add comments to explain complex logic or important sections of code.

Keep the code modular and well-structured for easier maintenance.

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(); 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.*;

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();

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.");

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.*;

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;

}
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; }

You might also like