Tuesday, May 25, 2021

Decision Tables

 A decision table is a visual tool that helps you see and understand the various actions that are taken for a variety of conditions in a complex process. Decision tables can be used for complex processes in programming, but are more often seen for policies and sets of rules that people must follow. For example, the decision table below shows the policy for paying various types of employees.




1. The top rows of the decision table state the name of the policy or process. In this case it's simply "Payroll Policy". The Rules (numbered 1, 2, 3, and 4 in this example) display the number of "rules" that exist in the policy or process.

2. Below the title of the decision table is the section where you list the conditions. In this case there are 2 conditions: The type of employee (salaried or hourly) and hours worked.

3. Across from the conditions are the condition alternatives. Here is where possible values for each condition are listed. They are also combined in all possible combinations. In this table, the "employee type" condition has two values: S (salaried) or H (hourly). The "hours worked" condition has 3 possible values: hours are more than 40, hours are equal to 40, and hours are less than 40. A condition alternative listed as a dash (--) means that the values for this condition are irrelevant.

4. Below the conditions, the actions are listed. In this example, there are four possible actions:

  1. Pay the employee their base salary
  2. Pay the employee their hourly wage
  3. Pay the employee overtime
  4. Produce an absence report

5. Beside the list of actions are the action entries. These X's tell you which actions should be performed given a particular set of condition values. For example, Rule 4 says that if an employee is hourly and works more than 40 hours, pay them their hourly wage and pay them overtime. You should realize that some sets of conditions might have more than one action, as is the case with Rule 4.

6. The Rules are the numbered columns in the table. To read a table, you locate the applicable rule by finding the column that matches a given criteria for the conditions.

For example, Rule 1 states that if an employee is salaried, it doesn't matter how many hours they worked: the action will always be to pay them their base salary. In this example, we are indifferent to the value of the "hours worked" condition - it's value doesn't matter.

See if you can explain rules 2 and 3 in your own words...

Rule 2: If an employee is hourly and works less than 40 hours, pay them their hourly wage and produce an absence report for this employee.

Rule 3: If an employee is hourly and works exactly forty hours, pay them their hourly wage.

We often need to convert decision tables into code. Usually they end up being a set of nested selections. For example, with the Payroll Policy, you need to check the employee type: If the employee is Salaried, you pay them the base salary. If the employee is not Salaried (and instead are hourly), then you need to find out how many hours they worked. Then you need to check the number of hours: if they worked under 40, produce an absence report and then pay the regular hourly wage; if they worked exactly 40, pay them the regular hourly wage; if they worked over 40 hours, pay them the regular hourly wage and then pay them overtime.

You could turn this into a program such as this:

PRINT "Enter employee type: (S)alaried or (H)ourly:"
GET employeeType
IF employeeType = "S" THEN
    CALL paySalary()
ELSE
    PRINT "Enter number of hours worked:"
    GET hoursWorked
    IF hoursWorked < 40 THEN
        CALL printAbsenceReport()
        CALL payRegularHours()
    ELSE IF hoursWorked = 40 THEN
        CALL payRegularHours()
    ELSE
        CALL payRegularHours()
        CALL payOvertime()
    END IF
END IF

Exercises

1. Use the decision table below to answer the following questions:

Delivery ChargesRules
123456
Purchase Amount>=$500<500 & >100<500 & >100<500 & >100<=100<=100
Preferred Customer?--YNN----
Paid Cash?----YNYN
Free DeliveryXX    
Charge $5  X X 
Charge $10   X X
  1. How much is delivery if a customer paid by credit card for a bill totaling $1005.49?
  2. How much is delivery if a preferred customer paid cash for a $99.95 bill?
  3. How much is delivery if a customer paid cash for a bill totaling $1250.00, if they are not a preferred customer?
  4. How much is delivery if the customer's bill comes out to $455.00, they paid by credit card, and they're a preferred customer?

Answer.
  1. Free delivery
  2. delivery is $5
  3. Free delivery
  4. Free delivery

2. The following decision table defines the policy for assigning students to various college residences:

Residence AssignmentRules
123
Student Age<21<21>=21
Residence TypeCo-EdNon Co-Ed--
Residence AssignedTrudeauMacDonaldLaurier

Write the pseudo-code for a program that prompts the user to enter the student's age and whether or not the student wants a co-ed dorm room. Then display the student's residence hall on the screen using the criteria in the table. Write the most efficient code possible.

Answer.

DISPLAY "Enter age of student:"
GET age
IF age >= 21 THEN
    residence = "Laurier"
ELSE
    DISPLAY "Does student request co-ed accomodation? (Y/N)"
    GET coed    
    IF coed = "Y" THEN
        residence = "Trudeau"
    ELSE
        residence = "MacDonald"
DISPLAY "Residence Assigned: " + residence

