From 01ac611650556e3dbf8218131a7d2338139a6bba Mon Sep 17 00:00:00 2001 From: Ivan Shamshurin Date: Wed, 29 Jan 2025 23:27:55 +0300 Subject: [PATCH 1/5] display sales table --- otus-06/src/otus_06/homework.clj | 79 +++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/otus-06/src/otus_06/homework.clj b/otus-06/src/otus_06/homework.clj index d5d5228..474e440 100644 --- a/otus-06/src/otus_06/homework.clj +++ b/otus-06/src/otus_06/homework.clj @@ -1,4 +1,6 @@ -(ns otus-06.homework) +(ns otus-06.homework + (:require [clojure.java.io :as io] + [clojure.string :as string])) ;; Загрузить данные из трех файлов на диске. ;; Эти данные сформируют вашу базу данных о продажах. @@ -94,3 +96,78 @@ ;; Файлы находятся в папке otus-06/resources/homework +(def tables {:customer "resources/homework/cust.txt" + :product "resources/homework/prod.txt" + :sales "resources/homework/sales.txt"}) + +(defn- split-line [line] + (string/split line #"\|")) + +(defn- fmt-line [line] + (-> (split-line line) + rest + vec)) + +(defn- print-table [fmt-fn name] + (with-open [rdr (io/reader (tables name))] + (doseq [lines (line-seq rdr)] + (println (fmt-fn lines))))) + +(defn- table-value [table-key id & rest-options] + (with-open [rdr (io/reader (tables table-key))] + (loop [lines (line-seq rdr)] + (let [line-vec (split-line (first lines)) + [value-col id-col] rest-options] + (cond (= (line-vec (or id-col 0)) id) (line-vec value-col) + (empty? (rest lines)) nil + :else (recur (rest lines))))))) + +(defn- fmt-sales-line [line] + (let [line-vec (fmt-line line) + [customer-id product-id count] line-vec] + (vector (table-value :customer customer-id 1) + (table-value :product product-id 1) + count))) + +(defn- print-total-sales [customer-name] + ) + +(defn- print-menu [] + (println " +*** Sales Menu *** +------------------ +1. Display Customer Table +2. Display Product Table +3. Display Sales Table +4. Total Sales for Customer +5. Total Count for Product +6. Exit + +Enter an option? +")) + +(defn- make-action [input] + (cond (= input "1") (do (print-table fmt-line :customer) + true) + (= input "2") (do (print-table fmt-line :product) + true) + (= input "3") (do (print-table fmt-sales-line :sales) + true) + (= input "6") nil + :else true)) + +(defn -main [] + (print-menu) + (loop [num (read-line)] + (and (make-action num) + (do (print-menu) + (recur (read-line)))))) + +(comment + (table-value :customer "2" 1) + (table-value :product "3" 2) + (slurp "resources/homework/cust.txt") + (with-out-str (print-table fmt-line :customer)) + (print-table fmt-line :sales) + (-main) + ()) From 58344b589759cc8b453cff858886bc1beb8d8c5e Mon Sep 17 00:00:00 2001 From: Ivan Shamshurin Date: Thu, 30 Jan 2025 00:26:12 +0300 Subject: [PATCH 2/5] print total customer price --- otus-06/src/otus_06/homework.clj | 39 +++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/otus-06/src/otus_06/homework.clj b/otus-06/src/otus_06/homework.clj index 474e440..445afed 100644 --- a/otus-06/src/otus_06/homework.clj +++ b/otus-06/src/otus_06/homework.clj @@ -113,11 +113,10 @@ (doseq [lines (line-seq rdr)] (println (fmt-fn lines))))) -(defn- table-value [table-key id & rest-options] +(defn- table-value [table-key id & [value-col id-col]] (with-open [rdr (io/reader (tables table-key))] (loop [lines (line-seq rdr)] - (let [line-vec (split-line (first lines)) - [value-col id-col] rest-options] + (let [line-vec (split-line (first lines))] (cond (= (line-vec (or id-col 0)) id) (line-vec value-col) (empty? (rest lines)) nil :else (recur (rest lines))))))) @@ -129,8 +128,27 @@ (table-value :product product-id 1) count))) +(defn- get-product-price [id] + (let [price-col 2] + (parse-double (table-value :product id price-col)))) + (defn- print-total-sales [customer-name] - ) + (let [customer-id (table-value :customer customer-name 0 1)] + (with-open [rdr (io/reader (tables :sales))] + (loop [lines (line-seq rdr) + total-price 0.0] + (if (empty? lines) + (println (vector customer-name total-price)) + (let [line (fmt-line (first lines)) + line-customer-id (line 0) + product-id (line 1) + product-count (Integer/parseInt (line 2)) + total-product-price (* (get-product-price product-id) + product-count) + rest-lines (rest lines)] + (recur rest-lines (if (= customer-id line-customer-id) + (+ total-price total-product-price) + total-price)))))))) (defn- print-menu [] (println " @@ -148,11 +166,14 @@ Enter an option? (defn- make-action [input] (cond (= input "1") (do (print-table fmt-line :customer) - true) + true) (= input "2") (do (print-table fmt-line :product) - true) + true) (= input "3") (do (print-table fmt-sales-line :sales) - true) + true) + (= input "4") (do (println "Input customer name:") + (print-total-sales (read-line)) + true) (= input "6") nil :else true)) @@ -164,10 +185,12 @@ Enter an option? (recur (read-line)))))) (comment - (table-value :customer "2" 1) (table-value :product "3" 2) + (table-value :customer "Sue Jones" 0 1) + (table-value :customer "3" 1) (slurp "resources/homework/cust.txt") (with-out-str (print-table fmt-line :customer)) (print-table fmt-line :sales) + (print-total-sales "Sue Jones") (-main) ()) From 7a5c94aff4ec75f465ee56b711f1dd603a049058 Mon Sep 17 00:00:00 2001 From: Ivan Shamshurin Date: Thu, 30 Jan 2025 01:37:42 +0300 Subject: [PATCH 3/5] print total product count --- otus-06/src/otus_06/homework.clj | 58 +++++++++++++++++++------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/otus-06/src/otus_06/homework.clj b/otus-06/src/otus_06/homework.clj index 445afed..07e271c 100644 --- a/otus-06/src/otus_06/homework.clj +++ b/otus-06/src/otus_06/homework.clj @@ -96,10 +96,6 @@ ;; Файлы находятся в папке otus-06/resources/homework -(def tables {:customer "resources/homework/cust.txt" - :product "resources/homework/prod.txt" - :sales "resources/homework/sales.txt"}) - (defn- split-line [line] (string/split line #"\|")) @@ -109,9 +105,12 @@ vec)) (defn- print-table [fmt-fn name] - (with-open [rdr (io/reader (tables name))] - (doseq [lines (line-seq rdr)] - (println (fmt-fn lines))))) + (let [tables {:customer "resources/homework/cust.txt" + :product "resources/homework/prod.txt" + :sales "resources/homework/sales.txt"}] + (with-open [rdr (io/reader (tables name))] + (doseq [lines (line-seq rdr)] + (println (fmt-fn lines)))))) (defn- table-value [table-key id & [value-col id-col]] (with-open [rdr (io/reader (tables table-key))] @@ -132,23 +131,32 @@ (let [price-col 2] (parse-double (table-value :product id price-col)))) -(defn- print-total-sales [customer-name] - (let [customer-id (table-value :customer customer-name 0 1)] +(defn- get-total-product-price [[_ product-id product-count-str]] + (let [product-count (Integer/parseInt product-count-str)] + (* (get-product-price product-id) + product-count))) + +(defn- get-total-product-count [[_ _ product-count]] + (Integer/parseInt product-count)) + +(defn- print-total [name table-key get-total-value] + (let [fomat-str {:customer "%s: %.2f" + :product "%s: %d"} + sales-columns {:customer 0 + :product 1} + id (table-value table-key name 0 1)] (with-open [rdr (io/reader (tables :sales))] (loop [lines (line-seq rdr) - total-price 0.0] + total 0] (if (empty? lines) - (println (vector customer-name total-price)) + (println (format (fomat-str table-key) name total)) (let [line (fmt-line (first lines)) - line-customer-id (line 0) - product-id (line 1) - product-count (Integer/parseInt (line 2)) - total-product-price (* (get-product-price product-id) - product-count) + line-id (line (sales-columns table-key)) + total-product (get-total-value line) rest-lines (rest lines)] - (recur rest-lines (if (= customer-id line-customer-id) - (+ total-price total-product-price) - total-price)))))))) + (recur rest-lines (if (= id line-id) + (+ total total-product) + total)))))))) (defn- print-menu [] (println " @@ -172,7 +180,10 @@ Enter an option? (= input "3") (do (print-table fmt-sales-line :sales) true) (= input "4") (do (println "Input customer name:") - (print-total-sales (read-line)) + (print-total (read-line) :customer get-total-product-price) + true) + (= input "5") (do (println "Input product name:") + (print-total (read-line) :product get-total-product-count) true) (= input "6") nil :else true)) @@ -188,9 +199,10 @@ Enter an option? (table-value :product "3" 2) (table-value :customer "Sue Jones" 0 1) (table-value :customer "3" 1) - (slurp "resources/homework/cust.txt") - (with-out-str (print-table fmt-line :customer)) + (print-table fmt-line :product) + (print-table fmt-line :customer) (print-table fmt-line :sales) - (print-total-sales "Sue Jones") + (print-total "Sue Jones" :customer get-total-product-price) + (print-total "shoess" :product get-total-product-count) (-main) ()) From 9d71fc9c6a3bf73620b52506f4f22ecff1009fc4 Mon Sep 17 00:00:00 2001 From: Ivan Shamshurin Date: Thu, 30 Jan 2025 18:55:56 +0300 Subject: [PATCH 4/5] fix errors --- otus-06/src/otus_06/homework.clj | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/otus-06/src/otus_06/homework.clj b/otus-06/src/otus_06/homework.clj index 07e271c..6fff0f7 100644 --- a/otus-06/src/otus_06/homework.clj +++ b/otus-06/src/otus_06/homework.clj @@ -103,14 +103,13 @@ (-> (split-line line) rest vec)) - +(def ^:private tables {:customer "resources/homework/cust.txt" + :product "resources/homework/prod.txt" + :sales "resources/homework/sales.txt"}) (defn- print-table [fmt-fn name] - (let [tables {:customer "resources/homework/cust.txt" - :product "resources/homework/prod.txt" - :sales "resources/homework/sales.txt"}] - (with-open [rdr (io/reader (tables name))] - (doseq [lines (line-seq rdr)] - (println (fmt-fn lines)))))) + (with-open [rdr (io/reader (tables name))] + (doseq [lines (line-seq rdr)] + (println (fmt-fn lines))))) (defn- table-value [table-key id & [value-col id-col]] (with-open [rdr (io/reader (tables table-key))] From 90e301c43cc4517c5105b7b84c9562d44ca6bb48 Mon Sep 17 00:00:00 2001 From: Ivan Shamshurin Date: Sat, 8 Feb 2025 13:37:37 +0300 Subject: [PATCH 5/5] update thread first usage --- otus-06/src/otus_06/homework.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/otus-06/src/otus_06/homework.clj b/otus-06/src/otus_06/homework.clj index 6fff0f7..53dbd9f 100644 --- a/otus-06/src/otus_06/homework.clj +++ b/otus-06/src/otus_06/homework.clj @@ -100,7 +100,8 @@ (string/split line #"\|")) (defn- fmt-line [line] - (-> (split-line line) + (-> line + split-line rest vec)) (def ^:private tables {:customer "resources/homework/cust.txt"