C#實(shí)訓(xùn)三 復(fù)盤2021-04-08

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位?。?!");//拋出異常

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? 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("賬戶號" + "\t" + "卡類" + "\t" + "姓名" + "\t" + "開戶日期" + "\t" + "{0,-18}" + "余額(元)", "身份證號");

? ? ? ? ? ? Console.WriteLine(ID + "\t" + "儲蓄卡" + "\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存放存儲卡信息

? ? ? ? ? ? Id = id++;

? ? ? ? }

? ? ? ? public void Swipe(double money)//刷卡

? ? ? ? {

? ? ? ? ? ? double temp = Money - money;

? ? ? ? ? ? if (temp < 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? if (BaseAccount.Money + temp < 0)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? Console.WriteLine("借記卡和儲蓄卡中金額皆不足,刷卡失敗");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? BaseAccount.Withdraw(-temp);

? ? ? ? ? ? ? ? ? ? Console.WriteLine("{0}刷卡{1}元(其中:借記卡刷卡{2}元,儲蓄卡賬戶刷卡{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("賬戶號" + "\t" + "卡類" + "\t" + "姓名" + "\t" + "開戶日期" + "\t" + "{0,-18}" + "余額(元)", "身份證號");

? ? ? ? ? ? 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;

? ? ? ? ? ? }

? ? ? ? }

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容