Hello. Im just starting out to learn java. I follow a course, and I have now been given a fairly big project (big for me as a beginner that is). I must point out I struggle quite a bit with understanding the whole picture
of the object oriented part, having just been introduced to it. If someone could look at the project details I will describe later and give me some pointers as to how to design/layout, and why, I would be
Im going to make a program that will register tenants of one appartment building(a company owns it and needs a program to administer). I need to register which room each tenant rents. Im also required to register
several more categories of information about the tenants. There are certain requirements like, there must be 4 classes, and objects of three of them to be accepted.
The program should be able to read/save information about who lives in which room/they dont all pay the same rent ++ from/to a file. The user should be able to administer the program through a console menu, and be able to register new tenants, see an overview/list of all the appartments with tenants, the rents they pay etc.
My problem is I struggle with understanding how to build all this, or how to lay it out. I am able to make arrays, arrays with pointers to objects, read/write to file, make constructors with paramenters etc in tiny programs, where
this is mostly what you do. But with all these classes Im getting nowhere. Ive been looking into UML quite a bit, and Ive seen UML examples of similar programs where they have 3 classes, this was designed in UML as 3 classes
with no less than four pointer arrays (from what I understood at least).
Main is used simply to move the program into another class where the menu system will be among other things. The objects of the three classes should be something like this
class Tenants: name, account(with the company), class AppartmentBuilding : rent, pointer to tenants. And the class where the menu system(lets call this Menu class) is as well should contain one object (the building as a whole, certain static expenses).
For instance I have a two dimensional array so far in the Menu class (the rooms are name: number letter X Y). Can this array(with pointers) then be used to call a constuctor in the other two classes?
or to create objects as I read from the file? this is one of many problems I have.
As i mentioned I have a lot of problems getting started. Im not asking for someone to code this for me, I just want some good ideas on how to structure it, and why. Thank you.
Here is my first pass on how I would break this down. I make heavy use of the HAS-A principle.
LandLord Handles all of the console tasks you mentioned. Assigns tenants to apartments, saves data to disk, reads from disk, and generates reports.
HAS-A ApartmentBuilding object as a member.
ApartmentBuilding Contains any details you need to store about the building itself, but mostly is a container for the rooms.
HAS-A Room object List as a member. (I would probably use an ArrayList for ease of management, though a hashmap might provide more flexibility)
Room The details of the room being rented (rent, square footage, etc...)
HAS-A Tenant object List as a member. (I would use an ArrayList since there could be more than one tenant in a room)
Tenant The details of the tenants
There is no inheritance involved here, but there is some cohesion between the objects since each contains elements of the others and they are all associated.
The Landlord object then has visibility to everything within the ApartmentBuilding they control so it is easy to then perform all of the management functions. The constructors for each object should initialize their member objects and Lists to avoid any null pointer exceptions.
That is just my first take in looking at the project. Hope that helps.
Are you sure you mean "cohesion", JS? If each contains elements of the others that looks more like coupling to me. Cohesion means that one class does its own work and hides all that from other classes.
Thank you John Storta Jr. for taking time to reply. Ive done some reading on the HAS A principle you mentioned, and it seems more clear now.
Also thanks for the welcome Campbell Ritchie
John Storta Jr.
Joined: Jul 26, 2009
Yes Campbell, you are right. I should have said Coupling between the objects. I do not know why (or how) I always get those mixed up. Everytime I look it up it is so obvious. Thank you for setting me straight.
I was thinking about the Homework issue as I was posting and, in my mind anyway, it looked like Per had about the same approach as I would have taken, but was looking for some clarification on the relationships. I hope I did not cross that line as it was not my intention.
I suppose in hindsight I might have been better just mentioning HAS-A and suggest considering the relationships between the objects rather than spelling out how I would break it down for each class.
Joined: Oct 13, 2005
We all have slightly different opinions about how far one is allowed to go, and I don't think you have overstepped the mark. Particularly since you said it was a "first take" and we have recognised problems; that allows Per Hansen to learn more by sorting out those problems and improving the suggestions.