JAVA小项目-搜搜移动大厅

搜搜移动大厅(SouSouMobileHall)

主菜单:
1. 登录:输入正确的手机卡号和对应的手机密码进入子菜单
登录子菜单:
- 本月账单查询:输出卡号的套餐资费、实际消费金额、账户余额。
- 套菜余量查询:输出卡号的套餐内剩余资费情况。
- 打印清单:输出消费详单。
- 套餐变更:输入本套餐提示已定,输入其他套餐需要判断当前余额是否满足被扣的套餐钱数。
- 退网:再次提示是否退网,是则从已注册卡号列表中删除此卡号,并退出系统。
2. 注册:录入信息并开卡,信息包括:用户选择的卡号、套餐类型、身份证号、用户名和密码、预存话费(预存话费必须能够支付所选套餐的费用)
3. 使用:首先输入正确的手机号和密码,随机进行场景,并记录消费信息。当余额不足以支持场景时,抛出异常。
4. 充值:首先输入正确的手机号和密码,为此卡充值。
5. 说明:打印输出每个套餐的资费详情。
6. 退出:退出程序。

资费查询采用xml读取方式实现。
整个用户的信息采用序列化方法实现。

项目目录结构:
这里写图片描述
有很多小bug,但整体框架已经搭出
仅供参考
需要完整的可以 点这下载

package pojo.able;

import pojo.MobileCard;

/*
 * 通话接口
 */
public interface CallServiceable {
    /*
     * 套餐内通话免费
     * 套餐外通话每分钟0.2元
     * 超出电话余额抛出异常提示余额不足
     */
    int   call(int minCount,MobileCard card) throws Exception;
}
package pojo.able;

import pojo.MobileCard;

/*
 * 上网接口
 */
public interface NetServiceable {
    /*
     * 套餐内上网流量免费
     * 套餐外每MB0.1元
     * 余额不足抛出异常提示余额不足
     */
    int  netPlay(int flow,MobileCard card)throws Exception;
}
package pojo.able;

import pojo.MobileCard;

/*
 * 短信接口
 */
public interface SendServiceable {
    /*
     * 套餐内短信免费
     * 套餐外短信每条1毛
     * 余额不足抛出异常
     */
    int  send(int count,MobileCard card)throws Exception;
}
package pojo.abs;
/*
 * 套餐抽象方法
 */
public abstract class ServicePackage {
    /*
     * 套餐月资费
     */
    public  double price;
    /*
     * 显示套餐信息
     */
    public abstract void  showInfo();
}
package pojo.packages;

import java.io.Serializable;

import pojo.MobileCard;
import pojo.able.NetServiceable;
import pojo.abs.ServicePackage;

/*
 * 网虫套餐
 */
public class NetPackage extends ServicePackage implements NetServiceable,Serializable {
    /*
     * 上网流量3000MB
     * 资费68元
     */
    public int talkTime=0;
    public int smsCount=0;
    public int flow=3000;


    public NetPackage() {
        super();
        super.price=68;
    }

    public NetPackage(int talkTime, int smsCount, int flow) {
        super();
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    @Override
    public int netPlay(int flow, MobileCard card)throws Exception  {
        int temp=flow;
        for(int i=1;i<flow;i++){
            if(this.flow-card.getRealFlow()>=1){
                card.setRealFlow(card.getRealFlow()+1);
            }else if(card.getMoney()>=0.1){
                card.setRealFlow(card.getRealFlow()+1);
                card.setMoney(card.getMoney());
                card.setConsumAomunt(card.getConsumAomunt()+0.1);
            }else{
                temp=i;
                throw new Exception("本次已经上网"+i+"MB,您的余额不足,请充值后再使用");
            }
        }
        return temp;
    }

    @Override
    public void showInfo() {
        System.out.println("网虫套餐:\n通话时长为:" + this.talkTime + "分钟\t短信条数:"
                + this.smsCount + "条\t上网流量:" + flow+"MB");
    }

}
package pojo.packages;

import java.io.Serializable;

import pojo.MobileCard;
import pojo.able.CallServiceable;
import pojo.able.NetServiceable;
import pojo.able.SendServiceable;
import pojo.abs.ServicePackage;

/*
 * 超人套餐
 */
public class SuperPackage extends ServicePackage  implements CallServiceable,SendServiceable,NetServiceable,Serializable {
    /*
     * 通话时长100分钟
     * 上网流量1000MB
     * 短信50条
     * 资费78元
     */
    public int talkTime=200;
    public int smsCount=50;
    public int flow=1000;


