Design Issues, suggestions welcome

I have stumbled across some design issues, with a carhire system i am making.

kept in

[CAR]->[GARAGE]

|

|

| Retives car from garage

|

|

|

[TIMESLOT]

As it stands the GARAGE class automatically has CAR objects added as attributes.

import java.util.*;

publicclass Garage{

//class attributes

private LinkedList carhold;

Car ford_ka =new Car("A",135);

Car ford_focus =new Car("B",149);

Car ford_Mondeo =new Car("C",179);

Car Vauxhall_Vectra =new Car("D",239);

Car Mercedes_E240 =new Car("H",290);

Car Renault_Espace =new Car("V",399);

public Garage(){

carhold =new LinkedList();

carhold.add(ford_ka);

carhold.add(ford_focus);

carhold.add(ford_Mondeo);

carhold.add(Vauxhall_Vectra);

carhold.add(Mercedes_E240);

carhold.add(Renault_Espace);

The TIMESLOT class has an GARAGE object as an attribute, thus enabling it access mehtods.

import java.util.*;

publicclass TimeSlot{

Garage g =new Garage();

//*******************************************

publicvoid getCarCost(String input){

Car theCar = g.search(input);

problem is each time a new TIMESLOT is create so too is a

new garage created with all the cars.

I really need the garage to be a seperate entity, but still allowing

the TIMESLOT class to use its methods.

would in heritence be the appropriate solution, or maybe something else.

I would like to hear other suggestions.

[2510 byte] By [Dash161a] at [2007-10-1 19:52:52]
# 1
I'm not sure what the TimeSlot is for, but you should have one instance of a Garage which has references to Cars, i.e. an ArrayList.
SoulTech2012a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Problem Statement

Customers should be able to get a quotation for hiring a car for a given period of time prior to making a booking. Customers should be able to choose a Group of car and specify any extras required. Having received a quotation indicating price and availability customers should then be able to make an on-line booking

when the User wants to get quote for a given period of time, a TIMESLOT gets created.

the TIMESLOT is responsible for calculatiing the total cost for the time period.

in-order to do this it must retrieve a CAR object.

there is one factor that effects wheather or not a user can have a certain CAR

IS THE CAR THEY REQUESTED AVAILABLE FOR THE PERIOD OF TIME SELECTED?

there is a finite number of cars available

Group AFord Ka (4 passengers)2 cars

Group BFord Focus 1.6 (5 passengers)2 cars

Group CFord Mondeo 1.8LX (5 passengers)2 cars

Group DVauxhall Vectra 2.2 Auto (5 passengers)2 cars

Group HMercedes E240 (5 passengers)1 car

Group VRenault Espace (7 passengers)1 car

i have another class TIMESLOT_COLLECTIONS where the time slots are added provide the

dates don't overlap.

I have been thinking of doing away with the CAR and GARAGE classes instead using a set of Car

variables and seperate collections.

do think that might be better?

or can you think of any better way to manage the cars?

Dash161a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
> or can you think of any better way to manage the cars?if you are using Java, chances are your instructor (I assume this is a homework assignment) wants you to use OO methodology. Thus a Car is a class, Garage is a class, etc...
SoulTech2012a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Yes we use the UML/ICONX methodology, but there is nothing to say that you must have a CAR class or a GARAGE class.

I am building a Web app for a Car hire firm. The I way was going to approch the problem was like this:

TIMESLOT CLASS - calculates the total cost of a particular hire.

TIMESLOT_COLLECTION - adds vaildated (ones that don't conflict) timeslots to a linked list.

BOOKING - (this im still getting my head round bit), this contains the customers details but must also obtain the data from TIMESLOT (total, to/from dates etc).

Once complete i can then look at storing the BOOKING using a persistence Manager.

Dash161a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
Sorry mate i didn't realise before, the diagram i drew did appear properly. [TIMESLOTS] has a [GARAGE]object in it.
Dash161a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

I'm not sure what ICONX is, or what UML has to do with it, but yes, there is plenty to say (in the world of OOP) that you should have a seperate class for a car and one for a garage. They are two DISTINCT objects in the real world, thus they are candidates for distinct classes. Moreover, a garage has almost NOTHING in common with a car so it's almost implied. But that's just my opinion.

SoulTech2012a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

I believe a Singleton pattern works perfectly here. A singleton ensures that only one instance of an object is created. You would use it as follows:

public class Garage {

private LinkedList carhold;

// Car declarations here

private Garage() {

carhold = new LinkedList();

// add cars here

}

public static Garage getInstance() {

static Garage instance = null;

if (instance == null)

instance = new Garage();

return instance;

}

}

public class TimeSlot {

public void getCarCost(String input) {

Garage g = Garage.getInstance();

// do some other stuff

}

}

Notice the PRIVATE constructor on the Garage class. This keeps classes other than Garage from instantiating it. In fact, the only way to get an instance of Garage is to call Garage.getInstance(), which will always return the same instance.

You might also consider the Builder pattern, which will keep you from having to instantiate all those Car objects inside of your Garage. You would do something like this:

public class Garage {

LinkedList carhold;

public Garage() {

carhold = new LinkedList();

}

public void addCar(Car c) {

carhold.add(c);

}

}

public class GarageBuilder {

public static final int BOBS_GARAGE = 0;

public static final int BILLS_GARAGE = 1;

public static Garage buildGarage(int garage) {

Garage g = new Garage();

switch (garage) {

case BOBS_GARAGE:

Car c = new Car("bobsCar", 200);

g.add(c);

break;

case BILLS_GARAGE:

Car c = new Car("billsCar", 400);

g.add(c);

break;

}

return g;

}

}

kcraft4826a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 8
That Singlton pattern seems to have done the trick. So now whenever a timeSlot is created will it won't duplicate a new six cars?
Dash161a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 9

Nope, it won't duplicate the cars. No matter how hard you try, you will always get the same Garage object. The only problem is if you want to use a DIFFERENT Garage object.

In a real application, this is bad design. You would want to use the Builder pattern that I showed you. This keeps your Garage object more flexible. But since this is just a simple program, the Singleton works great. The Singleton pattern is great for things like database connections. If your application is only going to use 1 database, why have 5 different connection objects, possibly using 5 different connections to the same database? Just have a singleton instance.

Good luck to you.

kcraft4826a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 10
Thank You Very Much.Thank you all for your help.
Dash161a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 11
> The TIMESLOT class has an GARAGE object as an> attribute, thus enabling it access mehtods. Why does TIMESLOT need to create a new GARAGE when it's created? Shouldn't TIMESLOT be passed the requested CAR's when created?
..u.j.a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 12

> Why does TIMESLOT need to create a new GARAGE when

> it's created?

It doesn't need to, the inital way, was the only way i could come up with but i knew it was flawed.

there is finite number of cars so there has to be a GARAGE to contain them.

the singleton pattern was a much better way than my intial way.

>Shouldn't TIMESLOT be passed the

> requested CAR's when created?

if you could show me an example of passing in an object from a linked list into another object as an attribute.

I could try implementing it in my solution.

Dash161a at 2007-7-11 16:19:04 > top of Java-index,Other Topics,Patterns & OO Design...