Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of https://github.com/johanvanzijl/HollingBerries

… into johan
  • Loading branch information...
commit 1312b1a6737353592d4208790895c5aea350f7f1 2 parents 1bb786e + 26e9a7c
@apauley authored
View
10 clojure/johanvanzijl/holling/.gitignore
@@ -0,0 +1,10 @@
+/target
+/lib
+/classes
+/checkouts
+pom.xml
+*.jar
+*.class
+.lein-deps-sum
+.lein-failures
+.lein-plugins
View
28 clojure/johanvanzijl/holling/README.md
@@ -0,0 +1,28 @@
+# HollingBerries Solution in Clojure
+
+A couple of comments:
+* I am very new to clojure so any comments on doing things better would be welcomed.
+* I re-used(read copied...) quite a few things from mschristiansen's implementation.
+* The approach I used here is similar to the my example in ABAP where I placed the Supplier and Product rules in records. I believe this will give a bit more flexibility should the business rules(e.g. vary markups, expiry dates, etc by either product or supplier).
+
+## Usage
+
+You'll need [Leiningen](https://github.com/technomancy/leiningen), a
+build tool for Clojure.
+
+Leiningen will fetch project dependencies described in the project.clj
+file.
+> lein deps
+
+Allows you to run the program, which will generate the pricefile.txt.
+> lein run
+
+Or start a repl (is it called a prompt in Python?)
+> lein repl
+
+To run the test:
+> lein test
+
+## License
+
+Distributed under the Eclipse Public License, the same as Clojure.
View
11 clojure/johanvanzijl/holling/project.clj
@@ -0,0 +1,11 @@
+(defproject holling "0.1.0-SNAPSHOT"
+ :description "An Example Holling Berries Implementation"
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.4.0"]
+ [org.clojure/math.numeric-tower "0.0.1"]
+ [org.clojure/data.csv "0.1.2"]
+ [clj-time "0.4.2"]]
+ :main holling.core
+ )
View
79 clojure/johanvanzijl/holling/src/holling/core.clj
@@ -0,0 +1,79 @@
+(ns holling.core
+ (:use [clojure.java.io :only [reader]]
+ [clojure.data.csv :only [read-csv]]
+ [clj-time.core :only [plus days]]
+ [clojure.math.numeric-tower :only [ceil]]
+ [clj-time.format :only [formatter parse unparse]]
+ ))
+
+(defrecord Suppinfo [ suppcode sellby markup-perc markup-curr roundup ])
+(def suppliers [ (Suppinfo. "32" -3 0 -2 false) ;Susan Windler (Supplier ID 32)
+ (Suppinfo. "101" -3 0 -2 false) ;Togetherness Tshabalala (Supplier ID 101)
+ (Suppinfo. "219" 0 0.1 0 true ) ;Promise Mashangu (Supplier ID 219)
+ (Suppinfo. "204" 0 0.1 0 true ) ;Karel Visser (Supplier ID 204)
+ ])
+
+(defrecord Prodtype [codefrom codeto sellby markup])
+(def prodtypes [ (Prodtype. 1000 1099 7 0.5) ;Other Fruit 1
+ (Prodtype. 1100 1199 14 0.4) ;Apples
+ (Prodtype. 1200 1299 5 0.35) ;Bananas
+ (Prodtype. 1300 1399 7 0.55) ;Berries
+ (Prodtype. 1400 1999 7 0.5) ;Other Fruit 2
+ ])
+
+(defrecord ProductRec [suppcode prodcode desc deldate cost qty] )
+
+;;; Define file locations and parse the file
+(def out-file "pricefile.txt")
+
+(defn parse-product [item]
+ "Parses each line of the input file to a ProductRec"
+ (ProductRec. (get item 0) ;supplier code
+ (Integer/parseInt (get item 1)) ;parse the prodcode to int
+ (get item 2) ;description
+ (parse (formatter "yyyy/MM/dd") (get item 3)) ;parse delivery date
+ (/ (Integer/parseInt (get item 4)) 100) ;parse cost
+ (Integer/parseInt (get item 5)))) ;parse qty
+
+(def products
+ "List of ProductRec. Reads input file"
+ (for [ item (drop 1 ;Leave headers out.
+ (with-open [file (reader "produce.csv")]
+ (doall (read-csv file)))) ]
+ (parse-product item)))
+
+(defn between? [n min max]
+ (and (>= n min) (<= n max)))
+
+(defn get-prodtype [prodcode prodtypes]
+ (first (for [prodtype prodtypes :when (let [{codefrom :codefrom codeto :codeto} prodtype]
+ (between? prodcode codefrom codeto))] prodtype)))
+
+(defn get-suppinfo [suppcode suppliers]
+ (first (filter (comp(partial = suppcode) :suppcode) suppliers)))
+
+(defn calc-product [product]
+ (let [suppinfo (get-suppinfo (:suppcode product) suppliers)
+ prodtype (get-prodtype (:prodcode product) prodtypes) ]
+ ( -> product
+ (assoc :price (if (nil? suppinfo)
+ (* (:cost product) (+ (:markup prodtype) 1))
+ (let [rawprice (+ (* (:cost product) (+ (:markup prodtype) (:markup-perc suppinfo) 1))
+ (:markup-curr suppinfo))]
+ (if (> rawprice 0)
+ (if (:roundup suppinfo) (ceil rawprice) rawprice)
+ 0 ))))
+ (assoc :expdate (plus (:deldate product) (days (:sellby prodtype))
+ (when suppinfo (days (:sellby suppinfo))))))))
+ ;(calc-product (nth products 2))
+(defn label-product [product]
+ "Generate the label for a single product"
+ (apply str ( repeat (:qty product) (str
+ "R" (format "%8.2f" (double (:price product)))
+ (unparse (formatter "yyyy/MM/dd") (:expdate product))
+ (subs (:desc product) 0 31)
+ "\n"))))
+
+(defn -main []
+ (spit "pricefile2.txt" (doall
+ (reduce str (for [product products] (label-product (calc-product product)))))))
View
10 clojure/johanvanzijl/holling/test/holling/core_test.clj
@@ -0,0 +1,10 @@
+(ns holling.core-test
+ (:use clojure.test
+ holling.core))
+
+(def test-file "pricefile.txt")
+(def new-file "pricefile2.txt")
+
+(deftest test-diff-file
+ (testing "Is the new-file equal to the test-file"
+ (is (= (slurp new-file) (slurp test-file)))))
Please sign in to comment.
Something went wrong with that request. Please try again.