利用面向过程程序设计的逻辑,解决下列问题:
某研究生从上海某211大学编程相关专业毕业后,在某头部互联网公司找到了一份年收入(税后)
30万元的工作。公司承诺每年薪金增长15%,到达60万每年后达到薪金天花板,每年薪金增长5%。
该学生刚毕业时在嘉定区看中了一套市价600万元的二手房,该二手房房价每年上涨10%。请问,
假设该学生的工资全部用来买房,需要多少年才能攒够首付(房价的30%)?若买得起房的当年该学生并未买房,10年后他还买得起这套房么?
'''
@File : 第五周作业.py
@Author: BC
@Date : 2023-04-19 20:22
'''
class Graduate:
def __init__(self, salary, year_of_graduation):
self.salary = salary
self.year_of_graduation = year_of_graduation
self.down_payment = 0.3 * 6000000
self.savings = 0
def calculate_savings(self):
years = 0
while self.savings < self.down_payment:
years += 1
if self.salary >= 600000:
self.salary *= 1.05
else:
self.salary *= 1.15
self.savings += self.salary
house_price = 6000000 * (1 + 0.1) ** years
self.down_payment = 0.3 * house_price
if self.savings >= self.down_payment:
print(f"恭喜您,攒够了{self.down_payment}元的首付,需要{years + 1}年买房")
break
if self.savings < self.down_payment:
print(f"很遗憾,攒不够{self.down_payment}元的首付,需要{years}年")
def can_afford_house(self, years):
house_price = 6000000 * (1 + 0.1) ** years
for i in range(years):
if self.salary >= 600000:
self.salary *= 1.05
else:
self.salary *= 1.15
self.savings += self.salary
if self.savings >= house_price * 0.3:
print(f"在{years}年后,您依然可以购买这套房子")
else:
print(f"很遗憾,在{years}年后,您无法购买这套房子")
graduate = Graduate(300000, 2023)
graduate.calculate_savings()
graduate.can_afford_house(10)