Java项目—嗖嗖移动业务大厅

2019-05-16 23:56:03来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

嗖嗖移动业务大厅包类(如下图):

  

 

SosoMgr:
  1 package cn.biz;
  2 
  3 import java.util.Scanner;
  4 
  5 import cn.common.Common;
  6 import cn.entity.MobileCard;
  7 import cn.entity.ServicePackage;
  8 import cn.utils.CardUtil;
  9 
 10 /**
 11  * 业务类
 12  */
 13 @SuppressWarnings("unused")
 14 public class SosoMgr {
 15     Scanner input = new Scanner(System.in);
 16     CardUtil utils = new CardUtil();
 17     
 18     public static void main(String[] args) {
 19         SosoMgr soso = new SosoMgr();
 20         soso.mainMenu();
 21         System.out.println("谢谢使用!");
 22     }
 23 
 24     /**
 25      * 主流程
 26      */
 27     public void mainMenu() {
 28         int menuChoose = 0;
 29         String mobileNumber= "";
 30         String password = "";
 31         utils.init();
 32         utils.initScenes();
 33         //Common.typesInit();
 34         do {
 35             System.out.println("\n*************欢迎使用嗖嗖移动业务大厅***************");
 36             System.out.println("1.用户登录   2.用户注册   3.使用嗖嗖   4.话费充值  5.资费说明  6.退出系统");
 37             System.out.print("请选择:");
 38             menuChoose = input.nextInt();
 39             // 分支语句:根据功能编号执行相应功能
 40             switch (menuChoose) {
 41             case 1:
 42                 //用户登录
 43                 System.out.print("请输入手机卡号:");
 44                 mobileNumber = input.next();
 45                 System.out.print("请输入密码:");
 46                 password = input.next();
 47                 if (utils.isExistCard(mobileNumber, password)) {
 48                     cardMenu(mobileNumber);
 49                 }else{
 50                     System.out.println("对不起,您输入的信息有误,无法登录!");
 51                 }
 52                 continue;
 53             case 2:
 54                 //用户注册
 55                 registCard();
 56                 continue;
 57             case 3:
 58                 
 59                 //使用嗖嗖
 60                 System.out.print("请输入手机卡号:");
 61                  mobileNumber = input.next();                
 62                 
 63                 if (utils.isExistCard(mobileNumber)) {
 64                     try {
 65 /*                        System.out.println("****使用之前****");
 66                         utils.showRemainDetail(mobileNumber);
 67                         utils.showAmountDetail(mobileNumber);*/
 68                         utils.userSoso(mobileNumber);
 69                     } catch (Exception e) {
 70                         System.err.println(e.getMessage());
 71                     }
 72                 }else{
 73                     System.out.println("对不起,该卡号未注册,不能使用!");
 74                 }
 75                 
 76                 /*System.out.println("****使用之后****");
 77                 utils.showRemainDetail(mobileNumber);
 78                 utils.showAmountDetail(mobileNumber);*/
 79                 continue;
 80             case 4:
 81                 //话费充值
 82                 System.out.print("请输入充值卡号:");
 83                 mobileNumber = input.next();
 84                 if (utils.isExistCard(mobileNumber)) {
 85                 System.out.print("请输入充值金额:");
 86                 double money = input.nextDouble();                
 87                 utils.chargeMoney(mobileNumber, money);
 88                 }else{
 89                     System.out.println("对不起,要充值的卡号未注册,无法充值!");
 90                 }
 91                 continue;                
 92             case 5:
 93                 System.out.println("\n*****资费说明******");
 94                 utils.showDescription();
 95                 continue;    
 96             case 6:
 97                 //退出系统
 98                 break;
 99             default:
100                 //选择其他数字退出系统
101                 break;
102             }
103             break;
104         } while (true);
105     }
106 
107     /**
108      * 手机卡功能菜单
109      * 
110      * @param number
111      * @return
112      */
113     public int cardMenu(String mobileNumber) {
114            int menuChoose = 0;
115         do {
116             System.out.println("\n*****嗖嗖移动用户菜单*****");
117             System.out.println("1.本月账单查询");
118             System.out.println("2.套餐余量查询");
119             System.out.println("3.打印消费详单");
120             System.out.println("4.套餐变更");
121             System.out.println("5.办理退网");
122             System.out.print("请选择(输入1~5选择功能,其他键返回上一级):");
123              menuChoose = input.nextInt();
124             switch (menuChoose) {
125             case 1:
126                 System.out.println("\n*****本月账单查询******");
127                 utils.showAmountDetail(mobileNumber);
128                 continue;
129             case 2:
130                 System.out.println("\n*****套餐余量查询******");
131                 utils.showRemainDetail(mobileNumber);
132                 continue;
133             case 3:
134                 System.out.println("\n*****消费详单查询******");
135                 utils.printConsumInfo(mobileNumber);
136                 continue;
137             case 4:
138                 System.out.println("\n*****套餐变更******");
139                 System.out.print("1.话唠套餐  2.网虫套餐  3.超人套餐  请选择(序号):");                
140                 utils.changingPack(mobileNumber, input.next());
141                 continue;
142             case 5:
143                 System.out.println("\n*****办理退网******");
144                 utils.delCard(mobileNumber);
145                 System.out.println("谢谢使用!");
146                 System.exit(1);     //办理退网后退出系统    
147                         
148             }
149             
150             break;
151         } while (true);
152         return menuChoose;
153     }
154     
155     /**
156      * 注册新卡流程
157      */
158     public void registCard(){
159         String[] newNumbers = utils.getNewNumbers(9);
160         //显示可供选择的手机号列表
161         System.out.println("*****可选择的卡号*****");
162         
163         for(int i=0;i<9;i++){
164             System.out.print((i+1)+"."+newNumbers[i]+"\t\t");
165             if((i+1)%3==0){
166                 System.out.println();
167             }
168         }
169         //选择手机号
170         System.out.print("请选择卡号(输入1~9的序号):");        
171         String number = newNumbers[input.nextInt()-1];
172         
173         //选择套餐类型
174         System.out.print("1.话唠套餐  2.网虫套餐  3.超人套餐,  ");
175         System.out.print("请选择套餐(输入序号):");
176         //utils.getPackList();
177         //获取套餐对象 
178         ServicePackage pack = utils.createPack(input.nextInt());
179         
180         //输入用户名
181         System.out.print("请输入姓名:");
182         String name = input.next();
183         
184         //输入密码
185         System.out.print("请输入密码:");
186         String password = input.next();
187         
188         //输入预存话费金额
189         double money = 0;
190         System.out.print("请输入预存话费金额:");
191          money = input.nextDouble();
192         while(money<pack.getPrice()){
193             System.out.print("您预存的话费金额不足以支付本月固定套餐资费,请重新充值:");
194             money = input.nextDouble();
195         }
196                 
197         //创建新卡对象并添加
198         MobileCard newCard = new MobileCard(name,password,number,pack,pack.getPrice(),money-pack.getPrice());
199         utils.addCard(newCard);        
200     }
201 }

 

