package com.dzl.biz;
import java.util.Scanner;
import com.dzl.entityclass.MobileCard;
import com.dzl.entityclass.ServicePackage;
import com.dzl.utilitys.CardUtil;
/**
* 业务类
* @author Mr.微笑
*
*/
public class SosoMgr {
Scanner input=new Scanner(System.in);
//实例化工具类
CardUtil cardUtil=new CardUtil();
//实例化嗖嗖移动卡类
MobileCard mobileCard=new MobileCard();
ServicePackage service= null;
public static void main(String[] args) {
SosoMgr sosoMgr = new SosoMgr();
sosoMgr.start();
}
/**
* 主菜单
*/
public void start() {
cardUtil.intitScene(); //调用初始化方法
boolean isEsc=true; //循环操作
do {
System.out.println("*******************欢迎使用嗖嗖移动业务大厅*******************");
System.out.println("1.用户登录 2.用户注册 3.使用嗖嗖 4.话费充值 5.资费说明 6.退出系统");
System.out.print("请选择:");
int key=1;
try {
key = input.nextInt();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("输入有误,请重新输入!");
}
switch (key) {
case 1:
cardMenu();
continue;
case 2:
registCard();
continue;
case 3:
System.out.println("执行使用嗖嗖功能");
System.out.println("请输入手机卡号:");
String number=input.next();
cardUtil.userSoso(number); //调用使用soso
continue;
case 4:
moneyRecharge(); //调用话费充值
continue;
case 5:
cardUtil.showDescription();
// System.out.println("");
continue;
case 6:
System.out.println("谢谢使用!");
break;
default:
System.out.println("输入有误,请重新输入!");
continue;
}
} while (isEsc); //判断是否循环
}
/**
* 话费充值方法
*/
public void moneyRecharge() {
System.out.println("请输入手机卡号:");
String strNum=input.next();
boolean is=cardUtil.isExistCard(strNum);
if (is) {
System.out.println("请输入充值余额:");
double strMoney=input.nextDouble();
cardUtil.chargeMoney(strNum,strMoney);
System.out.println("充值成功,当前话费余额为"+CardUtil.cards.get(strNum).getMoney()+"元");
}else {
System.out.println("手机输入有误,请重新输入手机号:");
}
}
/**
* 二级菜单
*/
public void cardMenu() {
System.out.println("请输入手机卡号:");
String numberMoney=input.next();
System.out.println("请输入密码:");
String pwd=input.next();
if (cardUtil.isExistCard(numberMoney,pwd)) {
System.out.println("登录成功");
}else {
return;
}
boolean isEsc=true; //循环操作
do {
System.out.println("******嗖嗖移动用户菜单******");
System.out.println("1.本月账单查询");
System.out.println("2.套餐余量查询");
System.out.println("3.打印消费详单");
System.out.println("4.套餐变更");
System.out.println("5.办理退网");
System.out.print("请选择(输入1~5选择功能,其他键返回上一级):");
int key=1;
try {
key = input.nextInt();
} catch (Exception e) {
new SosoMgr().start();
}
switch (key) {
case 1:
cardUtil.showAmountDetail(numberMoney); //调用本月账单查询
continue;
case 2:
cardUtil.showRemainDetail(numberMoney); //调用套餐余量查询
continue;
case 3:
cardUtil.printAmountDetail(numberMoney); //调用打印消费详单
continue;
case 4:
cardUtil.chargeingPack(numberMoney);
continue;
case 5:
cardUtil.delCard(numberMoney); //办理退网
continue;
default:
isEsc = false;
new SosoMgr().start();
}
} while (isEsc); // 判断是否循环
}
/**
* 用户注册流程
*/
public void registCard() {
System.out.println("************可选择的卡号************");
//通过超级循环遍历输出卡号
String[] cardNumbers = cardUtil.getNewNumber(9);
for (int i = 0; i = 1 && key <= 9) {
mobileCard.setCardNumber(cardNumbers[key - 1]);
System.out.println("1.话唠套餐 2.网虫套餐 3.超人套餐 , 请选择套餐(输入序号):");
} else {
System.out.print("输入错误!请输入(1~9)的整数:");
}
boolean bol = true;
int packageNum = input.nextInt();
while (bol) {
if (packageNum<=3 && packageNum>=1) {
service= cardUtil.createPackage(packageNum);
mobileCard.setSerPackage(service); //
bol = false;
} else {
System.out.println("输入错误,请重新选择:");
packageNum = input.nextInt();
}
}
System.out.println("请输入姓名:");
String userName = input.next();
System.out.println("请输入密码:");
String passWord = input.next();
System.out.println("请输入预存话费:");
double money=input.nextInt();
while (money
package com.dzl.common;
import java.text.DecimalFormat;
/**
* 公共类
* @author Mr.微笑
*
*/
public class Common {
/**
* double类型格式化方法
* @param data
* @return
*/
public static String dataFormat(double data) {
DecimalFormat format=new DecimalFormat("#0.00");
return format.format(data);
}
/**
* 类型两数相减
* @param sum1
* @param sum2
* @return
*/
public static double sub(double sum1, double sum2) {
return (sum1*10-sum2*10)/10;
}
}
package com.dzl.entityclass;
/**
* 实体:消费信息类
* @author Mr.微笑
*
*/
public class ConsumInfo {
//卡号
private String number;
//消费类型
private String type;
//短信条数 上网(MB)
private int consumData;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
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(String number, String type, int note) {
this.number = number;
this.type = type;
this.consumData = note;
}
}
package com.dzl.entityclass;
/**
* 实体:嗖嗖移动卡类
* @author Mr.微笑
*
*/
public class MobileCard {
// 卡号
private String cardNumber;
// 用户名
private String userName;
//密码
private String passWord;
//所属套餐
private ServicePackage serPackage;
//当月消费金额
private double consumAmout;
//账户余额
private double money;
//当月实际通话时长
private int realTakTime;
//当月实际发送短信条数
private int realSMSCount;
//当月实际上网流量
private int realFlow;
public MobileCard(){}
/**
* 有参构造
* @param cardNumber 卡号
* @param userName 名字
* @param passWord 密码
* @param serPackage 所属套餐
* @param consumAmout 当月消费金额
*/
public MobileCard(String cardNumber, String userName, String passWord, ServicePackage serPackage, double money){
this.cardNumber = cardNumber;
this.userName = userName;
this.passWord = passWord;
this.serPackage = serPackage;
this.money = money;
}
/**
* 初始化用户信息
* @param cardNumber 卡号
* @param userName 名字
* @param passWord 密码
* @param serPackage 所属套餐
* @param consumAmout 当月消费金额
* @param money 账户余额
* @param realTakTime 当月实际通话时长
* @param realSMSCount 当月实际发送短信条数
* @param realFlow 当月实际上网流量
*/
public MobileCard(String cardNumber, String userName, String passWord, ServicePackage serPackage,
double consumAmout, double money, int realTakTime, int realSMSCount, int realFlow) {
this.cardNumber = cardNumber;
this.userName = userName;
this.passWord = passWord;
this.serPackage = serPackage;
this.consumAmout = consumAmout;
this.money = money;
this.realTakTime = realTakTime;
this.realSMSCount = realSMSCount;
this.realFlow = realFlow;
}
/**
* 卡号
* @return
*/
public String getCardNumber() {
return cardNumber;
}
/**
* 卡号
* @return
*/
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
/**
* 用户名
* @return
*/
public String getUserName() {
return userName;
}
/**
* 用户名
* @return
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* 密码
* @return
*/
public String getPassWord() {
return passWord;
}
/**
* 密码
* @return
*/
public void setPassWord(String passWord) {
this.passWord = passWord;
}
/**
* 所属套餐
* @return
*/
public ServicePackage getSerPackage() {
return serPackage;
}
/**
* 所属套餐
* @return
*/
public void setSerPackage(ServicePackage serPackage) {
this.serPackage = serPackage;
}
/**
* 当月消费金额
* @return
*/
public double getConsumAmout() {
return consumAmout;
}
/**
* 当月消费金额
* @return
*/
public void setConsumAmout(double consumAmout) {
this.consumAmout = consumAmout;
}
/**
* 账户余额
* @return
*/
public double getMoney() {
return money;
}
/**
* 账户余额
* @return
*/
public void setMoney(double money) {
this.money = money;
}
/**
* 当月实际通话时长
* @return
*/
public int getRealTakTime() {
return realTakTime;
}
/**
* 当月实际通话时长
* @return
*/
public void setRealTakTime(int realTakTime) {
this.realTakTime = realTakTime;
}
/**
* 当月实际发送短信条数
* @return
*/
public int getRealSMSCount() {
return realSMSCount;
}
/**
* 当月实际发送短信条数
* @return
*/
public void setRealSMSCount(int realSMSCount) {
this.realSMSCount = realSMSCount;
}
/**
* 当月实际上网流量
* @return
*/
public int getRealFlow() {
return realFlow;
}
/**
* 当月实际上网流量
* @param realFlow 上网流量
*/
public void setRealFlow(int realFlow) {
this.realFlow = realFlow;
}
/**
* 嗖嗖移动卡信息
*/
public void showMeg() {
System.out.println("卡号:"+this.getCardNumber()+"用户名:"+this.getUserName()+"当前余额:"+this.getMoney()+"元");
this.serPackage.showInfo();
}
}
package com.dzl.entityclass;
import com.dzl.interfaces.NetService;
/**
* 实体:网虫套餐类
* @author Mr.微笑
*
*/
public class NetPackage extends ServicePackage implements NetService {
/**
* 上网流量
*/
private int flow;
/**
* 套餐月资费
*/
private double price;
/**
* 无参构造 初始化
*/
public NetPackage() {
this.flow =3;
this.price = 68;
}
/**
* 上网流量
*/
public int getFlow() {
return flow;
}
/**
* 上网流量
*/
public void setFlow(int flow) {
this.flow = flow;
}
/**
* 套餐月资费
*/
public double getPrice() {
return price;
}
/**
* 套餐月资费
*/
public void setPrice(double price) {
this.price = price;
}
/**
* 重写父类方法
*/
public void showInfo() {
System.out.println("网虫套餐:上网流量为:" + this.getFlow()
+ "GB/月," + "资费为:" + this.getPrice() + "元/月");
}
/**
* @throws Exception
*
*/
public int netPlay(int flow, MobileCard card) throws Exception {
int temp = flow;
for(int i = 0; i < flow; i++){
if(this.flow - card.getRealFlow() >= 1){
//第一种情况:流量剩余够 1 MB
card.setRealFlow(card.getRealFlow() + 1);
}else if(card.getMoney() >= 0.1){
//第二种情况:套餐内流量已经用完,剩下话费够支付 1 MB的流量
card.setRealFlow(card.getRealFlow() + 1); //实际短信数加 1 条
//账户余额消费0.1元,(1MB流量)
card.setMoney(card.getMoney() - 0.1);
card.setConsumAmout(card.getConsumAmout() + 0.1); //当月消费金额 + 0.1
}else{
temp = i; //记录使用流量多少MB
throw new Exception("流量已经使用" + i + "MB,您的余额不足,请充值后再使用!");
//System.err.println("流量已经使用" + i + "MB,您的余额不足,请充值后再使用!");
}
}
return temp;
}
}
package com.dzl.entityclass;
/**
* 实体:使用场景类
* @author Mr.微笑
*
*/
public class Scene {
private String type; //场景类型
private int data; //场景消费数据
private String description; //场景描述
public Scene() {
super();
}
public Scene(String type, int data, String description) {
this.type = type;
this.data = data;
this.description = description;
}
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 getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package com.dzl.entityclass;
/**
* 实体父类:品牌套餐类
* @author Mr.微笑
*
*/
public abstract class ServicePackage {
//月资费
private double price;
/**
* 月资费
* @return 月资费
*/
public double getPrice() {
return price;
}
/**
* 月资费
* @param price 月资费
*/
public void setPrice(double price) {
this.price = price;
}
public abstract void showInfo();
}
package com.dzl.entityclass;
import com.dzl.interfaces.CallService;
import com.dzl.interfaces.NetService;
import com.dzl.interfaces.SendService;
/**
* 实体:超人套餐类
* @author Mr.微笑
*
*/
public class SuperPackage extends ServicePackage implements CallService,SendService,NetService{
//通话时长
private int talkTime;
//短信条数
private int smsCount;
//上网流量
private int flow;
//套餐月资费
private double price;
public SuperPackage() {
this.talkTime = 200;
this.smsCount = 50;
this.flow = 1;
this.price=78;
}
/**
* 通话时长
* @return
*/
public int getTalkTime() {
return talkTime;
}
/**
* 通话时长
* @return
*/
public void setTalkTime(int talkTime) {
this.talkTime = talkTime;
}
/**
* 短信条数
* @return
*/
public int getSmsCount() {
return smsCount;
}
/**
* 短信条数
* @return
*/
public void setSmsCount(int smsCount) {
this.smsCount = smsCount;
}
/**
* 上网流量
* @return
*/
public int getFlow() {
return flow;
}
/**
* 上网流量
* @return
*/
public void setFlow(int flow) {
this.flow = flow;
}
/**
* 套餐月资费
* @return
*/
public double getPrice() {
return price;
}
/**
* 套餐月资费
* @return
*/
public void setPrice(double price) {
this.price = price;
}
/**
* 输出超人套餐信息
*/
public void showInfo() {
System.out.println("超人套餐:通话时长为"+this.getTalkTime()+"分钟/月,"
+ "短信条数为"+this.getSmsCount()+"条/月,上网流量为"
+this.getFlow()+"GB/月,资费"+this.getPrice()+"/月。");
}
@Override
//返回通话时长
public int call(int minCount, MobileCard card) throws Exception{
int temp = minCount;
for(int i = 0; i < minCount; i++){
if(this.talkTime - card.getRealTakTime() >= 1){
//第一种情况:套餐剩余通话时长可以支持1分钟通话
card.setRealTakTime(card.getRealTakTime() + 1);
}else if(card.getMoney() >= 0.2){
//第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付
card.setRealTakTime(card.getRealTakTime() + 1); //实际使用通话时长1分钟
//账户余额消费0.2元(1分钟 额外通话)
card.setMoney(card.getMoney() - 0.2);
card.setConsumAmout(card.getConsumAmout() + 0.2); //当月消费金额 + 0.2
}else{
temp = i; //记录实际通话分钟数
throw new Exception("本次已经通话" + i + "分钟,您的余额不足,请充值后再使用!");
//System.err.println("本次已经通话" + i + "分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
@Override
//返回使用流量数
public int netPlay(int flow, MobileCard card) throws Exception {
int temp = flow;
for(int i = 0; i < flow; i++){
if(this.flow - card.getRealFlow() >= 1){
//第一种情况:流量剩余够 1 MB
card.setRealFlow(card.getRealFlow() + 1);
}else if(card.getMoney() >= 0.1){
//第二种情况:套餐内流量已经用完,剩下话费够支付 1 MB的流量
card.setRealFlow(card.getRealFlow() + 1); //实际短信数加 1 条
//账户余额消费0.1元,(1MB流量)
card.setMoney(card.getMoney() - 0.1);
card.setConsumAmout(card.getConsumAmout() + 0.1); //当月消费金额 + 0.1
}else{
temp = i; //记录使用流量多少MB
throw new Exception("流量已经使用" + i + "MB,您的余额不足,请充值后再使用!");
}
}
return temp;
}
@Override
//返回实际发送短信条数
public int send(int count, MobileCard card) throws Exception {
int temp = count;
for(int i = 0; i < count; i++){
if(this.smsCount - card.getRealSMSCount() >= 1){
//第一种情况:套餐剩余短信数能够发送一个短信
card.setRealSMSCount(card.getRealSMSCount() + 1);
}else if(card.getMoney() >= 0.1){
//第二种情况:套餐内短信已经用完,剩下话费能够允许发一条短信
card.setRealSMSCount(card.getRealSMSCount() + 1); //实际短信数加 1 条
//账户余额消费0.1元,(一条短信)
card.setMoney(card.getMoney() - 0.1);
card.setConsumAmout(card.getConsumAmout() + 0.1); //当月消费金额 + 0.1
}else{
temp = i; //记录发短信条数
throw new Exception("短信已经发送" + i + "条,您的余额不足,请充值后再使用!");
}
}
return temp;
}
}
package com.dzl.entityclass;
import com.dzl.common.Common;
import com.dzl.interfaces.CallService;
import com.dzl.interfaces.SendService;
/**
* 实体:话唠套餐类
* @author Mr.微笑
*
*/
public class TalkPackage extends ServicePackage implements CallService,SendService {
//通话时长
private int talkTime;
//短信条数
private int smsCount;
//套餐月资费
private double price;
public TalkPackage() {
this.talkTime = 500;
this.smsCount = 30;
this.price=58;
}
/**
* 通话时长
* @return
*/
public int getTalkTime() {
return talkTime;
}
/**
* 通话时长
* @return
*/
public void setTalkTime(int talkTime) {
this.talkTime = talkTime;
}
/**
* 短信条数
* @return
*/
public int getSmsCount() {
return smsCount;
}
/**
* 短信条数
* @return
*/
public void setSmsCount(int smsCount) {
this.smsCount = smsCount;
}
/**
* 套餐月资费
* @return
*/
public double getPrice() {
return price;
}
/**
* 套餐月资费
* @return
*/
public void setPrice(double price) {
this.price = price;
}
/**
* 输出话唠套餐信息
*/
public void showInfo() {
System.out.println("话唠套餐:通话时长为:" + this.getTalkTime() + "分钟/月,短信条数为:"
+ this.getSmsCount() + "条/月,资费为:"
+ this.getPrice() + "元/月" );
}
@Override
public int send(int count, MobileCard card) throws Exception {
int temp = count;
for(int i = 0; i < count; i++){
if(this.smsCount - card.getRealSMSCount() >= 1){
card.setRealSMSCount(card.getRealSMSCount() + 1);
}else if(card.getMoney() >= 0.1){
card.setRealSMSCount(card.getRealSMSCount() + 1); //实际短信数加 1 条
//账户余额消费0.1元,(一条短信)
card.setMoney(card.getMoney() - 0.1);
card.setConsumAmout(card.getConsumAmout() + 0.1); //当月消费金额 + 0.1
}else{
temp = i; //记录发短信条数
throw new Exception("短信已经发送" + i + "条,您的余额不足,请充值后再使用!");
//System.err.println("短信已经发送" + i + "条,您的余额不足,请充值后再使用!");
}
}
return temp;
}
@Override
public int call(int minCount, MobileCard card) throws Exception{
int temp=minCount;
for (int i = 0; i < minCount; i++) {
if (this.talkTime-card.getRealTakTime()>=1) {
card.setRealTakTime(card.getRealTakTime()+1); //实际通话数据加1
}else if (card.getMoney()>=0.2) {
card.setRealTakTime(card.getRealTakTime()+1); // 实际通话时长1分钟
//账户余额消费0.2元(1分钟 额外通话)
card.setMoney(Common.sub(card.getMoney(),0.2));
card.setConsumAmout(card.getConsumAmout()+0.2);
}else {
temp=i;
throw new Exception("本次通话"+i+"分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
}
package com.dzl.interfaces;
import com.dzl.entityclass.MobileCard;
/**
* 服务接口
* @author Mr.微笑
*
*/
public interface CallService {
/**
* 通话
* @param minCount
* @param card
* @return
* @throws Exception
*/
public int call(int minCount,MobileCard card) throws Exception;
}
package com.dzl.interfaces;
import com.dzl.entityclass.MobileCard;
/**
* 网络服务接口
* @author Mr.微笑
*
*/
public interface NetService {
/**
* 上网
* @param flow
* @param card
* @return
* @throws Exception
*/
public int netPlay(int flow,MobileCard card) throws Exception;
}
package com.dzl.interfaces;
import com.dzl.entityclass.MobileCard;
/**
* 发送服务接口
* @author Mr.微笑
*
*/
public interface SendService {
/**
* 短信
* @param count
* @param card
* @return
* @throws Exception
*/
public int send(int count,MobileCard card) throws Exception;
}
package com.dzl.utilitys;
import java.awt.SystemTray;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import com.dzl.common.Common;
import com.dzl.entityclass.ConsumInfo;
import com.dzl.entityclass.MobileCard;
import com.dzl.entityclass.NetPackage;
import com.dzl.entityclass.Scene;
import com.dzl.entityclass.ServicePackage;
import com.dzl.entityclass.SuperPackage;
import com.dzl.entityclass.TalkPackage;
import com.dzl.interfaces.CallService;
import com.dzl.interfaces.SendService;
/**
* 工具类
*
* @author Mr.微笑
*
*/
public class CardUtil {
Scanner input=new Scanner(System.in);
// 已注册嗖嗖移动用户列表
public static Map cards = new HashMap();
// 所有卡号的消费记录列表
public static Map> consumInfos = new HashMap>();
//使用场景列表
Map scenes = new HashMap();
Scene scene0 = new Scene("通话", 90, "问候客户,谁知其如此难缠,通话90分钟");
Scene scene1 = new Scene("通话", 30, "询问妈妈身体状况,本地通话30分钟");
Scene scene2 = new Scene("短信", 5, "参与环境保护实施方案问卷调查,发送短信5条");
Scene scene3 = new Scene("短信", 50, "通知朋友手机换号,发送短信50条");
Scene scene4 = new Scene("上网", 1024, "和女朋友用微信视频聊天,使用流量1GB");
Scene scene5 = new Scene("上网", 2 * 1024, "晚上手机在线看韩剧,不留神睡着啦!使用2GB");
/**
* 用户初始化
*/
public void intitScene() {
MobileCard hl=new MobileCard("15815891967", "微笑", "123456", new TalkPackage(), 88, 100, 89, 88, 90); //话唠套餐
MobileCard wc=new MobileCard("12345678910", "微笑", "123456", new NetPackage(),99, 200, 200, 60, 20); //网虫套餐
MobileCard cr=new MobileCard("13812345678", "微笑", "123456", new SuperPackage(), 100, 300, 50, 20, 900); //超人套餐
CardUtil.cards.put("15815891967", hl); //添加到集合里
CardUtil.cards.put("12345678910", wc); //添加到集合里
CardUtil.cards.put("13812345678", cr); //添加到集合里
ConsumInfo info=new ConsumInfo("12345678910", "电话", 500);
List list=new ArrayList();
list.add(info);
consumInfos.put(info.getNumber(), list);
}
/**
* 注册新卡
*
* @param card
*/
public void addCard(MobileCard card) {
cards.put(card.getCardNumber(), card);
}
/**
* 话费充值
*
* @param number
* @param money
*/
public void chargeMoney(String number, double money) {
cards.get(number).setMoney(cards.get(number).getMoney()+money);
}
/**
* 使用搜搜
*
* @param number
*/
public void userSoso(String number) {
//添加场景Map集合的键值对
scenes.put(0, scene0);
scenes.put(1, scene1);
scenes.put(2, scene2);
scenes.put(3, scene3);
scenes.put(4, scene4);
scenes.put(5, scene5);
MobileCard card=cards.get(number); //获取此卡对象
ServicePackage pack=card.getSerPackage(); //获取此卡所属套餐
Random random=new Random();
int ranNum=0;
int temp=0; //记录个场景中实际消费
do {
ranNum=random.nextInt(6);
Scene scene=scenes.get(ranNum); //获取该序号所对应的场景
switch (ranNum) {
case 0:
case 1:
//序号为0或1为通话场景
//判断该卡所属套餐是否支持通话功能
if (pack instanceof CallService) {
//执行通话方法
System.out.println(scene.getDescription());
CallService callService=(CallService)pack;
try {
temp=callService.call(scene.getData(), card);
} catch (Exception e) {
e.printStackTrace();
}
//添加一条消费记录
addConsumInfo(number, new ConsumInfo(number, scene.getType(), temp));
break;
}else {
//如果该卡套餐不支持通话功能,则重新生成随机数选择其他场景
continue;
}
case 2:
case 3:
//判断该卡所属套餐是否支持短信功能
if (pack instanceof SendService) {
//执行短信方法
System.out.println(scene.getDescription());
SendService sendService=(SendService)pack;
try {
temp=sendService.send(scene.getData(), card);
} catch (Exception e) {
e.printStackTrace();
}
//添加一条消费记录
addConsumInfo(number, new ConsumInfo(number, scene.getType(), temp));
break;
}else {
//如果该卡套餐不支持通话功能,则重新生成随机数选择其他场景
continue;
}
case 4:
case 5:
//判断该卡所属套餐是否支持上网功能
if (pack instanceof NetPackage) {
//执行上网方法
System.out.println(scene.getDescription());
NetPackage netPackage=(NetPackage)pack;
try {
temp=netPackage.netPlay(scene.getData(), card);
} catch (Exception e) {
e.printStackTrace();
}
//添加一条消费记录
addConsumInfo(number, new ConsumInfo(number, scene.getType(), temp));
break;
}else {
//如果该卡套餐不支持通话功能,则重新生成随机数选择其他场景
continue;
}
}
break;
} while (true);
}
/**
* 资费说明
*/
public void showDescription() {
FileReader fr = null;
BufferedReader br = null;
try {
//创建一个FileReader对象
fr = new FileReader("D:/MyFile/MyMobileBusinessLobby/src/com/file_txt/套餐资费说明.txt");
//创建一个BufferedReader对象
br = new BufferedReader(fr);
//读取一行数据
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(br != null){
br.close();
}
if(fr != null){
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 本月账单查询
*
* @param number
*/
public void showAmountDetail(String number) {
StringBuffer meg = new StringBuffer();
MobileCard card=cards.get(number); //要查询的卡
meg.append("您的卡号:"+card.getCardNumber()+"当月账单:\n");
meg.append("套餐资费:"+card.getSerPackage().getPrice()+"元\n");
meg.append("合计:"+Common.dataFormat(card.getConsumAmout())+"元\n");
meg.append("账号余额:"+Common.dataFormat(card.getMoney())+"元");
//显示本月消费详细信息
System.out.println(meg);
}
/**
* 套餐余量查询
*
* @param number
*/
public void showRemainDetail(String number) {
MobileCard card=cards.get(number);
int remainTalkTime;
int remainSmsCount;
double remainFlow;
StringBuffer meg=new StringBuffer();
meg.append("您的卡号是"+number+",套餐内剩余:\n");
ServicePackage pack=card.getSerPackage();
if (pack instanceof TalkPackage) {
//向下转型为话唠套餐对象
TalkPackage cardPack=(TalkPackage)pack;
//话唠套餐,查询套餐内剩余的通话时长和短信条数
remainTalkTime=cardPack.getTalkTime()>card.getRealTakTime()?cardPack.getTalkTime()
-card.getRealTakTime():0;
meg.append("通话时长:"+remainTalkTime+"分钟\n");
remainSmsCount=cardPack.getSmsCount()>card.getRealSMSCount()?cardPack.getSmsCount()
-card.getRealSMSCount():0;
meg.append("短信条数:"+remainSmsCount+"条");
}else if (pack instanceof NetPackage) {
//向下转型为网虫套餐的信息
NetPackage netPackage=(NetPackage)pack;
remainFlow=netPackage.getFlow()*1024>card.getRealFlow()?netPackage.getFlow()*1024
-card.getRealFlow():0;
meg.append("上网流量:"+Common.dataFormat(remainFlow/1024)+"GB");
}else {
SuperPackage superPackage=(SuperPackage)pack;
remainTalkTime=superPackage.getTalkTime()>card.getRealTakTime()?superPackage.getTalkTime()
-card.getRealTakTime():0;
meg.append("通话时长:"+remainTalkTime+"分钟\n");
remainSmsCount=superPackage.getSmsCount()>card.getRealSMSCount()?superPackage.getSmsCount()
-card.getRealSMSCount():0;
meg.append("短信条数:"+remainSmsCount+"条");
remainFlow=superPackage.getFlow()*1024>card.getRealFlow()?superPackage.getFlow()*1024
-card.getRealFlow():0;
meg.append("上网流量:"+Common.dataFormat(remainFlow/1024)+"GB");
}
System.out.println(meg);
}
/**
* 打印消费详单
* @param number
*/
public void printAmountDetail(String number){
Writer fileWriter = null;
try {
fileWriter = new FileWriter("D:/MyFile/MyMobileBusinessLobby/src/com/file_txt/consumes.txt");
Set numbers = consumInfos.keySet();
Iterator it = numbers.iterator();
List infos = new ArrayList();
infos = consumInfos.get(number);
//存储指定卡的所有消费记录
//现有消费列表中是否存在此卡号的消费记录,是:true 否:false
boolean isExist = false;
while(it.hasNext()){
String numberKey = it.next();
if(number.equals(numberKey)){
isExist = true;
}/*else{ //如果
isExist = false;
}*/
}
if(isExist){
StringBuffer content = new StringBuffer("***********" + number + "消费记录************\n");
content.append("序号\t类型\t数据(通话(分钟)/上网(MB)/短信(条))\n");
for(int i = 0; i < infos.size(); i++){
ConsumInfo info = infos.get(i);
System.out.println((i + 1) + ".\t" + info.getType() + "\t" + info.getConsumData() + "\n");
content.append((i + 1) + ".\t" + info.getType() + "\t" + info.getConsumData() + "\n");
}
fileWriter.write(content.toString());
fileWriter.flush();
System.out.println("消息记录打印完毕!");
}else{
System.out.println("对不起,不存在此号码的消费记录,不能够打印!");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 套餐变更
*
* @param number
*/
public void chargeingPack(String number) {
System.out.println("1.话唠套餐 2.网虫套餐 3.超人套餐 :请选择(序号):");
int packNum = input.nextInt();
switch(packNum){
case 1: //选择变更的套餐为话唠套餐
if(cards.get(number).getSerPackage() instanceof TalkPackage){
System.out.println("对不起,您已经是该套餐用户,无需换套餐!");
}else{
if(cards.get(number).getMoney() < cards.get(number).getSerPackage().getPrice()){//如果剩余费用不够支持新的套餐费用
System.out.println("对不起,您的的余额不足以支付新的套餐本月资费,请充值后再办理业务!");
}else{
cards.get(number).setRealSMSCount(0);
cards.get(number).setRealTakTime(0);
cards.get(number).setRealFlow(0);
cards.get(number).setSerPackage(createPackage(packNum));
System.out.println("套餐更换成功!" );
// Common.talkPackage.showInfo();
createPackage(packNum).showInfo();
}
}
break;
case 2: //选择变更的套餐为网虫套餐
if(cards.get(number).getSerPackage() instanceof NetPackage){
System.out.println("对不起,您已经是该套餐用户,无需换套餐!");
}else{
if(cards.get(number).getMoney() < cards.get(number).getSerPackage().getPrice()){//如果剩余费用不够支持新的套餐费用
System.out.println("对不起,您的的余额不足以支付新的套餐本月资费,请充值后再办理业务!");
}else{
cards.get(number).setRealSMSCount(0);
cards.get(number).setRealTakTime(0);
cards.get(number).setRealFlow(0);
cards.get(number).setSerPackage(createPackage(packNum));
System.out.println("套餐更换成功!" );
// Common.netPackage.showInfo();
createPackage(packNum).showInfo();
}
}
break;
case 3://选择变更的套餐为超人套餐
if(cards.get(number).getSerPackage() instanceof SuperPackage){
System.out.println("对不起,您已经是该套餐用户,无需换套餐!");
}else{
if(cards.get(number).getMoney() < cards.get(number).getSerPackage().getPrice()){//如果剩余费用不够支持新的套餐费用
System.out.println("对不起,您的的余额不足以支付新的套餐本月资费,请充值后再办理业务!");
}else{
cards.get(number).setRealSMSCount(0);
cards.get(number).setRealTakTime(0);
cards.get(number).setRealFlow(0);
cards.get(number).setSerPackage(createPackage(packNum));
System.out.println("套餐更换成功!" );
// Common.superPackage.showInfo();
createPackage(packNum).showInfo();
}
}
break;
}
}
/**
* 办理退网
*
* @param number
*/
public void delCard(String number) {
if (cards.containsKey(number)) {
cards.remove(number); //移除手机号
System.out.println("卡号:"+number+"办理退网成功!");
System.out.println("谢谢使用!");
}else {
System.out.println("手机号码输入有误!");
}
}
/**
* 根据卡号和密码验证该卡是否验证
*
* @param number
* @param pwd
* @return
*/
public boolean isExistCard(String number, String pwd) {
if (cards.containsKey(number)&&(cards.get(number).getPassWord().equals(pwd))) {
return true;
}
System.out.println("登录失败!");
return false;
}
/**
* 根据卡号验证该卡是否验证 话费充值
*
* @param number
* @return
*/
public boolean isExistCard(String number) {
Set numbers=cards.keySet();
Iterator iterator=numbers.iterator();
while (iterator.hasNext()) {
String str=iterator.next();
if (str.equals(number))
return true;
}
return false;
}
/**
* 生成随机卡号
*
* @return 随机卡号
*/
public String createNumber(){
Random random = new Random();
//记录现有用户中是否存在此卡号用户 是:true 否:false
boolean isExist = false;
String number = "";
int temp = 0;
do{
isExist = false;//标志位重置为false,用于控制外重循环
//生成的随机数是8位,不能小于10000000,否则重新生成
//回头重写这段代码,执行效率太低
do{
temp = random.nextInt(100000000);
}while(temp < 10000000);
//生成之前,前面加“139”
number = "139" + temp;
//和现有用户的卡号比较,不能是重复的
if(cards != null){ //价格判断 否则 下方的一句会报空指针异常
Set cardNumbers = cards.keySet();
for(String cardNumber : cardNumbers){
if(number.equals(cardNumber)){
isExist = true;
break;
}
}
}
}while(isExist);
return number;
}
/**
* 生成指定个数卡号列表
*
* @param count
* @return
*/
public String[] getNewNumber(int count) {
String[] strs = new String[count];
for (int i = 0; i < count; i++) {
strs[i] = createNumber(); //通过随机生成给strs[i]赋值
}
return strs ;
}
/**
* 添加指定卡号的消费记录
*
* @param number
* @param info
*/
public void addConsumInfo(String number, ConsumInfo info) {
if (consumInfos.containsKey(number)) {
consumInfos.get(number).add(info);
}else{
List list = new ArrayList();
list.add(info);
consumInfos.put(number, list);
}
}
/**
* 根据用户选择的套餐序列返回套餐对象
*
* @param packId 套餐序列号
* @return
*/
public ServicePackage createPackage(int packId) {
ServicePackage servicePackage=null;
switch (packId) {
case 1:
servicePackage=new TalkPackage();
break;
case 2:
servicePackage=new NetPackage();
break;
case 3:
servicePackage=new SuperPackage();
break;
default:
break;
}
return servicePackage;
}
}