Advertisement
MateuszGrabarczyk

Basic classes

Mar 21st, 2022 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. class User:
  2.     def __init__(self, name, lastname, login, password):
  3.         self.name = name
  4.         self.lastname = lastname
  5.         self.login = login
  6.         self.password = password
  7.  
  8.     def welcome(self):
  9.         print("Welcome {}{}".format(self.name, self.lastname))
  10.  
  11. class Admin(User):
  12.     def __init__(self,name="Mateusz", lastname="Grabarczyk", login="admin", password="admin"):
  13.         super().__init__(name, lastname, login, password)
  14.  
  15.     def welcome(self):
  16.         print("Welcome {} {}, our dear Admin!".format(self.name, self.lastname))
  17.  
  18.  
  19. class Student(User):
  20.     def __init__(self, id, name, lastname, login, password):
  21.         super().__init__(name, lastname, login, password)
  22.         self.issued_books = []
  23.         self.id = id
  24.     def welcome(self):
  25.         print("Welcome {} {}, right now you have {} issued books.".format(self.name, self.lastname, len(self.issued_books)))
  26.  
  27.  
  28. class UserDatabase():
  29.     student_list = []
  30.  
  31.     def LoadUsers(self):
  32.         with open("users.txt") as f:
  33.             for line in f:
  34.                 data = line.strip().split(',')
  35.                 try:
  36.                     student = Student(data[0], data[1], data[2], data[3], data[4])
  37.                     if len(data) > 5:
  38.                         for i in range(5, len(data)):
  39.                             student.issued_books.append(data[i])
  40.                     self.student_list.append(student)
  41.                 except:
  42.                     pass
  43.  
  44.     def overwriteUsersTxt(self):
  45.         with open("users.txt", "w") as f:
  46.             for s in self.student_list:
  47.                 f.write("{},{},{},{},{}".format(s.id, s.name, s.lastname, s.login, s.password))
  48.                 f.write("\n")
  49.  
  50.     def print_users(self):
  51.         for user in self.student_list:
  52.             print("{} - {} {}".format(user.id, user.name, user.lastname))
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement