Object-oriented programming¶
Simple type extension¶
Goal: work with type extensions using record types containing numeric components.
Steps:
Implement the
Type_Extensions
package.
Declare the record type
T_Float
.Declare the record type
T_Mixed
Implement the
Init
function for theT_Float
type with a floating-point input parameter.Implement the
Init
function for theT_Float
type with an integer input parameter.Implement the
Image
function for theT_Float
type.Implement the
Init
function for theT_Mixed
type with a floating-point input parameter.Implement the
Init
function for theT_Mixed
type with an integer input parameter.Implement the
Image
function for theT_Mixed
type.
Requirements:
Record type
T_Float
contains the following component:
F
, a floating-point type.Record type
T_Mixed
is derived from theT_Float
type.
T_Mixed
extendsT_Float
with the following component:
I
, an integer component.Both components must be numerically synchronized:
For example, if the floating-point component contains the value 2.0, the value of the integer component must be 2.
In order to simplify the implementation, you can simply use
Integer (F)
to convert a floating-point variableF
to integer.Function
Init
returns an object of the corresponding type (T_Float
orT_Mixed
).
For each type, two versions of
Init
must be declared:
one with a floating-point input parameter,
another with an integer input parameter.
The parameter to
Init
is used to initialize the record components.Function
Image
returns a string for the components of the record type.
In case of the
Image
function for theT_Float
type, the string must have the format"{ F => <float value> }"
.
For example, the call
Image (T_Float'(Init (8.0))))
should return the string"{ F => 8.00000E+00 }"
.In case of the
Image
function for theT_Mixed
type, the string must have the format"{ F => <float value>, I => <integer value> }"
.
For example, the call
Image (T_Mixed'(Init (8.0))))
should return the string"{ F => 8.00000E+00, I => 8 }"
.
Online Store¶
Goal: create an online store for the members of an association.
Steps:
Implement the
Online_Store
package.
Declare the
Member
type.Declare the
Full_Member
type.Implement the
Get_Status
function for theMember
type.Implement the
Get_Price
function for theMember
type.Implement the
Get_Status
function for theFull_Member
type.Implement the
Get_Price
function for theFull_Member
type.Implement the
Online_Store.Tests
child package.
Implement the
Simple_Test
procedure.
Requirements:
Package
Online_Store
implements an online store application for the members of an association.
In this association, members can have one of the following status:
associate member, or
full member.
Function
Get_Price
returns the correct price of an item.
Associate members must pay the full price when they buy items from the online store.
Full members can get a discount.
The discount rate can be different for each full member — depending on factors that are irrelevant for this exercise.
Package
Online_Store
has following types:
Percentage
type, which represents a percentage ranging from 0.0 to 1.0.
Member
type for associate members containing following components:
Start
, which indicates the starting year of the membership.
This information is common for both associate and full members.
You can use the
Year_Number
type from the standardAda.Calendar
package for this component.
Full_Member
type for full members.
This type must extend the
Member
type above.It contains the following additional component:
Discount
, which indicates the discount rate that the full member gets in the online store.
This component must be of
Percentage
type.For the
Member
andFull_Member
types, you must implement the following functions:
Get_Status
, which returns a string with the membership status.
The string must be
"Associate Member"
or"Full Member"
, respectively.
Get_Price
, which returns the adapted price of an item — indicating the actual due amount.
For example, for a full member with a 10% discount rate, the actual due amount of an item with a price of 100.00 is 90.00.
Associated members don't get a discount, so they always pay the full price.
Procedure
Simple_Test
(from theOnline_Store.Tests
package) is used for testing.
Based on a list of members that bought on the online store and the corresponding full price of the item,
Simple_Test
must display information about each member and the actual due amount after discounts.Information about the members must be displayed in the following format:
Member # <number> Status: <status> Since: <year> Due Amount: <value> --------For this exercise,
Simple_Test
must use the following list:
#
Membership status
Start (year)
Discount
Full Price
1
Associate
2010
N/A
250.00
2
Full
1998
10.0 %
160.00
3
Full
1987
20.0 %
400.00
4
Associate
2013
N/A
110.00
In order to pass the tests, the information displayed by a call to
Simple_Test
must conform to the format described above.
You can find another example in the remarks below.
Remarks:
In previous labs, we could have implemented a simplified version of the system described above by simply using an enumeration type to specify the membership status. For example:
type Member_Status is (Associate_Member, Full_Member);
In this case, the
Get_Price
function would then evaluate the membership status and adapt the item price — assuming a fixed discount rate for all full members. This could be the corresponding function declaration:type Amount is delta 10.0**(-2) digits 10; function Get_Price (M : Member_Status; P : Amount) return Amount;In this exercise, however, we'll use type extension to represent the membership status in our application.
For the procedure
Simple_Test
, let's consider the following list of members as an example:
#
Membership status
Start (year)
Discount
Full Price
1
Associate
2002
N/A
100.00
2
Full
2005
10.0 %
100.00
For this list, the test procedure displays the following information (in this exact format):
Member # 1 Status: Associate Member Since: 2002 Due Amount: 100.00 -------- Member # 2 Status: Full Member Since: 2005 Due Amount: 90.00 --------Here, although both members had the same full price (as indicated by the last column), member #2 gets a reduced due amount of 90.00 because of the full membership status.