1、首先创建实体类
package soso.entity;
/**
* 场景类
*
* @author DELL
*
*/
public class Scene {
// 属性
private String type;// 场景类型
private int data; // 场景消费数据
private String descripition;//场景描述
// 封装方法
public Scene() {
// TODO Auto-generated constructor stub
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public String getDescripition() {
return descripition;
}
// 构造方法
public void setDescripition(String descripition) {
this.descripition = descripition;
}
public Scene(String type, int data, String descripition) {
this.type = type;
this.data = data;
this.descripition = descripition;
}
}
package soso.entity;
/**
* 业务套餐 抽象类 包含网虫套餐,话痨套餐,超人套餐
*
* @author DELL
*
*/
public abstract class ServicePackage {
// 属性
protected double price;//套餐价格
// 封装方法
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// 构造方法
public ServicePackage() {
}
public ServicePackage(double price) {
this.price = price;
}
// 抽象方法
public abstract void showInfo();
}
package soso.entity;
/**
* 手机卡类
*
* @author DELL
*
*/
public class MobileCard {
// 属性
private String cardNumber;// 卡号
private String userName;// 用户姓名
private String passWord;// 密码
private ServicePackage servicePackage;// 所属业务套餐
private double consumAmount;// 消费金额
private double money; // 剩余金额
private int realTalkTime;// 实际通话时长
private int realSMSCount;// 实际短信条数
private int realFlow;// 实际上网流量
// 封装方法
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public ServicePackage getServicePackage() {
return servicePackage;
}
public void setServicePackage(ServicePackage servicePackage) {
this.servicePackage = servicePackage;
}
public double getConsumAmount() {
return consumAmount;
}
public void setConsumAmount(double consumAmount) {
this.consumAmount = consumAmount;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public int getRealTalkTime() {
return realTalkTime;
}
public void setRealTalkTime(int realTalkTime) {
this.realTalkTime = realTalkTime;
}
public int getRealSMSCount() {
return realSMSCount;
}
public void setRealSMSCount(int realSMSCount) {
this.realSMSCount = realSMSCount;
}
public int getRealFlow() {
return realFlow;
}
public void setRealFlow(int realFlow) {
this.realFlow = realFlow;
}
// 构造方法
public MobileCard() {
// TODO Auto-generated constructor stub
}
public MobileCard(String cardNumber, String userName, String passWord, ServicePackage servicePackage,
double consumAmount, double money) {
this.cardNumber = cardNumber;
this.userName = userName;
this.passWord = passWord;
this.servicePackage = servicePackage;
this.consumAmount = consumAmount;
this.money = money;
}
public MobileCard(String cardNumber, String userName, String passWord, ServicePackage servicePackage,
double consumAmount, double money, int realTalkTime, int realSMSCount, int realFlow) {
this.cardNumber = cardNumber;
this.userName = userName;
this.passWord = passWord;
this.servicePackage = servicePackage;
this.consumAmount = consumAmount;
this.money = money;
this.realTalkTime = realTalkTime;
this.realSMSCount = realSMSCount;
this.realFlow = realFlow;
}
// 功能方法
/**
* 显示信息
*/
public void showMeg() {
}
}
package soso.entity;
/**
* 消费记录
*
* @author DELL
*
*/
public class ConsumInfo {
// 属性
private String cardNumber;// 卡号
private String type;//
private int consumData;
// 封装方法
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getConsumData() {
return consumData;
}
public void setConsumData(int consumData) {
this.consumData = consumData;
}
// 构造方法
public ConsumInfo() {
// TODO Auto-generated constructor stub
}
public ConsumInfo(String cardNumber, String type, int consumData) {
this.cardNumber = cardNumber;
this.type = type;
this.consumData = consumData;
}
}
2、创造功能接口
package soso.service;
import soso.entity.MobileCard;
/**
* 打电话业务接口
*
* @author DELL
*
*/
public interface CallService {
// 打电话功能
int call(int minCount, MobileCard card) throws Exception;
}
package soso.service;
import soso.entity.MobileCard;
/**
* 上网业务接口
*
* @author DELL
*
*/
public interface NetService {
// 上网功能
int netPlay(int flow, MobileCard card) throws Exception ;
}
package soso.service;
import soso.entity.MobileCard;
/**
* 发短信业务接口
*
* @author DELL
*
*/
public interface SendService {
// 发短信功能
int send(int cound, MobileCard card) throws Exception;
}
3、创建业务套餐实体类的子类
package soso.impl;
import soso.entity.MobileCard;
import soso.entity.ServicePackage;
import soso.service.NetService;
/**
* 网虫套餐
*
* @author DELL
*
*/
public class NetPackage extends ServicePackage implements NetService {
// 属性
private int flow;// 流量
// 封装方法
public int getFlow() {
return flow;
}
public void setFlow(int flow) {
this.flow = flow;
}
// 重写父类封装方法获得套餐费用
@Override
public double getPrice() {
return 68;
}
@Override
public void setPrice(double price) {
super.setPrice(price);
}
// 构造方法
public NetPackage(int flow) {
this.flow = flow;
}
public NetPackage() {
this.flow = 3 * 1024;
}
public NetPackage(double price) {
super(price);
}
// 重写方法
@Override
public int netPlay(int flow, MobileCard card) {
// 重写上网接口功能 获得套餐使用详情
int temp = 0;// 实际消耗流量数据
// 循环判断使用详情
for (int i = 0; i < flow; i++) {
// 第一种情况 套餐余额充足还可以使用1MB的流量
if (this.getFlow() - card.getRealFlow() >= 1) {
card.setRealFlow(card.getRealFlow() + 1);
;// 实际流量数据+1
temp++;
} else if (card.getMoney() >= 0.1) {
// 情况二:套餐剩余流量已经用完,但是账户余额还可以支持使用1MB流量,直接使用账户余额支付
card.setRealSMSCount(card.getRealSMSCount() + 1);// 实际流量数据+1
temp++;
// 剩余金额减少0.1元
card.setMoney(card.getMoney() - 0.1);
// 总消费增加0.1元
card.setConsumAmount(card.getConsumAmount() + 0.1);
} else {
try {
throw new Exception("本次已使用" + temp + "MB的流量,您的余额已不足,请充值后在使用!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 报错结束返回一个实际流量数据
return temp;
}
}
}
return temp;// 返回一个实际流量数据
}
@Override
public void showInfo() {
System.out.println("网虫套餐:上网流量为" + (this.flow / 1024) + "GB/月,资费为68.0元/月。");
}
}
package soso.impl;
import soso.entity.MobileCard;
import soso.entity.ServicePackage;
import soso.service.CallService;
import soso.service.NetService;
import soso.service.SendService;
/**
* 超人套餐
*
* @author DELL
*
*/
public class SuperPackage extends ServicePackage implements CallService, SendService, NetService {
// 属性
private int talkTime;// 通话时长
private int smsCount;// 短信条数
private int flow;// 使用流量
// 封装方法
public int getTalkTime() {
return talkTime;
}
public void setTalkTime(int talkTime) {
this.talkTime = talkTime;
}
public int getSmsCount() {
return smsCount;
}
public void setSmsCount(int smsCount) {
this.smsCount = smsCount;
}
public int getFlow() {
return flow;
}
public void setFlow