Best way to store parking charges
Right i am making a system that simulates a parking meter.
The user inserts coins which are validated and then sent to a buffer.
There are various charges:
Parking Fees
--
Up to 15 mins 10p
Up to 30 mins 20p
Up to 45 mins 30p
Up to 1 hour 40p
Up to 2 hours 80p
Up to 3 hours ?.20
Up to 4 hours ?.60
for example.
The user enters an amount into the System e.g. 15p - i need to have these charges stored in such a way that they are:
A) Easily Maintainable
B) I will be able to match the entered amount in coins to the equivilant parking charge or the next smallest.
What is the best way of doing this?
I have a separate class at the moment called 'Charges'
I have been toying with the idea of creating many 'Charge' objects which store parking time and price - and then to hold these in an ArrayList, but i dont think this would be the ideal option.
Thanks in anticipation.
Andy
[1013 byte] By [
patonara] at [2007-10-2 6:45:57]

you could do it that way, or you could have two objects, Duration and Charge, that you store in a Map. Duration is the key and Charge is the amount to pay.
For convenience, I'd also recommend that you encapsulate all this logic into a method in a single class. I'd pass in the time on the meter as a parameter to that method and return the charge.
If you're REALLY smart, you won't hard-wire the duration/charge pairs in code. You'll externalize them into a flat file or database and read them in on startup. That way you can change the rules of the parking garage (aka raise prices) without having to recompile your code.
%
> What is the best way of doing this?
>
> I have a separate class at the moment called
> 'Charges'
>
> I have been toying with the idea of creating many
> 'Charge' objects which store parking time and price -
> and then to hold these in an ArrayList, but i dont
> think this would be the ideal option.
>
> Thanks in anticipation.
>
I am by no means an expert in object oriented design, but that approach sounds ok to me. Perhaps a more descriptive name like ParkingPeriodFee. A FeeManager could hold the ArrayList and have a method to find the right period fee for a given set of coin values.What are your concerns with the ArrayList approach?
So where's your proposal?
Either way, you have to iterate through the ArrayList or the keys in the Map to find out which range the parameter falls into. It's O(n), where n is the number of entries in the Map.
Believe me, this is an in-memory operation that will be fast enough for your needs. If your code doesn't perform, I'll bet the problem will lie elsewhere.
If this isn't sufficient, formulate and write your own data structure or do some research to think of a faster way.
%
> >What are your concerns with the ArrayList
> approach?
>
> My concerns are that this isn't the most
> efficient/best way of doing this
Don't worry about most efficient or best until you have a way to do it. Once you've got it running and giving the answers you need, then worry about whether or not it's the most efficient. Don't let the perfect be the enemy of the good.
%