欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 编程语言 > c# >内容正文

c#

c#——银行atm程序demo -凯发ag旗舰厅登录网址下载

发布时间:2024/10/5 c# 35 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 c#——银行atm程序demo 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1. 使用面向对象的思想,模拟现实世界中的银行、账号、atm等对象,其中类中有字段、方法;

2. 在程序中适当的地方,使用属性、索引,注意使用修饰符;

3. 使用继承,继承账号(account类)得到一个子类(如信用账号),增加字段(如信用额度)、属性、方法,覆盖(overrid)一些方法(如withdrawmoney)。

4. 根据程序的需要(可选做),使用c#的其他语法成分,诸如:接口、结构、枚举等。 

using system;namespace 银行atm程序 {class account{//constructoraccount() { }account(long bank_cardid, string password){this.bank_cardid = bank_cardid;this.account_password = password;}//fieldsprivate static long initial_id = 1000000001; //the 1st one to create an account get this id numberprivate static string bank_name = "icbc";private long bank_cardid;public string account_password;private long total_amount = 100000; //initial accountprivate string[] data = new string[5];private string[] keys ={"card id","holder's name", "total sum", "latest withdraw","latest deposit"};//propertypublic 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; }//indexerpublic 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 timepublic 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);}elseconsole.writeline("lack of balance. operation is refused\n");}//deposit from the account, record the current timepublic 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);}elseconsole.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");}elseconsole.writeline("invalid input!!");}//inheritance, subclass creditaccountprotected class creditaccount : account{//constructorcreditaccount(long bank_cardid, string password){this.bank_cardid = bank_cardid;this.account_password = password;}//new fieldprivate long line_of_credit; //line of credit //new propertypublic string credit_rating { set; get; }//new methodpublic 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;elseline_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 numbervoid create_account(){console.writeline("######### " bank_name " #########"); //which bankconsole.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 passwordaccount a_create = new creditaccount(initial_id, password);a = a_create;ca = (creditaccount)a;a[0] = convert.tostring(initial_id); //save idconsole.write("your name: ");a[1] = console.readline(); //save owner's namea[2] = convert.tostring(a.total_amount);a.date_create = datetime.now.tostring(); //save the time that this account was createdconsole.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 userconsole.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 userconsole.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;}}}}}} }

 

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的c#——银行atm程序demo的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图