Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 实训二
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("201705020628 王珉锴 数媒192班");
Bank kg = new Bank("人民银行");
Console.WriteLine("--------------------------------------------------------------------------------------------");
Console.WriteLine("已创建银行:{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("已增加三个账户\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 实训二
{
public class Account
{
static long id = 100000;//静态固然就有
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//创建账户时间
{
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转为DateTime
Id = id++;
}
public Account(string name, string idnumber, string dt, double money)//重载
{
name1 = name;
idnumber1 = idnumber;
money1 = money;
createdate1 = DateTime.Parse(dt);//将字符串dt转为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 实训二
{
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 实训二
{
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)//导入的和继承的
{
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 实训二
{
public class Bank
{
private string name1;
private List
public string Name
{
get { return name1; }
}
/*
public List
{
get;
set;
}*/
//List
public List
{
get { return queue; }
set { queue = value; }
}
public Bank(string name)
{
name1 = name;
Queue = new List
}
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;
}
}
}
}