;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #| --------------------------------------------------------------- PROBLEM: Design an airline customer system. The system represents each customer via a title, first name, and last name. Airlines use the following titles: Dr., Mr., Ms. The system should also support a function for writing formal letter openers and a function for measuring the number of characters in a customer record. Problem 1: Create a data definition for customer records. Problem 2: Design a function that produces a letter formal opening from a customer record. Remember that a formal letter opening is something such as "Dear Dr. Racket:". Problem 3: Design a function that counts the characters in a customer record. |# (define-struct dr (first last)) (define-struct mr (first last)) (define-struct ms (first last)) ;; A Customer is one of: ;; - (make-dr String String) ;; - (make-mr String String) ;; - (make-ms String String) ;; cust-temp : Customer -> ? #;(define (cust-temp c) (cond [(dr? c) ... (dr-first c) ... (dr-last c) ...] [(mr? c) ... (mr-first c) ... (mr-last c) ...] [(ms? c) ... (ms-first c) ... (ms-last c) ...])) ;; opening: Customer -> String ;; produces a formal letter opening, e.g., "Dear Dr. Shivers:" (define (opening c) (cond [(dr? c) (string-append "Dear Dr. " (dr-last c) ":")] [(mr? c) (string-append "Dear Mr. " (mr-last c) ":")] [(ms? c) (string-append "Dear Ms. " (ms-last c) ":")])) ;; Examples of Customer: (define c1 (make-dr "Matthias" "Shivers")) (define c2 (make-mr "Olin" "Shivers")) (define c3 (make-ms "Leena" "Razzaq")) ;; Examples/Tests (check-expect (opening c1) "Dear Dr. Shivers:") (check-expect (opening c2) "Dear Mr. Shivers:") (check-expect (opening c3) "Dear Ms. Razzaq:") ;; Left to *you* as an exercise: ;; Problem 3: Design a function that counts the characters in a ;; customer record. E.g., number of characters in c1 is 18 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM ;; We have interns, who work at an hourly rate, and full-time ;; employees, who are salaried. We need to keep track of these ;; employees, and print out their paychecks once a month: ;; "Pay to the order of Olin Shivers, $15000." (define-struct nam (first last)) ;; A Name is a (make-name String String) (define-struct intern (wage hrs/wk name)) (define-struct fulltime (salary name)) ;; An Employee is one of: ;; - (make-intern Number Number Name) ;; - (make-fulltime Number Name) ;; employee-temp : Employee -> ? #;(define (employee-temp emp) (cond [(intern? emp) ... (intern-wage emp) ... (intern-hrs/wk emp) ... (intern-name emp) ...] [(fulltime? emp) ... (fulltime-salary emp) ... (fulltime-name emp) ...])) ;; Examples of Employee (define e1 (make-fulltime 15000 (make-nam "Olin" "Shivers"))) (define e2 (make-intern 20 30 (make-nam "Mark" "Speedy"))) #| ;; FIRST TRY: Just code it... ;; print-paycheck : Employee -> String ;; given an employee, print monthly paycheck ;; e.g. "Pay to the order of Olin Shivers, $15000." (define (print-paycheck emp) (cond [(intern? emp) (string-append (nam-first (intern-name emp)) " " (nam-last (intern-name emp)) ", $" (number->string (* 4 (intern-wage emp) (intern-hrs/wk emp))) ".")] [(fulltime? emp) (string-append "Pay to the order of " (nam-first (fulltime-name emp)) " " (nam-last (fulltime-name emp)) ", $" (number->string (fulltime-salary emp)) ".")])) |# ;; SECOND TRY: Let's try to make the code easier to read, avoid duplication, ... ;; monthly-pay : Employee -> Number ;; calculate monthly pay for employee (define (monthly-pay emp) (cond [(intern? emp) (* 4 (intern-wage emp) (intern-hrs/wk emp))] [(fulltime? emp) (fulltime-salary emp)])) (check-expect (monthly-pay e1) 15000) (check-expect (monthly-pay e2) 2400) ;; emp-name : Employee -> String ;; get the full name of emp (define (emp-name emp) (cond [(intern? emp) (string-append (nam-first (intern-name emp)) " " (nam-last (intern-name emp)))] [(fulltime? emp) (string-append (nam-first (fulltime-name emp)) " " (nam-last (fulltime-name emp)))])) (check-expect (emp-name e1) "Olin Shivers") (check-expect (emp-name e2) "Mark Speedy") ;; print-paycheck : Employee -> String ;; given an employee, print monthly paycheck ;; e.g. "Pay to the order of Olin Shivers, $15000." (define (print-paycheck emp) (string-append "Pay to the order of " (emp-name emp) ", $" (number->string (monthly-pay emp)) ".")) (check-expect (print-paycheck e1) "Pay to the order of Olin Shivers, $15000.") (check-expect (print-paycheck e2) "Pay to the order of Mark Speedy, $2400.")