Common:

 1 package cn.common;
 2 
 3 import java.text.DecimalFormat;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 /**
 8  * 公共类
 9  */
10 @SuppressWarnings("unused")
11 public class Common {
12     /**
13      * double类型格式化
14      * @param data
15      * @return
16      */
17     public static String dataFormat(double data) {
18         DecimalFormat formatData = new DecimalFormat("#.0");
19         return formatData.format(data);
20     }
21     
22     /**
23      * double类型两数相减
24      * @param num1
25      * @param num2
26      * @return
27      */
28     public static double sub(double num1,double num2){
29         return (num1*10-num2*10)/10;
30     }
31 }

 

ConsumType:

1 package cn.common;
2 
3 /**
4  * 消费类型
5  */
6 public enum ConsumType {
7    TALK,SMS,NETWORK
8 }

 

ConsumInfo:

 1 package cn.entity;
 2 
 3 import cn.common.ConsumType;
 4 
 5 /**
 6  * 消费信息
 7  */
 8 @SuppressWarnings("unused")
 9 public class ConsumInfo {
10     private String cardNumber;  //卡号
11     private String type;  //消费类型:通话、发短信、上网
12     private int consumData;   //消费数据   通话:分钟   发短信:条   上网:MB
13     
14     public ConsumInfo(){}
15     public ConsumInfo(String cardNumber, String type, int consumData) {
16         super();
17         this.cardNumber = cardNumber;
18         this.type = type;
19         this.consumData = consumData;
20     }
21     public String getCardNumber() {
22         return cardNumber;
23     }
24     public void setCardNumber(String cardNumber) {
25         this.cardNumber = cardNumber;
26     }
27     public String getType() {
28         return type;
29     }
30     public void setType(String type) {
31         this.type = type;
32     }
33     public int getConsumData() {
34         return consumData;
35     }
36     public void setConsumData(int consumData) {
37         this.consumData = consumData;
38     }    
39 }

 

MobileCard:

  1 package cn.entity;
  2 
  3 /**
  4  * 手机卡
  5  */
  6 public class MobileCard {
  7     private String cardNumber;  //卡号
  8     private String userName;  //用户名
  9     private String passWord;  //密码    
 10     private ServicePackage serPackage;  //所属套餐
 11     private double consumAmount;  //当月消费金额
 12     private double money;  //账户余额
 13     private int realTalkTime;  //实际通话时长(分钟)
 14     private int realSMSCount;  //实际发送短信条数(条)
 15     private int realFlow;  //实际上网流量
 16     
 17     public MobileCard(){}
 18 
 19     public MobileCard(String userName, String passWord, String cardNumber,
 20             ServicePackage serPackage, double consumAmount, double money) {
 21         super();
 22         this.userName = userName;
 23         this.passWord = passWord;
 24         this.cardNumber = cardNumber;
 25         this.serPackage = serPackage;
 26         this.consumAmount = consumAmount;
 27         this.money = money;
 28     }
 29 
 30     public String getUserName() {
 31         return userName;
 32     }
 33 
 34     public void setUserName(String userName) {
 35         this.userName = userName;
 36     }
 37 
 38     public String getPassWord() {
 39         return passWord;
 40     }
 41 
 42     public void setPassWord(String passWord) {
 43         this.passWord = passWord;
 44     }
 45 
 46     public String getCardNumber() {
 47         return cardNumber;
 48     }
 49 
 50     public void setCardNumber(String cardNumber) {
 51         this.cardNumber = cardNumber;
 52     }
 53 
 54     public ServicePackage getSerPackage() {
 55         return serPackage;
 56     }
 57 
 58     public void setSerPackage(ServicePackage serPackage) {
 59         this.serPackage = serPackage;
 60     }
 61 
 62     public double getConsumAmount() {
 63         return consumAmount;
 64     }
 65 
 66     public void setConsumAmount(double consumAmount) {
 67         this.consumAmount = consumAmount;
 68     }
 69 
 70     public double getMoney() {
 71         return money;
 72     }
 73 
 74     public void setMoney(double money) {
 75         this.money = money;
 76     }
 77 
 78     public int getRealTalkTime() {
 79         return realTalkTime;
 80     }
 81 
 82     public void setRealTalkTime(int realTalkTime) {
 83         this.realTalkTime = realTalkTime;
 84     }
 85 
 86     public int getRealSMSCount() {
 87         return realSMSCount;
 88     }
 89 
 90     public void setRealSMSCount(int realSMSCount) {
 91         this.realSMSCount = realSMSCount;
 92     }
 93 
 94     public int getRealFlow() {
 95         return realFlow;
 96     }
 97 
 98     public void setRealFlow(int realFlow) {
 99         this.realFlow = realFlow;
100     }
101     
102     /**
103      * 显示卡信息
104      */
105     public void showMeg(){
106         System.out.println("卡号:"+this.cardNumber+" 用户名:"+this.userName+" 当前余额:"+this.money+"元。");
107         this.serPackage.showInfo();
108     }    
109 }

 