3. Below is a decision table that defines the amount of reimbursement for a particular medical insurance policy:

Medical Insurance PolicyRules
1234
Deductible StatusMetMetMetNot Met
Type of visitDoctorHospitalLab--
Amt. of Reimbursement50%80%70%0%

Write the pseudo-code for a program that prompts the user to enter the total amount due for the medical visit, then find out if the client has paid the deductible. If they have, then ask the user which type of visit the client had. Display the appropriate reimbursement amount percentage based on the table.

Answer.

DISPLAY "Enter total amount due:"
GET amount
DISPLAY "Has deductible been paid? (Y/N)"
GET paidDeductable
IF paidDeductable = "Y" THEN
    DISPLAY "Enter type of visit: (D)octor, (H)ospital, or (L)ab: "
    GET visitType
    IF visitType = "D" THEN
        reimburseRate = .5
    ELSE IF visitType = "H" THEN
        reimburseRate = .8
    ELSE IF visitType = "L" THEN
        reimburseRate = .7
ELSE
    remiburseRate = 0
CALCULATE reimburseAmt AS amount * reimburseRate
DISPLAY "Reimbursement Amount: $" + reimburseAmt


Structured English

Rewrite the following process descriptions using Structured English:

NOTE: There is more than one correct answer for these; if you're not sure of your own, please check with your prof.

1. Our discount policy depends on whether a customer is a new or repeat customer, but we also give a seniors' discount: New customers get a 20% discount on their second order, and repeat customers get free shipping. Customers 65 and over also get a 10% discount their current purchase.

Discount Policy:
If the customer is new then
    Offer a 20% discount to their next order
Otherwise
    Give the customer free shipping
If the customer is 65 or over then
    Offer a 10% discount on their current purchase

2. A boss wants to give all her employees a nice bonus. She tells you:
"I need a report listing every employee and the bonus I plan to give him or her. Everybody gets at least $100. All the employees in Department 2 get $200, but if they have more than 5 dependents, they should get $500, and the employees in department 3 should all get $500."

Assigning Employee Bonuses:
Assign a bonus of $100 to all employees.
If the employee is in department 2 then
    If the employee has more than 5 dependents then
        Add $500 to the bonus amount
    Otherwise,
        Add $200 to the bonus amount
Otherwise, if the employee is in department 3 then
    Add $500 to the bonus amount

Reading Decision Tables

  1. Free delivery
  2. delivery is $5
  3. Free delivery
  4. Free delivery

Creating Decision Tables

1. Check Cashing Policy:

