第二天(继承只继承属性和方法,不继承数值!!)
(一)目的
完成借记卡账户类的设计以及编写,并调试,排错等;
(二)内容
1、借记卡账户方法的设计与编写
2、借记卡账户类中属性的设计与编写
3、借记卡账户类中构造函数的设计与编写
(三)指导
Card:借记卡账户类,继承自Account:
● 包含public属性:
2.1 ID:账号,long类型,只读唯一,范围是从到200000到299999;
2.2 BaseAccount:储蓄账户,Account类型,只读;
◆ 包含public方法:
2.2 Card:构造函数,参数为BaseAccount(必须先使用索引器查询出来)、date(string类型),必须调用基类的构造函数;
2.3 Swipe:刷卡,余额不足时可使用储蓄账户。
2.4 Query:重载基类的方法。
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.Write("请输入储蓄卡开户时间:");
DateTime dt = DateTime.Parse(Console.ReadLine());
Console.Write("请输入开户人姓名:");
string name = Console.ReadLine();
Console.Write("请输入身份证号:");
string idnumber = Console.ReadLine();
Console.Write("请输入期初存款:");
double money = double.Parse(Console.ReadLine());
Account a = new Account(name, idnumber, dt, money);//构造Account a
//借记卡
Console.Write("请输入借记卡开户时间:");
dt = DateTime.Parse(Console.ReadLine());
Console.Write("请输入开户人姓名:");
name = Console.ReadLine();
Console.Write("请输入身份证号:");
idnumber = Console.ReadLine();
Console.Write("请输入期初存款:");
money = double.Parse(Console.ReadLine());
Card b = new Card(a, name, idnumber, dt, money);//构造Card b account a作为参数
Console.Write("请输入存款金额:");
money = double.Parse(Console.ReadLine());
b.Deposit(money);//借记卡存钱
Console.Write("请输入刷卡金额:");
money = double.Parse(Console.ReadLine());
b.Swipe(money);//刷卡
b.Query();//借记卡清算
a.Query();//存储卡清算
Console.ReadKey();
}
}
}
Account.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 实训二
{
public class Account
{
private long 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, DateTime dt)//重载
{
name1 = name;
idnumber1 = idnumber;
createdate1 = dt;
id++;
}
public Account(string name, string idnumber, DateTime dt, double money)//重载
{
name1 = name;
idnumber1 = idnumber;
money1 = money;
createdate1 = dt;
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)//取钱
{
Money -= money;
return Money;
}
}
}
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;
public new long ID
{
get { return id; }
}
private Account b;
public Account BaseAccount
{
get { return b; }
}*/
public Card(Account BaseAccount, string name, string idnumber, DateTime dt, double money) : base(name, idnumber, dt, money)//导入的和继承的
{
b = BaseAccount;//b存放存储卡信息
id++;
}
public void Swipe(double money)//刷卡
{
double temp = Money - money;
if (temp < 0)
{
if (BaseAccount.Money + temp < 0)
{
Console.WriteLine("借记卡和储蓄卡中金额皆不足,刷卡失败");
}
else
{
Money = 0;//借记卡钱清0
BaseAccount.Withdraw(-temp);//多的钱去存储卡中取
Console.WriteLine("借记卡中金额不足,将从储蓄卡中扣除{0}元", -temp);
}
}
else
{
Money = temp;
}
}
public override void Query()//派生类重载
{
Console.WriteLine("账户号" + "\t" + "卡类" + "\t" + "姓名" + "\t" + "开户日期" + "\t" + "{0,-18}" + "余额(元)", "身份证号");//靠左对齐18个字符
Console.WriteLine(ID + "\t" + "借记卡" + "\t" + Name + "\t" + CreateDate.ToString("yyyy-MM-dd") + "\t" + "{0,-18}" + " " + Money.ToString("0.00"), IDNumber);
}
}
}