    public SuperPackage() {
        super();
        super.price=78;
    }

    public SuperPackage(int talkTime, int smsCount, int flow) {
        super();
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    @Override
    public int netPlay(int flow, MobileCard card)throws Exception  {
        int temp=flow;
        for(int i=1;i<flow;i++){
            if(this.flow-card.getRealFlow()>=1){
                card.setRealFlow(card.getRealFlow()+1);
            }else if(card.getMoney()>=0.1){
                card.setRealFlow(card.getRealFlow()+1);
                card.setMoney(card.getMoney());
                card.setConsumAomunt(card.getConsumAomunt()+0.1);
            }else{
                temp=i;
                throw new Exception("本次已经上网"+i+"MB,您的余额不足,请充值后再使用");
            }
        }
        return temp;
    }

    @Override
    public int  send(int minCount, MobileCard card) throws Exception {
        int temp=minCount;
        for(int i=1;i<minCount;i++){
            if(this.smsCount-card.getRealSMSCount()>=1){
                card.setRealSMSCount(card.getRealSMSCount()+1);
            }else if(card.getMoney()>=0.1){
                card.setRealSMSCount(card.getRealSMSCount()+1);
                card.setMoney(card.getMoney());
                card.setConsumAomunt(card.getConsumAomunt()+0.1);
            }else{
                temp=i;
                throw new Exception("本次已经发送短信"+i+"条,您的余额不足,请充值后再使用");
            }
        }
        return temp;
    }

    @Override
    public int  call(int minCount, MobileCard card)throws Exception {
        int temp=minCount;
        for(int i=1;i<minCount;i++){
            if(this.talkTime-card.getRealTalkTime()>=1){
                card.setRealTalkTime(card.getRealTalkTime()+1);
            }else if(card.getMoney()>=0.2){
                card.setRealTalkTime(card.getRealTalkTime()+1);
                card.setMoney(card.getMoney());
                card.setConsumAomunt(card.getConsumAomunt()+0.2);
            }else{
                temp=i;
                throw new Exception("本次已经通话"+i+"分钟,您的余额不足,请充值后再使用");
            }
        }
        return temp;
    }

    @Override
    public void showInfo() {
        System.out.println("超人套餐:\n通话时长为:" + this.talkTime + "分钟\t短信条数:"
                + this.smsCount + "条\t上网流量:" + flow+"MB");
    }

}
package pojo.packages;

import java.io.Serializable;

import pojo.MobileCard;
import pojo.able.CallServiceable;
import pojo.able.SendServiceable;
import pojo.abs.ServicePackage;

/*
 * 话痨套餐
 */
public class TalkPackage extends ServicePackage  implements CallServiceable,SendServiceable,Serializable{
    /*
     * 通话时长500分钟
     * 短信30条
     * 资费58
     */
    public int talkTime=500;
    public int smsCount=30;
    public int flow=0;


    public TalkPackage() {
        super();
        super.price=58;
    }

