Commits

Christoph Schindler committed 4e8da1c

Supermarket successfully implemented\!

Comments (0)

Files changed (2)

Supermarket/supermarket.py

+PRICELIST = {"Brot":160, "Nudeln":89, "Fertigsuppe":259,
+    "Apfel": 89, "Birne": 119}
+
+def calc_one(item):
+    count, product = item
+    return PRICELIST[product] * count
+
+def invoice_line(item):
+    count, product = item
+    return "%dx %s %0.2f" % (count, product, calc_one(item)/100.0)
+
+def calc(items):
+    return sum(calc_one(item) for item in items)
+
+def invoice(items):
+    bill = "\n".join(map(invoice_line, items))
+    bill += "\n%s\nTotal %.2f" % (20*"-", calc(items)/100.0)
+    return bill

Supermarket/test_supermarket.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+test_supermarket.py
+
+Created by Coding Dojo on 2012-06-24.
+Copyright (c) 2012 PyUGAT. All rights reversed.
+"""
+
+from nose.tools import assert_equal
+
+import supermarket
+
+def test_one_bread():
+    the_sum = supermarket.calc([(1, "Brot")])
+    assert_equal(the_sum, 160)
+    
+def test_one_noodles():
+    the_sum = supermarket.calc([(1, "Nudeln")])
+    assert_equal(the_sum, 89)
+    
+def test_one_instantsoup():
+    the_sum = supermarket.calc([(1, "Fertigsuppe")])
+    assert_equal(the_sum, 259)
+
+def test_one_apple():
+    the_sum = supermarket.calc([(1, "Apfel")])
+    assert_equal(the_sum, 89)
+    
+def test_one_pear():
+    the_sum = supermarket.calc([(1, "Birne")])
+    assert_equal(the_sum, 119)
+       
+def test_two_bread():
+    the_sum = supermarket.calc([(2, "Brot")])
+    assert_equal(the_sum, 160*2)
+    
+def test_four_noodles():
+    the_sum = supermarket.calc([(4, "Nudeln")])
+    assert_equal(the_sum, 89*4)
+    
+def test_three_instantsoup():
+    the_sum = supermarket.calc([(3, "Fertigsuppe")])
+    assert_equal(the_sum, 259*3)
+    
+def test_sum_list():
+    the_sum = supermarket.calc([(3, "Fertigsuppe"),(4, "Brot")])
+    assert_equal(the_sum, 3*259 + 4*160)
+
+def test_sum_list2():
+    the_sum = supermarket.calc([(2, "Brot"),(4, "Nudeln"),(3, "Fertigsuppe")])
+    assert_equal(the_sum, 1453)
+    
+def test_sum_list_printable():
+    invoice = supermarket.invoice([(2, "Brot"),(4, "Nudeln"),(3, "Fertigsuppe")])
+    assert_equal(invoice, "2x Brot 3.20\n4x Nudeln 3.56\n3x Fertigsuppe 7.77\n"+20*"-"+"\nTotal 14.53")
+    
+    
+    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.