1. 程式人生 > >JAVA小專案-搜搜移動大廳

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());