    public TalkPackage(int talkTime, int smsCount, int flow) {
        super();
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    @Override
    public void showInfo() {
        System.out.println("话痨套餐:\n通话时长为:" + this.talkTime + "分钟\t短信条数:"
                + this.smsCount + "条\t上网流量:" + flow+"MB");
    }

    @Override
    public int  send(int minCount, MobileCard card) throws Exception {
        int temp=minCount;
        for(int i=1;i<minCount;i++){
            if(this.smsCount-card.getRealSMSCount()>=1){
                card.setRealSMSCount(card.getRealSMSCount()+1);
            }else if(card.getMoney()>=0.1){
                card.setRealSMSCount(card.getRealSMSCount()+1);
                card.setMoney(card.getMoney());
                card.setConsumAomunt(card.getConsumAomunt()+0.1);
            }else{
                temp=i;
                throw new Exception("本次已经发送短信"+i+"条,您的余额不足,请充值后再使用");
            }
        }
        return temp;
    }

    @Override
    public int  call(int minCount, MobileCard card)throws Exception {
        int temp=minCount;
        for(int i=1;i<minCount;i++){
            if(this.talkTime-card.getRealTalkTime()>=1){
                card.setRealTalkTime(card.getRealTalkTime()+1);
            }else if(card.getMoney()>=0.2){
                card.setRealTalkTime(card.getRealTalkTime()+1);
                card.setMoney(card.getMoney());
                card.setConsumAomunt(card.getConsumAomunt()+0.2);
            }else{
                temp=i;
                throw new Exception("本次已经通话"+i+"分钟,您的余额不足,请充值后再使用");
            }
        }
        return temp;
    }
}
package pojo.packages;

import java.io.Serializable;

import pojo.MobileCard;
import pojo.able.CallServiceable;
import pojo.able.SendServiceable;
import pojo.abs.ServicePackage;

/*
 * 话痨套餐
 */
public class TalkPackage extends ServicePackage  implements CallServiceable,SendServiceable,Serializable{
    /*
     * 通话时长500分钟
     * 短信30条
     * 资费58
     */
    public int talkTime=500;
    public int smsCount=30;
    public int flow=0;


    public TalkPackage() {
        super();
        super.price=58;
    }

    public TalkPackage(int talkTime, int smsCount, int flow) {
        super();
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    @Override
    public void showInfo() {
        System.out.println("话痨套餐:\n通话时长为:" + this.talkTime + "分钟\t短信条数:"
                + this.smsCount + "条\t上网流量:" + flow+"MB");
    }

    @Override
    public int  send(int minCount, MobileCard card) throws Exception {
        int temp=minCount;
        for(int i=1;i<minCount;i++){
            if(this.smsCount-card.getRealSMSCount()>=1){
                card.setRealSMSCount(card.getRealSMSCount()+1);
            }else if(card.getMoney()>=0.1){
                card.setRealSMSCount(card.getRealSMSCount()+1);
                card.setMoney(card.getMoney());
                card.setConsumAomunt(card.getConsumAomunt()+0.1);
            }else{
                temp=i;
                throw new Exception("本次已经发送短信"+i+"条,您的余额不足,请充值后再使用");
            }
        }
        return temp;
    }

    @Override
    public int  call(int minCount, MobileCard card)throws Exception {
        int temp=minCount;
        for(int i=1;i<minCount;i++){
            if(this.talkTime-card.getRealTalkTime()>=1){
                card.setRealTalkTime(card.getRealTalkTime()+1);
            }else if(card.getMoney()>=0.2){
                card.setRealTalkTime(card.getRealTalkTime()+1);
                card.setMoney(card.getMoney());
                card.setConsumAomunt(card.getConsumAomunt()+0.2);
            }else{
                temp=i;
                throw new Exception("本次已经通话"+i+"分钟,您的余额不足,请充值后再使用");
            }
        }
        return temp;
    }
}
package pojo;

import java.io.Serializable;

import pojo.abs.ServicePackage;

/*
 * 手机卡号类
 */
@SuppressWarnings("unused")
public class MobileCard implements Serializable{
    MobileCard card;
    private double price;
    /*
     * 卡号
     */ 
    private String cardNumber;
    public double getPrice() {
        return price;
    }

    public void setPrice(double price2) {
        this.price = price2;
    }
    /*
     * 用户名
     */
    private String userName;
    /*
     * 密码
     */
    private String passWord;
    /*
     * 所属套餐
     */
    private ServicePackage serPackage;
    /*
     * 消费金额
     */
    private double consumAomunt=0.0;
    /*
     * 余额
     */
    private double money;
    /*
     * 实际通话时长
     */
    private int realTalkTime;
    /*
     * 实际发送短信数
     */
    private int realSMSCount;
    /*
     * 实际上网流量
     */
    private int realFlow;
    /*
     * 手机卡资费信息
     */
    public void showMeg(ServicePackage pack) {
        System.out.println("卡号:" + this.cardNumber + "\t用户名" + this.userName
                + "\t当前余额" + this.money + "元");
        pack.showInfo();
    }

