The check-charge problem

The bank will charge you for every check you write, but this amount that is charged is based on your balance. If your balance is less than $100, each check you write will cost 50 cents. If your balance is between $101 and $200, the bank will charge you 40 cents per check. If your balance is between $201 and $500, the check charge is only 30 cents. If you keep a $500 balance, then there is no charge for writing checks.

Write a function which takes a balance (in dollars) and returns the per-check charge.

In reading the problem, we realize that there are several cases to be considered. The data analysis step of the design recipe shows four cases: Now that we look at it this way, the problem is unclear. Does "between" $101 and $200 mean strictly between, i.e. greater than $101 and less than $200? What happens to a balance of $100, which is neither "less than $100" nor "between $101 and $200"? In real life, you would go to the customer to clear up these questions, without whose answers you cannot write a correct program. For this problem, we'll make some assumptions (and document them in comments in the program, so future readers of the program will know why we made the decisions we did!) I'll pretend the cases are actually These cases are now exhaustive (every possibility is covered) and exclusive (there is no way for two different cases to apply at once).

Now we'll write a contract, examples, header, and template for the function:

; check-charge : num (balance) => num (per-check charge)
; Ex: (check-charge 0) => 0.50
; Ex: (check-charge 100) => 0.50
; Ex: (check-charge 100.01) => 0.40
; Ex: (check-charge 200) => 0.40
; Ex: (check-charge 350) => 0.30
; Ex: (check-charge 500) => 0
; Ex: (check-charge 600) => 0
(define (check-charge balance)
	(cond [ question answer ]
	      [ question answer ]
	      [ question answer ]
	      [ question answer ]))
Note that we listed an example for each case, as well as each of the "borderlines", a total of seven examples.

Next we fill in the questions:

(define (check-charge balance)
	(cond [(< balance 100) answer ]
	      [(and (>= balance 100)
                    (< balance 200)) answer ]
	      [(and (>= balance 200)
                    (< balance 500)) answer ]
	      [(>= balance 500) answer ]))
Since the second case won't even be looked at if the first case works, and the third case won't be looked at if the first or second case works, we can rewrite this more briefly as
(define (check-charge balance)
	(cond [(< balance 100) answer ]
	      [(< balance 200) answer ]
	      [(< balance 500) answer ]
	      [(>= balance 500) answer ]))
We then fill in the answers:
(define (check-charge balance)
	(cond [(< balance 100) 0.50]
	      [(< balance 200) 0.40]
	      [(< balance 500) 0.30]
	      [(>= balance 500) 0]))
We now test this on each of the seven examples (which I'll leave to you) and confirm that it works.
Last modified: Mon Sep 20 16:13:57 EDT 1999
Stephen Bloch / sbloch@adelphi.edu