If you have trouble identifying the conditions and their values, and the actions, it helps to break up each individual sentence into its actions and conditions. This problem is particular difficult because it's written in conversational language, and you have to pick out the important bits of information and interpret them in the proper way:
1. this first sentence provides no useful information
2. Condition: If the person has a valid account.
3. Condition: If they do (if they meet condition in sentence 2) Action: Cash the check with just the signature
4. Condition: if the check is a government check and [the person] has a valud photo ID. Action: cash the check
5. This sentence just adds to the condition that they don't have to have a valid customer id to cash a government check with photo id, but it's still a condition (it would go with the condition in #2).

Conditions:
Customer has valid account number (Values = Y, N)
Check is a government check (Values = Y, N)
Customer has photo ID (Values = Y, N)

Actions:
Cash check with signature
Cash check
Don't cash check

Number of rules: 2 values * 2 values * 2 values = 8 rules.

When populating the condition alternatives section of this table, the first condition will have a pattern of 4 Y's and 4 N's (# rules / # values = 8 / 2 = 4 of each value). The second row, will be the previous row's result divided by the # of values for the 2nd condition (4 / 2 = 2 of each). The third row will be the previous row's result divided by the # of values for the third condition (2 / 2 = 1 of each).

Check Cashing PolicyRules
12345678
Customer has valid accountYYYYNNNN
Government checkYYNNYYNN
Customer has photo IDYNYNYNYN
Cash check    X   
Cash check with signatureXXXX    
Don't cash check     XXX

Reduce the table: We can reduce rules 1 to 4 because they have the same action for all possibilities under "customer has a valid account = Y" and the values of the rest of the conditions don't matter (they will use indifference (--) symbol). We can also reduce rules 7 and 8 because they have the same action for "customer has a valid account number = N" and "government check = N" (photo ID value doesn't matter so it will receive indifference (--) symbol).

Check Cashing PolicyRules
1234
Customer has valid accountYNNN
Government check--YYN
Customer has photo ID--YN--
Cash check X  
Cash check with signatureX   
Don't cash check  XX

2. Medical Insurance Policy:

Pick out actions and conditions in policy description:

1. Introduces the conditions (a couple of factors) and actions (patients receiving some kind of reimbursement)
2. Condition: Until the deductible has been met. Action: no reimbursement to the patient.
3. Condition: If the deductible has been met...
4. Conditions: type of visit is doctor's office; type of visit is hospital, type of visit is lab; Actions: Reimburse 80%, reimburse 70%, reimburse 50%

Conditions:
Deductible has been met (Values = Y, N)
Type of visit(Values = (D)octor, (H)ospital, (L)ab)

Actions:
No reimbursement
Reimburse 80%
Reimburse 70%
Reimburse 50%

Number of rules: 2 values * 3 values = 6 rules.

Medical Insurance PolicyRules
123456
Deductible has been metYYYNNN
Type of visitDHLDHL
No reimbursement   XXX
Reimburse 80% X    
Reimburse 70%  X   
Reimburse 50%X     

D = Doctor's visit, H = Hospital visit, L = Lab visit

We can reduce this table by combining rules 1 to 3 since they all have the same action and the "Deductible has been met" is Yes for all, and the "Type of Visit" covers all possible options (D, H, L):

Medical Insurance PolicyRules
1234
Deductible has been metYYYN
Type of visitDHL--
No reimbursement   X
Reimburse 80% X  
Reimburse 70%  X 
Reimburse 50%X   

D = Doctor's visit, H = Hospital visit, L = Lab visit

Extended Entry Version:

Medical Insurance PolicyRules
1234
Deductible StatusMetMetMetNot Met
Type of visitDoctorHospitalLab--
Amt. of Reimbursement50%80%70%0%

3. Company Discount Policy:

Pick out actions and conditions in policy description:

1. Just indicates there is a customer type: wholesale and retail.
2. Condition: customer is a wholesale customer. Action: customer receives 2% discount
3. Conditions: customers are wholesale or retail; customer paid cash; Action: an additional 2% discount.
4. Condition: customer purchased 50+ units; Action: additional 2% discount.

Conditions:
Customer type (Values = (W)holesale, (R)etail)
Customer paid cash (Values = Y, N)
Number of units purchased (Values = >=50, <50

Actions:
2% discount
additional 2% discount (4%)
additional 2% dicsount (6%)
No discount (I added this even though it wasn't in the description; you'll see why later)

Number of rules: 2 values * 2 values * 2 values = 8 rules.

Customer Discount PolicyRules
12345678
Type of customerWWWWRRRR
Customer paid cashYYNNYYNN
Number of units purchased<50>=50<50>=50<50>=50<50>=50
No discount        
2% discountXXXX    
additional 2% discountXX  XX  
additional 2% discount X X X X

W = Wholesale, R = Retail

Notice that rule #7 has no action. The policy description implies that retail customers who don't pay cash and don't buy 50 units or more get no discount, so an X should go in the "No discount" row of rule # 7.

Furthermore, we can clean up the table and make it easier to read by changing the discount actions to 2%, 4%, and 6%. Then we can have only one X per rule. This will also make it easier to determine if the table can be reduced:

Customer Discount PolicyRules
12345678
Type of customerWWWWRRRR
Customer paid cashYYNNYYNN
Number of units purchased<50>=50<50>=50<50>=50<50>=50
No discount      X 
2% discount  X X  X
4% discountX  X X  
6% discount X      

W = Wholesale, R = Retail

We can now easily see that the table can't be reduced at all.

As an Extended Entry Table:

Customer Discount PolicyRules
12345678
Type of customerWholesaleWholesaleWholesaleWholesaleRetailRetailRetailRetail
Customer paid cashYYNNYYNN
Number of units purchased<50>=50<50>=50<50>=50<50>=50
Amt. of Discount4%6%2%4%2%4%0%2%

4. Airline Discount Policy:

Conditions:
Destination (India, Asia)
Passenger Age (<= 2, > 2 && < 18, > 18
Depart on Monday or Friday (Yes, No)
Stay 6 days or more (Yes, No)

Actions:
Travel Free
0% discount
10% discount
20% discount
40% discount

Number of rules: 2 values * 3 values * 2 values * 2 values = 24 rules

Airline
Discount
Policy
Rules
123456789101112131415161718192021222324
Destination:IIIIIIIIAAAAAAAAAAAAAAAA
Passenger Age:<=2<=2<=2<=23-183-183-183-18>18>18>18>18<=2<=2<=2<=23-183-183-183-18>18>18>18>18
Depart Mon/Fri?YYNNYYNNYYNNYYNNYYNNYYNN
Stay >= 6 Days?YNYNYNYNYNYNYNYNYNYNYNYN
No Discount         X           X  
10% DiscountX X X X X X X X X X X X 
20% Discount          XX            
25% Discount              XX  XX  XX
40% Discount    XXXX        XXXX    
Travel FreeXXXX        XXXX        

For rules 1 to 4, and 13 to 16, you've got an interesting set of actions: some of these columns state a 10% discount for staying 6+ days, but all of these columns also state that these passengers travel for free. Since a passenger traveling for free can't receive an additional discount, we can combine all 8 rules into rule #1, below.

You can reduce rules 10 and 22 (passengers over 18 departing on a Monday/Friday who are staying less than 6 days, regardless of destination, get no discount at all) although this may be hard to see until after you reduce the table at least once.

We can also reduce rules 5/7 (passengers traveling to India, between 3 and 18 years of age, staying 6+ days, doesn't matter what day they leave) and 6/8 (same as previous, except staying less than 6 days).

You can reduce rules 10 and 22 (passengers over 18 departing on a Monday/Friday who are 6 days or more, regardless of destination, get no discount at all) although this may be hard to see until after you reduce the table at least once.

Similarly, you can reduce rules 9 and 21 (passengers over 18 departing on a Monday/Friday who are staying less than 6 days, regardless of destination, get a 10% discount).

Reduced Table:

Airline
Discount
Policy
Rules
12345678910111213
Destination:--II----IIAAAAAA
Passenger Age:<=23-183-18>18>18>18>183-183-183-183-18>18>18
Depart Mon/Fri?------YYNNYYNNNN
Stay >= 6 Days?--YNNYYNYNYNYN
No Discount   X         
10% Discount  X XX X X X 
20% Discount     XX      
25% Discount         XXXX
40% Discount XX    XXXX  
Travel FreeX            

You could display the action stub/entry in extended entry format:

Airline
Discount
Policy
Rules
12345678910111213
Destination:--II----IIAAAAAA
Passenger Age:<=23-183-18>18>18>18>183-183-183-183-18>18>18
Depart Mon/Fri?------YYNNYYNNNN
Stay >= 6 Days?--YNNYYNYNYNYN
Discount100%40%50%0%10%30%20%50%40%75%65%35%25%

5. Delivery Fee Policy

Pick out actions and conditions in policy description:

1. Condition: if a customer buys $500 or more; Action: delivery is free.
2. Condition: if a customer buys $500 to $100; unless they are a valued customer; Action: delivery is $10.
3. Condition: In this case (meaning, if the customer is a valued customer and buys between $500 and $100, continuing from #2); Action: delivery charge is $5; Condition: unless they paid cash; Action: delivery is free
4. Condition: if customer buys $100 or less; Action: delivery is $10; Condition: unless they paid cash; Action: delivery is $5

Conditions:
Purchase amount: (Values = >=500, <500 and >100, <=100
Valued customer: (Values = Y, N)
Customer paid cash (Values = Y, N)

Actions:
Free delivery
Delivery is $5
Deliver is $10

Number of rules: 3 values * 2 values * 2 values = 12 rules.

Delivery Fee PolicyRules
123456789101112
Purchase Amount $:>=500>=500>=500>=500500 to 100500 to 100500 to 100500 to 100<=100<=100<=100<=100
Valued CustomerYYNNYYNNYYNN
Paid CashYNYNYNYNYNYN
Delivery is $10      XX X X
Delivery is $5     X  X X 
Delivery is FreeXXXXX       

We can reduce this table by combining rules 1 to 4: They all have the same action and they cover all the possibilities for "Valued Customer" and "Paid Cash" when the customer has purchased $500 or more worth of merchandise. This should make sense from the first sentence in the description.

Additionally, we can combine rules 7 and 8, which say that if the purchase amount is between $500 and $100 and the customer is not a valued of customer, it doesn't matter if they paid cash or not, the action ($10 delivery fee) is the same.

We can also reduce rules 9 and 11: both show the same action of $5 delivery fee when the purchases of $100 or less and the customer paid cash, regardless of whether or not they're a valued customer.

Finally, we can reduce rules 10 and 12 because they indicate that a customer pays $10 delivery fee if they purchase $100 or less and don't pay with cash, regardless of whether or not they're a valued customer.

Delivery Fee PolicyRules
123456
Purchase Amount $:>=500500 to 100500 to 100500 to 100<=100<=100
Valued Customer--YYN----
Paid Cash--YN--YN
Delivery is $10   X X
Delivery is $5  X X 
Delivery is FreeXX    

As an Extended Entry Table:

Delivery Fee PolicyRules
123456
Purchase Amount $:>=500500 to 100500 to 100500 to 100<=100<=100
Valued Customer--YYN----
Payment Method--CashCredit/Debit--CashCredit/Debit
Delivery ChargeFreeFree$5.00$10.00$5.00$10.00

No comments:

Post a Comment

Logical and Physical Data Flow Diagrams

Data Flow Diagram (DFD)  is a graphical representation of data flow in any system. It is capable of illustrating incoming data flow, outgoin...