We help IT Professionals succeed at work.

Design Pattern question in relation to objects

g-spot
g-spot asked
on
172 Views
Last Modified: 2010-04-23
I have a class called called "Ticket" that will represent a ticket bought for a show. The class has public properties like "price", "date", ticket-type". This works OK becuase each property just takes a string, date or integer value.

My problem is dealing with the age groups that will belong to the ticket. For each ticket there may be a number of different age group properties.

For example 1 "real world" ticket may be for 6 people - 2 aged 18-65, 2 aged 3-17 and 2 aged 0-2.

How do I represent this is my class? Do I need to create separate properties for each possible age group or is there a more sophisticated way to tackle this? Such as an array property of an object?

Please go easy on me as I'm a relative beginner.

Cheers!
Comment
Watch Question

fcnatraSoftware...
CERTIFIED EXPERT

Commented:
If there are diferent age groups belonging to the ticket, and each time the number of age groups could be different , I recommend you to use an structure with the fields (initial age, final age and number of people) and a public array (or arraylist)  of that structure as a class property.

Think also about using a method to add elements to the age groups so you could avoid the age groups to be overlaped each other.

CN.

Author

Commented:
Cnatra

Thanks for your reply. That sounds like good advice. Would you be able to provide a code example or point me to a resource on thne web that can tell me more.

Thanks.

Author

Commented:
Should I make a "Person" object that has attributes of age, name etc?

How would I link this "Person" object to the "Ticket" object?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Define two properties:

AgeRangeMin
AgeRangeMax

Bob

Author

Commented:
Hi Bob

Yes, that makes sense. These would be properties of my "Person" object?

How would I link my "Person" objects to my "Ticket" object. Or do I need to at all?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I don't quite understand the relationship, but I was assuming that a Person had an age, and a ticket was for a certain age range.  So, you could check something like this:  

If Person.Age < Ticket.AgeRangeMin Or Person.Age > Ticket.AgeRangeMax
End If

Did I get that assumption wrong?

Bob

Author

Commented:
Bob

A ticket could be for a whole family and the ticket could "contain" multiple people. The cost of that ticket would depend on the age ranges of the people "contained" within the ticket.

I have a form where people choose the date etc and then input the number of people for each age range. This data needs to be processed somehow to make the price. My current system just has a whole lot of variables for each age range (No OOP at all) and I just wanted to try out some OOP methodology on my system.
stephen
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Do you have static age groups, or are they more dynamic?

Bob

Author

Commented:
Theyre static:

Age 81-89
Age 75-80
Age 70-74
Age 65-69
Age 46-64
Age 18-45
Age 18-23 (Student)
Age 3-17
Age 0-2

However, there may be  a time when things are more dynamic. But I'll stick with static for now! ;-)


CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Ticket (price, date, type)
|
+- Age Group (min, max)
     |
     +- Person (age)

Let me see if I can understand what you are talking about:

  1.  A ticket has 1..many age groups
  2.  An age group has 1..many persons

Bob

Author

Commented:
Yes. Thats right.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Bob. Thanks for your help on this.

I guess I'm getting in over my head as I'm not sure what strong type collections are.

I'd hoped that the OOP approach was going to be easier but seems to be more work.

Its going to take me a little while I work this through so I may not be back to you for a couple of days.

I think I'm getting the idea of what youre proposing.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Strong type collections are collections in which you can add elements to, and the compiler can check to make sure that you are adding the correct type.  Loose typed collections accept the 'Object' type and can be anything.  You can run into problems when you mix types in collections, so strong-typed collections are a good tool.

If you look around the .NET framework, you will see many examples of strong-typed collections:

System.Text.RegularExpressions.CaptureCollection
System.Text.RegularExpressions.GroupCollection
System.Web.UI.WebControls.DataListItemCollection
System.Web.UI.WebControls.TableRowCollection
...

Bob

Author

Commented:
Bob

How would I create these objects from the client code:

This woud create the Ticket object:

Dim objTicket As Ticket = New Ticket()
objTicket.Type = "Family
objTicket.date = Date
....

This would create the AgeGroup objects:
Dim objAgeGroup1 As AgeGroup = New AgeGroup()
objAgeGroup.Min = 18
obAgeGroup.Max = 45
obAgeGroup.People = txtAge18-45.Text

