嗖嗖移动大厅业务的整体架构思路和代码实现

整体架构思路:
分为6个功能区,使用switch case结构包围,分别是:
1.用户登录
2.用户注册
3.使用嗖嗖
4.话费充值
5.资费说明
6.退出系统

 

 

功能区模块:
1.用户登录:用if else结构整体判断是否存在该用户
如果存在:调用用户登录的方法:
此时有5个业务分支,用switch case结构:
1.本月账单查询
输出本月消费详细信息
2.套餐余量查询
分别判断用户属于三种套餐的哪一种,
然后输出套餐余量
3.打印消费详单
用到字符流写出的技能;
判断是否存在消费记录,如果不存在,则提示不存在此卡的消费记录;
如果存在,则利用Filewriter字符流写出记录,
4.套餐变更
判断用户是哪个套餐,如果是本套餐:
提示用户已经是本套餐,不需要换;
如果是其他套餐:
判断用户余额是否大于要换的套餐,如果余额不够,提示用户余额不足以支付新套餐的费用,让其充值,如果余额够,则用card.set属性接收新套餐。
5.办理退网
判断卡号是否存在,如果不存在,提示用户卡号不存在,不能退网!
如果存在使用remove()方法移除卡号

如果不存在:输入:对不起您输入的信息有误

 

2.用户注册
1.使用集合存放9个号码,通过下标定位用户选择的号码
根据需求,9个号码每行三列,用数组下标对3取余的方式 实现换行
2.选择套餐类型,三个类型,话痨,网虫,超人,(三个套餐各建一个类来实现具体方法)
3.输入用户名,密码,并接收
4.输入预存话费金额,此时要判断用户存的钱 是否小于月套餐资费,如果小于月套餐资费,提示用户预存的话费金额不足,让用户继续充值。
5.最后创建新卡,并提示用户卡号,用户名,当前余额等信息。


3.使用嗖嗖
也要先判断用户是否存在,存在的话,开始实现搜搜功能,此时利用生成随机数1-5,生成5个不同场景。

如果不存在 提示用户注册

4.话费充值

判断卡号是否存在,如果存在,开始充话费,此卡的金额信息 接收存入的金额,这里面可以细化,判断用户冲多少钱,如果小于50,提示用户重新充值,必须大于50元起充。
如果不存在,提示用户注册


5.资费说明 
此功能块,用到文档的读写相关技能,使用字符流FileReader读入资费说明的txt文档。