    public MobileCard() {
        super();
    }


    public String getCardNumber() {
        return cardNumber;
    }
    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }
    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 ServicePackage getSerPackage() {
        return serPackage;
    }
    public void setSerPackage(ServicePackage serPackage) {
        this.serPackage = serPackage;
    }
    public double getConsumAomunt() {
        //消费金额=套餐资费加上消费金额
        return consumAomunt;
    }
    public void setConsumAomunt(double consumAomunt) {
        this.consumAomunt = consumAomunt;
    }
    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;
    }

}
package pojo;

import java.io.Serializable;

/*
 * 场景使用类
 */
public class Scene implements Serializable{


       private String type; //场景类型
        private int data; //场景消费数据
        private String description; //场景描述
        public Scene() {
            super(); 
        }
        public Scene(String type, int data, String description) {
            super(); 
            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 service;

import java.util.Scanner;

import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;

import pojo.MobileCard;
import pojo.abs.ServicePackage;
import pojo.packages.NetPackage;
import pojo.packages.SuperPackage;
import pojo.packages.TalkPackage;
import test.SouSouTest;
import util.CardUtil;

public class SosoMgr {
    Scanner input = new Scanner(System.in);
    CardUtil cartutil = new CardUtil();
    SouSouTest test = new SouSouTest();

    /*
     * 二级菜单
     */
    @SuppressWarnings("static-access")
    public void cardMenu() {
        System.out.println("请输入手机卡号:");
        String cardNum = input.next();
        System.out.println("请输入手机密码:");
        String passWord = input.next();
        test.flag=true;
        CardUtil.init();
        // 验证卡号是否注册
        boolean existCard = cartutil.isExistCard(cardNum, passWord);
        // 验证用户是否返回上一级
        boolean flag = false;
        if (existCard) {
            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.println("请选择(输入1~5选择功能,其他键返回上一级)");
                String secondChoose = input.next();
                switch (secondChoose) {
                case "1":
                    cartutil.showAmountDetail(cardNum);
                    break;
                case "2":
                    cartutil.showRemainDetail(cardNum);
                    break;
                case "3":
                    cartutil.printAmountDetail(cardNum);
                    break;
                case "4":
                    System.out.println("请输入要变更的套餐序号(1.话痨套餐  2.网虫套餐  3.超人套餐)");
                    int packageNum = input.nextInt();
                    ServicePackage serpack = cartutil.createPack(packageNum);
                    serpack.showInfo();
                    cartutil.changeingPack(cardNum, packageNum);
                    break;
                case "5":
                    cartutil.delCard(cardNum);
                    System.out.println("谢谢使用");
                    return;
                default:
                    test.start();
                    break;
                }   
            }while(!flag);
        } else {
            System.out.println("您输入的卡号或密码不正确");
        }
    }

    /*
     * 注册
     */
    public void registCard() {
        MobileCard card = new MobileCard();
        System.out.println("******可选择的卡号******");
        // 显示可选择的卡号
        String[] cardNum = cartutil.getNewNumber(9);
        for (int i = 0; i < cardNum.length; i++) {
            System.out.print((i + 1) + "." + cardNum[i] + "\t");
            if (i == 2 || i == 5) {
                System.out.println();
            }
        }
        System.out.println();
        // 设置卡号
        System.out.println("请选择卡号(输入1-9之间的序号)");
        int cardChoose = input.nextInt();
        card.setCardNumber(cardNum[cardChoose - 1]);
        // 套餐
        System.out.println("1.话痨套餐    2.网虫套餐   3.超人套餐");
        System.out.println("请选择序号:");
        int packChoose = input.nextInt();
        ServicePackage pack = cartutil.createPack(packChoose);
        pack.showInfo();
        card.setSerPackage(pack);
        card.setPrice(pack.price);
        System.out.println("请输入注册用户名:");
        String userName = input.next();
        card.setUserName(userName);
        System.out.println("请输入注册密码:");
        String passWord = input.next();
        card.setPassWord(passWord);
        boolean userMoneyFlag = false;
        double userMoney;
        do {    
            System.out.println("请输入预存话费金额:");
            userMoney = (int) input.nextDouble();
            if (userMoney < pack.price) {
                System.out.println("您预存的话费金额不足以支付本月的固定套餐费用,请重新充值");
            } else {
                userMoneyFlag = true;
                card.setMoney(userMoney);
                break;
            }
        } while (!userMoneyFlag);
        card.setMoney(userMoney-(pack.price));
        System.out.println("注册成功!");
        cartutil.addCard(card);
        card.showMeg(pack);
    }

    /*
     * 主菜单
     */
    public void start(String hallChoose) {
        switch (hallChoose) {
        case "3":
            System.out.println("请输入您的卡号:");
            String number = input.next();
            cartutil.userSoso(number);
            break;
        case "4":
            System.out.println("请输入您的卡号:");
            String fourNumber = input.next();
            boolean flag = false;
            do {
                System.out.println("请输入您的充值钱数:");
                double money = input.nextDouble();
                if (money >= 50) {
                    flag = true;
                    cartutil.chargeMoney(fourNumber, money);
                    break;
                } else {
                    System.out.println("为了跟好的服务,请充值50元以上");
                }
            } while (flag);
            break;
        case "5":
            cartutil.showDescription();
            break;
        }
    }

}
package test;

import java.util.Scanner;

import service.SosoMgr;
import util.CardUtil;

public class SouSouTest {
    public static boolean flag = false;
    public static void start() {
        SosoMgr service = new SosoMgr();
        Scanner input = new Scanner(System.in);
        CardUtil.initScenes();
        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.println("***********        6.退出系统              ***********");
            String hallChoose = input.next();
            switch (hallChoose) {
            case "1":
                service.cardMenu();
                break;
            case "2":
                service.registCard();
                break;
            case "3":
            case "4":
            case "5":
                service.start(hallChoose);
                break;
            case "6":
            default:
                System.out.println("感谢您的使用,欢迎下次再来");
                flag = true;
                System.exit(0);
            }
        } while (!flag);
    }

