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("\n*************歡迎使用嗖嗖移動業務大廳***************");
            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("\n*****資費說明******");
                    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("\n*****嗖嗖移動用戶菜單*****");
            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("\n*****本月賬單查詢******");
                    utils.showAmountDetail(mobileNumber);
                    continue;
                case 2:
                    System.out.println("\n*****套餐余量查詢******");
                    utils.showRemainDetail(mobileNumber);
                    continue;
                case 3:
                    System.out.println("\n*****消費詳單查詢******");
                    utils.printConsumInfo(mobileNumber);
                    continue;
                case 4:
                    System.out.println("\n*****套餐變更******");
                    System.out.print("1.話嘮套餐  2.網蟲套餐  3.超人套餐  請選擇(序號):");
                    utils.changingPack(mobileNumber, input.next());
                    continue;
                case 5:
                    System.out.println("\n*****辦理退網******");
                    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]+"\t\t");
            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 + ",套餐內剩余:\n");
        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 + "分鍾\n");
            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 + "分鍾\n");
            remainSmsCount = cardPack.getSmsCount() > card
                    .getRealSMSCount() ? cardPack.getSmsCount()
                    - card.getRealSMSCount() : 0;
            meg.append("短信條數:" + remainSmsCount + "條\n");
            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() + ",當月賬單:\n");
        meg.append("套餐資費:" + card.getSerPackage().getPrice() + "元\n");
        meg.append("合計:" + Common.dataFormat(card.getConsumAmount()) + "元\n");
        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+"消費記錄******\n");
                content.append("序號\t類型\t數據(通話(條)/上網(MB)/短信(條))\n");
                for(int i=0;i<infos.size();i++){
                    ConsumInfo info = infos.get(i);
                    content.append((i+1)+".\t"+info.getType()+"\t"+info.getConsumData()+"\n");
                }
                fileWriter.write(content.toString());
                fileWriter.flush();

                System.out.println("消費記錄打印完畢!");
            }else{
                System.out.println("對不起,不存在此號碼的消費記錄,不能打印!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            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();
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM