个人更喜欢后面的 WinForm程序 (第二个),功能更多也更美观,使用起来也比控制台直观。这个WinForm程序写的很有成就感哈哈^~^(来自一个菜鸟的内心独白,大佬多多指点)—— WinForm的程序下载(解决方案)见文末
目录
控制台程序
WinForm应用程序(控制台的改进)—— 新添加 事件、委托、自定义异常等
功能描述
具体代码实现
具体代码:(可能有错,请帮忙指出^~^ 谢谢~~)
using System;
namespace Bank_ATM
{
class Account
{
//Constructor
Account() { }
Account(long bank_cardID, string password)
{
this.bank_cardID = bank_cardID;
this.account_password = password;
}
//fields
private static long initial_ID = 1000000001; //the 1st one to create an account get this ID number
private static string bank_name = "ICBC";
private long bank_cardID;
public string account_password;
private long total_amount = 100000; //initial account
private string[] data = new string[5];
private string[] keys =
{
"card ID","holder's name", "total sum", "latest withdraw","latest deposit"
};
//property
public long latest_withdraw { set; get; }
public long latest_deposit { set; get; }
public string date_withdraw { set; get; }
public string date_deposit { set; get; }
public string date_create { set; get; }
//indexer
public string this[int i]
{
set
{
data[i] = value;
}
get
{
if (i >= 0 && i < data.Length)
return data[i];
return null;
}
}
public string this[string key]
{
get
{
return this[FindIndex(key)];
}
}
private int FindIndex(string key)
{
for (int i = 0; i < keys.Length; i++)
if (keys[i] == key)
return i;
return -1;
}
//methods
//withdraw from the account, record the current time
public void withdrawMoney()
{
Console.Write("amount(withdraw): ");
latest_withdraw = Convert.ToInt32(Console.ReadLine());
if (latest_withdraw <= total_amount)
{
total_amount -= latest_withdraw;
this[2] = Convert.ToString(total_amount);
date_withdraw = DateTime.Now.ToString();
this[3] = Convert.ToString(latest_withdraw);
}
else
Console.WriteLine("Lack of balance. Operation is refused\n");
}
//deposit from the account, record the current time
public void depositMoney()
{
Console.Write("amount(deposit): ");
latest_deposit = Convert.ToInt32(Console.ReadLine());
if (latest_deposit > 0)
{
total_amount += latest_deposit;
this[2] = Convert.ToString(total_amount);
date_deposit = DateTime.Now.ToString();
this[4] = Convert.ToString(latest_deposit);
}
else
Console.WriteLine("Invalid operation\n");
}
//get information about the account
void get_card_info() //try 4 choices below
{
Console.WriteLine("( card ID / holder's name / total sum / latest withdraw / latest deposit )?");
string instr = Console.ReadLine();
if (instr == "card ID" || instr == "holder's name" || instr == "total sum" || instr == "latest withdraw"
|| instr == "latest deposit")
{
this[3] = Convert.TOString(latest_withdraw);
this[2] = Convert.ToString(total_amount);
Console.Write(instr + " is " + this[instr]);
if (instr == "latest withdraw")
Console.WriteLine(" " + date_withdraw);
else if (instr == "latest deposit")
Console.WriteLine(" " + date_deposit);
else if (instr == "card ID")
Console.WriteLine(" " + date_create);
else if (instr == "card ID" || instr == "total sum")
Console.WriteLine("\n");
}
else
Console.WriteLine("Invalid input!!");
}
//Inheritance, subclass CreditAccount
protected class CreditAccount : Account
{
//Constructor
CreditAccount(long bank_cardID, string password)
{
this.bank_cardID = bank_cardID;
this.account_password = password;
}
//new field
private long line_of_credit; //line of credit
//new property
public string credit_rating { set; get; }
//new method
public long get_line_of_credit() //line of credit according to the credit rating
{
if (credit_rating == "3" || credit_rating == "2")
line_of_credit = 50000;
else if (credit_rating == "1" || credit_rating == "0")
line_of_credit = 10000;
else
line_of_credit = 0;
return line_of_credit;
}
//override method withdrawMoney()
new public void withdrawMoney()
{
Console.Write("amount(withdraw): ");
latest_withdraw = Convert.ToInt32(Console.ReadLine());
if (latest_withdraw <= total_amount + line_of_credit)
{
total_amount -= latest_withdraw;
this[2] = Convert.ToString(total_amount);
date_withdraw = DateTime.Now.ToString();
this[3] = Convert.ToString(latest_withdraw);
if (latest_withdraw >= total_amount)
{
Console.WriteLine("warning: you're using your credit!! Withdraw successfully");
int temp = Convert.ToInt32(credit_rating);
credit_rating = Convert.ToString(--temp);
get_line_of_credit();
}
}
else
{
Console.WriteLine("Lack of balance. Operation is refused\n");
}
}
public static void Main(String[] args)
{
Account a;
CreditAccount ca;
string card_category;
//create a new account, set password, get an ID number
void create_account()
{
Console.WriteLine("######### " + bank_name + " #########"); //which bank
Console.Write("create an account ( normal / credit )?");
card_category = Console.ReadLine();
if (card_category != "credit" && card_category != "normal")
{
Console.WriteLine("Invalid input");
create_account();
}
Console.Write("set password: ");
string password = Console.ReadLine(); //set password
Account a_create = new CreditAccount(initial_ID, password);
a = a_create;
ca = (CreditAccount)a;
a[0] = Convert.ToString(initial_ID); //save ID
Console.Write("Your name: ");
a[1] = Console.ReadLine(); //save owner's name
a[2] = Convert.ToString(a.total_amount);
a.date_create = DateTime.Now.ToString(); //save the time that this account was created
Console.WriteLine("create successfully!!\nYour ID: " + initial_ID + " " +
"Remember your password:" + password + " You have $100000 initially.");
initial_ID++;
a.latest_deposit = 0;
a.latest_withdraw = 0;
if (card_category == "credit")
{
ca.credit_rating = "3";
ca.get_line_of_credit();
}
}
create_account();
while (true)
{
if (card_category == "normal")
{
//ask for the next instruction from the user
Console.WriteLine("( create again / get information / withdraw / deposit )?");
switch (Console.ReadLine())
{
case "create again": create_account(); break;
case "get information":
a.get_card_info(); break;
case "withdraw":
a.withdrawMoney();
a[2] = Convert.ToString(a.latest_withdraw);
break;
case "deposit":
a.depositMoney();
a[3] = Convert.ToString(a.latest_deposit);
break;
default:
Console.WriteLine("invalid input\n");
break;
}
}
else if (card_category == "credit")
{
//ask for the next instruction from the user
Console.WriteLine("( create again / get information / withdraw / deposit / line of credit )?");
switch (Console.ReadLine())
{
case "create again": create_account(); break;
case "get information":
ca.get_card_info(); break;
case "withdraw":
ca.withdrawMoney();
ca[2] = Convert.ToString(ca.latest_withdraw);
break;
case "deposit":
ca.depositMoney();
ca[3] = Convert.ToString(ca.latest_deposit);
break;
case "line of credit":
Console.WriteLine("LIne of credit: " + ca.get_line_of_credit());
break;
default:
Console.WriteLine("invalid input\n");
break;
}
}
}
}
}
}
}
******* 整个程序下载见文末 **********
对应题目:(我实现的功能涵盖并超过题目要求,题目可以忽略不看,功能描述中功能写的很清楚)
基本功能与上面的控制台应用相同,增加了事件以及委托、自定义异常,对用户错误的输入进行反馈。
具体:
运行 - (Form1出现)- 点击“create account” - (Form2出现)- 用户输入 - 选择创建”normal account“还是“credit account” (如果文本框中用户没有输入,则会抛出异常,信息提示)->
如果选择“normal account” - (Form4出现)- 可以先通过“get information”查看初始余额(设置有初始金额)- 可以取款或者存款(如果取款数额一次性超过10000,则会触发事件,有盗窃风险提示;如果取款数额超过现有余额,则抛出异常,不让取款)- 可以再通过“get information”查看现在的账户信息 - 有大约1/3的概率会提示有换钞票,不让取款或存款(模拟现实情况,抛出异常)
如果选择“credit account” - (Form5出现)-大部分操作与Form4相同(设置有信用额度,即最大透支金额,并且随着取款透支次数的增加,透支金额会减少)- 当前透支金额可以通过"current line of credit"查看 - 有大约1/3的概率会提示有换钞票,不让取款或存款(模拟现实情况,抛出异常)
Form1.cs代码如下:
//Form1.cs代码如下
using System;
using System.Windows.Forms;
namespace ATM
{
public partial class Form1 : Form
{
public static Account a = new Account();
public static CreditAccount ca = new CreditAccount();
public Form1()
{
InitializeComponent();
}
public static string name, pass;
private string login_input_ID, login_input_pass;
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
name = pass = null;
f2.Show();
}
private void button3_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(login_input_ID)&&!String.IsNullOrEmpty(login_input_pass))
{
Form4 f4 = new Form4();
Form5 f5 = new Form5();
if (login_input_ID == a[0] && login_input_pass == a.password)
f4.Show();
else if (login_input_ID == ca[0] && login_input_pass == ca.password)
f5.Show();
else
MessageBox.Show("wrong ID or password.");
}
else
{ //use custom Exception, Error number 0
try
{
Account.emptyInput(login_input_ID);
Account.emptyInput(login_input_pass);
}
catch (EmptyException ex)
{
MessageBox.Show(ex.Message + ex.getId());
}
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
login_input_ID = this.textBox1.Text;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
login_input_pass = this.textBox2.Text;
}
}
public class BigMoneyArgs : EventArgs
{
public int ID;
public int amount_fetch;
}
public delegate void BigMoneyHandler(object sender, BigMoneyArgs e);
//defining Exception classes
class SurpassException : ApplicationException
{
private int idnumber;
public SurpassException(String message, int id) : base(message)
{
this.idnumber = id;
}
public int getId()
{
return idnumber;
}
}
class EmptyException : ApplicationException
{
private int idnumber;
public EmptyException(String message, int id): base(message)
{
this.idnumber = id;
}
public int getId()
{
return idnumber;
}
}
class BadCashException : ApplicationException
{
private int idnumber;
public BadCashException(String message, int id) : base(message)
{
this.idnumber = id;
}
public int getId()
{
return idnumber;
}
}
public class Account
{
public event BigMoneyHandler BigMoneyFetched;
public Account() { this.BigMoneyFetched += ShowWarning; } //add events
//fields
protected static long ID = 1000001;
public string password;
protected long total_amount = 1000000;
protected string[] data = new string[5];
protected string[] keys =
{
"card ID","holder's name", "total sum", "latest withdraw","latest deposit"
};
//property
public long latest_withdraw { set; get; }
public long latest_deposit { set; get; }
public string date_withdraw { set; get; }
public string date_deposit { set; get; }
//indexer
public string this[int i]
{
set
{
data[i] = value;
}
get
{
if (i >= 0 && i < data.Length)
return data[i];
return null;
}
}
public string this[string key]
{
get
{
return this[FindIndex(key)];
}
}
private int FindIndex(string key)
{
for (int i = 0; i < keys.Length; i++)
if (keys[i] == key)
return i;
return -1;
}
public static void ShowWarning(object sender, BigMoneyArgs e)
{
MessageBox.Show($"Too much fetched, swindle warning.\nID:{e.ID:###}\nAmount Fetched:{e.amount_fetch:######}");
}
//Exception method
public void surpass(int amount)
{
if (amount > this.total_amount)
{
throw new SurpassException("withdraw failed.\nError number:", 0);
}
}
public static void emptyInput(String s)
{
if (String.IsNullOrEmpty(s))
throw new EmptyException("Empty Input is not allowed.\nError number: ", 1);
}
public void badCash(int rnd)
{
if (rnd < 1)
throw new BadCashException("Bad Cash Refuse !!!\nError number: ", 2);
}
//methods
public void create_account(string name, string pass)
{
this.password = pass;
this[0] = Convert.ToString(ID);
this[1] = name;
this[2] = Convert.ToString(total_amount);
this[3] = this[4] = "0";
MessageBox.Show("Remember your ID:\n" + ID + "\nYou'll use it to log in.");
ID++;
this.latest_deposit = this.latest_withdraw = 0;
}
public void withdraw(int withdraw_amount)
{
if (withdraw_amount <= total_amount&&withdraw_amount>0)
{
latest_withdraw = withdraw_amount;
total_amount -= withdraw_amount;
this[2] = Convert.ToString(total_amount);
date_withdraw = DateTime.Now.ToString();
this[3] = Convert.ToString(withdraw_amount);
if (withdraw_amount > 10000) //trigger Event
{
if (BigMoneyFetched != null)
{
BigMoneyArgs args = new BigMoneyArgs();
args.amount_fetch = withdraw_amount;
args.ID = Convert.ToInt32(this[0]);
BigMoneyFetched(this, args);
}
} //Exception BadCash
try
{
Random rnd = new Random();
badCash(rnd.Next(0, 3));
}catch(BadCashException eb)
{
MessageBox.Show(eb.Message + eb.getId() + "\nWithdraw failed.");
}
}
else
{ //Exception Surpass
try
{
surpass(withdraw_amount);
}catch(SurpassException e)
{
MessageBox.Show(e.Message + e.getId());
} //Exception EmptyInt
try
{
Account.emptyInput(Form4.wd);
}
catch (EmptyException ex)
{
MessageBox.Show(ex.Message + ex.getId());
}
}
}
public void deposit(int deposit_amount)
{
if (deposit_amount > 0)
{
latest_deposit = deposit_amount;
total_amount += deposit_amount;
this[2] = Convert.ToString(total_amount);
date_deposit = DateTime.Now.ToString();
this[4] = Convert.ToString(deposit_amount);
try //Exception BashCash
{
Random rnd = new Random();
badCash(rnd.Next(0, 2));
}
catch (BadCashException eb)
{
MessageBox.Show(eb.Message + eb.getId() + "\nDeposit failed.");
}
}
else
{ //Exception EmptyInput
try
{
Account.emptyInput(Form4.dp);
}
catch (EmptyException ex)
{
MessageBox.Show("Empty Input is not allowed.\nError number: " + ex.getId());
}
}
}
}
public class CreditAccount : Account
{
new public event BigMoneyHandler BigMoneyFetched;
public CreditAccount() { this.BigMoneyFetched += ShowWarning; }
private int line_of_credit;
public int credit_rating { set; get; }
public int get_line_of_credit()
{
if (credit_rating == 3 || credit_rating == 2)
line_of_credit = 50000;
else if (credit_rating == 1 || credit_rating == 0)
line_of_credit = 10000;
else
line_of_credit = 0;
return line_of_credit;
}
//override method
new public void surpass(int amount)
{
if (amount > this.total_amount+this.get_line_of_credit())
{
throw new SurpassException("upper bound surpass", 0);
}
}
new public void create_account(string pass, string name)
{
this.password = pass;
this[0] = Convert.ToString(ID);
this[1] = name;
this[2] = Convert.ToString(total_amount);
this[3] = this[4] = "0";
ID++;
this.latest_deposit = this.latest_withdraw = 0;
this.credit_rating = 3;
this.get_line_of_credit();
}
new public void withdraw(int withdraw_amount)
{
latest_withdraw = withdraw_amount;
if (withdraw_amount <= total_amount + line_of_credit && withdraw_amount > 0)
{
total_amount -= withdraw_amount;
this[2] = Convert.ToString(total_amount);
date_withdraw = DateTime.Now.ToString();
this[3] = Convert.ToString(withdraw_amount);
if (latest_withdraw > total_amount)
{
credit_rating--;
get_line_of_credit();
}
if (withdraw_amount > 10000) //trigger Event
{
if (BigMoneyFetched != null)
{
BigMoneyArgs args = new BigMoneyArgs();
args.amount_fetch = withdraw_amount;
args.ID = Convert.ToInt32(this[0]);
BigMoneyFetched(this, args);
}
}
}
else
{ //Exception surpass
try
{
surpass(withdraw_amount);
}
catch (SurpassException e)
{
MessageBox.Show("withdraw failed.\nError number: " + e.getId());
} //Exception EmptyInput
try
{
Account.emptyInput(Form4.wd);
}
catch (EmptyException ex)
{
MessageBox.Show("Empty Input is not allowed.\nError number: " + ex.getId());
}
}
}
}
}
Form2.cs代码如下:
//Form2.cs代码如下
using System;
using System.Windows.Forms;
namespace ATM
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
Form1.pass = this.textBox2.Text;
}
private void button1_Click(object sender, EventArgs e)
{
if(!String.IsNullOrEmpty(Form1.name)&&!String.IsNullOrEmpty(Form1.pass))
{
Form1.a.create_account(Form1.name, Form1.pass);
this.Close();
}
else
{ //Exception EmptyInput
try
{
Account.emptyInput(Form1.name);
Account.emptyInput(Form1.pass);
}
catch (EmptyException ex)
{
MessageBox.Show(ex.Message + ex.getId());
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Form1.name) && !String.IsNullOrEmpty(Form1.pass))
{
Form1.ca.create_account(Form1.name, Form1.pass);
this.Close();
}
else
{ //Exception EmptyInput
try
{
Account.emptyInput(Form1.name);
Account.emptyInput(Form1.pass);
}
catch (EmptyException ex)
{
MessageBox.Show(ex.Message + ex.getId());
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
Form1.name = this.textBox1.Text;
}
}
}
Form4.cs代码如下(因为一些原因,没有Form3.cs ^~^)
//Form4.cs代码如下
using System;
using System.Windows.Forms;
namespace ATM
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
public static string wd, dp;
private void button3_Click(object sender, EventArgs e)
{
this.textBox3.Text = Form1.a[0];
this.textBox4.Text = Form1.a[3] + " " + Form1.a.date_withdraw;
this.textBox5.Text = Form1.a[4] + " " + Form1.a.date_deposit;
this.textBox6.Text = Form1.a[2];
this.textBox7.Text = Form1.a[1];
}
private void button1_Click(object sender, EventArgs e)
{
if (wd == "")
wd = null;
Form1.a.withdraw(Convert.ToInt32(wd));
wd = null;
this.textBox1.Text = null;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
e.Handled = false;
else
{
MessageBox.Show("Only numbers allowed!");
e.Handled = true;
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
dp = this.textBox2.Text;
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
e.Handled = false;
else
{
MessageBox.Show("Only numbers allowed!");
e.Handled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (dp == "")
dp = null;
Form1.a.deposit(Convert.ToInt32(dp));
dp = null;
this.textBox2.Text = null;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
wd = this.textBox1.Text;
}
}
}
Form5.cs代码如下
//Form5.cs代码如下
using System;
using System.Windows.Forms;
namespace ATM
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox8.Text = Convert.ToString(Form1.ca.get_line_of_credit());
}
private void button3_Click_1(object sender, EventArgs e)
{
this.textBox3.Text = Form1.ca[0];
this.textBox4.Text = Form1.ca[3] + " " + Form1.ca.date_withdraw;
this.textBox5.Text = Form1.ca[4] + " " + Form1.ca.date_deposit;
this.textBox6.Text = Form1.ca[2];
this.textBox7.Text = Form1.ca[1];
}
private void button1_Click(object sender, EventArgs e)
{
if (Form4.wd == "")
Form4.wd = null;
Form1.ca.withdraw(Convert.ToInt32(Form4.wd));
Form4.wd = null;
this.textBox1.Text = null;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
Form4.wd = this.textBox1.Text;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
e.Handled = false;
else
{
MessageBox.Show("Only numbers allowed!");
e.Handled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (Form4.dp == "")
Form4.dp = null;
Form1.ca.deposit(Convert.ToInt32(Form4.dp));
Form4.dp = null;
this.textBox2.Text = null;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
Form4.dp = this.textBox2.Text;
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
e.Handled = false;
else
{
MessageBox.Show("Only numbers allowed!");
e.Handled = true;
}
}
}
}