Advertisement
niske

phoneProject

Mar 24th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. import os
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from tabulate import tabulate
  5. from collections import Counter
  6.  
  7. def tabelarno(phoneList):
  8. mydata = phoneList
  9. head = ["Ime", "Proizvođač", "Procesor", "Megapikseli kamere", "Boja", "Cena"]
  10. print(tabulate(mydata, headers=head, tablefmt="grid"))
  11.  
  12.  
  13. def plot_phone_prices(phoneList):
  14. # izdvajanje cena
  15. cene = [float(phone[5]) for phone in phoneList]
  16.  
  17. # pravljenje bar charta
  18. plt.bar(range(len(phoneList)), cene, tick_label=[phone[0] for phone in phoneList])
  19. plt.xlabel('Modeli telefona')
  20. plt.ylabel('Cena (EUR)')
  21. plt.title('Cene telefona')
  22. plt.xticks(rotation=45) # zbog citljivosti
  23. plt.show()
  24.  
  25.  
  26. def plot_zastupljenost_brendova(phoneList):
  27. brendovi = [phone[1] for phone in phoneList]
  28. brojac_brendova = Counter(brendovi)
  29.  
  30. plt.pie(brojac_brendova.values(), labels=brojac_brendova.keys(), autopct='%1.1f%%')
  31. plt.title('Zastupljenost brendova')
  32. plt.show()
  33.  
  34. def register():
  35.  
  36. status = True
  37.  
  38. while status:
  39. status = False
  40. username = input("Unesite korisnicko ime: ")
  41. with open("users.txt", "r") as file:
  42. for line in file:
  43. s = line.strip().split(":")
  44. if username == s[0]:
  45. status = True
  46. break
  47. if status:
  48. print("Korisnicko ime vec postoji, pokusajte ponovo! ")
  49. else:
  50. password = input("Unesite lozinku: ")
  51.  
  52. with open("users.txt", "a") as file:
  53. file.write(f"{username}:{password}\n")
  54. print("Registracija uspesna!")
  55.  
  56.  
  57. def login():
  58. username = input("Unesite korisničko ime: ")
  59. password = input("Unesite lozinku: ")
  60. tacnaSifra = False
  61. while not tacnaSifra:
  62. with open("users.txt", "r") as file:
  63. for line in file:
  64. stored_username, stored_password = line.strip().split(":")
  65. if username == stored_username and password == stored_password:
  66. tacnaSifra = True
  67. if not tacnaSifra:
  68. print("Netačno korisničko ime ili lozinka.")
  69. username = input("Unesite korisničko ime: ")
  70. password = input("Unesite lozinku: ")
  71. print("Uspešno ste se ulogovali")
  72.  
  73. def admin_login():
  74. adminlogin = input("Unesite administratorsko ime: ")
  75. adminpass = input("Unesite administratorsku lozinku: ")
  76.  
  77. tacnaSifra = False
  78. while not tacnaSifra:
  79. with open("admins.txt", "r") as file:
  80. for line in file:
  81. stored_username, stored_password = line.strip().split(":")
  82. if adminlogin == stored_username and adminpass == stored_password:
  83. tacnaSifra = True
  84. if not tacnaSifra:
  85. print("Netačno korisničko ime ili lozinka.")
  86. adminlogin = input("Unesite korisničko ime: ")
  87. adminlogin = input("Unesite lozinku: ")
  88. print("Imate admin pristup.")
  89.  
  90. def obrisiNalog(username):
  91. # administratorska funkcija
  92. try:
  93. # pravimo listu korisnika, laksa manipulacija
  94. userList = []
  95. infile = open("users.txt", "r")
  96. # citam red po red
  97. line = infile.readline()
  98. while line:
  99. userList.append(line.rstrip("\n").split(":"))
  100. line = infile.readline()
  101. infile.close()
  102.  
  103. except FileNotFoundError:
  104. print("Nema fajla sa korisnicima, pravim novi")
  105. userList = []
  106.  
  107.  
  108. os.system('cls')
  109. status = False
  110. for t in userList:
  111. if username == t[0]:
  112. while not status:
  113. userList.remove(t)
  114. print("Uspesno brisanje izabranog naloga")
  115. fajl = open("users.txt", "w")
  116. for t in userList:
  117. fajl.write(t[0] + ":" + t[1] + "\n")
  118. fajl.close()
  119. exit()
  120. else:
  121. print("Takav nalog ne postoji. ")
  122.  
  123.  
  124. #def obrisiTelefon():
  125. # administratorska funkcija
  126.  
  127.  
  128. def save_phone_list(phoneList):
  129. try:
  130. with open("phoneList.txt", "w") as outfile:
  131. for phone in phoneList:
  132. # Join the phone data into a comma-separated string
  133. phone_line = ",".join(phone)
  134. outfile.write(phone_line + "\n")
  135. outfile.close()
  136. print("Lista telefona uspešno sačuvana.")
  137. except IOError:
  138. print("Greška pri čuvanju liste.")
  139.  
  140.  
  141. def main():
  142.  
  143. print("Dobrodošli!")
  144.  
  145. choice = 0
  146. while choice != "1" and choice != "2":
  147. choice = input("Unesite '1' za registraciju, '2' za prijavu: ")
  148. if choice != "1" and choice != "2":
  149. print("Nepravilan izbor " + choice)
  150. if choice == "1":
  151. register()
  152. elif choice == "2":
  153. login()
  154. elif choice == "3":
  155. admin_login()
  156.  
  157. try:
  158.  
  159. # lista telefona
  160. phoneList = []
  161. infile = open("phoneList.txt", "r")
  162. # citamo red po red
  163. line = infile.readline()
  164. while line: # is true
  165. phoneList.append(line.rstrip("\n").split(","))
  166. line = infile.readline()
  167. infile.close()
  168. # ovo cita i apenduje sve dok ne ponestane za citanje
  169.  
  170. except FileNotFoundError:
  171. print("Fajl telefona nije pronadjen. ")
  172. print("Pravim novu listu! ")
  173. phoneList = []
  174.  
  175. choice = 0
  176. while choice != "8":
  177. print("Menadzer telfona")
  178. print("1) Dodaj telefon")
  179. print("2) Pretrazi telefon")
  180. print("3) Prikazi telefone")
  181. print("4) Prikazi graf cena")
  182. print("5) Tabelarni prikaz")
  183. print("6) Prikaz zastupljenosti brendova")
  184. print("7) Izlađi i sačuvaj")
  185. choice = int(input())
  186.  
  187. if choice == 1:
  188. print("Dodajem telefon...")
  189. nPhone = input("Unesi ime telefona: ")
  190. nManufacturer = input("Unesi proizvodjaca telefona: ")
  191. nCPU = input("Unesi procesor: ")
  192. nCamera = input("Unesi MP kamere: ")
  193. nColor = input("Unesi boju uredjaja: ")
  194. nPrice = input("Unesi cenu uredjaja: ")
  195.  
  196. phoneList.append([nPhone, nManufacturer, nCPU, nCamera, nColor, nPrice])
  197.  
  198. elif choice == 2:
  199. print("Pretrazujem telefon...")
  200. keyword = input("Unesi kljucnu rec: ")
  201. for phone in phoneList:
  202. if keyword in phone:
  203. print(phone)
  204.  
  205. elif choice == 3:
  206. print("Stampam sve unete telefone...")
  207. for i in range(len(phoneList)):
  208. print(phoneList[i])
  209.  
  210. elif choice == 4:
  211. print("Graficki prikaz cena: ")
  212. plot_phone_prices(phoneList)
  213.  
  214. elif choice == 5:
  215. tabelarno(phoneList)
  216.  
  217. elif choice == 6:
  218. plot_zastupljenost_brendova(phoneList)
  219.  
  220. elif choice == 7:
  221. print("Izlazim iz programa")
  222. save_phone_list(phoneList)
  223. exit()
  224.  
  225. print("Program prekinut. ")
  226.  
  227.  
  228. if __name__ == "__main__":
  229. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement