c# - 具有 2 种方法的简单银行程序,我无法完全解析 C#

标签 c#

我正在创建一个程序,其中包含我不太明白的这两种方法。它们是“取款”和“存款”,它们位于 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

最佳答案

简单的答案是

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

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

关于c# - 具有 2 种方法的简单银行程序,我无法完全解析 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15211777/

相关文章:

c# - Xamarin.Forms 在运行时删除文件 iOS

c# - 在 .NET MVC 4 (Web API) 中,如何拦截对 Controller 的请求并更改其内容类型?

c# - 在 Swagger 中禁用 "Try It Out"

c# - 从模型 MVC 4 动态加载列表

c# - 32 位 .NET 最大字节数组大小是否小于 2GB?

c# - 将循环转换为任务

c# - ElasticSearch NEST OR查询

c# - C#获取子窗口句柄

c# - 我在 asp.net 2.0 中的 session 变量有问题

c# - 扩展 Control 以提供始终如一的安全 Invoke/BeginInvoke 功能是否合适?