NetPackage:

 1 package cn.entity;
 2 
 3 import cn.common.Common;
 4 import cn.service.NetService;
 5 
 6 /**
 7  * 网虫套餐
 8  */
 9 public class NetPackage extends ServicePackage implements NetService {
10     private int flow; // 上网流量(MB)
11     
12     public NetPackage() {
13         //套餐数据初始化
14         this.flow = 1024 * 3;
15         this.price = 68.0;
16     }
17 
18     public NetPackage(int flow) {
19         super();
20         this.flow = flow;
21     }
22     
23     public int getFlow() {
24         return flow;
25     }
26 
27     public void setFlow(int flow) {
28         this.flow = flow;
29     }
30 
31     
32 
33     @Override
34     public void showInfo() {
35         System.out.println("网虫套餐:上网流量是" + flow / 1024 + "GB/月,月资费是"
36                 + this.price + "元/月。");
37     }
38 
39     /**
40      * 提供上网服务
41      */
42     @SuppressWarnings("unused")
43     public void netPlay2(int flow, MobileCard card) throws Exception {
44         int reminFlow = this.flow - card.getRealFlow();  //卡中可支付的免费流量
45         // 判断套餐中的上网流量是否足够支付本次上网服务
46         if (this.flow <= reminFlow) {
47             // 套餐中上网流量足够:修改该卡实际上网流量数据
48             card.setRealFlow(card.getRealFlow() + flow);
49         } else {
50             // 套餐中上网流量不够:额外消费需按0.1元/条付费,额外消费金额=0.1*(该卡实际消费上网流量+本次消费上网流量-套餐包含的上网流量)
51             double consumeMoney = 0.1 * (flow-reminFlow);
52             // 该卡账户余额足够支付:修改该卡实际使用的上网流量、账户余额、当月消费金额
53             if (card.getMoney() >= consumeMoney) {
54                 //消耗的流量增加
55                 card.setRealFlow(card.getRealFlow() + flow);
56                 // 当前账户余额=当前账户余额-额外消费金额
57                 card.setMoney(card.getMoney() - consumeMoney);
58                 // 当月消费金额=当月消费金额+额外消费金额
59                 card.setConsumAmount(card.getConsumAmount() + consumeMoney);
60             } else {
61                 
62                 int temp = (int)(card.getMoney()/0.1); //当前余额够大
63                 throw new Exception("您的余额不足,请充值后再使用!");
64             }
65         }
66     }
67     
68     /**
69      * 提供上网服务
70      */
71     public int netPlay(int flow, MobileCard card) throws Exception {
72         int temp = flow;
73         for(int i=0;i<flow;i++){
74             if(this.flow-card.getRealFlow()>=1){
75                 //第一种情况:套餐剩余流量可以支持使用1M流量            
76                 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB                
77             }else if(card.getMoney()>=0.1){
78                 //第二种情况:套餐流量已用完,账户余额可以支付1M流量,使用账户余额支付
79                 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB
80                 card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1M流量费用)
81                 card.setConsumAmount(card.getConsumAmount() + 0.1);
82             }else{
83                 temp = i;
84                 throw new Exception("本次已使用流量"+i+"MB,您的余额不足,请充值后再使用!");
85             }
86         }
87         return temp;
88     }
89 }

 

Scene:

 1 package cn.entity;
 2 
 3 /**
 4  * 使用场景
 5  */
 6 public class Scene {
 7     private String type;  //场景消费类型
 8     private int data;  //消费数据
 9     private String description;//场景描述
10     
11     public Scene(){}
12     public Scene(String type,int data,String description){
13         this.type = type;
14         this.data = data;
15         this.description = description;
16     }  
17     
18     public String getType() {
19         return type;
20     }
21     public void setType(String type) {
22         this.type = type;
23     }
24     public int getData() {
25         return data;
26     }
27     public void setData(int data) {
28         this.data = data;
29     }
30     public String getDescription() {
31         return description;
32     }
33     public void setDescription(String description) {
34         this.description = description;
35     }
36     
37 }

 

ServicePackage:

 1 package cn.entity;
 2 
 3 /**
 4  * 嗖嗖移动卡套餐
 5  */
 6 public abstract class ServicePackage {    
 7     protected double price;  //套餐月资费(元)
 8      
 9     public double getPrice() {
10         return price;
11     }
12 
13     public void setPrice(double price) {
14         this.price = price;
15     }
16 
17     //显示套餐数据
18     public abstract void showInfo();
19 }

 

