2017-03-24 2 views
0

次のコードがありますが、 'Account'と 'Program'の2つのクラスがあります。私がここでやろうとしているのは、ユーザーが 'Account'クラスのインスタンスを作成できる単純な銀行管理コンソールシステムをシミュレートすることです(List<Account> accountsメンバ変数(データ構造)に格納されています)。なぜaccountリストが混乱しているのですか?私はAccountオブジェクトを追加し、他の方法(Login()のような)でaccountsリストにアクセスしたときに更新していない。 私を導いてください。おかげクラスメンバの値List <>はC#で更新されません

namespace Banking_System 
{ 
    class Account 
    { 

     public Account(string f, string l, string id, string ph, string t, int bal, int no) 
     { 
      fname = f; 
      lname = l; 
      cnic = id; 
      phone = ph; 
      amount = bal; 
      acc_no = no; 
     } 

     public string fname { get; set; } 
     public string lname { get; set; } 
     public string cnic { get; set; } 
     public string phone { get; set; } 
     public string type { get; set; } 
     public int amount { get; set; } 
     public int acc_no { get; set; } 
    } 

    class Program 
    { 
     //private Program bank_instance; 
     private List<Account> accounts {get;set;} 

     static void Main(string[] args) 
     { 
      while (true) 
      { 
       Console.Clear(); 
       Console.WriteLine("Welcome to the bank"); 
       Console.WriteLine("====================\n"); 
       Console.WriteLine("Please choose an option from the list below"); 
       Console.WriteLine("============================================"); 
       Console.WriteLine("e ñ Existing account"); 
       Console.WriteLine("n ñ New account"); 
       Console.WriteLine("c ñ close"); 

       string input = Console.ReadLine(); 
       Account acc; 

       Program bank_instance = new Program(); 
       bank_instance.accounts = new List<Account>(); 

       if (input == "n" || input == "N") 
       { 
        bank_instance.NewAccount(); 
        int x = bank_instance.accounts.Count; 
       } 
       else if (input == "e" || input == "E") 
       { 
        acc = bank_instance.login(); 
        if (acc == null) 
        { 
         Console.WriteLine("Account does not exists !"); 
         continue; 
        } 
        bank_instance.operate(acc); 
       } 
       else if (input == "c" || input == "C") 
       { 
        Console.WriteLine("Goodbye !"); 
        break; 
       } 
       else 
       { 
        Console.WriteLine("You entered the wrong character \nPlease enter the correct one"); 
       } 
      } 

     } 

     private void operate(Account acc) 
     { 
      string input = null; 
      while (true) 
      { 
       Console.Clear(); 
       Console.WriteLine("Choose an operation:"); 
       Console.WriteLine("1) Check balance"); 
       Console.WriteLine("2) Deposit cash"); 
       Console.WriteLine("3) Withdraw cash"); 
       Console.WriteLine("4) Close"); 
       input = Console.ReadLine(); 
       if (input == "1") 
       { 
        check_balance(acc.acc_no); 
       } 
       else if (input == "2") 
       { 
        deposite_cash(acc); 
       } 
       else if (input == "3") 
       { 
        withdraw(acc); 
       } 
       else if (input == "4") 
       { 
        Console.WriteLine("Goodbye !"); 
        break; 
       } 
       else 
       { 
        Console.WriteLine("You entered the wrong character \nPlease enter the correct one"); 
       } 
      } 
     } 

     private void check_balance(int inp) 
     { 
      foreach (Account a in accounts) 
      { 
       if (a.acc_no.Equals(inp)) 
       { 
        Console.Write("Your Balance is : " + a.amount); 
       } 
      } 

     } 

     private void deposite_cash(Account acc) 
     { 
      int bal = 0; 
      Console.WriteLine("Enter the amount"); 
      bal = int.Parse(Console.ReadLine()); 
      if (bal <= 0) 
      { 
       Console.WriteLine("Please enter valid amount"); 
      } 
      else 
      { 
       acc.amount += bal; 
       Console.WriteLine("Amount has been deposited successfully!"); 
      } 
     } 

     private void withdraw(Account acc) 
     { 
      int bal = 0; 
      Console.WriteLine("Enter the amount"); 
      bal = int.Parse(Console.ReadLine()); 
      if (bal <= 0) 
      { 
       Console.WriteLine("Please enter valid amount"); 
      } 
      else 
      { 
       acc.amount -= bal; 
       Console.WriteLine("Amount has been Withdrawn successfully!"); 
      } 
     } 

     private void NewAccount() 
     { 
      Console.WriteLine("================================================================"); 
      Console.WriteLine("Please fill in the following information to create a New Account"); 
      Console.WriteLine("================================================================"); 
      Console.WriteLine("1) First name"); 
      string f_name = Console.ReadLine(); 
      Console.WriteLine("2) Last Name"); 
      string l_name = Console.ReadLine(); 
      int check_length; 
      int id_card_no; 
      do 
      { 
       Console.WriteLine("3) National ID card Number (xxxxxxxxxxxxx)"); 
       check_length = Console.ReadLine().Length; 
       id_card_no = Convert.ToInt32(check_length); 
      } 
      while (check_length != 13); 

      Console.WriteLine("4) Contact Number"); 
      System.Int64 contact_no = Convert.ToInt64(Console.ReadLine()); 
      Console.WriteLine("5) Account type - (Current , Saving) "); 
      string account_type = Console.ReadLine(); 
      Console.WriteLine("6) Initial amount to deposit (Minimum 50,000) "); 
      int ini_amount = int.Parse(Console.ReadLine()); 
      if (ini_amount <= 50000) 
      { 
       Console.WriteLine(ini_amount); 
      } 
      else 
      { 
       Console.WriteLine("teri mehrbani"); 
      } 

      // Generate a random number 
      Random rand = new Random(); 

      Account acc = new Account(f_name, l_name, id_card_no.ToString(), contact_no.ToString(), account_type, ini_amount, rand.Next(00000, 55555)); 

      accounts.Add(acc); 
      Console.WriteLine("Account created ! you Account number is : " + acc.acc_no + "\n Press any key to continue"); 
      Console.Read(); 
     } 

     private Account login() 
     { 
      int inp = 0; 
      Console.WriteLine("Enter your account number"); 
      inp = int.Parse(Console.ReadLine()); 

      foreach (Account a in accounts) 
      { 
       if (a.acc_no.Equals(inp)) 
       { 
        Console.Write("Welcome !"); 
        return a; 
       } 
      } 
      return null; 
     } 
    } 
} 

答えて

2

あなたwhileループを実行するたびに、あなたがの新しいインスタンスを作成していますbank_instanceとなり、bank_instance.accountsを新しいListに初期化すると、収集したすべてのデータが効果的に消去されます。

代わりに、ループの外でこれらのものを初期化してください。次回にそれらが存在するようにしてください。例えば

static void Main(string[] args) 
{ 
    Program bank_instance = new Program(); 
    bank_instance.accounts = new List<Account>(); 

    while (true) 
    { 
関連する問題