日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 实验五 银行系统

發布時間:2023/12/18 C# 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 实验五 银行系统 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

類設計

銀行卡共有儲蓄卡(Account)與信用卡(Credit)兩種

在銀行類(bank)中添加Account類的成員,ATM類要實現銀行的業務,BankDemo用于編寫main方法測試功能。

源碼

BankDemo類:

using System;namespace BankDemo {public class BankDemo { public static void Main(string[] args) { Bank bank = new Bank(); // 插入初始數據bank.OpenAccount("2222", "2222", 20); bank.OpenAccount("3333", "3333", 50);bank.OpenCredit("4444","4444",100,1000);ATM atm = new ATM(bank); bool login_ctr = false; // 用于控制登錄邏輯bool service_ctr = true; // 用于控制業務邏輯while (!login_ctr) {Account account = atm.Login();if (account == null) // 登錄失敗{Console.WriteLine("card invalid or password not corrent,please retry again");service_ctr = false;}else // 登錄成功{service_ctr = true; // 可以開始業務}while (service_ctr) {//login_ctr = true; // 登錄成功service_ctr = atm.Transaction(account);}Console.ReadKey(); // 停頓一下Console.Clear(); // 清屏}} } }

Account類:

using System; using System.Collections.Generic; using System.Text;namespace BankDemo {public class Account{protected double _money; //decimal money; protected string _id;protected string _pwd; //string name; public Account(string id, string pwd, double money){ //if( money < 0 ) throw new Exception("...."); Id = id;Pwd = pwd;Money = money;}public Account() { }public double Money{set { _money = value; }get { return _money; }}public string Id{set { _id = value; }get { return _id; }}public string Pwd{set { _pwd = value; }get { return _pwd; }}public virtual bool SaveMoney(double money){if (money < 0) return false; //衛語句 Money += money; return true;}public virtual bool WithdrawMoney(double money){if (Money >= money) { Money -= money; return true; }return false;}public bool IsMatch(string id, string pwd) { return id == Id && pwd == Pwd; }} }

ATM類:

using System; using System.Collections.Generic; using System.Text;namespace BankDemo {public class ATM{Bank bank;public ATM(Bank bank){this.bank = bank;}// 優化用戶登錄邏輯public Account Login(){Show("please insert your card");string id = GetInput();Show("please enter your password");string pwd = GetInput();Account account = bank.FindAccount(id, pwd);return account;}public bool Transaction(Account account){Show("1: display; 2: save; 3: withdraw; 4. exit");string op = GetInput();// 顯示余額if (op == "1"){Show("balance: " + account.Money);if (account is Credit){Credit c = (Credit)account;if (c.Spend > 0){Show("您已欠款:" + c.Spend + "元");}}}// 還款/存錢else if (op == "2"){Show("save money");string smoney = GetInput();double money = double.Parse(smoney);bool ok = account.SaveMoney(money);if (ok) Show("ok");else Show("eeer");Show("balance: " + account.Money);if (account is Credit){Credit c = (Credit)account;if(c.Spend > 0){Show("您仍需要還款:"+c.Spend+"元");}}}// 體現else if (op == "3"){Show("withdraw money");string smoney = GetInput();double money = double.Parse(smoney);bool ok = account.WithdrawMoney(money);if (ok){Show("取款成功!");if (account is Credit){Credit c = (Credit)account;if(c.Spend > 0){Show("您已透支:" + c.Spend + "元,還可透支:" + (c.Quota - c.Spend) + "元");}else{Show("balance: " + account.Money);}}}else{if (account is Credit){Show("您提取的金額超過您的可透支額度!無法為您提供取款服務");}else{Show("您提取的金額超過了您的總余額!無法為您提供取款服務");}}}// 退出登錄if (op == "4"){//account = null;Show("退出成功!");return false;}return true;}public void Show(string msg){Console.WriteLine(msg);}public string GetInput(){return Console.ReadLine();// 輸入字符 }} }

Bank類:

using System; using System.Collections; using System.Collections.Generic; using System.Text;namespace BankDemo {public class Bank{List<Account> accounts = new List<Account>(); // 生成Account對象序列public Account OpenAccount(string id, string pwd, double money) // 初始化Account對象{Account account = new Account(id, pwd, money);accounts.Add(account); // 將account添加到Account對象序列中return account;}public bool CloseAccount(Account account){int idx = accounts.IndexOf(account);if (idx < 0) return false;accounts.Remove(account);return true;}// 尋找是否存在目標用戶public Account FindAccount(string id, string pwd){foreach (Account account in accounts){if (account.IsMatch(id, pwd)){return account;}}return null;}// 信用卡業務public Account OpenCredit(string id, string pwd, double money, double quota){Account account = new Credit(id,pwd,money,quota); // 使用多態將父類account對象指向子類的Credit對象accounts.Add(account);return account;}} }

Credit類:

using System; using System.Collections.Generic; using System.Text;namespace BankDemo {class Credit:Account{// 構造函數public Credit(string id, string pwd, double money,double quota):base( id, pwd, money) // 構造函數,將id,pwd,money交給Account類去生成{Quota = quota;}private double quota ; // 額度private double spend; // 已用// 屬性public double Quota { set { quota = value; }get { return quota; }}public double Spend{set { spend = value; }get { return spend; }}// 覆蓋父類的取錢/*信用與儲蓄卡的不同之處主要是信用卡可以透支怎樣去實現透支功能呢?首先,我們要有個透支的額度;當需要用的錢大于余額,余額為0,可透支額度減少。*/public override bool WithdrawMoney(double money){double temp = Money - money;if (temp > 0){Money -= money;return true;}else if(temp < 0){if (temp > (Spend-Quota)){Spend += temp;Spend = (-Spend);//Quota += temp;Money = 0;return true; // 取錢成功}}return false; }// 還款/*1.當輸入的金額是負數,報錯2.當存在欠款時優先還欠款,多出來的存到余額*/public override bool SaveMoney(double money){if (money < 0) return false; //衛語句 if (Spend > 0){if ((Spend-money) <= 0){Money += (-(Spend - money));Spend = 0;}}else{Money += money;}return true;}} }

總結

除了完成作業的基本要求,我還優化了程序的執行邏輯,原本的程序,登錄成功后完成一次業務操作則需要重新登錄,十分麻煩!而且當5次操作后,程序將會關閉。經過我的優化后,用戶可登錄成功一次后,進行無數次的業務操作直到用戶自己退出登錄為止。為了數據的安全,我還修改了一些重要字段的修飾詞。在ATM類中,由于新增了信用卡業務,所以在進行業務處理的時候添加了判斷是否為信用卡的判斷功能,當是信用卡時,需要顯示欠費狀況。用到本章學到的多態、繼承、重載、重寫等知識。在Bank類中,Credit類繼承了Account類,用Account父類指向了Credit對象,從而實現將信用卡的對象添加到Account類的序列中,無需大規模改寫代碼。

總結

以上是生活随笔為你收集整理的C# 实验五 银行系统的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。