SuperPackage:

  1 package cn.entity;
  2 
  3 import cn.common.Common;
  4 import cn.service.CallService;
  5 import cn.service.NetService;
  6 import cn.service.SendService;
  7 
  8 /**
  9  * 超人套餐
 10  */
 11 public class SuperPackage extends ServicePackage implements CallService,
 12 SendService,NetService {
 13     private int talkTime;   //通话时长(分钟)
 14     private int smsCount;   //短信条数(条)
 15     private int flow;  //上网流量(MB)
 16        
 17     public int getTalkTime() {
 18         return talkTime;
 19     }
 20 
 21 
 22     public void setTalkTime(int talkTime) {
 23         this.talkTime = talkTime;
 24     }
 25 
 26 
 27     public int getSmsCount() {
 28         return smsCount;
 29     }
 30 
 31 
 32     public void setSmsCount(int smsCount) {
 33         this.smsCount = smsCount;
 34     }
 35     
 36     public int getFlow() {
 37         return flow;
 38     }
 39 
 40     public void setFlow(int flow) {
 41         this.flow = flow;
 42     }
 43     
 44     public SuperPackage(){
 45         //套餐数据初始化
 46         this.talkTime = 200;
 47         this.smsCount = 50;
 48         this.flow = 1*1024;  
 49         this.price = 78.0;
 50     }
 51     @Override
 52     public void showInfo() {
 53         System.out.println("超人套餐:通话时长为"+this.talkTime+"分钟/月,短信条数为"+this.smsCount+"条/月,上网流量为"+this.flow/1024+"GB/月。");
 54     }
 55 
 56 
 57     /**
 58      * 提供上网服务
 59      */
 60     public int netPlay(int flow, MobileCard card) throws Exception {
 61         int temp = flow;
 62         for(int i=0;i<flow;i++){
 63             if(this.flow-card.getRealFlow()>=1){
 64                 //第一种情况:套餐剩余流量可以支持使用1M流量            
 65                 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB                
 66             }else if(card.getMoney()>=0.1){
 67                 //第二种情况:套餐流量已用完,账户余额可以支付1M流量,使用账户余额支付
 68                 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB
 69                 card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1M流量费用)
 70                 card.setConsumAmount(card.getConsumAmount() + 0.1);
 71             }else{
 72                 temp = i;
 73                 throw new Exception("本次已使用流量"+i+"MB,您的余额不足,请充值后再使用!");
 74             }
 75         }
 76         return temp;
 77     }
 78 
 79     /**
 80      * 提供通话服务
 81      */
 82     public int call(int minCount, MobileCard card) throws Exception{
 83         int temp = minCount; 
 84         for(int i=0;i<minCount;i++){
 85             if(this.talkTime-card.getRealTalkTime()>=1){
 86                 //第一种情况:套餐剩余通话时长可以付1分钟通话            
 87                 card.setRealTalkTime(card.getRealTalkTime()+1); //实际通话数据加1                
 88             }else if(card.getMoney()>=0.2){
 89                 //第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付
 90                 card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用通话时长1分钟 
 91                 card.setMoney(Common.sub(card.getMoney(),0.2)); //账户余额消费0.2元(1分钟额外通话)
 92                 card.setConsumAmount(card.getConsumAmount() + 0.2);
 93             }else{
 94                 temp = i; //记录实现通话分钟数
 95                 throw new Exception("本次已通话"+i+"分钟,您的余额不足,请充值后再使用!");                
 96             }
 97         }
 98         return temp;
 99     }
100     
101     /**
102      * 提供短信服务
103      */
104     public int sendMessage(int smsCount, MobileCard card) throws Exception {
105         int temp = smsCount;
106         for(int i=0;i<smsCount;i++){
107             if(this.smsCount-card.getRealSMSCount()>=1){
108                 //第一种情况:套餐剩余短信条数可以付1条短信            
109                 card.setRealSMSCount(card.getRealSMSCount()+1); //实际使用短信条数加1                
110             }else if(card.getMoney()>=0.1){
111                 //第二种情况:套餐短信条数已用完,账户余额可以支付1条短信,使用账户余额支付
112                 card.setRealSMSCount(card.getRealSMSCount()+1); 
113                 card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1条短信费用)
114                 card.setConsumAmount(card.getConsumAmount() + 0.1);
115             }else{
116                 temp = i;
117                 throw new Exception("本次已发送短信"+i+"条,您的余额不足,请充值后再使用!");
118             }
119         }
120         return temp;
121     }
122 
123 }

 

TalkPackage:

 1 package cn.entity;
 2 
 3 import cn.common.Common;
 4 import cn.service.CallService;
 5 import cn.service.SendService;
 6 
 7 /**
 8  * 话唠套餐
 9  */
