Now when you order pizzas it tells you the cost - I'm not happy with the
implementation though, in needs to be more idiomatic and more efficient
This commit is contained in:
parent
46c32a90bb
commit
4c14838f7e
|
@ -7,16 +7,16 @@
|
||||||
(define port 4444)
|
(define port 4444)
|
||||||
(define hostname "127.0.0.1")
|
(define hostname "127.0.0.1")
|
||||||
|
|
||||||
; Return a list of pizzas from the local file
|
; Return a list of all the pizzas from the local JSON file
|
||||||
(define (get-pizzas-list)
|
(define (get-all-pizzas)
|
||||||
(let ((file (open-input-file "pizza_list.json")))
|
(let ((file (open-input-file "pizzas_list.json")))
|
||||||
(read-json file)))
|
(read-json file)))
|
||||||
|
|
||||||
; Send the pizza list as a JSON, prepending it with 4 bytes that contain the list length
|
; Send the list of all pizzas as a JSON, prepending it with 4 bytes that contain the list length
|
||||||
(define (send-pizzas-list output)
|
(define (send-pizzas-list output)
|
||||||
(let ((pizzas-list (get-pizzas-list)))
|
(let ((all-pizzas-list (get-all-pizzas)))
|
||||||
(write-bytes (integer->integer-bytes (bytes-length (jsexpr->bytes pizzas-list)) 4 #f) output)
|
(write-bytes (integer->integer-bytes (bytes-length (jsexpr->bytes all-pizzas-list)) 4 #f) output)
|
||||||
(write-json pizzas-list output)))
|
(write-json all-pizzas-list output)))
|
||||||
|
|
||||||
; Print the to-print string on the printer
|
; Print the to-print string on the printer
|
||||||
; TODO add synchronization to avoid race conditions
|
; TODO add synchronization to avoid race conditions
|
||||||
|
@ -25,12 +25,27 @@
|
||||||
(display (string-replace to-print "\n" "\n\n") file) ; always add an extra newline because the printer is a little messed up
|
(display (string-replace to-print "\n" "\n\n") file) ; always add an extra newline because the printer is a little messed up
|
||||||
(close-output-port file)))
|
(close-output-port file)))
|
||||||
|
|
||||||
; Format the pizzas array from the JSON in a human-readable way
|
; Return the price of the pizza * its quantity, searching in all-pizzas-list
|
||||||
(define (format-pizza pizzas-list)
|
(define (get-price-from-pizza pizza all-pizzas-list)
|
||||||
(let ((str ""))
|
; TODO rewrite in a more idiomatic way
|
||||||
|
(let ((found-price 0))
|
||||||
(for-each (lambda (pizza-hashmap)
|
(for-each (lambda (pizza-hashmap)
|
||||||
(set! str (string-append str (format "~a : ~s\n" (hash-ref pizza-hashmap 'quantity) (hash-ref pizza-hashmap 'name)))))
|
(if (equal? (hash-ref pizza 'name) (hash-ref pizza-hashmap 'name))
|
||||||
|
(set! found-price (hash-ref pizza-hashmap 'price))
|
||||||
|
#f))
|
||||||
|
all-pizzas-list)
|
||||||
|
(* found-price (hash-ref pizza 'quantity))))
|
||||||
|
|
||||||
|
; Format the pizzas list coming from the client in a human-readable way
|
||||||
|
(define (format-pizza pizzas-list)
|
||||||
|
(let ((str "")
|
||||||
|
(price 0)
|
||||||
|
(all-pizzas-list (hash-ref (get-pizzas-list) 'pizzas)))
|
||||||
|
(for-each (lambda (pizza-hashmap)
|
||||||
|
(set! str (string-append str (format "~a : ~s\n" (hash-ref pizza-hashmap 'quantity) (hash-ref pizza-hashmap 'name))))
|
||||||
|
(set! price (+ price (get-price-from-pizza pizza-hashmap all-pizzas-list))))
|
||||||
pizzas-list)
|
pizzas-list)
|
||||||
|
(set! str (string-append str (format "Total price ~a" price)))
|
||||||
str))
|
str))
|
||||||
|
|
||||||
; Parse the commands given in the JSON
|
; Parse the commands given in the JSON
|
||||||
|
|
Loading…
Reference in New Issue
Block a user