【问题标题】:Design a class named Account and test it设计一个名为 Account 的类并对其进行测试
【发布时间】:2019-09-21 23:25:49
【问题描述】:

好的,所以我在编写此代码时遇到了问题,并且用于测试的代码是,无论我尝试什么,我似乎都无法让它工作。

有问题的作业说:设计一个名为 Account 的类,其中包含:

■ 一个名为 id 的私有 int 数据字段,用于帐户。

■ 一个名为 balance 的私有浮动数据字段。

■ 一个名为 AnnualInterestRate 的私有浮点数据字段,用于存储当前 利率。

■ 创建具有指定 id(默认为 0)的帐户的构造函数,初始 余额(默认 100)和年利率(默认 0)。

■ id、balance 和annualInterestRate 的访问器和修改器方法。

■ 一个名为 getMonthlyInterestRate() 的方法,它返回每月 利率。

■ 一个名为 getMonthlyInterest() 的方法,用于返回每月利息。

■ 一个名为withdraw 的方法,从 帐户。

■ 一种名为 deposit 的方法,将指定金额存入帐户。

(提示:getMonthlyInterest()方法是返回月利息金额,不是 利率。使用此公式计算每月利息:余额 * 月利率。月利率是年利率 / 12. 请注意,annualInterestRate 是一个百分比(如 4.5%)。你需要 除以 100。)

编写一个测试程序,创建一个帐户 id 为 1122 的 Account 对象,一个 余额为 20,000 美元,年利率为 4.5%。使用提款 方法提取$2,500,使用存款方法存入$3,000,打印 id、余额、月利率、月利息。

这是我目前的课程代码:

class Account:

    def __init__(self, accountid = 0, initialbalance = 100, annualInterestrate = 0):
        self.accountid = accountid
        self.initalbalance = initialbalance
        self.annualInterestrate = annualInterestrate

    def balance(self):
        return float(self.balance)

    def id(self):
        return int(self.id)

    def annualInterestrate(self):
        return float(self.annaulInterestrate)

    def getid(self):
        return self.id

    def setbalance(self):
        return self.setbalance 

    def getannualInterestrate(self):
        return self.annualInterestrate

    def getMonthlyInterestRate(self):
        return self.annualInterestrate / 100

    def withdraw(self):
        amount = 28
        if self.balance>=amount: 
            self.balance-=amount 

    def deposit(self): 
        amount =  45
        self.balance += amount 

    def getMonthlyInterest(self):
        return  balance * monthlyInterestRate. monthlyInterestRate is annualInterestRate / 12

忽略数字 45 和 28,我只是将它们作为占位符放在那里。

这是我为测试程序准备的(目前不完整,因为我迷路了):

from Account import Account

def main():
    account1 = Account()
    print("Account id # is", account1.getid)


    print("Beginning Balance: ", account1.setbalance)
    print("Monthly Interest Rate: ", account1.getMonthlyInterestRate())
    print("Monthly Interest: ", account1.getMonthlyInterest())

main()

我不知道我在做什么。如果有人知道我应该做什么,请提供帮助。

我试图弄清楚我是如何将信息输入课程(或测试程序,如果需要的话)并让它输出如下内容:

帐号编号为 1122

初始余额:取款和/或存款后的余额

月利率:无论所有数学结果如何。

每月利息:再一次,不管所有的数学结果是什么

【问题讨论】:

  • 创建帐户时不应该传递一些信息吗? Account(id=1122, initialbalance=20000, annualInterestrate=0.045)?
  • 它通常有助于说明您的预期和实际得到的结果。否则,很难知道“错误”是什么意思。 :-)
  • @JohnSzakmeister 对,抱歉,关于缺少信息,我正试图弄清楚我应该在哪里插入该信息。具体来说,我如何将这些信息带入课堂(或测试程序,如果需要的话)并将其输出如下: 帐户 ID # 是,1122 开始余额:无论余额是什么,然后取款和/或存款 月利率:所有数学结果。 月利率:再次,无论所有数学结果。

标签: python variable-assignment


【解决方案1】:

在我开始谈论类的功能之前,我先谈谈类的设计。

■ 一个名为 id 的私有 int 数据字段,用于帐户。

■ 一个名为 balance 的私有浮动数据字段。

■ 一个名为 AnnualInterestRate 的私有浮点数据字段,用于存储当前利率。

您可能没有注意到,但您实际上是在公开您的数据字段。考虑以下代码:

class Account:
   id = 2

可以从代码中的其他任何地方访问 id,例如(请注意,这不是使用类的实例):

print(Account.id)

继续这个例子,我们可以让一个实例具有不同的 id 值。

class account:
  id = 2
  def __init__(self):
    self.id = 4

print(account.id)
foo = account()
print(foo.id)

