Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 實(shí)訓(xùn)二
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("201705020628 王珉鍇 數(shù)媒192班");
? ? ? ? ? ? Bank kg = new Bank("人民銀行");
? ? ? ? ? ? Console.WriteLine("--------------------------------------------------------------------------------------------");
? ? ? ? ? ? Console.WriteLine("已創(chuàng)建銀行:{0}\n", kg.Name);
? ? ? ? ? ? Account a = new Account("張三", "330101200102030045", "2018-01-01", 0);
? ? ? ? ? ? Account b = new Account("李四", "330206200106010091", "2018-02-02", 1000);
? ? ? ? ? ? Card c = new Card(b, "李四", "330206200106010091", "2018-03-03", 0);
? ? ? ? ? ? //b.IDNumber = "123";//引出異常
? ? ? ? ? ? kg.Register(a);
? ? ? ? ? ? kg.Register(b);
? ? ? ? ? ? kg.Register(c);
? ? ? ? ? ? Console.WriteLine("已增加三個(gè)賬戶\n");
? ? ? ? ? ? //Account f = kg["330101200102030045"];//索引器查找
? ? ? ? ? ? Console.WriteLine("{0}存款{1}元,取款{2}元", a.Name, 1000, 1500);
? ? ? ? ? ? a.Deposit(1000);
? ? ? ? ? ? Console.WriteLine("{0}存款{1}元", a.Name, 1000);
? ? ? ? ? ? a.Withdraw(1500);
? ? ? ? ? ? Console.WriteLine("{0}借記卡賬戶存款{1}元,刷卡{2}元", c.Name, 500, 1200);
? ? ? ? ? ? c.Deposit(500);
? ? ? ? ? ? Console.WriteLine("{0}存款{1}元", c.Name, 500);
? ? ? ? ? ? c.Swipe(1200);
? ? ? ? ? ? kg.Show();
? ? ? ? ? ? Console.ReadKey();
? ? ? ? }
? ? }
}
Account.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 實(shí)訓(xùn)二
{
? ? public class Account
? ? {
? ? ? ? static long id = 100000;//靜態(tài)固然就有
? ? ? ? private long Id;//Id記錄100000
? ? ? ? public long ID
? ? ? ? {
? ? ? ? ? ? get { return Id; }
? ? ? ? }
? ? ? ? private string name1;
? ? ? ? public string Name
? ? ? ? {
? ? ? ? ? ? get { return name1; }
? ? ? ? }
? ? ? ? private string idnumber1;
? ? ? ? public string IDNumber//身份證
? ? ? ? {
? ? ? ? ? ? get { return idnumber1; }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (value.Length != 18)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? throw new MyException("身份證必須要有18位Q柚小!挠羔!");//拋出異常
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? idnumber1 = value;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private DateTime createdate1;
? ? ? ? public DateTime CreateDate//創(chuàng)建賬戶時(shí)間
? ? ? ? {
? ? ? ? ? ? get { return createdate1; }
? ? ? ? }
? ? ? ? private double money1;
? ? ? ? public double Money
? ? ? ? {
? ? ? ? ? ? get { return money1; }
? ? ? ? ? ? set { money1 = value; }
? ? ? ? }
? ? ? ? public Account(string name, string idnumber, string dt)//重載
? ? ? ? {
? ? ? ? ? ? name1 = name;
? ? ? ? ? ? idnumber1 = idnumber;
? ? ? ? ? ? createdate1 = DateTime.Parse(dt);//將字符串dt轉(zhuǎn)為DateTime
? ? ? ? ? ? Id = id++;
? ? ? ? }
? ? ? ? public Account(string name, string idnumber, string dt, double money)//重載
? ? ? ? {
? ? ? ? ? ? name1 = name;
? ? ? ? ? ? idnumber1 = idnumber;
? ? ? ? ? ? money1 = money;
? ? ? ? ? ? createdate1 = DateTime.Parse(dt);//將字符串dt轉(zhuǎn)為DateTime
? ? ? ? ? ? Id=id++;
? ? ? ? }
? ? ? ? public virtual void Query()//可以被派生類重載 查詢
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("賬戶號(hào)" + "\t" + "卡類" + "\t" + "姓名" + "\t" + "開戶日期" + "\t" + "{0,-18}" + "余額(元)", "身份證號(hào)");
? ? ? ? ? ? Console.WriteLine(ID + "\t" + "儲(chǔ)蓄卡" + "\t" + Name + "\t" + CreateDate.ToString("yyyy-MM-dd") + "\t" + "{0,-18}" + "? ? " + Money.ToString("0.00"), IDNumber);
? ? ? ? }
? ? ? ? public double Deposit(double money)//存錢
? ? ? ? {
? ? ? ? ? ? Money += money;
? ? ? ? ? ? return Money;
? ? ? ? }
? ? ? ? public double Withdraw(double money)//取錢
? ? ? ? {
? ? ? ? ? ? double temp = Money - money;
? ? ? ? ? ? if (temp < 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("{0}賬戶余額不足(余額:{1}元),取款{2}元失敗\n", Name, Money, money);//如果錢不夠
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Money = Money - money;
? ? ? ? ? ? }
? ? ? ? ? ? return Money;
? ? ? ? }
? ? }
}
exception.cs://異常
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 實(shí)訓(xùn)二
{
? ? public class MyException : Exception
? ? {
? ? ? ? public MyException(string message) : base(message)
? ? ? ? { }
? ? }
}
Card.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 實(shí)訓(xùn)二
{
? ? class Card : Account
? ? {
? ? ? ? private long id = 200000;
? ? ? ? private long Id;
? ? ? ? public long ID
? ? ? ? {
? ? ? ? ? ? get { return Id; }
? ? ? ? }
? ? ? ? private Account b;
? ? ? ? public Account BaseAccount
? ? ? ? {
? ? ? ? ? ? get { return b; }
? ? ? ? }
? ? ? ? public Card(Account BaseAccount, string name, string idnumber, string dt, double money) : base(name, idnumber, dt, money)//導(dǎo)入的和繼承的
? ? ? ? {
? ? ? ? ? ? b = BaseAccount;//b存放存儲(chǔ)卡信息
? ? ? ? ? ? Id = id++;
? ? ? ? }
? ? ? ? public void Swipe(double money)//刷卡
? ? ? ? {
? ? ? ? ? ? double temp = Money - money;
? ? ? ? ? ? if (temp < 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (BaseAccount.Money + temp < 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Console.WriteLine("借記卡和儲(chǔ)蓄卡中金額皆不足,刷卡失敗");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? BaseAccount.Withdraw(-temp);
? ? ? ? ? ? ? ? ? ? Console.WriteLine("{0}刷卡{1}元(其中:借記卡刷卡{2}元埋嵌,儲(chǔ)蓄卡賬戶刷卡{3}元)", Name, money, Money, -temp);
? ? ? ? ? ? ? ? ? ? Money = 0;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Money = temp;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public override void Query()//查詢
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(ID + "\t" + "借記卡" + "\t" + Name + "\t" + CreateDate.ToString("yyyy-MM-dd") + "\t" + "{0,-18}" + "? ? " + Money.ToString("0.00"), IDNumber);
? ? ? ? }
? ? }
}
Bank.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 實(shí)訓(xùn)二
{
? ? public class Bank
? ? {
? ? ? ? private string name1;
? ? ? ? private List<Account> queue;
? ? ? ? public string Name
? ? ? ? {
? ? ? ? ? ? get { return name1; }
? ? ? ? }
? ? ? ? /*
? ? ? ? public List<Account> Queue
? ? ? ? {
? ? ? ? ? ? get;
? ? ? ? ? ? set;
? ? ? ? }*/
? ? ? ? //List<Account> queue = new List<Account>();
? ? ? ? public List<Account> Queue
? ? ? ? {
? ? ? ? ? ? get { return queue; }
? ? ? ? ? ? set { queue = value; }
? ? ? ? }
? ? ? ? public Bank(string name)
? ? ? ? {
? ? ? ? ? ? name1 = name;
? ? ? ? ? ? Queue = new List<Account>();
? ? ? ? }
? ? ? ? public void Register(Account node)//開戶
? ? ? ? {
? ? ? ? ? ? Queue.Add(node);
? ? ? ? }
? ? ? ? public void Show()
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("\n所有賬戶信息如下:");
? ? ? ? ? ? //Console.WriteLine("賬戶號(hào)" + "\t" + "卡類" + "\t" + "姓名" + "\t" + "開戶日期" + "\t" + "{0,-18}" + "余額(元)", "身份證號(hào)");
? ? ? ? ? ? foreach (Account node in Queue)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? node.Query();
? ? ? ? ? ? ? ? //Console.WriteLine(node);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public Account this[string idnumber]
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int flag = 0;
? ? ? ? ? ? ? ? foreach (Account node in Queue)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (node.IDNumber == idnumber)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? node.Query();
? ? ? ? ? ? ? ? ? ? ? ? flag++;
? ? ? ? ? ? ? ? ? ? ? ? if (flag == 2)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? return node;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (flag == 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Console.WriteLine("沒有找到");
? ? ? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}