简体   繁体   English

类成员List <>的值未在C#中更新

[英]Value of class member List<> not updating in C#

i have following code, there are 2 classes, 'Account' and 'Program'. 我有以下代码,有2个类,“帐户”和“程序”。 What i am trying to do here is just simulating a simple bank management console system where user can create instances of 'Account' class ( which are being stored in List<Account> accounts member variable ( Data structure ). I'am confused about why the account list is not updating when i add the Account object and access the accounts list in other methods ( like Login() ). Please guide me. Thanks 我在这里想要做的只是模拟一个简单的银行管理控制台系统,用户可以在其中创建“ Account”类的实例(这些实例存储在List<Account> accounts成员变量(数据结构)中。)当我添加Account对象并以其他方法(如Login() )访问accounts列表时, account列表未更新。请指导我。

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

Every time you run your while loop, you're creating a new instance of bank_instance , and you initialize the bank_instance.accounts to a new List , which effectively erases all the data you've collected. 你运行你的每一次while循环,你要创建的新实例bank_instance ,你初始化bank_instance.accounts到一个新的List ,从而有效地将删除所有你收集的数据。

Instead, try initializing these things outside your loop, so they exist the next time through. 相反,请尝试在循环外初始化这些内容,以便下次存在。

For example: 例如:

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

    while (true)
    {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM