Wednesday 29 July 2015

Electronic Point of Sale


Since I was getting bored with strings and the basics. I decided to take a fun little project. The challenge is to write a program that works like an electronic point of sale. In simpler terms a cash register.

The code gets a little messy because I was getting tired and frustrated and I'm not a programmer of any level of skill. Although I have Python 3.4 installed on my PC. Python 2.7 is used by default. Which it turns out is terrible at handling unicode characters. All I wanted it to print was the £ sign in a raw_input() prompt. The answer was to explicitly use Python 3.x.

With the unicode problem sorted and the program re-factored for Python 3.x I decided to call it a day with what I have for now. Back to basics again I think.


  • The program assumes a terminal with of 80 columns. Which used to be the norm.
  • The database of goods is a simple dictionary. Which actually works quite well for this application.
  • To select an item from the inventory simply type it in.
  • Only one item can be selected at a time. But you can allow the program to loop several times to build up a list of items.
  • An item can appear more than once in your list.
  • When you select "P" to pay. The program will ask how much you are paying and then calculate your change. If you don't give enough, you'll be told to come back when you can pay.
  • The program will pause for a few seconds to allow you to read the receipt. And then it moves on to the next customer.
  • Press "Q" to quit.


#!/usr/bin/env python3

# Project       : Electronic Point of Sale
# Author        : Kevin Lynch
# Created      : 29.07.2015

# Brief: Create a program that will hold a database of goods matched against
#        their prices which can then be used to tally up the cost of a shoppers
#        order.

# Function definitions.
def wait(t):
        for i in range(0,t):
                i = i + 0
                
def orderScreen(s): # Show goods for sale with prices.
        welcome = " Welcome to Python General Stores "
        instruct = "[P = Pay] [Q = Quit] | Please select an item."
        print("{:*^80}".format(welcome))
        
        for key in s.keys():
                print(u"{: >39} \xA3{: <39.2f}".format(key,s[key]))
                
        print("{:*^80}".format(""))
        print("{: ^80}".format(instruct))
        print("{:*^80}".format(""))

def acceptPayment(o,s):
        import time
        header = " Sale "
        prompt = u"{: >40}\xA3".format("Amount Given: ")
        total = 0.00
        given = 0.00
        change = 0.00
        
        for i in range(0,len(o)): # Calculate total due.
                for key in s.keys():
                        if o[i] == key:
                                total = total + s[key]
        
        print("{:*^80}".format(header)) # Print order and amount due.
        
        for i in range(0,len(o)):
                print(u"{: >39} \xA3{: <39.2f}".format(o[i],s[o[i]]))
                
        print(u"{: >40}\xA3{: <39.2f}".format("Amount Due: ",total))
        given = float(input(prompt)) # Amount given.
        if given >= total: # Calculate change due.
                change = given - total
                print(u"{: >40}\xA3{: <39.2f}".format("Change Due: ",change))
        else:
                print("{: ^80}".format("Please come back when you can pay."))
        time.sleep(3)

def takeOrder(s): # Get the customers order.
        kbd = ""
        order = []
        while str.upper(kbd) != "P": # Loop until customer decides to pay.
                if str.upper(kbd) == "Q": # Break loop to quit.
                        break 
                orderScreen(stock)
                kbd = input("::")

                for key in s.keys():
                        if key == str.capitalize(kbd):
                                order.append(str.capitalize(kbd))
                        
        if str.upper(kbd) == "P":
                acceptPayment(order,stock) # Call acceptPayment
                
        return kbd # Return to main program.

# Global variables.
stock = {"Apple":0.50,"Banana":0.50,"Milk":1.00,"Bread":1.50,"Bacon":5.60}

# Main Program
kbd = ""
while str.upper(kbd) != "Q":
        kbd = takeOrder(stock)

No comments:

Post a Comment