10 public class TalkPackage extends ServicePackage implements CallService,
11         SendService {
12     private int talkTime; // 通话时长(分钟)
13     private int smsCount; // 短信条数(条)
14 
15     public int getTalkTime() {
16         return talkTime;
17     }
18 
19     public void setTalkTime(int talkTime) {
20         this.talkTime = talkTime;
21     }
22 
23     public int getSmsCount() {
24         return smsCount;
25     }
26 
27     public void setSmsCount(int smsCount) {
28         this.smsCount = smsCount;
29     }
30 
31     public TalkPackage() {
32         //套餐数据初始化
33         this.talkTime = 500;
34         this.smsCount = 30;
35         this.price = 58.0;
36     }
37 
38     public TalkPackage(int talkTime, int smsCount) {
39         super();
40         this.talkTime = talkTime;
41         this.smsCount = smsCount;
42     }
43 
44     /**
45      * 显示套餐详情
46      */
47     public void showInfo() {
48         System.out.println("话唠套餐:通话时长为" + this.talkTime + "分钟/月,短信条数为"
49                 + this.smsCount + "条/月,资费为" + this.price + "元/月。");
50     }
51     
52     public int call(int minCount, MobileCard card) throws Exception{
53         int temp = minCount; 
54         for(int i=0;i<minCount;i++){
55             if(this.talkTime-card.getRealTalkTime()>=1){
56                 //第一种情况:套餐剩余通话时长可以付1分钟通话            
57                 card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用流量加1MB                
58             }else if(card.getMoney()>=0.2){
59                 //第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付
60                 card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用通话时长1分钟 
61                 card.setMoney(Common.sub(card.getMoney(),0.2)); //账户余额消费0.2元(1M流量费用)
62                 card.setConsumAmount(card.getConsumAmount() + 0.2);
63             }else{
64                 temp = i; //记录实现通话分钟数
65                 throw new Exception("本次已通话"+i+"分钟,您的余额不足,请充值后再使用!");                
66             }
67         }
68         return temp;
69     }
70         
71     public int sendMessage(int smsCount, MobileCard card) throws Exception {
72         int temp = smsCount;
73         for(int i=0;i<smsCount;i++){
74             if(this.smsCount-card.getRealSMSCount()>=1){
75                 //第一种情况:套餐剩余短信条数可以付1条短信            
76                 card.setRealSMSCount(card.getRealSMSCount()+1); //实际使用短信条数加1                
77             }else if(card.getMoney()>=0.1){
78                 //第二种情况:套餐短信条数已用完,账户余额可以支付1条短信,使用账户余额支付
79                 card.setRealSMSCount(card.getRealSMSCount()+1); 
80                 card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1条短信费用)
81                 card.setConsumAmount(card.getConsumAmount() + 0.1);
82             }else{
83                 temp = i;
84                 throw new Exception("本次已发送短信"+i+"条,您的余额不足,请充值后再使用!");
85             }
86         }
87         return temp;
88     }
89     
90 }

 

CallService:

 1 package cn.service;
 2 
 3 import cn.entity.MobileCard;
 4 
 5 /**
 6  * 通话服务接口
 7  */
 8 public interface CallService {
 9       //打电话
10       public int call(int minCount,MobileCard card) throws Exception;
11 }

 

NetService:

 1 package cn.service;
 2 
 3 import cn.entity.MobileCard;
 4 
 5 /**
 6  * 上网服务
 7  */
 8 public interface NetService {
 9     //上网
10      public int netPlay(int flow,MobileCard card) throws Exception;
11 }

 

SendService:

 1 package cn.service;
 2 
 3 import cn.entity.MobileCard;
 4 
 5 /**
 6  * 短信服务
 7  */
 8 public interface SendService {
 9     //发短信
10      public int sendMessage(int count,MobileCard card) throws Exception;
11 }

 

CardUtil:

  1 package cn.utils;
  2 
  3 import java.io.FileReader;
  4 import java.io.FileWriter;
  5 import java.io.IOException;
  6 import java.io.Reader;
  7 import java.io.Writer;
  8 import java.text.DecimalFormat;
  9 import java.util.ArrayList;
 10 import java.util.HashMap;
 11 import java.util.Iterator;
 12 import java.util.List;
 13 import java.util.Map;
 14 import java.util.Random;
 15 import java.util.Set;
 16 
 17 import cn.common.Common;
 18 import cn.entity.ConsumInfo;
 19 import cn.entity.MobileCard;
 20 import cn.entity.NetPackage;
 21 import cn.entity.Scene;
 22 import cn.entity.ServicePackage;
 23 import cn.entity.SuperPackage;
 24 import cn.entity.TalkPackage;
 25 import cn.service.CallService;
 26 import cn.service.NetService;
 27 import cn.service.SendService;
 28 
 29 /**
 30  * 手机卡工具类
 31  */
 32 @SuppressWarnings("unused")
 33 public class CardUtil {
 34     Map<String, MobileCard> cards = new HashMap<String, MobileCard>(); // 所有手机卡的列表
 35     Map<String, List<ConsumInfo>> consumInfos = new HashMap<String, List<ConsumInfo>>(); // 所有手机卡消费记录的列表
 36     List<Scene> scenes = new ArrayList<Scene>();
 37 
 38     // 初始化用户
 39     public void init() {
 40         MobileCard card1 = new MobileCard("何玲玲", "123", "13965756432",
 41                 new TalkPackage(), 58.0, 42.0);
 42         MobileCard card2 = new MobileCard("黄露露", "123", "13956712467",
 43                 new NetPackage(), 68.0, 32.0);
 44         MobileCard card3 = new MobileCard("朱蓉蓉", "123", "13911568956",
 45                 new SuperPackage(), 78.0, 22.0);
 46         MobileCard card4 = new MobileCard("桃跑跑", "123", "13924221868",
 47                 new TalkPackage(), 78.0, 2.0);
 48         card4.setConsumAmount(98.0);
 49         card4.setRealTalkTime(500);
 50         card4.setRealSMSCount(100);
 51         cards.put("13965756432", card1);
 52         cards.put("13956712467", card2);
 53         cards.put("13911568956", card3);
 54         cards.put("13924221868", card4);
 55     }
 56     
 57     /**
 58      * 使用场景初始化
 59      */
 60     public void initScenes(){    
 61         scenes.add(new Scene("通话",90,"问候客户,谁知其如此难缠 通话90分钟"));
 62         scenes.add(new Scene("通话",30,"询问妈妈身体状况 本地通话30分钟"));
 63         scenes.add(new Scene("短信",5,"参与环境保护实施方案问卷调查 发送短信5条"));
 64         scenes.add(new Scene("短信",50,"通知朋友手机换号,发送短信50条"));
 65         scenes.add(new Scene("上网",1*1024,"和女友微信视频聊天   使用流量1G"));
 66         scenes.add(new Scene("上网",2*1024,"晚上手机在线看韩剧,不留神睡着啦! 使用流量 2G"));        
 67     }
 68 
 69     /**
 70      * 是否存在此卡用户
 71 
 72      * 
 73      * @param number
 74      * @param passWord
 75      * @return
 76      */
 77     public boolean isExistCard(String number, String passWord) {
 78         Set<String> numbers = cards.keySet();
 79         Iterator<String> it = numbers.iterator();
 80         while (it.hasNext()) {
 81             String searchNum = it.next();
 82             if (searchNum.equals(number)
 83                     && (cards.get(searchNum)).getPassWord().equals(passWord)) {
 84                 return true;
 85             }
 86         }
 87         return false;
 88     }
 89     
 90     /**
 91      * 查找指定卡号是否已注册
 92      * 
 93      * @param searchNumber
 94      * @return 未注册:false 已注册:true
 95      */
 96     public boolean isExistCard(String searchNumber) {
 97         Set<String> numbers = cards.keySet();
 98         for (String number : numbers) {
 99             if (number.equals(searchNumber)) {
100                 return true;
101             }
102         }
103         return false;
104     }
105 
106     /**
107      * 创建卡号(以139开头 11位)
108      * 
109      * @return 生成的随机手机卡号
110      */
111     public String createNumber() {
112         Random random = new Random();
113         boolean isExist = false; // 记录现有用户中是否存在此卡号用户 是:true 否:false
114         String number = "";
115         int temp = 0;
116         do {
117             isExist = false; // 标志位重置为false,用于控制外重循环,当生成了
118             // 生成的随机数是8位 不能小于10000000,否则重新生成
119             do {
120                 temp = random.nextInt(100000000);
121             } while (temp < 10000000);
122             // 生成之前,前面加“139”
123             number = "139" + temp;
124             // 和现有用户的卡号比较,不能是重复
125             Set<String> cardNumbers = cards.keySet();
126             for (String cardNumber : cardNumbers) {
127                 if (number.equals(cardNumber)) {
128                     isExist = true;
129                     break;
130                 }
131             }
132         } while (isExist);
133         return number;
134     }
135 
136     /**
137      * 生成指定个数的新卡号列表
138      * 
139      * @param count
140      *            指定个数
141      * @return 卡号列表
142      */
143     public String[] getNewNumbers(int count) {
144 
145         String[] numbers = new String[count];
146         for (int i = 0; i < count; i++) {
147             numbers[i] = createNumber();
148         }
149         return numbers;
150     }
151 
152     /**
153      * 添加新卡
154      * 
155      * @param card
156      *            新卡
157      */
158     public void addCard(MobileCard card) {
159         cards.put(card.getCardNumber(), card);
160         System.out.print("注册成功!");
161         card.showMeg();
162     }
163 
164     /**
165      * 指定卡号办理退网
166      * 
167      * @param card
168      */
169     public void delCard(String delNumber) {
170         if (isExistCard(delNumber)) {
171             cards.remove(delNumber);
172             System.out.println("卡号" + delNumber + "办理退网成功!");
173         } else {
174             System.out.println("对不起,该卡号未注册,不能办退退网!");
175         }
176     }
177 
178     /**
179      * 查询指定卡套餐余量
180      * 
181      * @param number
182      */
183     public void showRemainDetail(String searchNumber) {
184         MobileCard card; // 要查询的卡
185         int remainTalkTime;
186         int remainSmsCount;
187         int remainFlow;
188         StringBuffer meg = new StringBuffer();
189             card = cards.get(searchNumber);
190             meg.append("您的卡号是" + searchNumber + ",套餐内剩余:\n");
191             ServicePackage pack = card.getSerPackage();
192             if (pack instanceof TalkPackage) {
193                 //向下转型为话唠套餐对象
194                 TalkPackage cardPack = (TalkPackage) pack;
195                 // 话唠套餐,查询套餐内剩余的通话时长和短信条数
196                 remainTalkTime = cardPack.getTalkTime() > card
197                         .getRealTalkTime() ? cardPack.getTalkTime()
198                         - card.getRealTalkTime() : 0;
199                 meg.append("通话时长:" + remainTalkTime + "分钟\n");
200                 remainSmsCount = cardPack.getSmsCount() > card
201                         .getRealSMSCount() ? cardPack.getSmsCount()
202                         - card.getRealSMSCount() : 0;
203                 meg.append("短信条数:" + remainSmsCount + "条");
204             } else if (pack instanceof NetPackage) {
205                 //向下转型为网虫套餐对象
206                 NetPackage cardPack = (NetPackage) pack;
207                 // 网虫套餐:查询套餐内剩余的上网流量
208                 remainFlow = cardPack.getFlow() > card.getRealFlow() ? cardPack
209                         .getFlow() - card.getRealFlow() : 0;
210                 meg.append("上网流量:" + Common.dataFormat(remainFlow * 1.0 / 1024)
211                         + "GB");
212             } else if (pack instanceof SuperPackage) {
213                 //向下转型为超人套餐对象
214                 SuperPackage cardPack = (SuperPackage) pack;
215                 // 超人套餐:查询套餐内剩余的通话时长、短信条数、上网流量
216                 remainTalkTime = cardPack.getTalkTime() > card
217                         .getRealTalkTime() ? cardPack.getTalkTime()
218                         - card.getRealTalkTime() : 0;
219                 meg.append("通话时长:" + remainTalkTime + "分钟\n");
220                 remainSmsCount = cardPack.getSmsCount() > card
221                         .getRealSMSCount() ? cardPack.getSmsCount()
222                         - card.getRealSMSCount() : 0;
223                 meg.append("短信条数:" + remainSmsCount + "条\n");
224                 remainFlow = cardPack.getFlow() > card.getRealFlow() ? cardPack
225                         .getFlow() - card.getRealFlow() : 0;
226                 meg.append("上网流量:" + Common.dataFormat(remainFlow * 1.0 / 1024)
227                         + "GB");
228             }
229             System.out.println(meg);
230     }
231 
232     /**
233      * 查询指定卡当月消费详单
234      * 
235      * @param searchNumber
236      */
237     public void showAmountDetail(String searchNumber) {
238         MobileCard card; // 要查询的卡
239         StringBuffer meg = new StringBuffer();
240         card = cards.get(searchNumber);
241         meg.append("您的卡号:" + card.getCardNumber() + ",当月账单:\n");
242         meg.append("套餐资费:" + card.getSerPackage().getPrice() + "元\n");
243         meg.append("合计:" + Common.dataFormat(card.getConsumAmount()) + "元\n");
244         meg.append("账户余额:" + Common.dataFormat(card.getMoney()) + "元");
245         // 显示本月消费详细信息
246         System.out.println(meg);
247     }
248 
249     
250     /**
251      * 指定卡号换套餐
252      * 
253      * @param number
254      * @param packType
255      */
256     public void changingPack(String number, String packNum) {
257         MobileCard card; // 指定的手机卡
258         ServicePackage pack; // 要换的套餐
259         if (isExistCard(number)) {
260             card = cards.get(number);
261             // 获取要换的套餐对象
262             switch (packNum) {
263             case "1":
264                 pack = new TalkPackage();
265                 break;
266             case "2":
267                 pack = new NetPackage();
268                 break;
269             default:
270                 pack = new SuperPackage();
271                 break;
272             }        
273             if (!(card.getSerPackage().getClass().getName().equals(pack.getClass().getName()))) {
274                 // 该卡余额中减去当月套餐资费
275                 if (card.getMoney() >= pack.getPrice()) {
276                     card.setMoney(card.getMoney() - pack.getPrice());
277                     // 换套餐
278                     card.setSerPackage(pack);
279                     // 当月实际使用数据清零
280                     card.setRealTalkTime(0);
281                     card.setRealFlow(0);
282                     card.setRealSMSCount(0);
283                     // 当月消费金额设置为新套餐月资费
284                     card.setConsumAmount(pack.getPrice());
285                     System.out.print("更换套餐成功!");
286                     pack.showInfo();
287                 } else {
288                     System.out.println("对不起,您的余额不足以支付新套餐本月资费,请充值后再办理更换套餐业务!");
289                     return;
290                 }
291             } else {
292                 System.out.println("对不起,您已经是该套餐用户,无需换套餐!");
293             }
294 
295         } else {
296             System.out.println("对不起,该卡号未注册,不能换套餐!");
297         }
298     }
299 
300     /**
301      * 为指定手机卡充值
302      * 
303      * @param number
304      *            指定充值的卡号
305      * @param money
306      *            充值金额
307      */
308     public void chargeMoney(String number, double money) {
309         MobileCard card; // 指定的手机卡
310         if (money < 50) {
311             System.out.println("对不起,最低充值金额为50元!");
312             return;
313         }
314             card = cards.get(number);
315             card.setMoney(card.getMoney() + money);
316             System.out.println("充值成功,当前话费余额为" + Common.dataFormat(card.getMoney()) + "元。");
317     }
318 
319     /**
320      * 添加一条指定卡的消费记录
321      * 
322      * @param number
323      *            要添加消费记录的卡
324      * @param info
325      *            要添加的消费记录
326      */
327     public void addConsumInfo(String number, ConsumInfo info) {
328         Set<String> numbers = consumInfos.keySet();
329         Iterator<String> it = numbers.iterator();
330         List<ConsumInfo> infos = new ArrayList<ConsumInfo>();
331         boolean isExist = false; // 现有消费列表中是否存在此卡号消费记录,是:true 否:false
332         while (it.hasNext()) {
333             if (it.next().equals(number)) {
334                 // 消费集合中已有该卡号消费记录,则找到该卡号的消费记录集合,添加一条即可
335                 infos = consumInfos.get(number);
336                 infos.add(info);
337                 isExist = true;
338                 System.out.println("已添加一条消费记录。");
339                 break;
340             }
341         }
342         // 该集合中没有此卡号消费记录,则添加
343         if (!isExist) {
344             infos.add(info);
345             consumInfos.put(number, infos);
346             System.out.println("不存在此卡的消费记录,已添加一条消费记录。");
347         }
348     }
349     
350     //打印消费记录
351     public void printConsumInfo(String number){
352         Writer fileWriter = null;
353         try {
354              fileWriter = new FileWriter(number+"消费记录.txt");            
355             Set<String> numbers = consumInfos.keySet();
356             Iterator<String> it = numbers.iterator();
357             List<ConsumInfo> infos = new ArrayList<ConsumInfo>();//存储指定卡所有消费记录
358             boolean isExist = false; // 现有消费列表中是否存在此卡号消费记录,是:true 否:false
359             while (it.hasNext()) {
360                 if (it.next().equals(number)) {
361                     infos = consumInfos.get(number);
362                     isExist = true;
363                     break;
364                 }
365             }
366             if(isExist){
367                 //存在 此卡消费记录,写入文本文件
368                 StringBuffer content = new StringBuffer("******"+number+"消费记录******\n");
369                 content.append("序号\t类型\t数据(通话(条)/上网(MB)/短信(条))\n");
370                 for(int i=0;i<infos.size();i++){
371                     ConsumInfo info = infos.get(i);
372                     content.append((i+1)+".\t"+info.getType()+"\t"+info.getConsumData()+"\n");
373                 }
374                 fileWriter.write(content.toString());
375                 fileWriter.flush();
376                 
377                 System.out.println("消费记录打印完毕!");
378             }else{
379                 System.out.println("对不起,不存在此号码的消费记录,不能打印!");
380             }            
381         } catch (IOException e) {            
382             e.printStackTrace();
383         }finally{
384             if(fileWriter!=null){
385                 try {
386                     fileWriter.close();
387                 } catch (IOException e) {                    
388                     e.printStackTrace();
389                 }
390             }
391         }
392     }
393     
394     /**
395      * 使用嗖嗖
396      * @param number 当前卡号
397      * @throws Exception 
398      */
399     public void userSoso(String number)  {        
400         MobileCard card = cards.get(number); // 获取此卡对象
401         ServicePackage pack = card.getSerPackage(); // 获取此卡所属套餐
402         Random random = new Random();
403         int ranNum = 0;
404         int temp = 0;  //记录各场景中实际消费数据
405         do{
406             
407             ranNum = random.nextInt(6);// 生成一个0~5之前的随机数
408             Scene scene = scenes.get(ranNum); //获取该序号所对应的场景
409             switch (ranNum) {
410             //序号为0或1为通话场景
411             case 0:
412             case 1:
413                 // 判断该卡所属套餐是否支持通话功能
414                 if (pack instanceof CallService) {
415                     // 执行通话方法
416                     System.out.println(scene.getDescription());
417                     CallService callService = (CallService) pack;
418                     try {
419                         temp = callService.call(scene.getData(), card);
420                     } catch (Exception e) {                            
421                         e.printStackTrace();
422                     }
423                     // 添加一条消费记录
424                     addConsumInfo(number, new ConsumInfo(number,
425                             scene.getType(), temp));
426                     break;
427                 } else {
428                     // 如果该卡套餐不支持通话功能,则重新生成随机数选择其他场景
429                     continue;
430                 }
431                 //序号为2或3为发短信场景
432             case 2:
433             case 3:
434                 // 判断该卡所属套餐是否支持短信功能
435                 if (pack instanceof SendService) {
436                     // 执行发短信方法
437                     System.out.println(scene.getDescription());
438                     SendService sendService = (SendService) pack;
439                     try {
440                         temp = sendService.sendMessage(scene.getData(), card);
441                     } catch (Exception e) {                                                    
442                         e.printStackTrace();
443                     }
444                     // 添加一条消费记录
445                     addConsumInfo(number, new ConsumInfo(number,
446                             scene.getType(), temp));
447                     break;
448                 } else {
449                     // 如果该卡套餐不支持发短信功能,则重新生成随机数选择其他场景
450                     continue;
451                 }
452                 //序号为4或5为发上网场景
453             case 4:
454             case 5:
455                 // 判断该卡所属套餐是否支持上网功能
456                 if (pack instanceof NetService) { 
457                     System.out.println(scene.getDescription());
458                     NetService netService = (NetService) pack;
459                     // 执行上网方法
460                     try {
461                         temp = netService.netPlay(scene.getData(), card);
462                     } catch (Exception e) {                        
463                         e.printStackTrace();
464                     }
465                     // 添加一条消费记录
466                     addConsumInfo(number, new ConsumInfo(number,
467                             scene.getType(), temp));
468                     break;
469                 } else {
470                     // 如果该卡套餐不支持上网功能,则重新生成随机数选择其他场景
471                     continue;
472                 }                
473             }    
474             break;
475         }while(true);
476     }
477 
478     /**
479      * 根据套餐序号返回套餐对象
480      * 
481      * @param packNum
482      *            套餐序号
483      * @return 套餐对象
484      */
485     public ServicePackage createPack(int packId) {
486         ServicePackage pack = null;
487         switch (packId) {
488         case 1:
489             pack = new TalkPackage();
490             break;
491         case 2:
492             pack = new NetPackage();
493             break;
494         case 3:
495             pack = new SuperPackage();
496             break;
497         }
498         return pack;
499     }
500     
501     /**
502      * 显示资费说明
503      */
504     public void showDescription(){
505         Reader rd = null;
506         try {
507             rd = new FileReader("套餐资费说明.txt");
508              char[] content = new char[1024];
509              int len = 0;
510              StringBuffer sb = new StringBuffer();
511                 while((len=rd.read(content))!=-1){
512                     sb.append(content,0,len);  //拼接字符串
513                 }
514                 System.out.println(sb);
515         } catch (IOException e) {
516             // TODO Auto-generated catch block
517             e.printStackTrace();
518         }
519     }
520     
521 }

 

执行程序结果图(一部分):

 


原文链接:https://www.cnblogs.com/zhang0923-/p/10860498.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:文本相似度计算之余弦定理

下一篇:SpringMVC配置文件 中 mvcview-controller 标签的使用