-
Notifications
You must be signed in to change notification settings - Fork 8
Homework4 #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MitrickX
wants to merge
8
commits into
Clojure-Developer:main
Choose a base branch
from
MitrickX:Homework4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Homework4 #24
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8afb0b8
Merge pull request #1 from OtusTeam/main
MitrickX d600784
Merge pull request #2 from OtusTeam/main
MitrickX 582decd
Merge branch 'OtusTeam:main' into main
MitrickX cae04c4
Merge branch 'OtusTeam:main' into main
MitrickX 5420696
Homework4: first touches - menu actions 1-3, 6
MitrickX f8ea5a0
Homework4: user interaction for choosing menu item
MitrickX 2b8111c
Homework4: display-total-sales-for-customer
MitrickX 121632d
Homework4: display-total-count-for-product
MitrickX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| (ns otus-06.homework) | ||
| (ns otus-06.homework | ||
| (:require [clojure.java.io :as io] | ||
| [clojure.string :as str]) | ||
| (:gen-class)) | ||
|
|
||
| ;; Загрузить данные из трех файлов на диске. | ||
| ;; Эти данные сформируют вашу базу данных о продажах. | ||
|
|
@@ -94,3 +97,168 @@ | |
|
|
||
|
|
||
| ;; Файлы находятся в папке otus-06/resources/homework | ||
|
|
||
| ;; *** 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 | ||
|
|
||
| (defn display-rows | ||
| [rows] | ||
| (doseq [{id :id data :data} rows] | ||
| (printf "%s: %s\n" id data))) | ||
|
|
||
| (defn load-table-data | ||
| [table-name] | ||
| (let [raw-rows (->> (str "homework/" table-name ".txt") | ||
| io/resource | ||
| slurp | ||
| str/split-lines | ||
| (map #(str/split % #"\|"))) | ||
| rows (mapv | ||
| (fn [[id & data]] {:id id :data (vec data)}) | ||
| raw-rows) | ||
| index (zipmap | ||
| (map :id rows) | ||
| (range))] | ||
| {:rows rows :index index})) | ||
|
|
||
| (def read-cust-table (partial load-table-data 'cust)) | ||
| (def read-prod-table (partial load-table-data 'prod)) | ||
| (def read-sales-table (partial load-table-data 'sales)) | ||
|
|
||
| (defn enrich-sales-rows | ||
| [sales-rows customers products with-product-cost?] | ||
| (let [{customers-rows :rows customers-index :index} customers | ||
| {products-rows :rows products-index :index} products] | ||
| (mapv | ||
| (fn [{id :id [cid pid & other] :data}] | ||
| (let [c-idx (get customers-index cid) | ||
| p-idx (get products-index pid) | ||
| c-row (get customers-rows c-idx) | ||
| p-row (get products-rows p-idx) | ||
| c-name (get-in c-row [:data 0]) | ||
| p-name (get-in p-row [:data 0]) | ||
| p-cost (get-in p-row [:data 1]) | ||
| data (if with-product-cost? | ||
| (into [c-name p-name p-cost] other) | ||
| (into [c-name p-name] other))] | ||
| {:id id :data data})) sales-rows))) | ||
|
|
||
| (defn load-enriched-sales-table | ||
| [with-product-cost?] | ||
| (let [customers (read-cust-table) | ||
| products (read-prod-table) | ||
| sales (read-sales-table) | ||
| sales-rows (enrich-sales-rows (:rows sales) customers products with-product-cost?)] | ||
| (assoc sales :rows sales-rows))) | ||
|
|
||
| (def read-enriched-sales-table (partial load-enriched-sales-table false)) | ||
| (def read-enriched-sales-table-with-product-cost (partial load-enriched-sales-table true)) | ||
|
|
||
| (defn calc-aggregation | ||
| [reader filter map reduce] | ||
| (let [{rows :rows} (reader) | ||
| aggregation (->> rows filter map reduce)] | ||
| aggregation)) | ||
|
|
||
| (defn calc-total-sales-for-customer | ||
| [customer-name] | ||
| (calc-aggregation | ||
| read-enriched-sales-table-with-product-cost | ||
| (partial filter #(= customer-name (get-in % [:data 0]))) | ||
| (partial map (fn [{[_ _ cost qnt] :data}] (* (parse-double cost) (parse-long qnt)))) | ||
| (partial reduce +))) | ||
|
|
||
| (defn calc-total-count-for-product | ||
| [target-product-name] | ||
| (calc-aggregation | ||
| read-enriched-sales-table | ||
| (partial filter #(= target-product-name (get-in % [:data 1]))) | ||
| (partial map #(parse-long (get-in % [:data 2]))) | ||
| (partial reduce +))) | ||
|
|
||
| (defn display-table | ||
| [reader] | ||
| (let [{rows :rows} (reader)] | ||
| (display-rows rows))) | ||
|
|
||
| (defn display-customer-table [] | ||
| (display-table read-cust-table)) | ||
|
|
||
| (defn display-product-table [] | ||
| (display-table read-prod-table)) | ||
|
|
||
| (defn display-sales-table [] | ||
| (display-table read-enriched-sales-table)) | ||
|
|
||
| (defn display-total-sales-for-customer | ||
| [] | ||
| (println "Enter customer name: ") | ||
| (let [customer-name (read-line)] | ||
| (->> customer-name | ||
| calc-total-sales-for-customer | ||
| (format "%s: $%.2f" customer-name) | ||
| println))) | ||
| (defn display-total-count-for-product | ||
| [] | ||
| (println "Enter product name: ") | ||
| (let [product-name (read-line)] | ||
| (->> product-name | ||
| calc-total-count-for-product | ||
| (format "%s: %d" product-name) | ||
| println))) | ||
|
|
||
| (defn goodbye [] | ||
| (println "Goodbye") | ||
| (System/exit 0)) | ||
|
|
||
| (def menu | ||
| [{:name "Display Customer Table" :action display-customer-table} | ||
| {:name "Display Product Table" :action display-product-table} | ||
| {:name "Display Sales Table" :action display-sales-table} | ||
| {:name "Total Sales for Customer" :action display-total-sales-for-customer} | ||
| {:name "Total Count for Product" :action display-total-count-for-product} | ||
| {:name "Exit" :action goodbye}]) | ||
|
|
||
| (defn print-menu | ||
| [menu] | ||
| (println "*** Sales Menu ***") | ||
| (println "==================") | ||
| (doseq [i (range 0 (count menu))] | ||
| (printf "%s. %s\n" (inc i) (get-in menu [i :name])))) | ||
|
Comment on lines
+232
to
+233
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. есть еще одна функция dotimes |
||
|
|
||
| (defn execute-action | ||
| [menu menu-item-id] | ||
| (let [menu-item (get menu (dec menu-item-id)) | ||
| {action :action} menu-item] | ||
| (action))) | ||
|
|
||
| (defn choose-menu-item-prompt | ||
| [menu] | ||
| (print-menu menu) | ||
| (println) | ||
| (println "Enter an option?") | ||
| (let [input (read-line) | ||
| parse-result (try | ||
| (let [menu-item-id (Integer/parseInt input)] | ||
| [menu-item-id (<= 1 menu-item-id (inc (count menu)))]) | ||
| (catch Exception _ [input false]))] | ||
| (when (false? (second parse-result)) | ||
| (println "Wrong menu option:" input "\n")) | ||
| parse-result)) | ||
|
|
||
| (defn execute-interactive-step | ||
| [menu] | ||
| (let [[id] (->> (repeatedly (partial choose-menu-item-prompt menu)) | ||
| (filter #(true? (second %))) | ||
| (first))] | ||
| (execute-action menu id) | ||
| (println))) | ||
|
|
||
| (defn -main [] | ||
| (while true (execute-interactive-step menu))) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