    public static void main(String[] args) {
        start();
    }
}
package util;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import pojo.ConsumInfo;
import pojo.MobileCard;
import pojo.Scene;
import pojo.able.CallServiceable;
import pojo.able.NetServiceable;
import pojo.able.SendServiceable;
import pojo.abs.ServicePackage;
import pojo.packages.NetPackage;
import pojo.packages.SuperPackage;
import pojo.packages.TalkPackage;

public class CardUtil {
    static ObjectFile obj=new ObjectFile();
    /*
     * 已经注册搜搜移动的用户列表
     */
    public static Map<String, MobileCard> cards = new HashMap<String, MobileCard>();

    //每次都最先加载反序列化
    public static void init(){
        ArrayList<MobileCard>list = obj.objectInputFile();
        for(MobileCard card:list){
            cards.put(card.getCardNumber(), card);
        }
    }
    /*
     * 所有卡号的消费记录列表
     */
    public static Map<String, List<ConsumInfo>> consumlnfos = new HashMap<>();
    /*
     * 定义消费场景集合
     */
    static List<Scene> scenes=new ArrayList<>();
    public static 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("上网", 1024, "和女朋友用微信视频聊天,使用流量1GB"));
        scenes.add(new Scene("上网", 2 * 1024,"晚上手机在线看韩剧,不留神睡着啦!使用2GB"));

    }

    /*
     * 注册卡号操作
     */
    public void addCard(MobileCard card) {
        //每次注册都序列化进文件
        obj.objectOutputFile(card);
        //每次注册都放入Map
        cards.put(card.getCardNumber(), card);

    }

    /*
     * 话费充值操作
     */
    public void chargeMoney(String number, double money) {
        boolean existCard = isExistCard(number);
        if (existCard) {
            cards.get(number).setMoney(money);
            System.out.println("您好," + number + "充值成功" + money + "元。");
        } else {
            System.out.println("您的账号还未注册,请先注册");
        }
    }

