62,628
社区成员
发帖
与我相关
我的任务
分享
/**
* 利用继承机制完成, 模拟移动的计费程序. 要求: 1. 可以满足现有业务:如"神舟行", "全球通" 等业务. 2. 现在增加一个业务"大灵通" ,
* 要求不用改变原有的计算程序. 3. 要求自定义一个类,用于处理异常情况.如余额不够,时间格式不对等.
*
* 提示: 1. 建一个抽象类, PhoneCard. 具有初始金额,余额等信息,有一个计算方法caculate() 方法,用来计算余额. 2.
* 具体业务从PhoneCard 继承,覆盖父类的caculate()方法, 计算账户余额.原有系统计算程序过程如下:
*
* for(...){ //调用phoneCard的caculate()方法,并打印信息 }
*
* 信息: 神舟行: 接.拨0.60/分 大灵通: 16元包接听,拨0.25/分 全球通: 26元座机费, 0.40/分(拨,接)
*/
public abstract class PhoneCard {
private double initMoney;
private double haveMoney;
private static String cardTypeName;
private double receiveCallTime;//接听电话时间 单位:分钟
private double sendCallTime;//拨打电话时间 单位:分钟
private static double sendPrice; //拨打电话单价 分钟为单位 * 元/分
private static double receivePrice;// 接听电话单价 分钟为单位 * 元/分
private static double monthCast;//月基本费,即月租
public PhoneCard() {
}
public PhoneCard(double initMoney) {
this.setInitMoney(initMoney);
}
public double getHaveMoney() {
return haveMoney;
}
public void setHaveMoney(double haveMoney) {
this.haveMoney = haveMoney;
}
public double getInitMoney() {
return initMoney;
}
public void setInitMoney(double initMoney) {
if (initMoney >= 0)
this.initMoney = initMoney;
else
System.out.println("InitMOney Err!"); // 相关处理
}
public static double getReceivePrice() {
return receivePrice;
}
public static void setReceivePrice(double receivePrice) {
PhoneCard.receivePrice = receivePrice;
}
public static double getSendPrice() {
return sendPrice;
}
public static void setSendPrice(double sendPrice) {
PhoneCard.sendPrice = sendPrice;
}
public double getReceiveCallTime() {
return receiveCallTime;
}
public void setReceiveCallTime(double receiveCallTime) {
this.receiveCallTime = receiveCallTime;
}
public double getSendCallTime() {
return sendCallTime;
}
public void setSendCallTime(double sendCallTime) {
this.sendCallTime = sendCallTime;
}
public abstract void caculate();
public static double getMonthCast() {
return monthCast;
}
public static void setMonthCast(double monthCast) {
PhoneCard.monthCast = monthCast;
}
public static String getCardTypeName() {
return cardTypeName;
}
protected static void setCardTypeName(String cardType){
cardTypeName=cardType;
}
}
class ShenZhouXing extends PhoneCard{
static{
setCardTypeName("神州行");
setMonthCast(0);
setSendPrice(0.6);
setReceivePrice(0.6);
}
public ShenZhouXing(){
}
public ShenZhouXing(double initMoney){
super(initMoney);
}
public void caculate(){
double cast=
ShenZhouXing.getMonthCast()
+this.getSendCallTime()*this.getSendCallTime()
+this.getSendCallTime()*super.getSendPrice();
this.setHaveMoney(getInitMoney()-cast);
System.out.println("消费金额:"+cast+" 余额:"+this.getHaveMoney());
}
}
class DaLingTong extends PhoneCard{
static{
setCardTypeName("大灵通");
setMonthCast(16);
setSendPrice(0.6);
setReceivePrice(0);
}
public DaLingTong(){
}
public DaLingTong(double initMoney){
super(initMoney);
}
public void caculate(){
double cast=
DaLingTong.getMonthCast()
+this.getSendCallTime()*this.getSendCallTime()
+this.getSendCallTime()*super.getSendPrice();
this.setHaveMoney(getInitMoney()-cast);
System.out.println("消费金额:"+cast+" 余额:"+this.getHaveMoney());
}
}
class QuanQiuTong extends PhoneCard{
static{
setCardTypeName("大灵通");
setMonthCast(16);
setSendPrice(0.6);
setReceivePrice(0);
}
public QuanQiuTong(){
}
public QuanQiuTong(double initMoney){
super(initMoney);
}
public void caculate(){
double cast=
QuanQiuTong.getMonthCast()
+this.getSendCallTime()*this.getSendCallTime()
+this.getSendCallTime()*super.getSendPrice();
this.setHaveMoney(getInitMoney()-cast);
System.out.println("消费金额:"+cast+" 余额:"+this.getHaveMoney());
}
}