Dim objAgeGroup2 As AgeGroup = New AgeGroup()
objAgeGroup.Min = 46
obAgeGroup.Max = 65
obAgeGroup.People = txtAge46-65.Text

Dim objAgeGroup3 As AgeGroup = New AgeGroup()
objAgeGroup.Min = 0
obAgeGroup.Max = 2
obAgeGroup.People = txtAge0-2.Text

Then add the AgeGroups to the Ticket:

objTicket.AgeGroups = Not sure what to do here

Also, how would I create the people from this?



fcnatraSoftware...
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi.

Apologies for the delay in coming back to this. I've been busy all week.

I'm still trying to get my head round these objects and collections. Taking things a step at a time I can create a bunch of AgeGroup objects. I can also add those objects to a AgeGroups collection:

        Dim objAgeGroup1 As AgeGroup = New AgeGroup()
        Dim objAgeGroup2 As AgeGroup = New AgeGroup()
        Dim objAgeGroup3 As AgeGroup = New AgeGroup()
        Dim objAgeGroup4 As AgeGroup = New AgeGroup()
        Dim objAgeGroup5 As AgeGroup = New AgeGroup()
        Dim objAgeGroup6 As AgeGroup = New AgeGroup()

        objAgeGroup1.Min = 80
        objAgeGroup1.Max = 89
        objAgeGroup1.Quantity = 1

        objAgeGroup2.Min = 75
        objAgeGroup2.Max = 80
        objAgeGroup2.Quantity = 2

        objAgeGroup3.Min = 70
        objAgeGroup3.Max = 74
        objAgeGroup3.Quantity = 3

        objAgeGroup4.Min = 65
        objAgeGroup4.Max = 69
        objAgeGroup4.Quantity = 4

        objAgeGroup5.Min = 46
        objAgeGroup5.Max = 64
        objAgeGroup5.Quantity = 2

        objAgeGroup6.Min = 18
        objAgeGroup6.Max = 45
        objAgeGroup6.Quantity = 2

        Dim AgeGroups As AgeGroupCollection = New AgeGroupCollection()

        AgeGroups.Add(objAgeGroup1)
        AgeGroups.Add(objAgeGroup2)
        AgeGroups.Add(objAgeGroup3)
        AgeGroups.Add(objAgeGroup4)
        AgeGroups.Add(objAgeGroup5)
        AgeGroups.Add(objAgeGroup6)

Now, say i want to output the Min and Max Age and Quantity for each AgeGroup in my AgeGroups collection. How would I do that?

Author

Commented:
OK. Well I think this seems to work:

        For Each a As AgeGroup In AgeGroups

            Response.Write("Min Age: " & a.Min & "<br /> Max Age: " & a.Max & "<br /> Quantity: " & a.Quantity & "<br /><br /><br />")

        Next


I would like to pass the AgeGroups object to a function which would then process all the AgeGroup objects within:

Call to function:

intCost = GetQuote(strProductCode, strGeographicArea, strGroupType, strDuration, AgeGroups)

Function:

Public Function GetQuote(ByVal strProduct As String, ByVal strGeographicArea As String, ByVal strGroupType As String, ByVal intDuration As Integer, ByVal objAgeGroup As Object) As Integer

                Dim AgeGroup As AgeGroup = New AgeGroup()

                For Each a As AgeGroup In objAgeGroup

                    intPrice = intPrice + GetPrice(strProduct, strGeographicArea, strGroupType, intDuration, AgeGroup.Min, AgeGroup.Max)

                Next

                Return intPrice

End Function

The GetPrice function calls a database to get the price.

However this code cant get at the AgeGroup objects within the the AgeGroups object. How do i get at those values?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
The age groups are part of the ticket:

For Each age As AgeGroup In ticket.AgeGroups
Next age

Bob

Author

Commented:
Bob

I was taking things one step at a time and just created a bunch of AgeGroup objects and an AgeGroups collection. I managed to fix the code above so all is working in that respect.

                Dim AgeGroups As AgeGroupCollection = objAgeGroup
                For Each a As AgeGroup In objAgeGroup
                    decPrice = GetPrice(strProduct, strGeographicArea, strGroupType, intDuration, a.Min, a.Max)
                Next

I will add the AgeGroups collection to the ticket object later. Thanks for your help and advice on this.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.