6.退出系统
输入“谢谢使用!”

  1 public class SosoMgr {
  2     Scanner input = new Scanner(System.in);
  3     CardUtil utils = new CardUtil();
  4     
  5     public static void main(String[] args) {
  6         SosoMgr soso = new SosoMgr();
  7         soso.mainMenu();
  8         System.out.println("谢谢使用!");
  9     }
 10 
 11     /**
 12      * 主流程
 13      */
 14     public void mainMenu() {
 15         int menuChoose = 0;
 16         String mobileNumber= "";
 17         String password = "";
 18         utils.init();
 19         utils.initScenes();
 20         //Common.typesInit();
 21         do {
 22             System.out.println("\n*************欢迎使用嗖嗖移动业务大厅***************");
 23             System.out.println("1.用户登录   2.用户注册   3.使用嗖嗖   4.话费充值  5.资费说明  6.退出系统");
 24             System.out.print("请选择:");
 25             menuChoose = input.nextInt();
 26             // 分支语句:根据功能编号执行相应功能
 27             switch (menuChoose) {
 28             case 1:
 29                 //用户登录
 30                 System.out.print("请输入手机卡号:");
 31                 mobileNumber = input.next();
 32                 System.out.print("请输入密码:");
 33                 password = input.next();
 34                 if (utils.isExistCard(mobileNumber, password)) {
 35                     cardMenu(mobileNumber);
 36                 }else{
 37                     System.out.println("对不起,您输入的信息有误,无法登录!");
 38                 }
 39                 continue;
 40             case 2:
 41                 //用户注册
 42                 registCard();
 43                 continue;
 44             case 3:
 45                 //使用嗖嗖
 46                 System.out.print("请输入手机卡号:");
 47                 mobileNumber = input.next();                
 48                 if (utils.isExistCard(mobileNumber)) {
 49                     try {
 50                         utils.userSoso(mobileNumber);
 51                     } catch (Exception e) {
 52                         System.err.println(e.getMessage());
 53                     }
 54                 }else{
 55                     System.out.println("对不起,该卡号未注册,不能使用!");
 56                 }
 57                 continue;
 58             case 4:
 59                 //话费充值
 60                 System.out.print("请输入充值卡号:");
 61                 mobileNumber = input.next();
 62                 if (utils.isExistCard(mobileNumber)) {
 63                 System.out.print("请输入充值金额:");
 64                 double money = input.nextDouble();                
 65                 utils.chargeMoney(mobileNumber, money);
 66                 }else{
 67                     System.out.println("对不起,要充值的卡号未注册,无法充值!");
 68                 }
 69                 continue;                
 70             case 5:
 71                 //System.out.println("\n*****资费说明******");
 72                 utils.showDescription();
 73                 continue;    
 74             case 6:
 75                 //退出系统
 76                 break;
 77             default:
 78                 //选择其他数字退出系统
 79                 break;
 80             }
 81             break;
 82         } while (true);
 83     }
 84 
 85     /**
 86      * 手机卡功能菜单
 87      */
 88     public int cardMenu(String mobileNumber) {
 89            int menuChoose = 0;
 90         do {
 91             System.out.println("\n*****嗖嗖移动用户菜单*****");
 92             System.out.println("1.本月账单查询");
 93             System.out.println("2.套餐余量查询");
 94             System.out.println("3.打印消费详单");
 95             System.out.println("4.套餐变更");
 96             System.out.println("5.办理退网");
 97             System.out.print("请选择(输入1~5选择功能,其他键返回上一级):");
 98              menuChoose = input.nextInt();
 99             switch (menuChoose) {
100             case 1:
101                 System.out.println("\n*****本月账单查询******");
102                 utils.showAmountDetail(mobileNumber);
103                 continue;
104             case 2:
105                 System.out.println("\n*****套餐余量查询******");
106                 utils.showRemainDetail(mobileNumber);
107                 continue;
108             case 3:
109                 System.out.println("\n*****消费详单查询******");
110                 utils.printConsumInfo(mobileNumber);
111                 continue;
112             case 4:
113                 System.out.println("\n*****套餐变更******");
114                 System.out.print("1.话唠套餐  2.网虫套餐  3.超人套餐  请选择(序号):");                
115                 utils.changingPack(mobileNumber, input.nextInt());
116                 continue;
117             case 5:
118                 System.out.println("\n*****办理退网******");
119                 utils.delCard(mobileNumber);
120                 System.out.println("谢谢使用!");
121                 System.exit(1);     //办理退网后退出系统    
122                         
123             }
124             
125             break;
126         } while (true);
127         return menuChoose;
128     }
129     
130     /**
131      * 注册新卡流程
132      */
133     public void registCard(){
134         //1.获取卡号信息集合
135         String[] newNumbers = utils.getNewNumbers(9);
136         
137         //2.(每行三列)显示可供选择的手机号列表
138         System.out.println("*****可选择的卡号*****");
139         for(int i=0;i<9;i++){
140             System.out.print((i+1)+"."+newNumbers[i]+"\t\t");
141             if((i+1)%3==0){
142                 System.out.println();
143             }
144         }
145         
146         //3.选择手机号
147         System.out.print("请选择卡号(输入1~9的序号):");        
148         String number = newNumbers[input.nextInt()-1];
149         
150         //4.选择套餐类型
151         System.out.print("1.话唠套餐  2.网虫套餐  3.超人套餐,  ");
152         System.out.print("请选择套餐(输入序号):");
153         ServicePackage pack = utils.createPack(input.nextInt());
154         
155         //5.输入用户名
156         System.out.print("请输入姓名:");
157         String name = input.next();
158         
159         //6.输入密码
160         System.out.print("请输入密码:");
161         String password = input.next();
162         
163         //7.输入预存话费金额
164         double money = 0;
165         System.out.print("请输入预存话费金额:");
166          money = input.nextDouble();
167         while(money<pack.getPrice()){
168             System.out.print("您预存的话费金额不足以支付本月固定套餐资费,请重新充值:");
169             money = input.nextDouble();
170         }
171                 
172         //8.创建新卡对象并添加一条数据
173         MobileCard newCard = new MobileCard(name,password,number,pack,pack.getPrice(),money-pack.getPrice());
174         utils.addCard(newCard);        
175     }
176 }
 1 public class Common {
 2     /**
 3      * double类型格式化
 4      */
 5     public static String dataFormat(double data) {
 6         DecimalFormat formatData = new DecimalFormat("#.0");
 7         return formatData.format(data);
 8     }
 9     
10     /**
11      * double类型两数相减
12      */
13     public static double sub(double num1,double num2){
14         return (num1*10-num2*10)/10;
15     }
16 }
1 public enum ConsumType {
2    TALK,SMS,NETWORK
3 }
 1 public class ConsumInfo {
 2     private String cardNumber;  //卡号
 3     private String type;  //消费类型:通话、发短信、上网
 4     private int consumData;   //消费数据   通话:分钟   发短信:条   上网:MB
 5     
 6     public ConsumInfo(){}
 7     public ConsumInfo(String cardNumber, String type, int consumData) {
 8         super();
 9         this.cardNumber = cardNumber;
10         this.type = type;
11         this.consumData = consumData;
12     }
13     public String getCardNumber() {
14         return cardNumber;
15     }
16     public void setCardNumber(String cardNumber) {
17         this.cardNumber = cardNumber;
18     }
19     public String getType() {
20         return type;
21     }
22     public void setType(String type) {
23         this.type = type;
24     }
25     public int getConsumData() {
26         return consumData;
27     }
28     public void setConsumData(int consumData) {
29         this.consumData = consumData;
30     }    
31 }
  1 public class MobileCard {
  2     private String cardNumber;  //卡号
  3     private String userName;  //用户名
  4     private String passWord;  //密码    
  5     private ServicePackage serPackage;  //所属套餐
  6     private double consumAmount;  //当月消费金额
  7     private double money;  //账户余额
  8     private int realTalkTime;  //实际通话时长(分钟)
  9     private int realSMSCount;  //实际发送短信条数(条)
 10     private int realFlow;  //实际上网流量
 11     
 12     public MobileCard(){}
 13 
 14     public MobileCard(String userName, String passWord, String cardNumber,
 15             ServicePackage serPackage, double consumAmount, double money) {
 16         super();
 17         this.userName = userName;
 18         this.passWord = passWord;
 19         this.cardNumber = cardNumber;
 20         this.serPackage = serPackage;
 21         this.consumAmount = consumAmount;
 22         this.money = money;
 23     }
 24 
 25     public String getUserName() {
 26         return userName;
 27     }
 28 
 29     public void setUserName(String userName) {
 30         this.userName = userName;
 31     }
 32 
 33     public String getPassWord() {
 34         return passWord;
 35     }
 36 
 37     public void setPassWord(String passWord) {
 38         this.passWord = passWord;
 39     }
 40 
 41     public String getCardNumber() {
 42         return cardNumber;
 43     }
 44 
 45     public void setCardNumber(String cardNumber) {
 46         this.cardNumber = cardNumber;
 47     }
 48 
 49     public ServicePackage getSerPackage() {
 50         return serPackage;
 51     }
 52 
 53     public void setSerPackage(ServicePackage serPackage) {
 54         this.serPackage = serPackage;
 55     }
 56 
 57     public double getConsumAmount() {
 58         return consumAmount;
 59     }
 60 
 61     public void setConsumAmount(double consumAmount) {
 62         this.consumAmount = consumAmount;
 63     }
 64 
 65     public double getMoney() {
 66         return money;
 67     }
 68 
 69     public void setMoney(double money) {
 70         this.money = money;
 71     }
 72 
 73     public int getRealTalkTime() {
 74         return realTalkTime;
 75     }
 76 
 77     public void setRealTalkTime(int realTalkTime) {
 78         this.realTalkTime = realTalkTime;
 79     }
 80 
 81     public int getRealSMSCount() {
 82         return realSMSCount;
 83     }
 84 
 85     public void setRealSMSCount(int realSMSCount) {
 86         this.realSMSCount = realSMSCount;
 87     }
 88 
 89     public int getRealFlow() {
 90         return realFlow;
 91     }
 92 
 93     public void setRealFlow(int realFlow) {
 94         this.realFlow = realFlow;
 95     }
 96     
 97     /**
 98      * 显示卡信息
 99      */
100     public void showMeg(){
101         System.out.println("卡号:"+this.cardNumber+" 用户名:"+this.userName+" 当前余额:"+this.money+"元。");
102         this.serPackage.showInfo();
103     }    
104 }
 1 public class NetPackage extends ServicePackage implements NetService {
 2     private int flow; // 上网流量(MB)
 3     
 4     public NetPackage() {
 5         //套餐数据初始化
 6         this.flow = 1024 * 3;
 7         this.price = 68.0;
 8     }
 9 
10     public NetPackage(int flow) {
11         super();
12         this.flow = flow;
13     }
14     
15     public int getFlow() {
16         return flow;
17     }
18 
19     public void setFlow(int flow) {
20         this.flow = flow;
21     }
22 
23     
24 
25     @Override
26     public void showInfo() {
27         System.out.println("网虫套餐:上网流量是" + flow / 1024 + "GB/月,月资费是"
28                 + this.price + "元/月。");
29     }
30 
31     /**
32      * 提供上网服务
33      */
34     public void netPlay2(int flow, MobileCard card) throws Exception {
35         int reminFlow = this.flow - card.getRealFlow();  //卡中可支付的免费流量
36         // 判断套餐中的上网流量是否足够支付本次上网服务
37         if (this.flow <= reminFlow) {
38             // 套餐中上网流量足够:修改该卡实际上网流量数据
39             card.setRealFlow(card.getRealFlow() + flow);
40         } else {
41             // 套餐中上网流量不够:额外消费需按0.1元/条付费,额外消费金额=0.1*(该卡实际消费上网流量+本次消费上网流量-套餐包含的上网流量)
42             double consumeMoney = 0.1 * (flow-reminFlow);
43             // 该卡账户余额足够支付:修改该卡实际使用的上网流量、账户余额、当月消费金额
44             if (card.getMoney() >= consumeMoney) {
45                 //消耗的流量增加
46                 card.setRealFlow(card.getRealFlow() + flow);
47                 // 当前账户余额=当前账户余额-额外消费金额
48                 card.setMoney(card.getMoney() - consumeMoney);
49                 // 当月消费金额=当月消费金额+额外消费金额
50                 card.setConsumAmount(card.getConsumAmount() + consumeMoney);
51             } else {
52                 
53                 int temp = (int)(card.getMoney()/0.1); //当前余额够大
54                 throw new Exception("您的余额不足,请充值后再使用!");
55             }
56         }
57     }
58     
59     /**
60      * 提供上网服务
61      */
62     public int netPlay(int flow, MobileCard card) throws Exception {
63         int temp = flow;
64         for(int i=0;i<flow;i++){
65             if(this.flow-card.getRealFlow()>=1){
66                 //第一种情况:套餐剩余流量可以支持使用1M流量            
67                 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB                
68             }else if(card.getMoney()>=0.1){
69                 //第二种情况:套餐流量已用完,账户余额可以支付1M流量,使用账户余额支付
70                 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB
71                 card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1M流量费用)
72                 card.setConsumAmount(card.getConsumAmount() + 0.1);
73             }else{
74                 temp = i;
75                 throw new Exception("本次已使用流量"+i+"MB,您的余额不足,请充值后再使用!");
76             }
77         }
78         return temp;
79     }
80 }
 1 public class Scene {
 2     private String type;  //场景消费类型
 3     private int data;  //消费数据
 4     private String description;//场景描述
 5     
 6     public Scene(){}
 7     public Scene(String type,int data,String description){
 8         this.type = type;
 9         this.data = data;
10         this.description = description;
11     }  
12     
13     public String getType() {
14         return type;
15     }
16     public void setType(String type) {
17         this.type = type;
18     }
19     public int getData() {
20         return data;
21     }
22     public void setData(int data) {
23         this.data = data;
24     }
25     public String getDescription() {
26         return description;
27     }
28     public void setDescription(String description) {
29         this.description = description;
30     }
31     
32 }
 1 public abstract class ServicePackage {    
 2     protected double price;  //套餐月资费(元)
 3      
 4     public double getPrice() {
 5         return price;
 6     }
 7 
 8     public void setPrice(double price) {
 9         this.price = price;
10     }
11 
12     //显示套餐数据
13     public abstract void showInfo();
14 }
  1 public class SuperPackage extends ServicePackage implements CallService,
  2 SendService,NetService {
  3     private int talkTime;   //通话时长(分钟)
  4     private int smsCount;   //短信条数(条)
  5     private int flow;  //上网流量(MB)
  6        
  7     public int getTalkTime() {
  8         return talkTime;
  9     }
 10 
 11 
 12     public void setTalkTime(int talkTime) {
 13         this.talkTime = talkTime;
 14     }
 15 
 16 
 17     public int getSmsCount() {
 18         return smsCount;
 19     }
 20 
 21 
 22     public void setSmsCount(int smsCount) {
 23         this.smsCount = smsCount;
 24     }
 25     
 26     public int getFlow() {
 27         return flow;
 28     }
 29 
 30     public void setFlow(int flow) {
 31         this.flow = flow;
 32     }
 33     
 34     public SuperPackage(){
 35         //套餐数据初始化
 36         this.talkTime = 200;
 37         this.smsCount = 50;
 38         this.flow = 1*1024;  
 39         this.price = 78.0;
 40     }
 41     @Override
 42     public void showInfo() {
 43         System.out.println("超人套餐:通话时长为"+this.talkTime+"分钟/月,短信条数为"+this.smsCount+"条/月,上网流量为"+this.flow/1024+"GB/月。");
 44     }
 45 
 46 
 47     /**
 48      * 提供上网服务
 49      */
 50     public int netPlay(int flow, MobileCard card) throws Exception {
 51         int temp = flow;
 52         for(int i=0;i<flow;i++){
 53             if(this.flow-card.getRealFlow()>=1){
 54                 //第一种情况:套餐剩余流量可以支持使用1M流量            
 55                 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB                
 56             }else if(card.getMoney()>=0.1){
 57                 //第二种情况:套餐流量已用完,账户余额可以支付1M流量,使用账户余额支付
 58                 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB
 59                 card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1M流量费用)
 60                 card.setConsumAmount(card.getConsumAmount() + 0.1);
 61             }else{
 62                 temp = i;
 63                 throw new Exception("本次已使用流量"+i+"MB,您的余额不足,请充值后再使用!");
 64             }
 65         }
 66         return temp;
 67     }
 68 
 69     /**
 70      * 提供通话服务
 71      */
 72     public int call(int minCount, MobileCard card) throws Exception{
 73         int temp = minCount; 
 74         for(int i=0;i<minCount;i++){
 75             if(this.talkTime-card.getRealTalkTime()>=1){
 76                 //第一种情况:套餐剩余通话时长可以付1分钟通话            
 77                 card.setRealTalkTime(card.getRealTalkTime()+1); //实际通话数据加1                
 78             }else if(card.getMoney()>=0.2){
 79                 //第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付
 80                 card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用通话时长1分钟 
 81                 card.setMoney(Common.sub(card.getMoney(),0.2)); //账户余额消费0.2元(1分钟额外通话)
 82                 card.setConsumAmount(card.getConsumAmount() + 0.2);
 83             }else{
 84                 temp = i; //记录实现通话分钟数
 85                 throw new Exception("本次已通话"+i+"分钟,您的余额不足,请充值后再使用!");                
 86             }
 87         }
 88         return temp;
 89     }
 90     
 91     /**
 92      * 提供短信服务
 93      */
 94     public int sendMessage(int smsCount, MobileCard card) throws Exception {
 95         int temp = smsCount;
 96         for(int i=0;i<smsCount;i++){
 97             if(this.smsCount-card.getRealSMSCount()>=1){
 98                 //第一种情况:套餐剩余短信条数可以付1条短信            
 99                 card.setRealSMSCount(card.getRealSMSCount()+1); //实际使用短信条数加1                
100             }else if(card.getMoney()>=0.1){
101                 //第二种情况:套餐短信条数已用完,账户余额可以支付1条短信,使用账户余额支付
102                 card.setRealSMSCount(card.getRealSMSCount()+1); 
103                 card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1条短信费用)
104                 card.setConsumAmount(card.getConsumAmount() + 0.1);
105             }else{
106                 temp = i;
107                 throw new Exception("本次已发送短信"+i+"条,您的余额不足,请充值后再使用!");
108             }
109         }
110         return temp;
111     }
112 
113 }
 1 public class TalkPackage extends ServicePackage implements CallService,
 2         SendService {
 3     private int talkTime; // 通话时长(分钟)
 4     private int smsCount; // 短信条数(条)
 5 
 6     public int getTalkTime() {
 7         return talkTime;
 8     }
 9 
10     public void setTalkTime(int talkTime) {
11         this.talkTime = talkTime;
12     }
13 
14     public int getSmsCount() {
15         return smsCount;
16     }
17 
18     public void setSmsCount(int smsCount) {
19         this.smsCount = smsCount;
20     }
21 
22     public TalkPackage() {
23         //套餐数据初始化
24         this.talkTime = 500;
25         this.smsCount = 30;
26         this.price = 58.0;
27     }
28 
29     public TalkPackage(int talkTime, int smsCount) {
30         super();
31         this.talkTime = talkTime;
32         this.smsCount = smsCount;
33     }
34 
35     /**
36      * 显示套餐详情
37      */
38     public void showInfo() {
39         System.out.println("话唠套餐:通话时长为" + this.talkTime + "分钟/月,短信条数为"
40                 + this.smsCount + "条/月,资费为" + this.price + "元/月。");
41     }
42     
43     public int call(int minCount, MobileCard card) throws Exception{
44         int temp = minCount; 
45         for(int i=0;i<minCount;i++){
46             if(this.talkTime-card.getRealTalkTime()>=1){
47                 //第一种情况:套餐剩余通话时长可以付1分钟通话            
48                 card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用流量加1MB                
49             }else if(card.getMoney()>=0.2){
50                 //第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付
51                 card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用通话时长1分钟 
52                 card.setMoney(Common.sub(card.getMoney(),0.2)); //账户余额消费0.2元(1M流量费用)
53                 card.setConsumAmount(card.getConsumAmount() + 0.2);
54             }else{
55                 temp = i; //记录实现通话分钟数
56                 throw new Exception("本次已通话"+i+"分钟,您的余额不足,请充值后再使用!");                
57             }
58         }
59         return temp;
60     }
61         
62     public int sendMessage(int smsCount, MobileCard card) throws Exception {
63         int temp = smsCount;
64         for(int i=0;i<smsCount;i++){
65             if(this.smsCount-card.getRealSMSCount()>=1){
66                 //第一种情况:套餐剩余短信条数可以付1条短信            
67                 card.setRealSMSCount(card.getRealSMSCount()+1); //实际使用短信条数加1                
68             }else if(card.getMoney()>=0.1){
69                 //第二种情况:套餐短信条数已用完,账户余额可以支付1条短信,使用账户余额支付
70                 card.setRealSMSCount(card.getRealSMSCount()+1); 
71                 card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1条短信费用)
72                 card.setConsumAmount(card.getConsumAmount() + 0.1);
73             }else{
74                 temp = i;
75                 throw new Exception("本次已发送短信"+i+"条,您的余额不足,请充值后再使用!");
76             }
77         }
78         return temp;
79     }
80     
81 }
1 public interface CallService {
2     //打电话
3       public int call(int minCount,MobileCard card) throws Exception;
4 }
1 public interface NetService {
2     //上网
3      public int netPlay(int flow,MobileCard card) throws Exception;
4 }
1 public interface SendService {
2     //发短信
3      public int sendMessage(int count,MobileCard card) throws Exception;
4 }
  1 public class CardUtil {
  2     Map<String, MobileCard> cards = new HashMap<String, MobileCard>(); // 所有手机卡的列表
  3     Map<String, List<ConsumInfo>> consumInfos = new HashMap<String, List<ConsumInfo>>(); // 所有手机卡消费记录的列表
  4     List<Scene> scenes = new ArrayList<Scene>();
  5 
  6     // 初始化用户
  7     public void init() {
  8         MobileCard card1 = new MobileCard("何玲玲", "123", "13965756432",
  9                 new TalkPackage(), 58.0, 42.0);
 10         MobileCard card2 = new MobileCard("黄露露", "123", "13956712467",
 11                 new NetPackage(), 68.0, 32.0);
 12         MobileCard card3 = new MobileCard("朱蓉蓉", "123", "13911568956",
 13                 new SuperPackage(), 78.0, 22.0);
 14         MobileCard card4 = new MobileCard("桃跑跑", "123", "13924221868",
 15                 new TalkPackage(), 78.0, 2.0);
 16         card4.setConsumAmount(98.0);
 17         card4.setRealTalkTime(500);
 18         card4.setRealSMSCount(100);
 19         cards.put("13965756432", card1);
 20         cards.put("13956712467", card2);
 21         cards.put("13911568956", card3);
 22         cards.put("13924221868", card4);
 23     }
 24     
 25     /**
 26      * 使用场景初始化
 27      */
 28     public void initScenes(){    
 29         scenes.add(new Scene("通话",90,"问候客户,谁知其如此难缠 通话90分钟"));
 30         scenes.add(new Scene("通话",30,"询问妈妈身体状况 本地通话30分钟"));
 31         scenes.add(new Scene("短信",5,"参与环境保护实施方案问卷调查 发送短信5条"));
 32         scenes.add(new Scene("短信",50,"通知朋友手机换号,发送短信50条"));
 33         scenes.add(new Scene("上网",1*1024,"和女友微信视频聊天   使用流量1G"));
 34         scenes.add(new Scene("上网",2*1024,"晚上手机在线看韩剧,不留神睡着啦! 使用流量 2G"));        
 35     }
 36 
 37     /**
 38      * 是否存在此卡用户
 39      */
 40     public boolean isExistCard(String number, String passWord) {
 41         Set<String> numbers = cards.keySet();
 42         Iterator<String> it = numbers.iterator();
 43         while (it.hasNext()) {
 44             String searchNum = it.next();
 45             if (searchNum.equals(number)
 46                     && (cards.get(searchNum)).getPassWord().equals(passWord)) {
 47                 return true;
 48             }
 49         }
 50         return false;
 51     }
 52     
 53     /**
 54      * 查找指定卡号是否已注册
 55      * 
 56      */
 57     public boolean isExistCard(String searchNumber) {
 58         Set<String> numbers = cards.keySet();
 59         for (String number : numbers) {
 60             if (number.equals(searchNumber)) {
 61                 return true;
 62             }
 63         }
 64         return false;
 65     }
 66 
 67     /**
 68      * 创建卡号(以139开头 11位)
 69      */
 70     public String createNumber() {
 71         Random random = new Random();
 72         boolean isExist = false; // 记录现有用户中是否存在此卡号用户 是:true 否:false
 73         String number = "";
 74         int temp = 0;
 75         do {
 76             isExist = false; // 标志位重置为false,用于控制外重循环,当生成了
 77             // 生成的随机数是8位 不能小于10000000,否则重新生成
 78             do {
 79                 temp = random.nextInt(100000000);
 80             } while (temp < 10000000);
 81             // 生成之前,前面加“139”
 82             number = "139" + temp;
 83             // 和现有用户的卡号比较,不能是重复
 84             Set<String> cardNumbers = cards.keySet();
 85             for (String cardNumber : cardNumbers) {
 86                 if (number.equals(cardNumber)) {
 87                     isExist = true;
 88                     break;
 89                 }
 90             }
 91         } while (isExist);
 92         return number;
 93     }
 94 
 95     /**
 96      * 生成指定个数的新卡号列表
 97      */
 98     public String[] getNewNumbers(int count) {
 99 
100         String[] numbers = new String[count];
101         for (int i = 0; i < count; i++) {
102             numbers[i] = createNumber();
103         }
104         return numbers;
105     }
106 
107     /**
108      * 添加新卡
109      */
110     public void addCard(MobileCard card) {
111         cards.put(card.getCardNumber(), card);
112         System.out.print("注册成功!");
113         card.showMeg();
114     }
115 
116     /**
117      * 指定卡号办理退网
118      */
119     public void delCard(String delNumber) {
120         if (isExistCard(delNumber)) {
121             cards.remove(delNumber);
122             System.out.println("卡号" + delNumber + "办理退网成功!");
123         } else {
124             System.out.println("对不起,该卡号未注册,不能办退退网!");
125         }
126     }
127 
128     /**
129      * 查询指定卡套餐余量
130      */
131     public void showRemainDetail(String searchNumber) {
132         MobileCard card; // 要查询的卡
133         int remainTalkTime;
134         int remainSmsCount;
135         int remainFlow;
136         StringBuffer meg = new StringBuffer();
137             card = cards.get(searchNumber);
138             meg.append("您的卡号是" + searchNumber + ",套餐内剩余:\n");
139             ServicePackage pack = card.getSerPackage();
140             if (pack instanceof TalkPackage) {
141                 //向下转型为话唠套餐对象
142                 TalkPackage cardPack = (TalkPackage) pack;
143                 // 话唠套餐,查询套餐内剩余的通话时长和短信条数
144                 remainTalkTime = cardPack.getTalkTime() > card
145                         .getRealTalkTime() ? cardPack.getTalkTime()
146                         - card.getRealTalkTime() : 0;
147                 meg.append("通话时长:" + remainTalkTime + "分钟\n");
148                 remainSmsCount = cardPack.getSmsCount() > card
149                         .getRealSMSCount() ? cardPack.getSmsCount()
150                         - card.getRealSMSCount() : 0;
151                 meg.append("短信条数:" + remainSmsCount + "条");
152             } else if (pack instanceof NetPackage) {
153                 //向下转型为网虫套餐对象
154                 NetPackage cardPack = (NetPackage) pack;
155                 // 网虫套餐:查询套餐内剩余的上网流量
156                 remainFlow = cardPack.getFlow() > card.getRealFlow() ? cardPack
157                         .getFlow() - card.getRealFlow() : 0;
158                 meg.append("上网流量:" + Common.dataFormat(remainFlow * 1.0 / 1024)
159                         + "GB");
160             } else if (pack instanceof SuperPackage) {
161                 //向下转型为超人套餐对象
162                 SuperPackage cardPack = (SuperPackage) pack;
163                 // 超人套餐:查询套餐内剩余的通话时长、短信条数、上网流量
164                 remainTalkTime = cardPack.getTalkTime() > card
165                         .getRealTalkTime() ? cardPack.getTalkTime()
166                         - card.getRealTalkTime() : 0;
167                 meg.append("通话时长:" + remainTalkTime + "分钟\n");
168                 remainSmsCount = cardPack.getSmsCount() > card
169                         .getRealSMSCount() ? cardPack.getSmsCount()
170                         - card.getRealSMSCount() : 0;
171                 meg.append("短信条数:" + remainSmsCount + "条\n");
172                 remainFlow = cardPack.getFlow() > card.getRealFlow() ? cardPack
173                         .getFlow() - card.getRealFlow() : 0;
174                 meg.append("上网流量:" + Common.dataFormat(remainFlow * 1.0 / 1024)
175                         + "GB");
176             }
177             System.out.println(meg);
178     }
179 
180     /**
181      * 查询指定卡当月消费详单
182      */
183     public void showAmountDetail(String searchNumber) {
184         MobileCard card; // 要查询的卡
185         StringBuffer meg = new StringBuffer();
186         card = cards.get(searchNumber);
187         meg.append("您的卡号:" + card.getCardNumber() + ",当月账单:\n");
188         meg.append("套餐资费:" + card.getSerPackage().getPrice() + "元\n");
189         meg.append("合计:" + Common.dataFormat(card.getConsumAmount()) + "元\n");
190         meg.append("账户余额:" + Common.dataFormat(card.getMoney()) + "元");
191         // 显示本月消费详细信息
192         System.out.println(meg);
193     }
194 
195     
196     /**
197      * 指定卡号换套餐
198      */
199     public void changingPack(String number, int packNum) {
200         MobileCard card; // 指定的手机卡
201         ServicePackage pack; // 要换的套餐
202         if (isExistCard(number)) {
203             card = cards.get(number);
204             // 获取要换的套餐对象
205             switch (packNum) {
206             case 1:
207                 pack = new TalkPackage();
208                 break;
209             case 2:
210                 pack = new NetPackage();
211                 break;
212             default:
213                 pack = new SuperPackage();
214                 break;
215             }        
216             if (!(card.getSerPackage().getClass().getName().equals(pack.getClass().getName()))) {
217                 // 该卡余额中减去当月套餐资费
218                 if (card.getMoney() >= pack.getPrice()) {
219                     card.setMoney(card.getMoney() - pack.getPrice());
220                     // 换套餐
221                     card.setSerPackage(pack);
222                     // 当月实际使用数据清零
223                     card.setRealTalkTime(0);
224                     card.setRealFlow(0);
225                     card.setRealSMSCount(0);
226                     // 当月消费金额设置为新套餐月资费
227                     card.setConsumAmount(pack.getPrice());
228                     System.out.print("更换套餐成功!");
229                     pack.showInfo();
230                 } else {
231                     System.out.println("对不起,您的余额不足以支付新套餐本月资费,请充值后再办理更换套餐业务!");
232                     return;
233                 }
234             } else {
235                 System.out.println("对不起,您已经是该套餐用户,无需换套餐!");
236             }
237 
238         } else {
239             System.out.println("对不起,该卡号未注册,不能换套餐!");
240         }
241     }
242 
243     /**
244      * 为指定手机卡充值
245      */
246     public void chargeMoney(String number, double money) {
247         MobileCard card; // 指定的手机卡
248         if (money < 50) {
249             System.out.println("对不起,最低充值金额为50元!");
250             return;
251         }
252             card = cards.get(number);
253             card.setMoney(card.getMoney() + money);
254             System.out.println("充值成功,当前话费余额为" + Common.dataFormat(card.getMoney()) + "元。");
255     }
256 
257     /**
258      * 添加一条指定卡的消费记录
259      */
260     public void addConsumInfo(String number, ConsumInfo info) {
261         Set<String> numbers = consumInfos.keySet();
262         Iterator<String> it = numbers.iterator();
263         List<ConsumInfo> infos = new ArrayList<ConsumInfo>();
264         boolean isExist = false; // 现有消费列表中是否存在此卡号消费记录,是:true 否:false
265         while (it.hasNext()) {
266             if (it.next().equals(number)) {
267                 // 消费集合中已有该卡号消费记录,则找到该卡号的消费记录集合,添加一条即可
268                 infos = consumInfos.get(number);
269                 infos.add(info);
270                 isExist = true;
271                 System.out.println("已添加一条消费记录。");
272                 break;
273             }
274         }
275         // 该集合中没有此卡号消费记录,则添加
276         if (!isExist) {
277             infos.add(info);
278             consumInfos.put(number, infos);
279             System.out.println("不存在此卡的消费记录,已添加一条消费记录。");
280         }
281     }
282     
283     //打印消费记录
284     public void printConsumInfo(String number){
285         Writer fileWriter = null;
286         try {
287              fileWriter = new FileWriter(number+"消费记录.txt");            
288             Set<String> numbers = consumInfos.keySet();
289             Iterator<String> it = numbers.iterator();
290             List<ConsumInfo> infos = new ArrayList<ConsumInfo>();//存储指定卡所有消费记录
291             boolean isExist = false; // 现有消费列表中是否存在此卡号消费记录,是:true 否:false
292             while (it.hasNext()) {
293                 if (it.next().equals(number)) {
294                     infos = consumInfos.get(number);
295                     isExist = true;
296                     break;
297                 }
298             }
299             if(isExist){
300                 //存在 此卡消费记录,写入文本文件
301                 StringBuffer content = new StringBuffer("******"+number+"消费记录******\n");
302                 content.append("序号\t类型\t数据(通话(条)/上网(MB)/短信(条))\n");
303                 for(int i=0;i<infos.size();i++){
304                     ConsumInfo info = infos.get(i);
305                     content.append((i+1)+".\t"+info.getType()+"\t"+info.getConsumData()+"\n");
306                 }
307                 fileWriter.write(content.toString());
308                 fileWriter.flush();
309                 
310                 System.out.println("消费记录打印完毕!");
311             }else{
312                 System.out.println("对不起,不存在此号码的消费记录,不能打印!");
313             }            
314         } catch (IOException e) {            
315             e.printStackTrace();
316         }finally{
317             if(fileWriter!=null){
318                 try {
319                     fileWriter.close();
320                 } catch (IOException e) {                    
321                     e.printStackTrace();
322                 }
323             }
324         }
325     }
326     
327     /**
328      * 使用嗖嗖
329      */
330     public void userSoso(String number)  {        
331         MobileCard card = cards.get(number); // 获取此卡对象
332         ServicePackage pack = card.getSerPackage(); // 获取此卡所属套餐
333         Random random = new Random();
334         int ranNum = 0;
335         int temp = 0;  //记录各场景中实际消费数据
336         do{
337             
338             ranNum = random.nextInt(6);// 生成一个0~5之前的随机数
339             Scene scene = scenes.get(ranNum); //获取该序号所对应的场景
340             switch (ranNum) {
341             //序号为0或1为通话场景
342             case 0:
343             case 1:
344                 // 判断该卡所属套餐是否支持通话功能
345                 if (pack instanceof CallService) {
346                     // 执行通话方法
347                     System.out.println(scene.getDescription());
348                     CallService callService = (CallService) pack;
349                     try {
350                         temp = callService.call(scene.getData(), card);
351                     } catch (Exception e) {                            
352                         e.printStackTrace();
353                     }
354                     // 添加一条消费记录
355                     addConsumInfo(number, new ConsumInfo(number,
356                             scene.getType(), temp));
357                     break;
358                 } else {
359                     // 如果该卡套餐不支持通话功能,则重新生成随机数选择其他场景
360                     continue;
361                 }
362                 //序号为2或3为发短信场景
363             case 2:
364             case 3:
365                 // 判断该卡所属套餐是否支持短信功能
366                 if (pack instanceof SendService) {
367                     // 执行发短信方法
368                     System.out.println(scene.getDescription());
369                     SendService sendService = (SendService) pack;
370                     try {
371                         temp = sendService.sendMessage(scene.getData(), card);
372                     } catch (Exception e) {                                                    
373                         e.printStackTrace();
374                     }
375                     // 添加一条消费记录
376                     addConsumInfo(number, new ConsumInfo(number,
377                             scene.getType(), temp));
378                     break;
379                 } else {
380                     // 如果该卡套餐不支持发短信功能,则重新生成随机数选择其他场景
381                     continue;
382                 }
383                 //序号为4或5为发上网场景
384             case 4:
385             case 5:
386                 // 判断该卡所属套餐是否支持上网功能
387                 if (pack instanceof NetService) { 
388                     System.out.println(scene.getDescription());
389                     NetService netService = (NetService) pack;
390                     // 执行上网方法
391                     try {
392                         temp = netService.netPlay(scene.getData(), card);
393                     } catch (Exception e) {                        
394                         e.printStackTrace();
395                     }
396                     // 添加一条消费记录
397                     addConsumInfo(number, new ConsumInfo(number,
398                             scene.getType(), temp));
399                     break;
400                 } else {
401                     // 如果该卡套餐不支持上网功能,则重新生成随机数选择其他场景
402                     continue;
403                 }                
404             }    
405             break;
406         }while(true);
407     }
408 
409     /**
410      * 根据套餐序号返回套餐对象
411      */
412     public ServicePackage createPack(int packId) {
413         ServicePackage pack = null;
414         switch (packId) {
415         case 1:
416             pack = new TalkPackage();
417             break;
418         case 2:
419             pack = new NetPackage();
420             break;
421         case 3:
422             pack = new SuperPackage();
423             break;
424         }
425         return pack;
426     }
427     
428     /**
429      * 显示资费说明
430      */
431     public void showDescription(){
432         Reader rd = null;
433         try {
434             rd = new FileReader("套餐资费说明.txt");
435             int len = 0;
436              char[] content = new char[1024];
437              StringBuffer sb = new StringBuffer();
438                 while((len=rd.read(content))!=-1){
439                     sb.append(content,0,len);  //拼接字符串
440                 }
441                 System.out.println(sb);
442         } catch (IOException e) {
443             e.printStackTrace();
444         }
445     }
446     
447 }
posted @ 2019-12-18 17:37  戴文祥  阅读(4003)  评论(0编辑  收藏  举报