Sie sind auf Seite 1von 10

import

import
import
import

java.util.*;
java.io.*;
javax.swing.JOptionPane;
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
int
int
int
int

A = 0;
B = 0;
IDno = 0;
tDate = 0;
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", "No
vember", "December"};
String inputDel;
int searchDel;
String inputreturn;
char returnToMenu;
do
{

input = JOptionPane.showInputDialog("Welcome user.n" +


"Thi
s is a program to manage the university apartments.n" +
"Ple
ase enter the task of your choice: nn" +
"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" +
"Regis
ter 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 roomnn" +
"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%.nn" +
"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 sel
ected 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 apar
tment 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 en
ter 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 monthn" +
"RM100.00 for utility charges and rental deposit for a monthnn" +
"Total: RM" +twoDecimals.format(apt.getcostA()) + "nn" +
"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.DAT
E))+"-"+(months[gcalendar.get(Calendar.MONTH)])+"-"+(gcalendar.get(Calendar.YEAR
)) );
JOptionPane.showMessageDialog(null, "Registratio
n details: nn" +
"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 sel
ected 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 apar
tment 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 en
ter 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 monthn" +
"RM100.00 for utility charges and rental deposit for a monthnn" +
"Total: RM" +twoDecimals.format(apt.getcostB()) + "nn" +
"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.DAT
E))+"-"+(months[gcalendar.get(Calendar.MONTH)])+"-"+(gcalendar.get(Calendar.YEAR
)) );
JOptionPane.showMessageDialog(null, "Registratio
n details: nn" +
"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 sel
ected 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 mast
er 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 en
ter 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 mo
nthn" +
"RM100.00 for utility charges and rental deposit for a monthnn" +
"Total: RM" +twoDecimals.format(apt.getcostB_master()) + "nn" +
"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.DAT
E))+"-"+(months[gcalendar.get(Calendar.MONTH)])+"-"+(gcalendar.get(Calendar.YEAR
)) );
JOptionPane.showMessageDialog(null, "Registratio
n details: nn" +
"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" +
"Updat
e 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_m
aster() == 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("Th
e following student will be removed:n" +

"Name: " + nameA[i] + "n" +


"ID: " + ID_A[i] + "nn" +
"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("Th
e following student will be removed:n" +
"Name: " + nameB[i] + "n" +
"ID: " + ID_B[i] + "nn" +
"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("Th
e following student will be removed:n" +
"Name: " + nameB_master[i] + "n" +

"ID: " + ID_B_master[i] + "nn" +


"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" +
"Gener
ate a report of the apartment status.n" +
"Click
OK to continue.");
if (apt.getTypeA() == 0 && apt.getTypeB() == 0 && apt.getTypeB_m
aster() == 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 t
he 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] + "nn");
}
for(int i = 0; i <= apt.getTypeB(); i++)

{
JOptionPane.showMessageDialog(null, "These are t
he 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] + "nn");
}
for(int i = 0; i <= apt.getTypeB_master(); i++)
{
JOptionPane.showMessageDialog(null, "These are t
he 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] + "nn");
}
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() + "nn");
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()) + "nn");
}
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);
}
}

Das könnte Ihnen auch gefallen