Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to implement a hostel management application for more than one apartment unit?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to implement a hostel management application for more than one apartment unit?

    Hi. I need to design a program to manage the university apartment/hostel. The program must be able to carry out the basic tasks:
    1. Register a new student
    2. Remove a student
    3. Search for a student
    4. Generate a report of the apartment's occupancy

    There are two types of apartments, type A and type B. type A has 2 rooms (but able to house 3 students) and type B has 2 rooms and one master bedroom.


    I have almost completed the program. I still have not implemented feature '3' (search for a student). The problem is that my program only works for one unit of type A and one unit of type B. I would like to know how to design the program so that it works for maybe 10 units of type A and 10 units of type B.

    Also, I would like to know how to I delete a student automatically when the rental expires, which means I do not have to use feature '2' and remove a student manually.

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

    I'm using Student class and Apartment class. Here are the source code for Student.class and Apartment.class

    public class Student
    {
    	private String Name;
    	private int Student_ID;
    	private String delete = null;
    	private int ID_delete = 0;
     
     
    	/*public Student(String studentName, int studentID)
    	{
    		Name = studentName;
    		Student_ID = studentID;
    	}*/
     
    	public void storeName(String studentName)
    	{
    		Name = studentName;
    	}
     
    	public void storeID(int studentID)
    	{
    		Student_ID = studentID;
    	}
     
    	public String getName()
    	{
    		return Name;
    	}
     
    	public int getID()
    	{
    		return Student_ID;
    	}
    }


    class Apartment
    {
    	private int TypeA;
    	private int TypeB;
    	private int TypeB_master;
     
    	private final double MonthlycostA;
    	private final double MonthlycostB;
    	private final double MonthlycostB_master;
     
    	private final double costA;
    	private final double costB;
    	private final double costB_master;
     
    	public Apartment()
    	{
    		TypeA = 0;
    		TypeB = 0;
    		TypeB_master = 0;
     
    		MonthlycostA = 300.00;
    		MonthlycostB = 200.00;
    		MonthlycostB_master = (1.4f)*(MonthlycostB);
     
    		costA = 400.00;
    		costB = 300.00;
    		costB_master = 380.00;
    	}
     
    	public void TypeAoccupied()
    	{
    		TypeA++;
    	}
    	public void TypeAvacant()
    	{
    		TypeA--;
    	}
    	public void TypeBoccupied()
    	{
    		TypeB++;
    	}
    	public void TypeBvacant()
    	{
    		TypeB--;
    	}
    	public void TypeB_masteroccupied()
    	{
    		TypeB_master++;
    	}
    	public void TypeB_mastervacant()
    	{
    		TypeB_master--;
    	}
     
    	public double getcostA()
    	{
    		return costA;
    	}
    	public double getcostB()
    	{
    		return costB;
    	}
    	public double getcostB_master()
    	{
    		return costB_master;
    	}
     
    	public double getMonthlycostA()
    	{
    		return MonthlycostA;
    	}
    	public double getMonthlycostB()
    	{
    		return MonthlycostB;
    	}
    	public double getMonthlycostB_master()
    	{
    		return MonthlycostB_master;
    	}
     
    	public int getTypeA()
    	{
    		return TypeA;
    	}
    	public int getTypeB()
    	{
    		return TypeB;
    	}
    	public int getTypeB_master()
    	{
    		return TypeB_master;
    	}
    }

    I hope my problem would be solved. Thanks


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to implement a hostel management application for more than one apartment unit

    How do you do it for just one? ...do that ten times

Similar Threads

  1. Group/Unit, Folder/File?
    By rohmoh in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2012, 06:13 PM
  2. Need suggestion to implement PULL model within application
    By java_user_new in forum Java Theory & Questions
    Replies: 0
    Last Post: June 6th, 2012, 04:37 AM
  3. Java Unit Testing How-To Gettin' started
    By Massaslayer in forum Java Theory & Questions
    Replies: 2
    Last Post: May 1st, 2012, 07:58 AM
  4. HTML Unit Help
    By Seegee in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 2nd, 2012, 08:38 PM
  5. Unit Vector
    By mingming8888 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 14th, 2010, 02:53 PM