这个 id 仍然可以从类的实例外部访问,它仍然是公共的。为了使其私有化,我们在变量前添加了一个 __。现在我们应该在尝试访问实例的 id 时出错,但在函数访问的 id 时不会出错。

class account:
  def __init__(self):
    self.__id = 4

  def getId(self):
    return self.__id

foo = account()
print(foo.getId())
print(foo.__id) # Will produce an error

如果您在这一点上仍然感到困惑,我建议您阅读“访问说明符”和“封装”。如果你用 Java 或 C++ 等不同的语言阅读它也没关系,它们是完全相同的概念。

另外,我会将 accountid 转换为 int 并将 initialbalanceannualInterestRate 转换为构造函数中的浮点数,因此您不必在其他“getter”方法中进行转换(那些只是返回变量的方法)。无论如何,它们都应该保存为那些数据类型,因为这是要求。

下一个要点指出(跳过构造函数一个,你得到了那个):

■ id、balance 和annualInterestRate 的访问器和修改器方法。

■ 一个名为 getMonthlyInterestRate() 的方法,它返回月利率。

■ 一个名为 getMonthlyInterest() 的方法,用于返回每月利息。

■ 一种名为withdraw 的方法,用于从帐户中提取指定金额。

■ 一种名为 deposit 的方法,将指定金额存入帐户。

现在类设计已经完成,我们需要处理类功能。我们将通过仅返回变量来减少“getter”(访问器)方法中的代码,因为它已经是正确的数据类型。现在我们只需要创建“setter”(mutator)方法,它会改变值。看了你的代码,我觉得你对getter和setter方法还不是很了解,我就简单介绍一下吧。

getter 方法只返回一个隐藏的类变量(因为它是私有的)

setter 方法只是改变类变量的值。您需要通过方法来执行此操作,因为该变量是私有的。我们将为 setter 方法使用一个参数,它不会返回任何内容。

这个类现在应该看起来像这样

'''
Random Project
--------------
This is what I use for testing things
'''
class account:
  # Constructor
  def __init__(self, id = 0, balance = 100.0, annualInterestRate = 0.0):
    self.__id = int(id)
    self.__balance = float(balance)
    self.__annualInterestRate = float(annualInterestRate)

  # Getters (Accessors)
  def getId(self):
    return self.__id

  def getBalance(self):
    return self.__balance

  def getAnnualInterestRate(self):
    return self.__annualInterestRate

  # Setters (Mutators)
  '''
  Notice that they are the exact
  same as the constructor, just
  that they edit the variables
  individually
  '''
  def setId(self, id):
    self.__id = int(id)

  def setBalance(self, balance):
    self.__balance = float(balance)

  def setAnnualInterestRate(self, annualInterestRate):
    self.__annualInterestRate = float(annualInterestRate)

最后的要点是:

■ 一个名为 getMonthlyInterestRate() 的方法,它返回月利率。

■ 一个名为 getMonthlyInterest() 的方法,用于返回每月利息。

■ 一种名为withdraw 的方法,用于从帐户中提取指定金额。

■ 一种名为 deposit 的方法,将指定金额存入帐户。

您的提款和存款功能很好,但您确实应该使用参数来代替

def withdraw(self, amount):
   self.__balance -= amount

def deposit(self, amount):
   self.__balance += amount

你的getmonthlyInterestRate()方法应该是/12,最后,monthlyInterestRate没有定义在getMonthlyInterest()的范围内。将其替换为 self.getMonthlyInterestRate()。测试时,请确保使用参数初始化您的 Account 对象

最后,这是整个类的代码:

class account:
  # Constructor
  def __init__(self, id = 0, balance = 100.0, annualInterestRate = 0.0):
    self.__id = int(id)
    self.__balance = float(balance)
    self.__annualInterestRate = float(annualInterestRate)

  # Getters (Accessors)
  def getId(self):
    return self.__id

  def getBalance(self):
    return self.__balance

  def getAnnualInterestRate(self):
    return self.__annualInterestRate

  # Setters (Mutators)
  '''
  Notice that they are the exact
  same as the constructor, just
  that they edit the variables
  individually
  '''
  def setId(self, id):
    self.__id = int(id)

  def setBalance(self, balance):
    self.__balance = float(balance)

  def setAnnualInterestRate(self, annualInterestRate):
    self.__annualInterestRate = float(annualInterestRate)

  # Calculating functions
  def getMonthlyInterestRate(self):
    return self.__annualInterestRate / 12

  def getMonthlyInterest(self):
    return self.__balance * self.getMonthlyInterestRate()

我认为你应该对函数做更多的研究,如何使用它们来简化你的代码,类访问说明符,这样你就可以理解最小权限的原则。我知道这现在可能没有意义,但是通过一些谷歌搜索和一些代码,这一切都会有意义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2011-12-25
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多