嗖嗖移动业务大厅项目(代码+注释全)
package info;
public class ConsumInfo {
public String cardNumber;
public String type;
public int consumDate;
public ConsumInfo() {
}
public ConsumInfo(String cardNumber, String type, int consumDate) {
super();
this.cardNumber = cardNumber;
this.type = type;
this.consumDate = consumDate;
}
}
`
package info;
import myPackage.ServicePackage;
public class MobileCard {
public String cardNumble;
public String uesrName;
public String passWord;
public ServicePackage setPackage;
public double consumAmount;
public double money;
public int realTalkTime;
public int realSMSCount;
public int realFlow;
public MobileCard() {
}
public MobileCard(String cardNumble, String uesrName, String passWord,
ServicePackage setPackage, double consumAmount, double money,
int realTalkTime, int realSMSCount, int realFlow) {
super();
this.cardNumble = cardNumble;
this.uesrName = uesrName;
this.passWord = passWord;
this.setPackage = setPackage;
this.consumAmount = consumAmount;
this.money = money;
this.realTalkTime = realTalkTime;
this.realSMSCount = realSMSCount;
this.realFlow = realFlow;
}
public void showMeg() {
System.out.println("卡号:" + this.cardNumble + " 用户名:" + this.uesrName + " 当前余额:" + this.money+"元");
}
}
package info;
import java.util.Random;
public class Scene {
public String type;
public int data;
public String description;
public Scene() {
super();
}
public Scene(String type, int data, String description) {
super();
this.type = type;
this.data = data;
this.description = description;
}
}
package myPackage;
import info.MobileCard;
import Service.NetSevice;
public class NetPackage extends ServicePackage implements NetSevice {
public int talkTime = 0;
public int smsCount = 0;
public int flow=3072;
public NetPackage() {
super();
super.price=68;
}
public NetPackage(int talkTime, int smsCount, int flow) {
super();
this.talkTime = talkTime;
this.smsCount = smsCount;
this.flow = flow;
}
public void showInfo() {
System.out.println("网虫套餐:上网流量为3GB/月,资费为68.0元/月。");
}
@Override
public int netPlay(int flow, MobileCard card)throws Exception {
int temp=flow;
for(int i=0;i<flow;i++){
if(this.flow-card.realFlow>=1){
card.realFlow++;
}else if(card.money>=0.1){
card.realFlow++;
card.money-=0.1;
card.consumAmount+=0.1;
}else{
temp=i;
throw new Exception("本次上网"+i+"分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
}
package myPackage;
public abstract class ServicePackage {
public double price;
public abstract void showInfo();
}
package myPackage;
import info.MobileCard;
import Service.CallService;
import Service.NetSevice;
import Service.SendService;
public class SuperPackage extends ServicePackage implements CallService,SendService,NetSevice{
public int talkTime=200;
public int smsCount=50;
public int flow=1024;
public SuperPackage(){
super();
super.price=78;
}
public SuperPackage(int talkTime, int smsCount, int flow) {
super();
this.talkTime = talkTime;
this.smsCount = smsCount;
this.flow = flow;
}
public void showInfo() {
System.out.println("超人套餐:通话时长为200分钟/月,上网流量为1GB/月,短信条数为50条/月,资费为78.0元/月。");
}
public int send(int count, MobileCard card) throws Exception{
int temp=count;
for(int i=0;i<count;i++){
if(this.smsCount-card.realSMSCount>=1){
card.realSMSCount++;
}else if(card.money>=0.1){
card.realSMSCount++;
card.money-=0.1;
card.consumAmount+=0.1;
}else{
temp=i;
throw new Exception("本次发短信"+i+"分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
@Override
public int call(int miniCount, MobileCard card) throws Exception {
int temp=miniCount;
for(int i=0;i<miniCount;i++){
if(this.talkTime-card.realTalkTime>=1){
card.realTalkTime++;
}else if(card.money>=0.2){
card.realTalkTime++;
card.money-=0.2;
card.consumAmount+=0.2;
}else{
temp=i;
throw new Exception("本次通话"+i+"分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
@Override
public int netPlay(int flow, MobileCard card)throws Exception {
int temp=flow;
for(int i=0;i<flow;i++){
if(this.flow-card.realFlow>=1){
card.realFlow++;
}else if(card.money>=0.1){
card.realFlow++;
card.money-=0.1;
card.consumAmount+=0.1;
}else{
temp=i;
throw new Exception("本次上网"+i+"分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
}
package myPackage;
import info.MobileCard;
import Service.CallService;
import Service.SendService;
public class TalkPackage extends ServicePackage implements CallService,SendService{
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;
}
public void showInfo() {
System.out.println("话唠套餐:通话时长为500分钟/月,短信条数为30条/月,资费为58.0元/月。");
}
public int send(int count, MobileCard card)throws Exception {
int temp=count;
for(int i=0;i<count;i++){
if(this.smsCount-card.realSMSCount>=1){
card.realSMSCount++;
}else if(card.money>=0.1){
card.realSMSCount++;
card.money-=0.1;
card.consumAmount+=0.1;
}else{
temp=i;
throw new Exception("本次发短信"+i+"分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
@Override
public int call(int miniCount, MobileCard card) throws Exception{
int temp=miniCount;
for(int i=0;i<miniCount;i++){
if(this.talkTime-card.realTalkTime>=1){
card.realTalkTime++;
}else if(card.money>=0.2){
card.realTalkTime++;
card.money-=0.2;
card.consumAmount+=0.2;
}else{
temp=i;
throw new Exception("本次通话"+i+"分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
}
package Service;
import info.MobileCard;
public interface CallService {
public int call(int miniCount,MobileCard card) throws Exception;
}
package Service;
import info.MobileCard;
public interface NetSevice {
public int netPlay(int flow,MobileCard card) throws Exception;
}
package Service;
import info.MobileCard;
public interface SendService {
public int send(int count,MobileCard card)throws Exception;
}
package start;
import java.util.Scanner;
public class Manage {
public void start(){
Scanner input = new Scanner(System.in);
Util util = new Util();
boolean iscontinue = true;
do {
System.out.println();
System.out.println("*************欢迎使用嗖嗖移动业务大厅***************");
System.out.println("1.用户登录 2.用户注册 3.使用嗖嗖 4.话费充值 5.资费说明 6.退出系统");
System.out.print("请选择:");
int option = input.nextInt();
switch (option) {
case 1:
util.login();
break;
case 2:
util.signIn();
break;
case 3:
util.uesSoso();
break;
case 4:
util.addPayMoney();
break;
case 5:
util.ziFei();
break;
case 6:
System.out.println("欢迎您下次继续使用!");
iscontinue=false;
break;
default:
System.out.println("输入有误!请重新输入");
break;
}
} while (iscontinue);
}
}
package start;
public class Start {
public static void main(String[] args) {
Manage manage=new Manage();
manage.start();
}
}
package start;
import info.ConsumInfo;
import info.MobileCard;
import info.Scene;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
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.Scanner;
import java.util.Set;
import Service.CallService;
import Service.NetSevice;
import Service.SendService;
import myPackage.NetPackage;
import myPackage.ServicePackage;
import myPackage.SuperPackage;
import myPackage.TalkPackage;
public class Util {
private static final ServicePackage TalkPackage = null;
private static final ServicePackage NetPackage = null;
private static final ServicePackage SuperPackage = null;
Scanner input = new Scanner(System.in);
ServicePackage sp;
MobileCard card = new MobileCard();
Map<String, MobileCard> map = new HashMap<String, MobileCard>();
Map<String, List<ConsumInfo>> consumInfo = new HashMap<String, List<ConsumInfo>>();
List<ConsumInfo> list = new ArrayList<ConsumInfo>();
Map<Integer, Scene> scenes = new HashMap<Integer, Scene>();
Scene scene0 = new Scene("通话", 90, "问候客户,怎知其如此慢,通话90分钟");
Scene scene1 = new Scene("通话", 600, "给老婆打电话,问问工作和生活情况,通话600分钟");
Scene scene2 = new Scene("短信", 7, "给10086发短信查询相关业务,发送短信7条");
Scene scene3 = new Scene("短信", 40, "给妈妈发短信问最近身体状况,发送短信40条");
Scene scene4 = new Scene("上网", 2048, "用手机在线观看视频,使用流量2GB");
Scene scene5 = new Scene("上网", 1024, "和老婆语音童话,通宵一晚,使用流量1GB");
public void initData() {
MobileCard card1 = new MobileCard("13979387057", "大大", "1234",
new TalkPackage(), 68, 32, 200, 20, 300);
MobileCard card2 = new MobileCard("13979387058", "发阿萨德", "12345",
new NetPackage(), 78, 22, 100, 10, 3000);
MobileCard card3 = new MobileCard("13979387059", "当阿萨德", "123456",
new SuperPackage(), 88, 12, 200, 20, 500);
map.put("13979387057", card1);
map.put("13979387058", card2);
map.put("13979387059", card3);
ConsumInfo info = new ConsumInfo("13979387057", "通话", 200);
list.add(info);
consumInfo.put("13979387057", list);
}
public String[] getNum(int count) {
String[] nums = new String[9];
String number = null;
for (int i = 0; i < count; i++) {
number = "139";
Random random = new Random();
for (int j = 0; j < 8; j++) {
number += random.nextInt(9);
}
nums[i] = number;
}
return nums;
}
public void signIn() {
System.out.println("*****可选择的卡号*****");
String[] nums = getNum(9);
for (int i = 0; i < nums.length; i++) {
System.out.print((i + 1) + "、" + nums[i] + "\t");
if ((i + 1) % 3 == 0) {
System.out.println();
}
}
System.out.print("请选择卡号(输入1~9的序号):");
int cardChose = input.nextInt();
if (cardChose < 1 || cardChose > 9) {
System.out.println("输入错误!");
} else if (!map.containsKey(nums[cardChose - 1])) {
card.cardNumble = nums[cardChose - 1];
} else {
System.out.println("对不起,此号码已被注册,请重新选择");
}
System.out.print("1.话唠套餐 2.网虫套餐 3.超人套餐, 请选择套餐(输入序号):");
int packageChose = input.nextInt();
System.out.print("请输入姓名:");
card.uesrName = input.next();
System.out.print("请输入密码:");
card.passWord = input.next();
System.out.print("请输入预存话费金额:");
double pay = input.nextInt();
switch (packageChose) {
case 1:
sp = new TalkPackage();
break;
case 2:
sp = new NetPackage();
break;
case 3:
sp = new SuperPackage();
break;
default:
System.out.println("输入有误!");
break;
}
card.setPackage = sp;
card.consumAmount = sp.price;
card.money = pay - card.consumAmount;
while (card.money < 0) {
System.out.print("您预存的话费金额不足以支付本月固定套餐资费,请重新充值:");
pay = input.nextInt();
card.money = pay - card.consumAmount;
}
map.put(card.cardNumble, card);
if (map.containsKey(card.cardNumble)) {
card.showMeg();
sp.showInfo();
System.out.println("注册成功!");
} else {
System.out.println("注册失败,请重新注册!");
}
}
public void login() {
System.out.print("请输入手机卡号:");
String yourNumber = input.next();
System.out.print("请输入密码:");
String yourPassWord = input.next();
if (map.containsKey(yourNumber)) {
if (map.get(yourNumber).passWord.equals(yourPassWord)) {
boolean flag = true;
do {
System.out.println();
System.out.println("*****嗖嗖移动用户菜单*****");
System.out
.print("1.本月账单查询\n2.套餐余量查询\n3.打印消费详单\n4.套餐变更\n5.办理退网\n请选择(输入1~5选择功能,其他键返回上一级):");
int functionChose = input.nextInt();
switch (functionChose) {
case 1:
showAmountDetail(yourNumber);
break;
case 2:
showRemaiDetail(yourNumber);
break;
case 3:
printAmountDetail(yourNumber);
break;
case 4:
changePackage(yourNumber);
break;
case 5:
delateCard(yourNumber);
break;
default:
flag = false;
break;
}
} while (flag);
} else {
System.out.println("密码输入错误!");
}
} else {
System.out.println("对不起,您输入的信息有误,无法登录!");
}
}
public void showAmountDetail(String yourNumber) {
System.out.println();
System.out.println("*****本月账单查询******");
System.out.println("您的卡号:" + yourNumber + ",当月账单:");
System.out.println("套餐资费:" + sp.price + "元");
System.out.println("合计:" + dataFormat(map.get(yourNumber).consumAmount)
+ "元");
System.out.println("账户余额:" + dataFormat(map.get(yourNumber).money)
+ "元");
}
public void showRemaiDetail(String yourNumber) {
System.out.println();
System.out.println("*****套餐余量查询******");
System.out.println("您的卡号:" + yourNumber + ",套餐内剩余:");
StringBuffer meg = new StringBuffer();
int remainTalkTime;
int remainSmsCount;
int remainFlow;
if (map.get(yourNumber).setPackage instanceof TalkPackage) {
TalkPackage cardPackage = (TalkPackage) (map.get(yourNumber).setPackage);
remainTalkTime = cardPackage.talkTime > map.get(yourNumber).realTalkTime ? cardPackage.talkTime
- card.realTalkTime
: 0;
meg.append("童话时长:" + remainTalkTime + "分钟\n");
remainSmsCount = cardPackage.smsCount > map.get(yourNumber).realSMSCount ? cardPackage.smsCount
- map.get(yourNumber).realSMSCount
: 0;
meg.append("短信条数:" + remainSmsCount + "条\n");
} else if (map.get(yourNumber).setPackage instanceof NetPackage) {
NetPackage cardPackage = (NetPackage) (map.get(yourNumber).setPackage);
remainFlow = cardPackage.flow > map.get(yourNumber).realFlow ? cardPackage.flow
- map.get(yourNumber).realFlow
: 0;
meg.append("上网流量:" + remainFlow + "M\n");
} else if (map.get(yourNumber).setPackage instanceof SuperPackage) {
SuperPackage cardPackage = (SuperPackage) (map.get(yourNumber).setPackage);
remainTalkTime = cardPackage.talkTime > map.get(yourNumber).realTalkTime ? cardPackage.talkTime
- map.get(yourNumber).realTalkTime
: 0;
meg.append("童话时长:" + remainTalkTime + "分钟\n");
remainSmsCount = cardPackage.smsCount > map.get(yourNumber).realSMSCount ? cardPackage.smsCount
- map.get(yourNumber).realSMSCount
: 0;
meg.append("短信条数:" + remainSmsCount + "条\n");
remainFlow = cardPackage.flow > map.get(yourNumber).realFlow ? cardPackage.flow
- map.get(yourNumber).realFlow
: 0;
meg.append("上网流量:" + remainFlow + "M\n");
}
System.out.println(meg);
}
public void printAmountDetail(String yourNumber) {
Set<String> numbles = consumInfo.keySet();
Iterator<String> it = numbles.iterator();
boolean isExist = false;
while (it.hasNext()) {
String numbleKey = it.next();
if (numbleKey.equals(yourNumber)
&& consumInfo.get(yourNumber).size() != 0) {
isExist = true;
}
}
if (isExist) {
StringBuffer brf = new StringBuffer("*******" + yourNumber
+ "消费记录*******\n");
brf.append("序号\t类型\t数据(通话(分钟)/上网(M)/短信(条)\n");
List<ConsumInfo> infos = consumInfo.get(yourNumber);
for (int i = 0; i < infos.size(); i++) {
brf.append((i + 1) + "\t" + infos.get(i).type + "\t"
+ infos.get(i).consumDate + "\n");
}
Writer os = null;
BufferedWriter bw = null;
try {
os = new FileWriter("消费记录.txt");
bw = new BufferedWriter(os);
bw.write(brf.toString());
bw.flush();
System.out.println("消费记录打印完毕!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
System.out.println("对不起,不存在此卡的消费记录,不能打印!");
}
}
public void changePackage(String yourNumber) {
boolean isCoreact = true;
System.out.print("1.话唠套餐 2.网虫套餐 3.超人套餐, 请重新选择套餐(输入序号):");
int optionrevie = input.nextInt();
switch (optionrevie) {
case 1:
sp = new TalkPackage();
if (map.get(yourNumber).setPackage instanceof TalkPackage) {
System.out.println("您已是此套餐,无需变更!");
} else {
map.get(yourNumber).setPackage = TalkPackage;
System.out.println("套餐更改成功!");
}
break;
case 2:
sp = new NetPackage();
if (map.get(yourNumber).setPackage instanceof NetPackage) {
System.out.println("您已是此套餐,无需变更!");
} else {
map.get(yourNumber).setPackage = NetPackage;
System.out.println("套餐更改成功!");
}
break;
case 3:
sp = new SuperPackage();
if (map.get(yourNumber).setPackage instanceof SuperPackage) {
System.out.println("您已是此套餐,无需变更!");
} else {
map.get(yourNumber).setPackage = SuperPackage;
System.out.println("套餐更改成功!");
}
break;
default:
System.out.println("请输入正确的序号!");
break;
}
}
public void delateCard(String yourNumber) {
System.out.print("您确定注销此手机号码吗?Y/N:");
String chouseyes = input.next();
if (chouseyes.equalsIgnoreCase("Y")) {
map.remove(yourNumber);
System.out.println("您已成功注销该手机号!");
}
}
public void uesSoso() {
scenes.put(0, scene0);
scenes.put(1, scene1);
scenes.put(2, scene2);
scenes.put(3, scene3);
scenes.put(4, scene4);
scenes.put(5, scene5);
System.out.print("请输入手机卡号:");
String yourNumber = input.next();
System.out.print("请输入密码:");
String yourPassWord = input.next();
if (map.containsKey(yourNumber)) {
if (map.get(yourNumber).passWord.equals(yourPassWord)) {
card = map.get(yourNumber);
ServicePackage pack = card.setPackage;
Random random = new Random();
int ranNum = 0;
int temp = 0;
boolean iscontinued = true;
do {
ranNum = random.nextInt(6);
Scene scene = scenes.get(ranNum);
switch (ranNum) {
case 0:
case 1:
if (pack instanceof CallService) {
System.out.println(scene.description);
try {
temp = ((CallService) pack).call(scene.data,
card);
} catch (Exception e) {
e.printStackTrace();
}
list.add(new ConsumInfo(yourNumber, scene.type,
temp));
iscontinued = false;
break;
} else {
continue;
}
case 2:
case 3:
if (pack instanceof SendService) {
System.out.println(scene.description);
try {
temp = ((SendService) pack).send(scene.data,
card);
} catch (Exception e) {
e.printStackTrace();
}
list.add(new ConsumInfo(yourNumber, scene.type,
temp));
iscontinued = false;
break;
} else {
continue;
}
case 4:
case 5:
if (pack instanceof NetSevice) {
System.out.println(scene.description);
try {
temp = ((NetSevice) pack).netPlay(scene.data,
card);
} catch (Exception e) {
e.printStackTrace();
}
list.add(new ConsumInfo(yourNumber, scene.type,
temp));
iscontinued = false;
break;
} else {
continue;
}
}
System.out.println("添加一条消费记录!");
consumInfo.put(yourNumber, list);
} while (iscontinued);
} else {
System.out.println("密码输入错误!");
}
} else {
System.out.println("对不起,您输入的信息有误,无法登录!");
}
}
public void addPayMoney() {
System.out.print("请输入您要充值的手机号码:");
String yourNumber = input.next();
System.out.print("请输入您要充值手机号码的密码:");
String yourPassWord = input.next();
if (map.containsKey(yourNumber)) {
if (map.get(yourNumber).passWord.equals(yourPassWord)) {
System.out.print("请输入您要充值的金额:");
double payMoney = input.nextDouble();
map.get(yourNumber).money += payMoney;
System.out.println("话费充值成功,您当前话费余额" + map.get(yourNumber).money
+ "元");
} else {
System.out.println("密码输入错误!");
}
} else {
System.out.println("对不起,您输入的信息有误,无法登录!");
}
}
public void ziFei() {
System.out.println();
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
is = new FileInputStream("资费说明.txt");
isr = new InputStreamReader(is, "GBK");
br = new BufferedReader(isr);
String info =null;
while((info=br.readLine())!=null){
System.out.println(info);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
isr.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String dataFormat(double data) {
DecimalFormat format = new DecimalFormat("##.00");
return format.format(data);
}
public double sub(double num1, double num2) {
return (num1 * 10 - num2 * 10) / 10;
}
}
*****资费说明******
套餐类型:话唠套餐
通话时长:200分钟
短信条数:50条
月资费:58元
——————————————
套餐类型:网虫套餐
上网流量:5GB
月资费:68元
——————————————
套餐类型:超人套餐
通话时长:200分钟
短信条数:100条
上网流量:1GB
月资费:78元
——————————————
超出套餐计费:
通话时长:0.2元/分钟
短信条数:0.1元/分钟
上网流量:0.1元/分钟
*******13948368681消费记录*******
序号 类型 数据(通话(分钟)/上网(M)/短信(条)
1 短信 40
2 通话 600
3 通话 0
**本项目属于原创,请大家多多支持!需要源码的请联系作者!**