i am trying to complete a homework task based on structure,but when i came to the third bullet point where it asked me to create a function for a customer instance,i got confused how am i supposed to do that,because i don't know which arguments to take.Here is the assignment brief.
A chain of supermarkets has decided to offer a loyalty card to its regular customers. Customers
can collect points to get exclusive discounts on several products.
1. Define a struct type to represent a customer. The structure should contain fields
allowing to store the name of a customer, the number of points collected by a
customer, the number of points already spent by a customer (i.e., point used to obtain
special discount) and the current number of points owned by a customer, initialized to 0
(i.e., points collected - points spent).
2. Define five named instances, e.g., “James Anita”, 3200, 1200, 0. (Note: the number of
points collected by a customer should always be greater or equal to the number of
points he already spent)
3. Write a function that takes as input an instance of a customer struct and updates the
field corresponding to the current number of points owned by a customer. The current
number of points is computed by subtracting the points spent from the points collected.
E.g., given the struct “James Anita”, 3200, 1200, 0, it should update the last field with
2000.
4. Before proceeding to the payment of his purchase, a customer can choose to use his
points. Write a function that will take as input an instance of a customer struct, the
number of points which would be collected by the customer by proceeding to the
payment of his purchase, and the number of points the customer intends to use.
a. If the number of points which would be collected by a customer by proceeding
to the payment of his purchase and his current number of points are greater
than the number of points he intends to spend, his loyalty card account will
automatically be updated after payment (number of points collected, number of
points spent, current number of points).
b. If not, the message "Unfortunately, you need XXX extra customer points!!",
where XXX corresponds to the number of points missing, will be displayed on the
screen.
and here is my code so far
(define-struct customer [name nopcbc nofpasbac cnopobac])
;; A custmoer is a structure:
;; (make-custmoer string number number number )
;(make-custmomer a b c d) represents a custmoers details
;; with a name
;; and a nopcbc b
; nofpasbac c
;cnopobac d
;; Interpretation:
;; name is the name of the customer
;; nopcbc is he number of points collected by a customer
; nofpasbac is the number of points already spent by a customer
;cnopobac is current number of points owned by a customer, initialized to 0
(define new-customer1 (make-customer “James Anita” 3200 1200 0))
(define new-customer2 (make-customer “John wood” 4200 1800 0))
(define new-customer3 (make-customer “Tom Smith” 3600 1500 0))
(define new-customer4 (make-customer “Brooke eve” 3100 1000 0))
(define new-customer5 (make-customer “Osman Hassan” 3900 1400 0))
CodePudding user response:
(define-struct customer (name points-collected points-spent points-current)
#:transparent
#:mutable)
(define customer-1 (customer "James Anita" 3200 1200 0))
(define customer-2 (customer "John Wood" 4200 1000 0))
(define customer-3 (customer "Tom Smith" 3600 1500 0))
(define customer-4 (customer "Booke Eve" 3100 1000 0))
(define customer-5 (customer "Osman Hassan" 3900 1400 0))
(define (update-current cust)
(set-customer-points-current! cust (- (customer-points-collected cust)
(customer-points-spent cust))))
(define (purchase cust points-to-collect points-to-spend)
(update-current cust)
(if (<= points-to-spend ( points-to-collect (customer-points-current cust)))
(let ((collected ( (customer-points-collected cust) points-to-collect))
(spent ( (customer-points-spent cust) points-to-spend))
(curr (- (customer-points-current cust)
(- points-to-spend points-to-collect))))
(set-customer-points-collected! cust collected)
(set-customer-points-spent! cust spent)
(set-customer-points-current! cust curr))
(string-append "Unfortunately, you need "
(number->string (- points-to-spend
(customer-points-current cust)
points-to-collect))
" extra customer points!!")))
CodePudding user response:
but when i came to the third bullet point
Just a possible solution of the third bullet point.
(define-struct customer [name nopcbc nofpasbac [cnopobac #:mutable]])
(define new-customer1 (make-customer "James Anita" 3200 1200 0))
(define new-customer2 (make-customer "John wood" 4200 1800 0))
(define new-customer3 (make-customer "Tom Smith" 3600 1500 0))
(define new-customer4 (make-customer "Brooke eve" 3100 1000 0))
(define new-customer5 (make-customer "Osman Hassan" 3900 1400 0))
(define (update c)
(let ((nopcbc (customer-nopcbc c))
(nofpasbac (customer-nofpasbac c)))
(set-customer-cnopobac! c (- nopcbc nofpasbac))))
(displayln (customer-cnopobac new-customer1))
(update new-customer1)
(displayln (customer-cnopobac new-customer1))
;; 0
;; 2000