我有以下代码,有2个类,“帐户”和“程序”。我在这里尝试的只是模拟一个简单的银行管理控制台系统,用户可以在其中创建“帐户”类的实例(存储在List<Account> accounts
成员变量(数据结构)中。我很困惑为什么当我添加Account对象并以其他方法(如account
)访问accounts
列表时,Login()
列表不会更新。
请指导我。感谢
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;
}
}
}
答案 0 :(得分: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)
{