实验七

 1 class Account:
 2     def  __init__(self,name,account_number,initial_amount):
 3 
 4 
 5 
 6         self._name = name
 7         self._card_no = account_number
 8         self._balance = initial_amount
 9 
10 
11     def deposit(self,amount):
12         self._balance += amount
13 
14     def withdraw(self,amount):
15         self._balance -= amount
16 
17 
18     def info(self):
19         print('账户信息:  持卡人姓名,账户,当前余额')
20         print(f'{self._name},{self._card_no},{self._balance}')
21 
22 
23     def get_balance(self):
24         return self._balance
25 
26 
27 
28 a1 = Account('Bob','500231',2000)
29 a2 = Account('Joe','2006692',2000)
30 
31 
32 a1.deposit(5000)
33 a1.withdraw(4000)
34 
35 
36 a2.withdraw(10000)
37 a2.withdraw(5000)
38 
39 
40 a1.info()
41 a2.info()

 

 

 

 1 '''
 2 
 3 银行账户
 4 数据:持卡人姓名,账号,当前余额
 5 操作:提款,存款,打印账户信息
 6 
 7 
 8 '''
 9 
10 
11 class Account:
12     def __init__(self,name,account_number,initial_amount):
13         self._name = name
14         self._card_no = account_number
15         self._balance = initial_amount
16 
17     def deposit(self,amount):
18         self._balance += amount
19 
20     def withdraw(self,amount):
21         self._balance -= amount
22 
23     def info(self):
24         print('账户信息: 持卡人姓名,账号,当前余额')
25         print(f'{self._name},{self._card_no},{self._balance}')
26 
27 
28     def get_balance(self):
29         return self._balance
30 
31 
32 def main():
33     a1 = Account('Bob','545684',20000)
34     a2 = Account('Joe','87987987',20000)
35     a1.deposit(5000)
36     a1.withdraw(4000)
37     a2.withdraw(10000)
38     a2.withdraw(5000)
39     a1.info()
40     a2.info()
41 
42 
43 
44 if __name__ == '__main__':
45     print('模块信息: ',__doc__)
46     print('Account类信息:',Account.__doc__)
47     main()
48     

 

 

 1 class User():
 2     name='guest'
 3     password='111111'
 4     def __init__(self,name,password):
 5         self.name=name
 6         self.password=password
 7         self.status=1
 8     def info(self):
 9         if self.status==1:
10             print(self.name,self.password,'状态正常')
11         else:
12             print(self.name,self.password,'封禁')
13     def modify_password(self):
14         n=0
15         while n<3:
16             old=input('旧密码')
17             if self.password!=old:
18                 print('密码错误')
19                 n=n+1
20             else:
21                 new=input('新密码')
22                 self.password=new
23                 print(self.name,'密码修改成功')
24                 break
25         else:
26             self.status=0
27             print('账户已锁,请稍后再试')
28 x1=User('张三','123465')
29 x1.info()
30 x1.modify_password()

 

 

 1 class User():
 2     count = 0
 3     def __init__(self, username='username', password='111', status='1'):
 4         self.username = username
 5         self.password = password
 6         self.status = status
 7         User.count += 1
 8 
 9     def __del__(self):
10         User.count -= 1
11 
12     def info(self):
13         if self.status == '1':
14             print(f"{self.username}, {self.password}, 账户正常")
15         else:
16             print(f"{self.username}, {self.password}, 账户被封禁")
17     def modify_password(self):
18         for i in range(3):
19             old_password = input("旧密码: ")
20             if old_password == self.password:
21                 self.password = input("新密码: ")
22                 print("密码修改成功!")
23                 break
24             elif i == 2:
25                 print("账户已锁, 请稍后再试...")
26                 self.status = '0'
27             elif old_password != self.password:
28                 continue
29 
30     def show_count():
31         print(f"用户总数: {User.count}")
32 
33 
34 class Admin(User):
35     def __init__(self, username='Admin', password='999999', status='1'):
36         super().__init__(username, password, status)
37 
38     def info(self):
39         print("管理员账户: ")
40         super().info()
41     def reset_password(self, user):
42         user.password = input("请输入你要修改的密码: ")
43 
44     def ban_user(self, user):
45         user.status = '0'
46 
47     def unlock_user(self, user_name):
48         user_name.status = '1'
49 
50 
51 u1=User('User1','0000','1')
52 u1.info()
53 u1.modify_password()
54 u1.info()
55 
56 admin1=Admin('Admin1','9999','1')
57 admin1.info()
58 User.show_count()
59 
60 admin1.reset_password(u1)
61 admin1.ban_user(u1)
62 u1.info()
63 admin1.unlock_user(u1)
64 u1.info()
65 User.show_count()

 

posted @ 2022-06-06 23:09  lsqb27  阅读(19)  评论(1编辑  收藏  举报