Advertisement
Guest User

Prodavnica

a guest
Jan 11th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. userFile = "prodavci.txt"
  2. productFile = "proizvodi.txt"
  3. delimiter = "|"
  4. sentinel = "quit"
  5.  
  6. def loadUsers():
  7.     fUsers = open(userFile, "r")
  8.  
  9.     for user in fUsers.readlines():
  10.         user = user[:-1]
  11.         user = user.split(delimiter)
  12.         users.append(user)
  13.  
  14.     fUsers.close()
  15.  
  16. def logIn(username, password):
  17.     for user in users:
  18.         if username == user[0] and password == user[1]:
  19.             return True
  20.  
  21.     return False
  22.  
  23. def inputLogIn():
  24.     username = input("Unesite korsnicko ime: ")
  25.     password = input("Unesite lozinku: ")
  26.  
  27.     return logIn(username, password)
  28.  
  29. users = []
  30. loadUsers()
  31.  
  32. def addProduct(name, price, amount):
  33.     products.append([name, price, amount])
  34.  
  35. def loadProducts():
  36.     try:
  37.         fProducts = open(productFile, "r")
  38.  
  39.         for product in fProducts.readlines():
  40.             product = product[:-1]
  41.             product = product.split(delimiter)
  42.             addProduct(product[0], float(product[1]), int(product[2]))
  43.  
  44.         fProducts.close()
  45.     except IOError:
  46.         pass
  47.    
  48. def saveProduct(name, price, amount):
  49.     fProducts = open(productFile, "a")
  50.  
  51.     productLine = name + delimiter + str(price) + delimiter + str(amount)+ "\n"
  52.     fProducts.write(productLine)
  53.  
  54.     fProducts.close()
  55.  
  56. def inputProduct():
  57.     while True:
  58.         name = input("Unestie naziv: ")
  59.         if name == sentinel:
  60.             return False
  61.         elif name == "":
  62.             print("Neispravan unos! Naziv ne sme biti prazan.")
  63.             continue
  64.         break
  65.  
  66.     while True:
  67.         price = input("Unesite cenu: ")
  68.         if price == sentinel:
  69.             return False
  70.         try:
  71.             price = float(price)
  72.         except ValueError:
  73.             print("Neispravan unos! Cena mora biti broj.")
  74.             continue
  75.         if price <= 0:
  76.             print("Neispravan unos! Cena mora biti pozitivan broj.")
  77.             continue
  78.         break
  79.  
  80.     while True:
  81.         amount = input("Unesite kolicinu: ")
  82.         if amount == sentinel:
  83.             return False
  84.         try:
  85.             amount = int(amount)
  86.         except ValueError:
  87.             print("Neispravan unos! Kolicina mora biti ceo broj.")
  88.             continue
  89.         if amount < 0:
  90.             print("Neispravan unos! Kolicina ne sme biti negativan broj.")
  91.             continue
  92.         break
  93.  
  94.     addProduct(name, price, amount)
  95.     saveProduct(name, price, amount)
  96.  
  97.     return True
  98.  
  99. def listProducts():
  100.     print("\nProizvodi:")
  101.     print("----------------------------")
  102.     print("{0:<10}{1:<10}{2:<10}".format("Naziv", "Cena", "Kolicina"))
  103.  
  104.     print("----------------------------")
  105.     for product in products:
  106.         print("{0:<10}{1:<10.2f}{2:<10}".format(product[0], product[1], product[2]))
  107.  
  108. products = []
  109. loadProducts()
  110.  
  111. def main():
  112.     print("Prodavnica")
  113.     print("----------")
  114.  
  115.     print("\nPrijava na sistem...")
  116.     while not inputLogIn():
  117.         print("\nNeuspesna prijava!")
  118.  
  119.     while True:
  120.         listProducts()
  121.  
  122.         print("\nUnos novog proizovda...")
  123.         if not inputProduct():
  124.             print("\nPrekid programa!")
  125.             break
  126.  
  127. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement