• Java面向对象和高级特性 项目实战(一)


    一、项目简介

    • 项目名:嗖嗖移动业务大厅

    • 技能点:

      

    二、技能点 

      

    三、系统概述

      

    四、整体开发思路

      

    五、实体类和接口开发

       

       

    六、 创建工具类

      

    七、使用集合存储数据

      

    八、开发计划

      

    九、代码实现  

      1.项目目录

      

      2.具体代码

    • 测试类
    package cn.soso.biz;
    
    import java.util.Scanner;
    
    import cn.soso.entity.MobileCard;
    import cn.soso.entity.ServicePackage;
    import cn.soso.utils.CardUtil;
    
    /**
     * 业务类
     *
     * @author yu
     *
     */
    public class SosoMgr {
        Scanner input = new Scanner(System.in);
        CardUtil utils = new CardUtil();
    
        public static void main(String[] args) {
            SosoMgr soso = new SosoMgr();
            soso.mainMenu();
            System.out.println("谢谢使用!");
        }
    
        /**
         * 主流程
         */
        public void mainMenu() {
            int menuChoose = 0;
            String mobileNumber= "";
            String password = "";
            utils.init();
            utils.initScenes();
            //Common.typesInit();
            do {
                System.out.println("
    *************欢迎使用嗖嗖移动业务大厅***************");
                System.out.println("1.用户登录   2.用户注册   3.使用嗖嗖   4.话费充值  5.资费说明  6.退出系统");
                System.out.print("请选择:");
                menuChoose = input.nextInt();
                // 分支语句:根据功能编号执行相应功能
                switch (menuChoose) {
                    case 1:
                        //用户登录
                        System.out.print("请输入手机卡号:");
                        mobileNumber = input.next();
                        System.out.print("请输入密码:");
                        password = input.next();
                        if (utils.isExistCard(mobileNumber, password)) {
                            cardMenu(mobileNumber);
                        }else{
                            System.out.println("对不起,您输入的信息有误,无法登录!");
                        }
                        continue;
                    case 2:
                        //用户注册
                        registCard();
                        continue;
                    case 3:
    
                        //使用嗖嗖
                        System.out.print("请输入手机卡号:");
                        mobileNumber = input.next();
    
                        if (utils.isExistCard(mobileNumber)) {
                            try {
                            /*System.out.println("****使用之前****");
                            cn.soso.utils.showRemainDetail(mobileNumber);
                            cn.soso.utils.showAmountDetail(mobileNumber);*/
                                utils.userSoso(mobileNumber);
                            } catch (Exception e) {
                                System.err.println(e.getMessage());
                            }
                        }else{
                            System.out.println("对不起,该卡号未注册,不能使用!");
                        }
    
                    /*System.out.println("****使用之后****");
                    cn.soso.utils.showRemainDetail(mobileNumber);
                    cn.soso.utils.showAmountDetail(mobileNumber);*/
                        continue;
                    case 4:
                        //话费充值
                        System.out.print("请输入充值卡号:");
                        mobileNumber = input.next();
                        if (utils.isExistCard(mobileNumber)) {
                            System.out.print("请输入充值金额:");
                            double money = input.nextDouble();
                            utils.chargeMoney(mobileNumber, money);
                        }else{
                            System.out.println("对不起,要充值的卡号未注册,无法充值!");
                        }
                        continue;
                    case 5:
                        System.out.println("
    *****资费说明******");
                        utils.showDescription();
                        continue;
                    case 6:
                        //退出系统
                        break;
                    default:
                        //选择其他数字退出系统
                        break;
                }
                break;
            } while (true);
        }
    
        /**
         * 手机卡功能菜单
         *
         * @param number
         * @return
         */
        public int cardMenu(String mobileNumber) {
            int menuChoose = 0;
            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选择功能,其他键返回上一级):");
                menuChoose = input.nextInt();
                switch (menuChoose) {
                    case 1:
                        System.out.println("
    *****本月账单查询******");
                        utils.showAmountDetail(mobileNumber);
                        continue;
                    case 2:
                        System.out.println("
    *****套餐余量查询******");
                        utils.showRemainDetail(mobileNumber);
                        continue;
                    case 3:
                        System.out.println("
    *****消费详单查询******");
                        utils.printConsumInfo(mobileNumber);
                        continue;
                    case 4:
                        System.out.println("
    *****套餐变更******");
                        System.out.print("1.话唠套餐  2.网虫套餐  3.超人套餐  请选择(序号):");
                        utils.changingPack(mobileNumber, input.next());
                        continue;
                    case 5:
                        System.out.println("
    *****办理退网******");
                        utils.delCard(mobileNumber);
                        System.out.println("谢谢使用!");
                        System.exit(1);     //办理退网后退出系统
    
                }
    
                break;
            } while (true);
            return menuChoose;
        }
    
        /**
         * 注册新卡流程
         */
        public void registCard(){
            String[] newNumbers = utils.getNewNumbers(9);
            //显示可供选择的手机号列表
            System.out.println("*****可选择的卡号*****");
    
            for(int i=0;i<9;i++){
                System.out.print((i+1)+"."+newNumbers[i]+"		");
                if((i+1)%3==0){
                    System.out.println();
                }
            }
            //选择手机号
            System.out.print("请选择卡号(输入1~9的序号):");
            String number = newNumbers[input.nextInt()-1];
    
            //选择套餐类型
            System.out.print("1.话唠套餐  2.网虫套餐  3.超人套餐,  ");
            System.out.print("请选择套餐(输入序号):");
            //cn.soso.utils.getPackList();
            //获取套餐对象
            ServicePackage pack = utils.createPack(input.nextInt());
    
            //输入用户名
            System.out.print("请输入姓名:");
            String name = input.next();
    
            //输入密码
            System.out.print("请输入密码:");
            String password = input.next();
    
            //输入预存话费金额
            double money = 0;
            System.out.print("请输入预存话费金额:");
            money = input.nextDouble();
            while(money<pack.getPrice()){
                System.out.print("您预存的话费金额不足以支付本月固定套餐资费,请重新充值:");
                money = input.nextDouble();
            }
    
            //创建新卡对象并添加
            MobileCard newCard = new MobileCard(name,password,number,pack,pack.getPrice(),money-pack.getPrice());
            utils.addCard(newCard);
        }
    }

    • 公共类
    package cn.soso.common;
    
    import java.text.DecimalFormat;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * 公共类
     * @author yu
     *
     */
    public class Common {
        /**
         * double类型格式化
         * @param data
         * @return
         */
        public static String dataFormat(double data) {
            DecimalFormat formatData = new DecimalFormat("#.0");
            return formatData.format(data);
        }
    
        /**
         * double类型两数相减
         * @param num1
         * @param num2
         * @return
         */
        public static double sub(double num1,double num2){
            return (num1*10-num2*10)/10;
        }
    }
    package cn.soso.common;
    /**
     * 消费类型
     * @author rong.zhou
     *
     */
    public enum ConsumType {
       TALK,SMS,NETWORK
    }
    • 实体类
    package cn.soso.entity;
    import cn.soso.common.ConsumType;
    /**
     * 消费信息
     * @author yu
     *
     */
    public class ConsumInfo {
        private String cardNumber;  //卡号
        private String type;  //消费类型:通话、发短信、上网
        private int consumData;   //消费数据   通话:分钟   发短信:条   上网:MB
    
        public ConsumInfo(){}
        public ConsumInfo(String cardNumber, String type, int consumData) {
            super();
            this.cardNumber = cardNumber;
            this.type = type;
            this.consumData = consumData;
        }
        public String getCardNumber() {
            return cardNumber;
        }
        public void setCardNumber(String cardNumber) {
            this.cardNumber = cardNumber;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public int getConsumData() {
            return consumData;
        }
        public void setConsumData(int consumData) {
            this.consumData = consumData;
        }
    }
    package cn.soso.entity;
    /**
     * 手机卡
     * @author yu
     *
     */
    public class MobileCard {
        private String cardNumber;  //卡号
        private String userName;  //用户名
        private String passWord;  //密码
        private ServicePackage serPackage;  //所属套餐
        private double consumAmount;  //当月消费金额
        private double money;  //账户余额
        private int realTalkTime;  //实际通话时长(分钟)
        private int realSMSCount;  //实际发送短信条数(条)
        private int realFlow;  //实际上网流量
    
        public MobileCard(){}
    
        public MobileCard(String userName, String passWord, String cardNumber,
                          ServicePackage serPackage, double consumAmount, double money) {
            super();
            this.userName = userName;
            this.passWord = passWord;
            this.cardNumber = cardNumber;
            this.serPackage = serPackage;
            this.consumAmount = consumAmount;
            this.money = money;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassWord() {
            return passWord;
        }
    
        public void setPassWord(String passWord) {
            this.passWord = passWord;
        }
    
        public String getCardNumber() {
            return cardNumber;
        }
    
        public void setCardNumber(String cardNumber) {
            this.cardNumber = cardNumber;
        }
    
        public ServicePackage getSerPackage() {
            return serPackage;
        }
    
        public void setSerPackage(ServicePackage serPackage) {
            this.serPackage = serPackage;
        }
    
        public double getConsumAmount() {
            return consumAmount;
        }
    
        public void setConsumAmount(double consumAmount) {
            this.consumAmount = consumAmount;
        }
    
        public double getMoney() {
            return money;
        }
    
        public void setMoney(double money) {
            this.money = money;
        }
    
        public int getRealTalkTime() {
            return realTalkTime;
        }
    
        public void setRealTalkTime(int realTalkTime) {
            this.realTalkTime = realTalkTime;
        }
    
        public int getRealSMSCount() {
            return realSMSCount;
        }
    
        public void setRealSMSCount(int realSMSCount) {
            this.realSMSCount = realSMSCount;
        }
    
        public int getRealFlow() {
            return realFlow;
        }
    
        public void setRealFlow(int realFlow) {
            this.realFlow = realFlow;
        }
    
        /**
         * 显示卡信息
         */
        public void showMeg(){
            System.out.println("卡号:"+this.cardNumber+" 用户名:"+this.userName+" 当前余额:"+this.money+"元。");
            this.serPackage.showInfo();
        }
    }
    package cn.soso.entity;
    
    import cn.soso.common.Common;
    import cn.soso.service.NetService;
    
    /**
     * 网虫套餐
     *
     * @author yu
     *
     */
    public class NetPackage extends ServicePackage implements NetService {
        private int flow; // 上网流量(MB)
    
        public NetPackage() {
            //套餐数据初始化
            this.flow = 1024 * 3;
            this.price = 68.0;
        }
    
        public NetPackage(int flow) {
            super();
            this.flow = flow;
        }
    
        public int getFlow() {
            return flow;
        }
    
        public void setFlow(int flow) {
            this.flow = flow;
        }
    
    
    
        @Override
        public void showInfo() {
            System.out.println("网虫套餐:上网流量是" + flow / 1024 + "GB/月,月资费是"
                    + this.price + "元/月。");
        }
    
        /**
         * 提供上网服务
         */
        public void netPlay2(int flow, MobileCard card) throws Exception {
            int reminFlow = this.flow - card.getRealFlow();  //卡中可支付的免费流量
            // 判断套餐中的上网流量是否足够支付本次上网服务
            if (this.flow <= reminFlow) {
                // 套餐中上网流量足够:修改该卡实际上网流量数据
                card.setRealFlow(card.getRealFlow() + flow);
            } else {
                // 套餐中上网流量不够:额外消费需按0.1元/条付费,额外消费金额=0.1*(该卡实际消费上网流量+本次消费上网流量-套餐包含的上网流量)
                double consumeMoney = 0.1 * (flow-reminFlow);
                // 该卡账户余额足够支付:修改该卡实际使用的上网流量、账户余额、当月消费金额
                if (card.getMoney() >= consumeMoney) {
                    //消耗的流量增加
                    card.setRealFlow(card.getRealFlow() + flow);
                    // 当前账户余额=当前账户余额-额外消费金额
                    card.setMoney(card.getMoney() - consumeMoney);
                    // 当月消费金额=当月消费金额+额外消费金额
                    card.setConsumAmount(card.getConsumAmount() + consumeMoney);
                } else {
    
                    int temp = (int)(card.getMoney()/0.1); //当前余额够大
                    throw new 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){
                    //第一种情况:套餐剩余流量可以支持使用1M流量
                    card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB
                }else if(card.getMoney()>=0.1){
                    //第二种情况:套餐流量已用完,账户余额可以支付1M流量,使用账户余额支付
                    card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB
                    card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1M流量费用)
                    card.setConsumAmount(card.getConsumAmount() + 0.1);
                }else{
                    temp = i;
                    throw new Exception("本次已使用流量"+i+"MB,您的余额不足,请充值后再使用!");
                }
            }
            return temp;
        }
    }
    package cn.soso.entity;
    /**
     * 使用场景
     * @author yu
     *
     */
    public class Scene {
        private String type;  //场景消费类型
        private int data;  //消费数据
        private String description;//场景描述
    
        public Scene(){}
        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 cn.soso.entity;
    /**
     * 嗖嗖移动卡套餐
     * @author yu
     *
     */
    public abstract class ServicePackage {
        protected double price;  //套餐月资费(元)
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        //显示套餐数据
        public abstract void showInfo();
    }
    package cn.soso.entity;
    
    import cn.soso.common.Common;
    import cn.soso.service.CallService;
    import cn.soso.service.NetService;
    import cn.soso.service.SendService;
    
    /**
     * 超人套餐
     * @author yu
     *
     */
    public class SuperPackage extends ServicePackage implements CallService,
            SendService,NetService {
        private int talkTime;   //通话时长(分钟)
        private int smsCount;   //短信条数(条)
        private int flow;  //上网流量(MB)
    
        public int getTalkTime() {
            return talkTime;
        }
    
    
        public void setTalkTime(int talkTime) {
            this.talkTime = talkTime;
        }
    
    
        public int getSmsCount() {
            return smsCount;
        }
    
    
        public void setSmsCount(int smsCount) {
            this.smsCount = smsCount;
        }
    
        public int getFlow() {
            return flow;
        }
    
        public void setFlow(int flow) {
            this.flow = flow;
        }
    
        public SuperPackage(){
            //套餐数据初始化
            this.talkTime = 200;
            this.smsCount = 50;
            this.flow = 1*1024;
            this.price = 78.0;
        }
        @Override
        public void showInfo() {
            System.out.println("超人套餐:通话时长为"+this.talkTime+"分钟/月,短信条数为"+this.smsCount+"条/月,上网流量为"+this.flow/1024+"GB/月。");
        }
    
    
        /**
         * 提供上网服务
         */
        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){
                    //第一种情况:套餐剩余流量可以支持使用1M流量
                    card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB
                }else if(card.getMoney()>=0.1){
                    //第二种情况:套餐流量已用完,账户余额可以支付1M流量,使用账户余额支付
                    card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB
                    card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1M流量费用)
                    card.setConsumAmount(card.getConsumAmount() + 0.1);
                }else{
                    temp = i;
                    throw new Exception("本次已使用流量"+i+"MB,您的余额不足,请充值后再使用!");
                }
            }
            return temp;
        }
    
        /**
         * 提供通话服务
         */
        public int call(int minCount, MobileCard card) throws Exception{
            int temp = minCount;
            for(int i=0;i<minCount;i++){
                if(this.talkTime-card.getRealTalkTime()>=1){
                    //第一种情况:套餐剩余通话时长可以付1分钟通话
                    card.setRealTalkTime(card.getRealTalkTime()+1); //实际通话数据加1
                }else if(card.getMoney()>=0.2){
                    //第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付
                    card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用通话时长1分钟
                    card.setMoney(Common.sub(card.getMoney(),0.2)); //账户余额消费0.2元(1分钟额外通话)
                    card.setConsumAmount(card.getConsumAmount() + 0.2);
                }else{
                    temp = i; //记录实现通话分钟数
                    throw new Exception("本次已通话"+i+"分钟,您的余额不足,请充值后再使用!");
                }
            }
            return temp;
        }
    
        /**
         * 提供短信服务
         */
        public int sendMessage(int smsCount, MobileCard card) throws Exception {
            int temp = smsCount;
            for(int i=0;i<smsCount;i++){
                if(this.smsCount-card.getRealSMSCount()>=1){
                    //第一种情况:套餐剩余短信条数可以付1条短信
                    card.setRealSMSCount(card.getRealSMSCount()+1); //实际使用短信条数加1
                }else if(card.getMoney()>=0.1){
                    //第二种情况:套餐短信条数已用完,账户余额可以支付1条短信,使用账户余额支付
                    card.setRealSMSCount(card.getRealSMSCount()+1);
                    card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1条短信费用)
                    card.setConsumAmount(card.getConsumAmount() + 0.1);
                }else{
                    temp = i;
                    throw new Exception("本次已发送短信"+i+"条,您的余额不足,请充值后再使用!");
                }
            }
            return temp;
        }
    
    }
    package cn.soso.entity;
    import cn.soso.common.Common;
    import cn.soso.service.CallService;
    import cn.soso.service.SendService;
    
    /**
     * 话唠套餐
     *
     * @author yu
     *
     */
    public class TalkPackage extends ServicePackage implements CallService,
            SendService {
        private int talkTime; // 通话时长(分钟)
        private int smsCount; // 短信条数(条)
    
        public int getTalkTime() {
            return talkTime;
        }
    
        public void setTalkTime(int talkTime) {
            this.talkTime = talkTime;
        }
    
        public int getSmsCount() {
            return smsCount;
        }
    
        public void setSmsCount(int smsCount) {
            this.smsCount = smsCount;
        }
    
        public TalkPackage() {
            //套餐数据初始化
            this.talkTime = 500;
            this.smsCount = 30;
            this.price = 58.0;
        }
    
        public TalkPackage(int talkTime, int smsCount) {
            super();
            this.talkTime = talkTime;
            this.smsCount = smsCount;
        }
    
        /**
         * 显示套餐详情
         */
        public void showInfo() {
            System.out.println("话唠套餐:通话时长为" + this.talkTime + "分钟/月,短信条数为"
                    + this.smsCount + "条/月,资费为" + this.price + "元/月。");
        }
    
        public int call(int minCount, MobileCard card) throws Exception{
            int temp = minCount;
            for(int i=0;i<minCount;i++){
                if(this.talkTime-card.getRealTalkTime()>=1){
                    //第一种情况:套餐剩余通话时长可以付1分钟通话
                    card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用流量加1MB
                }else if(card.getMoney()>=0.2){
                    //第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付
                    card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用通话时长1分钟
                    card.setMoney(Common.sub(card.getMoney(),0.2)); //账户余额消费0.2元(1M流量费用)
                    card.setConsumAmount(card.getConsumAmount() + 0.2);
                }else{
                    temp = i; //记录实现通话分钟数
                    throw new Exception("本次已通话"+i+"分钟,您的余额不足,请充值后再使用!");
                }
            }
            return temp;
        }
    
        public int sendMessage(int smsCount, MobileCard card) throws Exception {
            int temp = smsCount;
            for(int i=0;i<smsCount;i++){
                if(this.smsCount-card.getRealSMSCount()>=1){
                    //第一种情况:套餐剩余短信条数可以付1条短信
                    card.setRealSMSCount(card.getRealSMSCount()+1); //实际使用短信条数加1
                }else if(card.getMoney()>=0.1){
                    //第二种情况:套餐短信条数已用完,账户余额可以支付1条短信,使用账户余额支付
                    card.setRealSMSCount(card.getRealSMSCount()+1);
                    card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1条短信费用)
                    card.setConsumAmount(card.getConsumAmount() + 0.1);
                }else{
                    temp = i;
                    throw new Exception("本次已发送短信"+i+"条,您的余额不足,请充值后再使用!");
                }
            }
            return temp;
        }
    
    }
    • 接口: 
    package cn.soso.service;
    
    import cn.soso.entity.MobileCard;
    
    /**
     * 通话服务接口
     * @author yu
     *
     */
    public interface CallService {
        //打电话
        public int call(int minCount,MobileCard card) throws Exception;
    }
    package cn.soso.service;
    
    import cn.soso.entity.MobileCard;
    
    /**
     * 上网服务
     * @author yu
     *
     */
    public interface NetService {
        //上网
        public int netPlay(int flow,MobileCard card) throws Exception;
    }
    package cn.soso.service;
    
    import cn.soso.entity.MobileCard;
    
    /**
     * 短信服务
     * @author yu
     *
     */
    public interface SendService {
        //发短信
        public int sendMessage(int count,MobileCard card) throws Exception;
    }
    • 工具类
    package cn.soso.utils;
    
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.Writer;
    import java.text.DecimalFormat;
    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.Set;
    
    import cn.soso.common.Common;
    import cn.soso.entity.ConsumInfo;
    import cn.soso.entity.MobileCard;
    import cn.soso.entity.NetPackage;
    import cn.soso.entity.Scene;
    import cn.soso.entity.ServicePackage;
    import cn.soso.entity.SuperPackage;
    import cn.soso.entity.TalkPackage;
    import cn.soso.service.CallService;
    import cn.soso.service.NetService;
    import cn.soso.service.SendService;
    
    /**
     * 手机卡工具类
     *
     * @author yu
     *
     */
    public class CardUtil {
        Map<String, MobileCard> cards = new HashMap<String, MobileCard>(); // 所有手机卡的列表
        Map<String, List<ConsumInfo>> consumInfos = new HashMap<String, List<ConsumInfo>>(); // 所有手机卡消费记录的列表
        List<Scene> scenes = new ArrayList<Scene>();
    
        // 初始化用户
        public void init() {
            MobileCard card1 = new MobileCard("何玲玲", "123", "13965756432",
                    new TalkPackage(), 58.0, 42.0);
            MobileCard card2 = new MobileCard("黄露露", "123", "13956712467",
                    new NetPackage(), 68.0, 32.0);
            MobileCard card3 = new MobileCard("朱蓉蓉", "123", "13911568956",
                    new SuperPackage(), 78.0, 22.0);
            MobileCard card4 = new MobileCard("桃跑跑", "123", "13924221868",
                    new TalkPackage(), 78.0, 2.0);
            card4.setConsumAmount(98.0);
            card4.setRealTalkTime(500);
            card4.setRealSMSCount(100);
            cards.put("13965756432", card1);
            cards.put("13956712467", card2);
            cards.put("13911568956", card3);
            cards.put("13924221868", card4);
        }
    
        /**
         * 使用场景初始化
         */
        public void initScenes(){
            scenes.add(new Scene("通话",90,"问候客户,谁知其如此难缠 通话90分钟"));
            scenes.add(new Scene("通话",30,"询问妈妈身体状况 本地通话30分钟"));
            scenes.add(new Scene("短信",5,"参与环境保护实施方案问卷调查 发送短信5条"));
            scenes.add(new Scene("短信",50,"通知朋友手机换号,发送短信50条"));
            scenes.add(new Scene("上网",1*1024,"和女友微信视频聊天   使用流量1G"));
            scenes.add(new Scene("上网",2*1024,"晚上手机在线看韩剧,不留神睡着啦! 使用流量 2G"));
        }
    
        /**
         * 是否存在此卡用户
    
         *
         * @param number
         * @param passWord
         * @return
         */
        public boolean isExistCard(String number, String passWord) {
            Set<String> numbers = cards.keySet();
            Iterator<String> it = numbers.iterator();
            while (it.hasNext()) {
                String searchNum = it.next();
                if (searchNum.equals(number)
                        && (cards.get(searchNum)).getPassWord().equals(passWord)) {
                    return true;
                }
            }
            return false;
        }
    
        /**
         * 查找指定卡号是否已注册
         *
         * @param searchNumber
         * @return 未注册:false 已注册:true
         */
        public boolean isExistCard(String searchNumber) {
            Set<String> numbers = cards.keySet();
            for (String number : numbers) {
                if (number.equals(searchNumber)) {
                    return true;
                }
            }
            return false;
        }
    
        /**
         * 创建卡号(以139开头 11位)
         *
         * @return 生成的随机手机卡号
         */
        public String createNumber() {
            Random random = new Random();
            boolean isExist = false; // 记录现有用户中是否存在此卡号用户 是:true 否:false
            String number = "";
            int temp = 0;
            do {
                isExist = false; // 标志位重置为false,用于控制外重循环,当生成了
                // 生成的随机数是8位 不能小于10000000,否则重新生成
                do {
                    temp = random.nextInt(100000000);
                } while (temp < 10000000);
                // 生成之前,前面加“139”
                number = "139" + temp;
                // 和现有用户的卡号比较,不能是重复
                Set<String> cardNumbers = cards.keySet();
                for (String cardNumber : cardNumbers) {
                    if (number.equals(cardNumber)) {
                        isExist = true;
                        break;
                    }
                }
            } while (isExist);
            return number;
        }
    
        /**
         * 生成指定个数的新卡号列表
         *
         * @param count
         *  指定个数
         * @return 卡号列表
         */
        public String[] getNewNumbers(int count) {
    
            String[] numbers = new String[count];
            for (int i = 0; i < count; i++) {
                numbers[i] = createNumber();
            }
            return numbers;
        }
    
        /**
         * 添加新卡
         *
         * @param card
         *  新卡
         */
        public void addCard(MobileCard card) {
            cards.put(card.getCardNumber(), card);
            System.out.print("注册成功!");
            card.showMeg();
        }
    
        /**
         * 指定卡号办理退网
         *
         * @param card
         */
        public void delCard(String delNumber) {
            if (isExistCard(delNumber)) {
                cards.remove(delNumber);
                System.out.println("卡号" + delNumber + "办理退网成功!");
            } else {
                System.out.println("对不起,该卡号未注册,不能办退退网!");
            }
        }
    
        /**
         * 查询指定卡套餐余量
         *
         * @param number
         */
        public void showRemainDetail(String searchNumber) {
            MobileCard card; // 要查询的卡
            int remainTalkTime;
            int remainSmsCount;
            int remainFlow;
            StringBuffer meg = new StringBuffer();
            card = cards.get(searchNumber);
            meg.append("您的卡号是" + searchNumber + ",套餐内剩余:
    ");
            ServicePackage pack = card.getSerPackage();
            if (pack instanceof TalkPackage) {
                //向下转型为话唠套餐对象
                TalkPackage cardPack = (TalkPackage) pack;
                // 话唠套餐,查询套餐内剩余的通话时长和短信条数
                remainTalkTime = cardPack.getTalkTime() > card
                        .getRealTalkTime() ? cardPack.getTalkTime()
                        - card.getRealTalkTime() : 0;
                meg.append("通话时长:" + remainTalkTime + "分钟
    ");
                remainSmsCount = cardPack.getSmsCount() > card
                        .getRealSMSCount() ? cardPack.getSmsCount()
                        - card.getRealSMSCount() : 0;
                meg.append("短信条数:" + remainSmsCount + "条");
            } else if (pack instanceof NetPackage) {
                //向下转型为网虫套餐对象
                NetPackage cardPack = (NetPackage) pack;
                // 网虫套餐:查询套餐内剩余的上网流量
                remainFlow = cardPack.getFlow() > card.getRealFlow() ? cardPack
                        .getFlow() - card.getRealFlow() : 0;
                meg.append("上网流量:" + Common.dataFormat(remainFlow * 1.0 / 1024)
                        + "GB");
            } else if (pack instanceof SuperPackage) {
                //向下转型为超人套餐对象
                SuperPackage cardPack = (SuperPackage) pack;
                // 超人套餐:查询套餐内剩余的通话时长、短信条数、上网流量
                remainTalkTime = cardPack.getTalkTime() > card
                        .getRealTalkTime() ? cardPack.getTalkTime()
                        - card.getRealTalkTime() : 0;
                meg.append("通话时长:" + remainTalkTime + "分钟
    ");
                remainSmsCount = cardPack.getSmsCount() > card
                        .getRealSMSCount() ? cardPack.getSmsCount()
                        - card.getRealSMSCount() : 0;
                meg.append("短信条数:" + remainSmsCount + "条
    ");
                remainFlow = cardPack.getFlow() > card.getRealFlow() ? cardPack
                        .getFlow() - card.getRealFlow() : 0;
                meg.append("上网流量:" + Common.dataFormat(remainFlow * 1.0 / 1024)
                        + "GB");
            }
            System.out.println(meg);
        }
    
        /**
         * 查询指定卡当月消费详单
         *
         * @param searchNumber
         */
        public void showAmountDetail(String searchNumber) {
            MobileCard card; // 要查询的卡
            StringBuffer meg = new StringBuffer();
            card = cards.get(searchNumber);
            meg.append("您的卡号:" + card.getCardNumber() + ",当月账单:
    ");
            meg.append("套餐资费:" + card.getSerPackage().getPrice() + "元
    ");
            meg.append("合计:" + Common.dataFormat(card.getConsumAmount()) + "元
    ");
            meg.append("账户余额:" + Common.dataFormat(card.getMoney()) + "元");
            // 显示本月消费详细信息
            System.out.println(meg);
        }
    
    
        /**
         * 指定卡号换套餐
         *
         * @param number
         * @param packType
         */
        public void changingPack(String number, String packNum) {
            MobileCard card; // 指定的手机卡
            ServicePackage pack; // 要换的套餐
            if (isExistCard(number)) {
                card = cards.get(number);
                // 获取要换的套餐对象
                switch (packNum) {
                    case "1":
                        pack = new TalkPackage();
                        break;
                    case "2":
                        pack = new NetPackage();
                        break;
                    default:
                        pack = new SuperPackage();
                        break;
                }
                if (!(card.getSerPackage().getClass().getName().equals(pack.getClass().getName()))) {
                    // 该卡余额中减去当月套餐资费
                    if (card.getMoney() >= pack.getPrice()) {
                        card.setMoney(card.getMoney() - pack.getPrice());
                        // 换套餐
                        card.setSerPackage(pack);
                        // 当月实际使用数据清零
                        card.setRealTalkTime(0);
                        card.setRealFlow(0);
                        card.setRealSMSCount(0);
                        // 当月消费金额设置为新套餐月资费
                        card.setConsumAmount(pack.getPrice());
                        System.out.print("更换套餐成功!");
                        pack.showInfo();
                    } else {
                        System.out.println("对不起,您的余额不足以支付新套餐本月资费,请充值后再办理更换套餐业务!");
                        return;
                    }
                } else {
                    System.out.println("对不起,您已经是该套餐用户,无需换套餐!");
                }
    
            } else {
                System.out.println("对不起,该卡号未注册,不能换套餐!");
            }
        }
    
        /**
         * 为指定手机卡充值
         *
         * @param number
         *  指定充值的卡号
         * @param money
         * 充值金额
         */
        public void chargeMoney(String number, double money) {
            MobileCard card; // 指定的手机卡
            if (money < 50) {
                System.out.println("对不起,最低充值金额为50元!");
                return;
            }
            card = cards.get(number);
            card.setMoney(card.getMoney() + money);
            System.out.println("充值成功,当前话费余额为" + Common.dataFormat(card.getMoney()) + "元。");
        }
    
        /**
         * 添加一条指定卡的消费记录
         *
         * @param number
         *            要添加消费记录的卡
         * @param info
         *            要添加的消费记录
         */
        public void addConsumInfo(String number, ConsumInfo info) {
            Set<String> numbers = consumInfos.keySet();
            Iterator<String> it = numbers.iterator();
            List<ConsumInfo> infos = new ArrayList<ConsumInfo>();
            boolean isExist = false; // 现有消费列表中是否存在此卡号消费记录,是:true 否:false
            while (it.hasNext()) {
                if (it.next().equals(number)) {
                    // 消费集合中已有该卡号消费记录,则找到该卡号的消费记录集合,添加一条即可
                    infos = consumInfos.get(number);
                    infos.add(info);
                    isExist = true;
                    System.out.println("已添加一条消费记录。");
                    break;
                }
            }
            // 该集合中没有此卡号消费记录,则添加
            if (!isExist) {
                infos.add(info);
                consumInfos.put(number, infos);
                System.out.println("不存在此卡的消费记录,已添加一条消费记录。");
            }
        }
    
        //打印消费记录
        public void printConsumInfo(String number){
            Writer fileWriter = null;
            try {
                fileWriter = new FileWriter(number+"消费记录.txt");
                Set<String> numbers = consumInfos.keySet();
                Iterator<String> it = numbers.iterator();
                List<ConsumInfo> infos = new ArrayList<ConsumInfo>();//存储指定卡所有消费记录
                boolean isExist = false; // 现有消费列表中是否存在此卡号消费记录,是:true 否:false
                while (it.hasNext()) {
                    if (it.next().equals(number)) {
                        infos = consumInfos.get(number);
                        isExist = true;
                        break;
                    }
                }
                if(isExist){
                    //存在 此卡消费记录,写入文本文件
                    StringBuffer content = new StringBuffer("******"+number+"消费记录******
    ");
                    content.append("序号	类型	数据(通话(条)/上网(MB)/短信(条))
    ");
                    for(int i=0;i<infos.size();i++){
                        ConsumInfo info = infos.get(i);
                        content.append((i+1)+".	"+info.getType()+"	"+info.getConsumData()+"
    ");
                    }
                    fileWriter.write(content.toString());
                    fileWriter.flush();
    
                    System.out.println("消费记录打印完毕!");
                }else{
                    System.out.println("对不起,不存在此号码的消费记录,不能打印!");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(fileWriter!=null){
                    try {
                        fileWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        /**
         * 使用嗖嗖
         * @param number 当前卡号
         * @throws Exception
         */
        public void userSoso(String number)  {
            MobileCard card = cards.get(number); // 获取此卡对象
            ServicePackage pack = card.getSerPackage(); // 获取此卡所属套餐
            Random random = new Random();
            int ranNum = 0;
            int temp = 0;  //记录各场景中实际消费数据
            do{
    
                ranNum = random.nextInt(6);// 生成一个0~5之前的随机数
                Scene scene = scenes.get(ranNum); //获取该序号所对应的场景
                switch (ranNum) {
                    //序号为0或1为通话场景
                    case 0:
                    case 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;
                        }
                        //序号为2或3为发短信场景
                    case 2:
                    case 3:
                        // 判断该卡所属套餐是否支持短信功能
                        if (pack instanceof SendService) {
                            // 执行发短信方法
                            System.out.println(scene.getDescription());
                            SendService sendService = (SendService) pack;
                            try {
                                temp = sendService.sendMessage(scene.getData(), card);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            // 添加一条消费记录
                            addConsumInfo(number, new ConsumInfo(number,
                                    scene.getType(), temp));
                            break;
                        } else {
                            // 如果该卡套餐不支持发短信功能,则重新生成随机数选择其他场景
                            continue;
                        }
                        //序号为4或5为发上网场景
                    case 4:
                    case 5:
                        // 判断该卡所属套餐是否支持上网功能
                        if (pack instanceof NetService) {
                            System.out.println(scene.getDescription());
                            NetService netService = (NetService) pack;
                            // 执行上网方法
                            try {
                                temp = netService.netPlay(scene.getData(), card);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            // 添加一条消费记录
                            addConsumInfo(number, new ConsumInfo(number,
                                    scene.getType(), temp));
                            break;
                        } else {
                            // 如果该卡套餐不支持上网功能,则重新生成随机数选择其他场景
                            continue;
                        }
                }
                break;
            }while(true);
        }
    
        /**
         * 根据套餐序号返回套餐对象
         *
         * @param packNum
         *            套餐序号
         * @return 套餐对象
         */
        public ServicePackage createPack(int packId) {
            ServicePackage pack = null;
            switch (packId) {
                case 1:
                    pack = new TalkPackage();
                    break;
                case 2:
                    pack = new NetPackage();
                    break;
                case 3:
                    pack = new SuperPackage();
                    break;
            }
            return pack;
        }
    
        /**
         * 显示资费说明
         */
        public void showDescription(){
            Reader rd = null;
            try {
                rd = new FileReader("套餐资费说明.txt");
                char[] content = new char[1024];
                int len = 0;
                StringBuffer sb = new StringBuffer();
                while((len=rd.read(content))!=-1){
                    sb.append(content,0,len);  //拼接字符串
                }
                System.out.println(sb);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    同样功能的SQL语句,写成参数和写成常量怎么效率会有那么大的差别
    遭遇钓鱼网站
    SQL Server 2005与Oracle同步注意NUMBER类型转换
    Oracle数据类型(转)
    如何使用枚举的组合值
    社保,交得越多亏得越多(转)
    使用OPENXML函数将XML文档转换为行结果集
    发布一个性能测试工具的破解补丁
    如何将SQLServer2005中的数据同步到Oracle中
    Repository模式
  • 原文地址:https://www.cnblogs.com/yutianbao/p/10713905.html
Copyright © 2020-2023  润新知