C# 实验五 银行系统
類設(shè)計(jì)
銀行卡共有儲(chǔ)蓄卡(Account)與信用卡(Credit)兩種
在銀行類(bank)中添加Account類的成員,ATM類要實(shí)現(xiàn)銀行的業(yè)務(wù),BankDemo用于編寫main方法測(cè)試功能。
源碼
BankDemo類:
using System;namespace BankDemo {public class BankDemo { public static void Main(string[] args) { Bank bank = new Bank(); // 插入初始數(shù)據(jù)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; // 用于控制業(yè)務(wù)邏輯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; // 可以開始業(yè)務(wù)}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; //衛(wèi)語句 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;}// 優(yōu)化用戶登錄邏輯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+"元");}}}// 體現(xiàn)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("您提取的金額超過您的可透支額度!無法為您提供取款服務(wù)");}else{Show("您提取的金額超過了您的總余額!無法為您提供取款服務(wù)");}}}// 退出登錄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對(duì)象序列public Account OpenAccount(string id, string pwd, double money) // 初始化Account對(duì)象{Account account = new Account(id, pwd, money);accounts.Add(account); // 將account添加到Account對(duì)象序列中return account;}public bool CloseAccount(Account account){int idx = accounts.IndexOf(account);if (idx < 0) return false;accounts.Remove(account);return true;}// 尋找是否存在目標(biāo)用戶public Account FindAccount(string id, string pwd){foreach (Account account in accounts){if (account.IsMatch(id, pwd)){return account;}}return null;}// 信用卡業(yè)務(wù)public Account OpenCredit(string id, string pwd, double money, double quota){Account account = new Credit(id,pwd,money,quota); // 使用多態(tài)將父類account對(duì)象指向子類的Credit對(duì)象accounts.Add(account);return account;}} }Credit類:
using System; using System.Collections.Generic; using System.Text;namespace BankDemo {class Credit:Account{// 構(gòu)造函數(shù)public Credit(string id, string pwd, double money,double quota):base( id, pwd, money) // 構(gòu)造函數(shù),將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; }}// 覆蓋父類的取錢/*信用與儲(chǔ)蓄卡的不同之處主要是信用卡可以透支怎樣去實(shí)現(xiàn)透支功能呢?首先,我們要有個(gè)透支的額度;當(dāng)需要用的錢大于余額,余額為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.當(dāng)輸入的金額是負(fù)數(shù),報(bào)錯(cuò)2.當(dāng)存在欠款時(shí)優(yōu)先還欠款,多出來的存到余額*/public override bool SaveMoney(double money){if (money < 0) return false; //衛(wèi)語句 if (Spend > 0){if ((Spend-money) <= 0){Money += (-(Spend - money));Spend = 0;}}else{Money += money;}return true;}} }總結(jié)
除了完成作業(yè)的基本要求,我還優(yōu)化了程序的執(zhí)行邏輯,原本的程序,登錄成功后完成一次業(yè)務(wù)操作則需要重新登錄,十分麻煩!而且當(dāng)5次操作后,程序?qū)?huì)關(guān)閉。經(jīng)過我的優(yōu)化后,用戶可登錄成功一次后,進(jìn)行無數(shù)次的業(yè)務(wù)操作直到用戶自己退出登錄為止。為了數(shù)據(jù)的安全,我還修改了一些重要字段的修飾詞。在ATM類中,由于新增了信用卡業(yè)務(wù),所以在進(jìn)行業(yè)務(wù)處理的時(shí)候添加了判斷是否為信用卡的判斷功能,當(dāng)是信用卡時(shí),需要顯示欠費(fèi)狀況。用到本章學(xué)到的多態(tài)、繼承、重載、重寫等知識(shí)。在Bank類中,Credit類繼承了Account類,用Account父類指向了Credit對(duì)象,從而實(shí)現(xiàn)將信用卡的對(duì)象添加到Account類的序列中,無需大規(guī)模改寫代碼。
總結(jié)
以上是生活随笔為你收集整理的C# 实验五 银行系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot项目实例——简易版
- 下一篇: C#调用Onnx模型