2

我正在创建一个具有这两种我无法弄清楚的方法的程序。它们是“提款”和“存款”,它们位于 CheckingAccount 类中。在这些方法中,我希望最初的值为 0 然后添加到它。然后我想取新数字并从中减去。我想“存入”250 美元。然后我想“提取” 98 美元。我不确定在哪里存储这些值以及如何执行它们。当我将提款和存款方法留空时,我知道显示应该如何结束。

账户类:

class Account
{
    protected string firstName;
    protected string lastName;
    protected long number;

    public string FirstName
    {
        set
        {
            firstName = value;
        }
    }
    public string LastName
    {
        set
        {
            lastName = value;
        }
    }
    public long Number
    {
        set
        {
            number = value;
        }
    }
    public override string ToString()
    {
        return firstName + " " + lastName + "\nAccount #: " + number;
    }
}
}

支票账户类:

    class CheckingAccount : Account
{
    private decimal balance;

    public CheckingAccount(string firstName, string lastName, long number, decimal initialBalance)
    {
        FirstName = firstName;
        LastName = lastName;
        Number = number;
        Balance = initialBalance;
    }
    public decimal Balance
    {
        get
        {
            return balance;
        }
        set
        {
            balance = value;
        }
    }


    public void deposit(decimal amount)
    {
        //initial value should be 0 and should be adding 250 to it.
    }
    public void withdraw(decimal amount)
    {
        //this takes the 250 amount and subtracts 98 from it
    }


    public void display()
    {
        Console.WriteLine(ToString());
        Console.WriteLine("Balance: ${0}", Balance);
    }
}
}

展示类:

    class Display
{
    static void Main(string[] args)
    {

        CheckingAccount check = new CheckingAccount("John", "Smith", 123456, 0M);

        Console.WriteLine("After Account Creation...");
        check.display();

        Console.WriteLine("After Depositing $250...");
        //constructor
        Console.WriteLine("After Withdrawing $98...");
        //constructor
    }
}
}

我希望我的输出看起来像这样:

创建帐户后...
John Smith
帐户编号:123456
余额:0

存入 250 美元后...
John Smith
帐号:123456
余额:250

提取 98 美元后...
John Smith
帐号:123456
余额:152

4

3 回答 3

5

简单的答案是

public void deposit(decimal amount)
{
    balance += amount;
}
public void withdraw(decimal amount)
{
    balance -= amount;
}

随意添加必要的验证(透支?试图存入负数?)

于 2013-03-04T21:51:02.287 回答
1

要创建帐户:

Checking Account ca = new Checking Account (John, Smith, 123456, 0);

存入 250 美元:

ca.deposit(250);

提取 98 美元:

ca.withdraw(98);

商业逻辑:

public void deposit(decimal amount)
{
    balance += amount;
}
public void withdraw(decimal amount)
{
    balance -= amount;
}

正如另一个答案所述,当您的帐户中没有钱(或更少)时验证透支或提款等场景是明智的!


奖金:

您还可以编写一个 GetBalance 函数来验证存款/取款。

public decimal GetBalance(long accountNumber)
{
    return balance;
}

并使用以下方法调用它:

var currentBalance = ca.GetBalance(123456);
于 2013-03-04T21:53:59.220 回答
1

您可能希望为提款方法提供额外的逻辑以防止透支。

public void withdraw(decimal amount)
{
    if (balance >= amount)
    {
       balance -= amount;
    }
    else
    {
       Console.WriteLine("You can't withdraw money that you don't have!");
       // or else you could charge an overdraft fee as long as you're within 
       // a certain tolerance (minimum of -1000 or something like that). 
    }
}
于 2013-03-04T22:10:07.190 回答