2017-05-18 67 views
0

我一直在尝试通过创建一个允许用户添加当前或储蓄帐户的基本银行应用程序来学习一些C#。如果它是一个经常账户,那么它将起始余额乘以0.2,如果它是一个储蓄,那么它乘以0.6。当他们添加一个帐户时,应用程序应该将其保存到列表中,并最终显示所有帐户名称。到目前为止,我有一个允许用户添加名为AddAccount.cs的帐户的表单。然后,我有一个Account.cs应该设置帐户,然后AccountList.cs将其添加到列表中。C#检索数据并将其存储在列表中

我需要什么帮助:

  1. 我如何通过新的帐户信息并设置他们在Account.cs?
  2. 然后如何将账户添加到列表中并显示账户的名称?

Account.cs:

abstract class Account 
    { 
     public string accName, accId, accType; 
     public double balance; 

     public void setValues(string name, string id, double bal, string type) 
     { 
      this.accName = name; 
      this.accId = id; 
      this.balance = bal; 
      this.accType = type; 
     } 
    } 

    class CurrentAccount : Account 
    { 
     public double interst() 
     { 
      return balance * 0.2; 
     } 
    } 

    class SavingsAccount : Account 
    { 
     public double interst() 
     { 
      return balance * 0.6; 
     } 
    } 

AddAccount.cs:

private void btn_AddAccount_Click(object sender, EventArgs e) 
     { 
      string name, id, type; 
      double balance; 

      name = input_AccountName.Text; 
      id = input_AccountNo.Text; 
      balance = Convert.ToDouble(input_StartBalance.Text); 

      if (radio_CurrentAccount.Checked) 
      { 
       type = "C"; 
      } 
      else 
      { 
       type = "S"; 
      } 

      //closes the form when clicked 
      this.Close(); 
     } 

AccountList.cs:

class AccountList 
    { 
     private List<Account> accountlst; 
     public List<Account> AccountLst 
     { 
      get { return accountlst; } 
     } 
    } 

如果我完全错了,请让我知道。即使最细微的帮助,将不胜感激。

+1

在AddAccount.cs您提取的输入,并设置局部变量。当代码从添加退出点击你失去了一切,你需要类型账户的一类级别的变量,并设置其属性,那么你这个帐户添加到列表中 – Steve

回答

1

那么假设,如果类型是“C”,那么你创建型活期账户的对象,如果类型为“S”,那么你创建一个储蓄账户,将看起来像这样(的方式我去做到这一点在伪代码):

if (type is C) 
    Create new CurrentAccount object 
    call setValues(name, id, bal, type) //these are the local variable you created in AddAccount.cs 
    getAccountlst().add(CurrentAccount object you created) //adds to list 
else 
    Create new SavingsAccount object 
    call setValues(name, id, bal, type) 
    getAccountlst().add(SavingsAccount object you created) //adds to list 

顺便说2的问题,因为你从来没有通过调用new运算符初始化内部AccountList.cs的accountlst对象,它是)设置为空,所以,当你调用getAccountlst(它将返回一个空对象,如果你尝试添加它,你会得到一个空指针异常!而pther问题,因为你的AccountList.cs必须用新的运营商进行初始化,您可以失去你的列表里面的信息,来解决这个问题,你可以这样做:

static class AccountList { 

    List<Account> accountList = new List<Account>(); 

    public List<Account> Accountlst { 

     get { 
      return accountList; 
     } 
    } 
} 

我们添加到您的列表中的所有你所要做的就是AccountList.Accountlst.add(Account object here);

+0

谢谢你,这是完美的! :) – TheGarrett

相关问题