import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class ApartmentManagement
{
public static void main(String[] args) throws IOException
{
DecimalFormat twoDecimals = new DecimalFormat("0.00");
String input;
int task;
String apartment;
int choice;
String confirmation;
char confirm;
String name;
String IDinput;
int ID;
String[] todayDate = new String[6];
String[] expiryDate = new String[6];
String[] nameA = new String[6];
int[] ID_A = new int[6];
String[] nameB = new String[6];
int[] ID_B = new int[6];
String[] nameB_master = new String[1];
int[] ID_B_master = new int[6];
int A = 0;
int B = 0;
int IDno = 0;
int tDate = 0;
int eDate = 0;
Student studentInfo = new Student();
Apartment apt = new Apartment();
GregorianCalendar gcalendar = new GregorianCalendar();
String[] months = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
String inputDel;
int searchDel;
String inputreturn;
char returnToMenu;
do
{
input = JOptionPane.showInputDialog("Welcome user.\n" +
"This is a program to manage the university apartments.\n" +
"Please enter the task of your choice: \n\n" +
"1. Register a new student and assign an apartment.\n" +
"2. Update the apartment status.\n" +
"3. Generate a report of the apartment status.\n" +
"4. Search for a student's apartment unit.\n" +
"5. Exit the program.");
task = Integer.parseInt(input);
switch (task)
{
//====================================================== Task 1 ===========================================================================
case 1:
JOptionPane.showMessageDialog(null, "You selected task 1:\n" +
"Register a new student and assign an apartment.\n" +
"Click OK to continue.");
apartment = JOptionPane.showInputDialog("There are two different types of apartment for rent.\n" +
"Type A: 2 bedrooms with kitchen and laundry facilities.\n" +
"Monthly rental: RM300 per room\n\n" +
"Type B: 3 bedrooms including one master bedroom with attached bathroom " +
"but without kitchen and laundry facilities.\n" +
"Monthly rental: RM200 per room, master bedroom costs additional 40%.\n\n" +
"For Type A, enter 1.\n" +
"For Type B, enter 2.\n" +
"For Type B master bedroom, enter 3.\n" +
"Please enter your choice.");
choice = Integer.parseInt(apartment);
if(choice == 1)
{
confirmation = JOptionPane.showInputDialog("You have selected apartment Type A.\n" +
"Enter 'Y' to confirm.");
confirm = confirmation.charAt(0);
if (confirm == 'Y' || confirm == 'y')
if (apt.getTypeA() >= 3)
JOptionPane.showMessageDialog(null, "Type A apartment is already full. Please select another apartment");
else if (apt.getTypeA() < 3)
{
name = JOptionPane.showInputDialog("Please enter the student's name.");
IDinput = JOptionPane.showInputDialog("Please enter the student's ID.");
ID = Integer.parseInt(IDinput);
studentInfo.storeName(name);
studentInfo.storeID(ID);
nameA[A] = studentInfo.getName();
ID_A[IDno] = studentInfo.getID();
apt.TypeAoccupied();
JOptionPane.showMessageDialog(null, "The amount to be paid upon registration: \n" +
"RM" + twoDecimals.format(apt.getMonthlycostA()) + " for rental this month\n" +
"RM100.00 for utility charges and rental deposit for a month\n\n" +
"Total: RM" +twoDecimals.format(apt.getcostA()) + "\n\n" +
"Please pay RM" + twoDecimals.format(apt.getMonthlycostA()) +
" for the following months till the rental expires.");
todayDate[tDate] = ((gcalendar.get(Calendar.DATE))+"-"+(months[gcalendar.get(Calendar.MONTH)])+"-"+(gcalendar.get(Calendar.YEAR)) );
gcalendar.add(Calendar.DATE, + 140);
expiryDate[eDate] = ((gcalendar.get(Calendar.DATE))+"-"+(months[gcalendar.get(Calendar.MONTH)])+"-"+(gcalendar.get(Calendar.YEAR)) );
JOptionPane.showMessageDialog(null, "Registration details: \n\n" +
"Name: " + nameA[A] + "\n" +
"Student ID: " + ID_A[IDno] + "\n" +
"Date of registration: " + todayDate[tDate] + "\n" +
"Rental expiry date: " + expiryDate[eDate]);
A++;
IDno++;
tDate++;
eDate++;
}
}
if(choice == 2)
{
confirmation = JOptionPane.showInputDialog("You have selected apartment Type B.\n" +
"Enter 'Y' to confirm.");
confirm = confirmation.charAt(0);
if (confirm == 'Y' || confirm == 'y')
if (apt.getTypeB() >= 2)
JOptionPane.showMessageDialog(null, "Type B apartment is already full. Please select another apartment");
else if (apt.getTypeB() < 2)
{
name = JOptionPane.showInputDialog("Please enter the student's name.");
IDinput = JOptionPane.showInputDialog("Please enter the student's ID.");
ID = Integer.parseInt(IDinput);
studentInfo.storeName(name);
studentInfo.storeID(ID);
nameB[B] = studentInfo.getName();
ID_B[IDno] = studentInfo.getID();
apt.TypeBoccupied();
JOptionPane.showMessageDialog(null, "The amount to be paid upon registration: \n" +
"RM" + twoDecimals.format(apt.getMonthlycostB()) + " for rental this month\n" +
"RM100.00 for utility charges and rental deposit for a month\n\n" +
"Total: RM" +twoDecimals.format(apt.getcostB()) + "\n\n" +
"Please pay RM" + twoDecimals.format(apt.getMonthlycostB()) +
" for the following months till the rental expires.");
todayDate[tDate] = ((gcalendar.get(Calendar.DATE))+"-"+(months[gcalendar.get(Calendar.MONTH)])+"-"+(gcalendar.get(Calendar.YEAR)) );
gcalendar.add(Calendar.DATE, + 140);
expiryDate[eDate] = ((gcalendar.get(Calendar.DATE))+"-"+(months[gcalendar.get(Calendar.MONTH)])+"-"+(gcalendar.get(Calendar.YEAR)) );
JOptionPane.showMessageDialog(null, "Registration details: \n\n" +
"Name: " + nameB[B] + "\n" +
"Student ID: " + ID_B[IDno] + "\n" +
"Date of registration: " + todayDate[tDate] + "\n" +
"Rental expiry date: " + expiryDate[eDate]);
B++;
IDno++;
tDate++;
eDate++;
}
}
if(choice == 3)
{
confirmation = JOptionPane.showInputDialog("You have selected apartment Type B master bedroom.\n" +
"Enter 'Y' to confirm.");
confirm = confirmation.charAt(0);
if (confirm == 'Y' || confirm == 'y')
if (apt.getTypeB_master() >= 1)
JOptionPane.showMessageDialog(null, "Type B master bedroom is already full. Please select another apartment");
else if (apt.getTypeB_master() < 1)
{
name = JOptionPane.showInputDialog("Please enter the student's name.");
IDinput = JOptionPane.showInputDialog("Please enter the student's ID.");
ID = Integer.parseInt(IDinput);
studentInfo.storeName(name);
studentInfo.storeID(ID);
nameB_master[0] = studentInfo.getName();
ID_B_master[IDno] = studentInfo.getID();
apt.TypeB_masteroccupied();
JOptionPane.showMessageDialog(null, "The amount to be paid upon registration: \n" +
"RM" + twoDecimals.format(apt.getMonthlycostB_master()) + " for rental this month\n" +
"RM100.00 for utility charges and rental deposit for a month\n\n" +
"Total: RM" +twoDecimals.format(apt.getcostB_master()) + "\n\n" +
"Please pay RM" + twoDecimals.format(apt.getMonthlycostB_master()) +
" for the following months till the rental expires.");
todayDate[tDate] = ((gcalendar.get(Calendar.DATE))+"-"+(months[gcalendar.get(Calendar.MONTH)])+"-"+(gcalendar.get(Calendar.YEAR)) );
gcalendar.add(Calendar.DATE, + 140);
expiryDate[eDate] = ((gcalendar.get(Calendar.DATE))+"-"+(months[gcalendar.get(Calendar.MONTH)])+"-"+(gcalendar.get(Calendar.YEAR)) );
JOptionPane.showMessageDialog(null, "Registration details: \n\n" +
"Name: " + nameB_master[0] + "\n" +
"Student ID: " + ID_B_master[IDno] + "\n" +
"Date of registration: " + todayDate[tDate] + "\n" +
"Rental expiry date: " + expiryDate[eDate]);
IDno++;
tDate++;
eDate++;
}
}
break;
//====================================================== Task 2 ===========================================================================
case 2:
JOptionPane.showMessageDialog(null, "You selected task 2:\n" +
"Update the apartment status.\n" +
"This task is to remove a student from an apartment.\n" +
"Click OK to continue.");
if (apt.getTypeA() == 0 && apt.getTypeB() == 0 && apt.getTypeB_master() == 0)
{
JOptionPane.showMessageDialog(null, "There are currently no student registered in " +
"any of the apartment units.");
}
else
{
inputDel = JOptionPane.showInputDialog("Please enter the ID of the student " +
"you wish to delete.");
searchDel = Integer.parseInt(inputDel);
for(int i = 0; i <= apt.getTypeA(); i++)
{
if(searchDel == ID_A[i])
{
confirmation = JOptionPane.showInputDialog("The following student will be removed:\n" +
"Name: " + nameA[i] + "\n" +
"ID: " + ID_A[i] + "\n\n" +
"Enter 'Y' to confirm.");
confirm = confirmation.charAt(0);
if(confirm == 'Y' || confirm == 'y')
{
nameA[i] = null;
ID_A[i] = 0;
apt.TypeAvacant();
JOptionPane.showMessageDialog(null, "The particular student has been removed.\n" +
"Apartment Type A now has " + (3 - apt.getTypeA()) + " rooms available.");
}
}
}
for(int i = 0; i <= apt.getTypeB(); i++)
{
if(searchDel == ID_B[i])
{
confirmation = JOptionPane.showInputDialog("The following student will be removed:\n" +
"Name: " + nameB[i] + "\n" +
"ID: " + ID_B[i] + "\n\n" +
"Enter 'Y' to confirm.");
confirm = confirmation.charAt(0);
if(confirm == 'Y' || confirm == 'y')
{
nameB[i] = null;
ID_B[i] = 0;
apt.TypeBvacant();
JOptionPane.showMessageDialog(null, "The particular student has been removed.\n" +
"Apartment Type A now has " + (2 - apt.getTypeB()) + " rooms available.");
}
}
}
for(int i = 0; i <= apt.getTypeB_master(); i++)
{
if(searchDel == ID_B_master[i])
{
confirmation = JOptionPane.showInputDialog("The following student will be removed:\n" +
"Name: " + nameB_master[i] + "\n" +
"ID: " + ID_B_master[i] + "\n\n" +
"Enter 'Y' to confirm.");
confirm = confirmation.charAt(0);
if(confirm == 'Y' || confirm == 'y')
{
nameB_master[i] = null;
ID_B_master[i] = 0;
apt.TypeB_mastervacant();
JOptionPane.showMessageDialog(null, "The particular student has been removed.\n" +
"Apartment Type A now has " + (1 - apt.getTypeB_master()) + " rooms available.");
}
}
}
}
break;
//====================================================== Task 3 ===========================================================================
case 3:
JOptionPane.showMessageDialog(null, "You selected task 3:\n" +
"Generate a report of the apartment status.\n" +
"Click OK to continue.");
if (apt.getTypeA() == 0 && apt.getTypeB() == 0 && apt.getTypeB_master() == 0)
{
JOptionPane.showMessageDialog(null, "There are currently no student registered in " +
"any of the apartment units.");
}
else
{
for(int i = 0; i <= apt.getTypeA(); i++)
{
JOptionPane.showMessageDialog(null, "These are the list of students staying " +
"in apartment Type A: \n" +
"Name: " + nameA[i] + "\n" +
"ID: " + ID_A[i] + "\n" +
"Date of registration: " + todayDate[i] + "\n" +
"Date of rental expiry: " + expiryDate[i] + "\n\n");
}
for(int i = 0; i <= apt.getTypeB(); i++)
{
JOptionPane.showMessageDialog(null, "These are the list of students staying " +
"in apartment Type B: \n" +
"Name: " + nameB[i] + "\n" +
"ID: " + ID_B[i] + "\n" +
"Date of registration: " + todayDate[i] + "\n" +
"Date of rental expiry: " + expiryDate[i] + "\n\n");
}
for(int i = 0; i <= apt.getTypeB_master(); i++)
{
JOptionPane.showMessageDialog(null, "These are the list of students staying " +
"in apartment Type B master bedroom: \n" +
"Name: " + nameB_master[i] + "\n" +
"ID: " + ID_B_master[i] + "\n" +
"Date of registration: " + todayDate[i] + "\n" +
"Date of rental expiry: " + expiryDate[i] + "\n\n");
}
JOptionPane.showMessageDialog(null, "The total number of occupants in the following apartments are: \n" +
"Type A: " + apt.getTypeA() + "\n" +
"Type B: " + apt.getTypeB() + "\n" +
"Type B master bedroom: " + apt.getTypeB_master() + "\n\n");
JOptionPane.showMessageDialog(null, "The total number of vacant rooms in the following apartments are: \n" +
"Type A: " + (3 - apt.getTypeA()) + "\n" +
"Type B: " + (2 - apt.getTypeB()) + "\n" +
"Type B master bedroom: " + (1 - apt.getTypeB_master()) + "\n\n");
}
break;
}
inputreturn = JOptionPane.showInputDialog("Do you want to return to the main menu?\n" +
"Enter 'Y' for yes.");
returnToMenu = inputreturn.charAt(0);
} while(returnToMenu == 'Y' || returnToMenu == 'y');
System.exit(0);
}
}