简单的银行程序,有2个方法,我不能很好地解决c#

本文关键字:不能 很好 解决 方法 2个 程序 简单 | 更新日期: 2023-09-27 18:02:27

我正在创建一个程序,其中有这两个方法,我不能完全弄清楚。它们是'取款'和'存款'它们位于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
    }
}
}

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

帐户创建后…
约翰·史密斯
账户号:123456
平衡:0

存入$250后…
约翰·史密斯
账户号:123456
平衡:250

取出$98后…
约翰·史密斯
账户号:123456
平衡:152

简单的银行程序,有2个方法,我不能很好地解决c#

简单的答案是

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

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

创建帐户:

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);

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

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). 
    }
}