Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- userFile = "prodavci.txt"
- productFile = "proizvodi.txt"
- delimiter = "|"
- sentinel = "quit"
- def loadUsers():
- fUsers = open(userFile, "r")
- for user in fUsers.readlines():
- user = user[:-1]
- user = user.split(delimiter)
- users.append(user)
- fUsers.close()
- def logIn(username, password):
- for user in users:
- if username == user[0] and password == user[1]:
- return True
- return False
- def inputLogIn():
- username = input("Unesite korsnicko ime: ")
- password = input("Unesite lozinku: ")
- return logIn(username, password)
- users = []
- loadUsers()
- def addProduct(name, price, amount):
- products.append([name, price, amount])
- def loadProducts():
- try:
- fProducts = open(productFile, "r")
- for product in fProducts.readlines():
- product = product[:-1]
- product = product.split(delimiter)
- addProduct(product[0], float(product[1]), int(product[2]))
- fProducts.close()
- except IOError:
- pass
- def saveProduct(name, price, amount):
- fProducts = open(productFile, "a")
- productLine = name + delimiter + str(price) + delimiter + str(amount)+ "\n"
- fProducts.write(productLine)
- fProducts.close()
- def inputProduct():
- while True:
- name = input("Unestie naziv: ")
- if name == sentinel:
- return False
- elif name == "":
- print("Neispravan unos! Naziv ne sme biti prazan.")
- continue
- break
- while True:
- price = input("Unesite cenu: ")
- if price == sentinel:
- return False
- try:
- price = float(price)
- except ValueError:
- print("Neispravan unos! Cena mora biti broj.")
- continue
- if price <= 0:
- print("Neispravan unos! Cena mora biti pozitivan broj.")
- continue
- break
- while True:
- amount = input("Unesite kolicinu: ")
- if amount == sentinel:
- return False
- try:
- amount = int(amount)
- except ValueError:
- print("Neispravan unos! Kolicina mora biti ceo broj.")
- continue
- if amount < 0:
- print("Neispravan unos! Kolicina ne sme biti negativan broj.")
- continue
- break
- addProduct(name, price, amount)
- saveProduct(name, price, amount)
- return True
- def listProducts():
- print("\nProizvodi:")
- print("----------------------------")
- print("{0:<10}{1:<10}{2:<10}".format("Naziv", "Cena", "Kolicina"))
- print("----------------------------")
- for product in products:
- print("{0:<10}{1:<10.2f}{2:<10}".format(product[0], product[1], product[2]))
- products = []
- loadProducts()
- def main():
- print("Prodavnica")
- print("----------")
- print("\nPrijava na sistem...")
- while not inputLogIn():
- print("\nNeuspesna prijava!")
- while True:
- listProducts()
- print("\nUnos novog proizovda...")
- if not inputProduct():
- print("\nPrekid programa!")
- break
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement