一、實驗目的
1.加深理解面向對象編程的概念,如類、對象、實例化等。
2.熟練掌握類的封裝、繼承和多態機制。
3.掌握編程常用的幾種排序算法。
4.理解異常的產生過程和異常處理的概念,掌握C#異常處理的方法。
5.能夠將面向對象思想應用與編程實踐,開發出規模稍大的應用,如圖書管理、銀行卡存取款業務等。
二、實驗內容
【實驗3-2】銀行卡存取款業務。
實驗A:
假設某銀行共發出M張儲蓄卡,每張儲蓄卡擁有唯一的卡號,每天每張儲蓄卡至多支持持有者的N筆“存款”或“取款”業務。根據實際發生的業務,實時處理數據,以反映最新情況。
設儲蓄卡包含的數據域有:卡號,當前余額,允許當日發生的業務次數(定義成靜態變量,為所有Card類所共享),當日實際發生的業務數,以及一個數組記錄發生的具體業務。它提供的主要方法有store(),處理判斷是否超過當日允許發生的最大筆數,當前余額是否足以取款,以及實時修改當前數據等。
當持卡者輸入正確的卡號、存款或取款金額后,程序進行相應的處理;若輸入了不正確的數據,程序會提示持卡者重新輸入;若輸入的卡號為負數,銀行終止當日業務。
實驗B:
修改Card類,增加每日使用金額額度不超過5000的限制功能。
?再次修改Card類,要求對銀行卡進行操作前必須驗證用戶密碼,并且在輸入密碼時屏幕上用“*”顯示。為簡單起見,初始密碼設為123456。
三、實驗步驟
實驗A:
1.類:Card,屬性:cardNo: long,balance: decimal,currentNum: int, number: static int,currentMoney: decimal[]
2.方法: Card(): 構造函數,初始化currentMoney數組;Card(long No, decimal Balance): 構造函數,初始化cardNo、balance和currentMoney數組;store(decimal Money, out int status): 存款或取款方法,更新balance和currentMoney數組,設置status狀態;show(): 顯示卡號、余額和存款/取款情況
3.靜態屬性:Number: 設置number值
4.屬性: CardNo: 獲取卡號
在EXP3步驟:
Main方法:
- 輸入當日存款或取款總次數和儲蓄卡總數
- 創建Card對象數組person
- 循環輸入每張卡的卡號和余額
- 創建Card對象并添加到person數組中
5.進入業務處理循環
????1)輸入卡號
????2)如果卡號小于0,結束循環
????3)根據卡號定位到對應的Card對象
????4)輸入金額
????5)調用Card對象的store方法進行存款或取款,并獲取狀態status
????6)根據status打印相應的提示信息
????7)調用Card對象的show方法顯示卡號、余額和存款/取款情況
?Locate(Card[] person, long cardNo)方法: 根據卡號定位到對應的Card對象,返回索引值或-1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EXP3
{class Card{long cardNo;decimal balance;int currentNum;static int number;decimal[] currentMoney;public Card(){currentMoney = new decimal[number];}public Card(long No, decimal Balance){cardNo = No;balance = Balance;currentMoney = new decimal[number];}public void store(decimal Money, out int status){if (currentNum == number){status = 0;return;}if (balance + Money < 0){status = -1;return;}currentMoney[currentNum] = Money;balance += Money;currentNum++;status = 1;}public void show(){Console.WriteLine("卡號:{0},當前余額:{1},當日發生業務的次數:{2}", cardNo, balance, currentNum);for (int i = 0; i < currentNum; i++){Console.WriteLine("當前存款/取款的情況:{0}", currentMoney[i]);}}static public int Number{set{number = value;}}public long CardNo{get{return cardNo;}}}class EXP3{static void Main(string[] args){EXP3 E = new EXP3();Card[] person;int Num, status, k;long CardNo;decimal Balance, Money;Console.Write("請輸入允許當日存款或取款的總次數:");string sline = Console.ReadLine();Card.Number = int.Parse(sline);Console.Write("請輸入某銀行發出的儲蓄卡總數:");sline = Console.ReadLine();Num = int.Parse(sline);person = new Card[Num];for(int i = 0; i < Num; i++){Console.Write("請輸入卡號:");sline = Console.ReadLine();CardNo = long.Parse(sline);Console.Write("請輸入{0} 賬戶余額:", CardNo);sline = Console.ReadLine();Balance = decimal.Parse(sline);person[i] = new Card(CardNo, Balance);}while (true){Console.WriteLine("現在正在進行存款取款的業務處理,如果輸入的卡號<0,則結束業務處理");Console.Write("請輸入卡號:");sline = Console.ReadLine();CardNo = long.Parse(sline);if (CardNo < 0)break;k = E.Locate(person, CardNo);if (k == -1){Console.WriteLine("對不起,不存在{0}號的儲蓄卡", CardNo);continue;}Console.WriteLine("請輸入卡金額(正值代表存款,負值代表取款):");sline = Console.ReadLine();Money = decimal.Parse(sline);person[k].store(Money, out status);switch (status){case -1:Console.WriteLine("存款余額不足,不能完成本次的取款業務");break;case 0:Console.WriteLine("本卡已達到當日允許的業務次數");break;case 1:Console.WriteLine("成功處理完當前業務");person[k].show();break;}}}int Locate(Card[] person,long cardNo){for (int i = 0; i < person.Length; i++){if (person[i].CardNo == cardNo)return i;}return -1;}}
}
?實驗B:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EXP3
{class Card{long cardNo;decimal balance;int currentNum;static int number;decimal[] currentMoney;string password;public Card(){currentMoney = new decimal[number];password = "123456";}public Card(long No, decimal Balance){cardNo = No;balance = Balance;currentMoney = new decimal[number];password = "123456";}public void store(decimal Money, out int status){if (currentNum == number){status = 0;return;}if (balance + Money < 0){status = -1;return;}if (Money > 5000 || Money < -5000){status = -2;return;}currentMoney[currentNum] = Money;balance += Money;currentNum++;status = 1;}public void show(){Console.WriteLine("卡號:{0},當前余額:{1},當日發生業務的次數:{2}", cardNo, balance, currentNum);for (int i = 0; i < currentNum; i++){Console.WriteLine("當前存款/取款的情況:{0}", currentMoney[i]);}}static public int Number{set{number = value;}}public long CardNo{get{return cardNo;}}public bool VerifyPassword(string input){return password == input;}}class EXP3{static void Main(string[] args){EXP3 E = new EXP3();Card[] person;int Num, status, k;long CardNo;decimal Balance, Money;Console.Write("請輸入允許當日存款或取款的總次數:");string sline = Console.ReadLine();Card.Number = int.Parse(sline);Console.Write("請輸入某銀行發出的儲蓄卡總數:");sline = Console.ReadLine();Num = int.Parse(sline);person = new Card[Num];for (int i = 0; i < Num; i++){Console.Write("請輸入卡號:");sline = Console.ReadLine();CardNo = long.Parse(sline);Console.Write("請輸入{0} 賬戶余額:", CardNo);sline = Console.ReadLine();Balance = decimal.Parse(sline);person[i] = new Card(CardNo, Balance);}while (true){Console.WriteLine("現在正在進行存款取款的業務處理,如果輸入的卡號<0,則結束業務處理");Console.Write("請輸入卡號:");sline = Console.ReadLine();CardNo = long.Parse(sline);if (CardNo < 0)break;k = E.Locate(person, CardNo);if (k == -1){Console.WriteLine("對不起,不存在{0}號的儲蓄卡", CardNo);continue;}Console.WriteLine("請輸入密碼:");string password = ReadPassword();if (!person[k].VerifyPassword(password)){Console.WriteLine("密碼錯誤");continue;}Console.WriteLine("請輸入卡金額(正值代表存款,負值代表取款):");sline = Console.ReadLine();Money = decimal.Parse(sline);person[k].store(Money, out status);switch (status){case -1:Console.WriteLine("存款余額不足,不能完成本次的取款業務");break;case -2:Console.WriteLine("每日使用金額超過限制(5000),不能完成本次的存款或取款業務");break;case 0:Console.WriteLine("本卡已達到當日允許的業務次數");break;case 1:Console.WriteLine("成功處理完當前業務");person[k].show();break;}}}int Locate(Card[] person, long cardNo){for (int i = 0; i < person.Length; i++){if (person[i].CardNo == cardNo)return i;}return -1;}private static string ReadPassword(){string password = "";ConsoleKeyInfo keyInfo;do{keyInfo = Console.ReadKey(true);if (keyInfo.Key != ConsoleKey.Backspace && keyInfo.Key != ConsoleKey.Enter){password += keyInfo.KeyChar;Console.Write("*");}else{if (keyInfo.Key == ConsoleKey.Backspace && password.Length > 0){password = password.Substring(0, (password.Length - 1));Console.Write("\b \b");}}} while (keyInfo.Key != ConsoleKey.Enter);Console.WriteLine();return password;}}
}
四、實驗結果展示
實驗A:
實驗B:
?五、實驗小結
該實驗在程序開始時,用戶需要輸入允許當日存款或取款的總次數和某銀行發出的儲蓄卡總數,然后,根據輸入的卡號和賬戶余額創建相應的儲蓄卡對象;用戶可以進行存款或取款的業務處理。用戶輸入要操作的卡號,如果輸入的卡號小于0,則結束業務處理。程序會檢查輸入的卡號是否存在,并要求輸入密碼進行驗證。然后用戶輸入要存取的金額,程序會對金額進行合法性檢查,如余額是否充足、每日使用金額是否超過限制等。根據檢查結果,程序會輸出相應的提示信息,并更新儲蓄卡的余額和操作次數;程序中還包括一個Locate方法,用于根據卡號在儲蓄卡數組中查找對應的儲蓄卡對象。從時間復雜度的角度來看,該程序的主要操作是在存取款業務處理循環中進行的。在每次循環中,需要進行卡號的查找、密碼驗證和金額合法性檢查等操作,操作的時間復雜度都是O(n),其中n為儲蓄卡數組的長度。因此,整個程序的時間復雜度為O(N*M),其中N為儲蓄卡總數,M為當日存款或取款的總次數。
通過實驗,將面向對象的思想應用于銀行卡存取款業務中。這種思想的應用可以使代碼更加模塊化、可擴展和易維護。在實際開發中,我可以學習將面向對象思想應用于開發規模稍大的應用,如圖書管理系統、銀行系統等。這樣的實際開發中,根據需求設計不同的類和對象,并通過封裝、繼承和多態來實現復雜的業務邏輯,提高開發效率和代碼質量。
?