    /*
     * 使用搜搜
     */
    public void userSoso(String number) {
        //在cards对象Map数组中根据号码取得card对象
        MobileCard card = cards.get(number);
        //根据号码获取该号码的所属套餐
        ServicePackage pack = card.getSerPackage();
        Random random = new Random();
        int ranNum = 0;
        int temp = 0;
        do {
            //生成[0,6)的随机数。
            ranNum = random.nextInt(6);
            //根据随机数获取场景对象
            Scene scene = scenes.get(ranNum);
            switch (ranNum) {
            case 0:
            case 1:
                //如果该号码对象实现了打电话的服务接口再进行使用,否则跳过本次重新生成
                if (pack instanceof CallServiceable) {
                    //输出场景信息
                    System.out.println(scene.getDescription());
                    //将套餐类强转为具体的打电话服务
                    CallServiceable callService = (CallServiceable) pack;
                    try {
                        //调用具体的号码打电话的方法,传参获取消费数据和卡号码
                        temp = callService.call(scene.getData(), card);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //添加消费数据,传参卡号码和新建消费数据类对象,消费数据对象中传入卡号,场景的消费类型,场景消费的分钟数或MB数或短信条数
                    addConsumInfo(number,
                            new ConsumInfo(number, scene.getType(), temp));
                    break;
                } else {
                    continue;
                }
            case 2:
            case 3:
                if (pack instanceof SendServiceable) {
                    System.out.println(scene.getDescription());
                    SendServiceable sendService = (SendServiceable) pack;
                    try {
                        temp = sendService.send(scene.getData(), card);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    addConsumInfo(number,
                            new ConsumInfo(number, scene.getType(), temp));
                    break;
                } else {
                    continue;
                }

            case 4:
            case 5:
                if (pack instanceof NetServiceable) {
                    System.out.println(scene.getDescription());
                    NetServiceable netService = (NetServiceable) 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);
    }

    /*
     * 资费说明
     */
    public void showDescription() {
        Document doc = null;
        try {
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read("Need/Description.xml");
            // 得到整个DOM数的根节点
            Element root = doc.getRootElement();
            Iterator itBrand = root.elementIterator();
            String value = null;
            while (itBrand.hasNext()) {
                Element element = (Element) itBrand.next();
                String name = element.attributeValue("name");
                System.out.println(name);
                Iterator itatt = element.elementIterator();
                while (itatt.hasNext()) {
                    Element ele = (Element) itatt.next();
                    String desc = ele.attributeValue("name");
                    value = ele.getText();
                    System.out.println(desc + "\t" + value);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

    /*
     * 本月账单查询
     */
    public void showAmountDetail(String number) {
        MobileCard card;
        StringBuffer meg = new StringBuffer();
        card = cards.get(number);
        meg.append("您的卡号:" + card.getCardNumber() + ",当月账单:\n");
        meg.append("套餐资费:" + card.getPrice() + "元\n");
        meg.append("合计:"
                + ((card.getSerPackage().price) + (card.getConsumAomunt()))
                + "元\n");
        meg.append("账户余额:"
                + ((card.getMoney()) - (card.getSerPackage().price) - (card
                        .getConsumAomunt())) + "\n");
        System.out.println(meg);
    }

    /*
     * 套餐余量查询
     */
    public void showRemainDetail(String number) {
        MobileCard card;
        int remainTalkTime;
        int remainSmsCount;
        int remainFlow;
        StringBuffer meg = new StringBuffer();
        card = cards.get(number);
        meg.append("您的卡号是" + number + ",套餐内剩余:\n");
        ServicePackage pack = card.getSerPackage();
        if (pack instanceof TalkPackage) {
            TalkPackage cardPack = (TalkPackage) pack;
            // 若是话痨套餐,若套餐内通话时间比实际通话时间大。则用套餐内通话时间-实际通话时间。否则返回0.
            remainTalkTime = cardPack.talkTime > card.getRealTalkTime() ? cardPack.talkTime
                    - card.getRealTalkTime()
                    : 0;
            meg.append("通话时长:" + remainTalkTime + "分钟\n");
            remainSmsCount = cardPack.smsCount > card.getRealSMSCount() ? cardPack.smsCount
                    - card.getRealSMSCount()
                    : 0;
            meg.append("短信条数:" + remainSmsCount + "条\n");
            remainFlow = cardPack.flow > card.getRealFlow() ? cardPack.flow
                    - card.getRealFlow() : 0;
            meg.append("上网流量:" + remainFlow + "MB\n");
        } else if (pack instanceof NetPackage) {
            NetPackage cardPack = (NetPackage) pack;
            // 若是上网套餐
            remainTalkTime = cardPack.talkTime > card.getRealTalkTime() ? cardPack.talkTime
                    - card.getRealTalkTime()
                    : 0;
            meg.append("通话时长:" + remainTalkTime + "分钟\n");
            remainSmsCount = cardPack.smsCount > card.getRealSMSCount() ? cardPack.smsCount
                    - card.getRealSMSCount()
                    : 0;
            meg.append("短信条数:" + remainSmsCount + "条\n");
            remainFlow = cardPack.flow > card.getRealFlow() ? cardPack.flow
                    - card.getRealFlow() : 0;
            meg.append("上网流量:" + remainFlow + "MB\n");
        } else {
            SuperPackage cardPack = (SuperPackage) pack;
            remainTalkTime = cardPack.talkTime > card.getRealTalkTime() ? cardPack.talkTime
                    - card.getRealTalkTime()
                    : 0;
            meg.append("通话时长:" + remainTalkTime + "分钟\n");
            remainSmsCount = cardPack.smsCount > card.getRealSMSCount() ? cardPack.smsCount
                    - card.getRealSMSCount()
                    : 0;
            meg.append("短信条数:" + remainSmsCount + "条\n");
            remainFlow = cardPack.flow > card.getRealFlow() ? cardPack.flow
                    - card.getRealFlow() : 0;
            meg.append("上网流量:" + remainFlow + "MB\n");
        }
        System.out.println(meg);
    }

    /*
     * 打印消费详单
     */
    public void printAmountDetail(String number) {
        Writer fileWriter = null;
        try {
            fileWriter = new FileWriter("Need/"+number + "消费记录.txt");
            List<ConsumInfo> infos = new ArrayList<ConsumInfo>();
            // 存储指定卡号的所有消费记录
            // 现有消费列表中是否存在此卡号,是:true 否:false
            // 从consumlnfos中查找是否存在该卡的消费记录
            boolean isExist = false;
            for (Map.Entry<String, List<ConsumInfo>> maps : consumlnfos
                    .entrySet()) {
                for (ConsumInfo con : maps.getValue()) {
                    if ((con.getCardName() == null
                            || (con.getConsumData() == 0) || con.getType() == null)) {
                        break;
                    } else {
                        isExist = true;
                        break;
                    }
                }
                break;
            }
            if (isExist) {
                StringBuffer content = new StringBuffer("******" + number
                        + "消费记录******\n");
                content.append("序号\t类型\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();
                }
            }
        }
    }

    /*
     * 套餐变更
     */
    public void changeingPack(String number, int packageNum) {
        ServicePackage serpack = createPack(packageNum);
        if ((cards.get(number).equals(packageNum))) {
            System.out.println("对不起,您已经是该套餐用户,无须更换套餐");
        } else if (cards.get(number).getMoney() < serpack.price) {
            System.out.println("对不起,您的余额不足以支付当前套餐月租,请充值后再更换");
        } else {
            for (Entry<String, MobileCard> maps : cards.entrySet()) {
                maps.getValue().setRealTalkTime(0);
                maps.getValue().setRealSMSCount(0);
                maps.getValue().setRealFlow(0);
                maps.getValue().setSerPackage(serpack);
                maps.getValue().setMoney(
                        (cards.get(number).getMoney()) - (serpack.price));
                maps.getValue().setConsumAomunt(serpack.price);
                System.out.println("更换套餐成功");
                serpack.showInfo();
            }
        }

    }

    /*
     * 办理退网
     */
    public void delCard(String number) {
        boolean flag = isExistCard(number);
        if (flag) {
            for (Map.Entry<String, MobileCard> maps : cards.entrySet()) {
                cards.remove(number);
                System.out.println("卡号\t" + number + "\t退网成功,感谢使用");
            }
        } else {
            System.out.println("您的卡号异常或不存在,请稍后再次");
        }

    }

    /*
     * 根据卡号和密码验证卡号是否注册
     */
    public boolean isExistCard(String number, String passWord) {
        CardUtil.init();
        for (Map.Entry<String, MobileCard> maps : cards.entrySet()) {
            if (number.equals(maps.getKey())
                    && (passWord.equals(maps.getValue().getPassWord()))) {
                return true;
            }
        }
        return false;
    }

    /*
     * 根据卡号验证该卡是否注册
     */
    public boolean isExistCard(String number) {
        CardUtil.init();
        for (Map.Entry<String, MobileCard> maps : cards.entrySet()) {
            if (number.equals(maps.getKey())) {
                return true;
            }
        }
        return false;

    }

    /*
     * 生成随机卡号
     */

    public String createNumber() {
        // 记录现有用户中是否存在此卡号
        boolean flag = false;
        String number = null;
        do {
            // 指定以","为分隔符分隔此字符串
            String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153"
                    .split(",");
            // 生成随机的字符串数组中的下标
            int index = (int) (Math.random() * (telFirst.length));
            // 随机卡号开头确认
            String first = telFirst[index];
            // 生成18889的随机数。截取1。卡号的中间4位确认
            String second = String.valueOf(
                    (int) (Math.random() * ((888) + 1) + 10000)).substring(1);
            // 生成18889的随机数。截取1。卡号的中间4位确认
            String third = String.valueOf(
                    (int) (Math.random() * ((9100) + 1) + 10000)).substring(1);
            number = first + second + third;
            for (Map.Entry<String, MobileCard> maps : cards.entrySet()) {
                if (number.equals(maps.getKey())) {
                    flag = true;
                    break;
                }
            }
        } while (flag);
        return number;
    }

    /*
     * 生成指定个数的卡号数组
     */
    public String[] getNewNumber(int count) {
        String[] str = new String[count];
        for (int i = 0; i < str.length; i++) {
            str[i] = createNumber();
        }
        return str;
    }

    /*
     * 添加指定卡号的消费记录
     */
    public void addConsumInfo(String number, ConsumInfo info) {
        List<ConsumInfo> infos=new ArrayList<ConsumInfo>();
        boolean isExit=false;
        for(Map.Entry<String, List<ConsumInfo>>maps:consumlnfos.entrySet()){
            //消费集合中 已经有该卡号的消费记录
            if(number.equals(maps.getKey())){
                //追加消费记录
                infos.add(info);
                isExit=true;
                System.out.println("存在此卡的 消费记录。已经追加一条");
                break;
            }
        }
        if(!isExit){
            infos.add(info);
            consumlnfos.put(number, infos);
            System.out.println("不存在此卡的消费记录,已经写入");
        }
    }

    /*
     * 根据用户选择的套餐序号返回套餐对象
     */
    public ServicePackage createPack(int packId) {
        if (packId == 1) {
            return new TalkPackage();
        } else if (packId == 2) {
            return new NetPackage();
        }
        return new SuperPackage();
    }

}
package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import pojo.MobileCard;

public class ObjectFile {
    /*
     * 序列化
     */
    public void objectOutputFile(MobileCard card){
        File file=new File("Need/cardUser.txt");
        OutputStream out = null;
        ObjectOutputStream obs = null;
        try {
            out = new FileOutputStream(file);
            obs=new ObjectOutputStream(out);
                obs.writeObject(card);      
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                out.close();
                obs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    /*
     * 反序列化
     */
    @SuppressWarnings("unchecked")
    public ArrayList objectInputFile(){
        ArrayList<MobileCard>list=new ArrayList<MobileCard>();
        File file=new File("Need/cardUser.txt");
        InputStream in = null;
        ObjectInputStream oni = null;
        try {
            in=new FileInputStream(file);
            oni=new ObjectInputStream(in);
            MobileCard card = (MobileCard) oni.readObject();
            list.add(card);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                in.close();
                oni.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return list;
    }
}
  • 30
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值