嗖嗖移动营业大厅(Java面向对象编程)
用Java面向对象思想写的一个移动营业厅项目
信息介绍:
类图如下:
工具类:
代码展示:
接口:CallSevice(通话),NetService(上网),SendService(短信)
package cn.Inter;
import cn.Hall.MobileCard;
/**
* 通话
* */
public interface CallSevice {
int call(int minCount, MobileCard card) throws Exception;
}
package cn.Inter;
import cn.Hall.MobileCard;
/**
* 上网
* */
public interface NetService {
int netPlay(int flow, MobileCard card) throws Exception;
}
package cn.Inter;
import cn.Hall.MobileCard;
/**
* 短信
* */
public interface SendService {
int send(int count, MobileCard card) throws Exception;
}
公共类:Common
package cn.Hall;
import java.text.DecimalFormat;
/**
* 公共类
* */
public class Common {
public static String dataFormat(double data){
DecimalFormat df = new DecimalFormat("#0.00");
return df.format(data);
}
public static double sub(double sum1,double sum2){
return (sum1*10-sum2*10)/10;
}
}
工具类:CardUtil
package cn.Hall;
import cn.Inter.CallSevice;
import cn.Inter.NetService;
import cn.Inter.SendService;
import java.io.*;
import java.util.*;
/**
* 工具类
* */
public class CardUtil {
Scanner input = new Scanner(System.in);
//用户列表
public static Map<String,MobileCard> cards = new HashMap<>();
//卡号消费记录列表
public static Map<String, List<ConsumInfo>> consumInfos = new HashMap<>();
//使用场景列表
Map<Integer, Scene> scenes = new HashMap<Integer, Scene>();
Scene scene0 = new Scene("通话", 90, "问候客户,谁知其如此难缠,通话90分钟");
Scene scene1 = new Scene("通话", 30, "询问妈妈身体状况,本地通话30分钟");
Scene scene2 = new Scene("短信", 5, "参与环境保护实施方案问卷调查,发送短信5条");
Scene scene3 = new Scene("短信", 50, "通知朋友手机换号,发送短信50条");
Scene scene4 = new Scene("上网", 1024, "和女朋友用微信视频聊天,使用流量1GB");
Scene scene5 = new Scene("上网", 2 * 1024, "晚上手机在线看韩剧,不留神睡着啦!使用2GB");
/**
* 初始化
* */
public void initScene(){
ServicePackage servicePackage = new ServicePackage();
//话唠
MobileCard ss = new MobileCard("13739663177", "骄傲", "123456", new TalkPackage(), 88, 100, 89, 88, 90); //话唠套餐
//网虫
MobileCard mm = new MobileCard("13639663177", "的走", "123456", new NetPackage(),99, 200, 200, 60, 20); //网虫套餐
//超级
MobileCard yy = new MobileCard("13539663177", "下去", "123456", new SuperPackage(), 100, 300, 50, 20, 900); //超人套餐
//添加到集合中
CardUtil.cards.put(ss.getCardNumber(),ss);
CardUtil.cards.put(mm.getCardNumber(),mm);
CardUtil.cards.put(yy.getCardNumber(),yy);
ConsumInfo info = new ConsumInfo(mm.getCardNumber(),"电话",500);
List<ConsumInfo> list = new ArrayList<>();
list.add(info);
CardUtil.consumInfos.put(info.getCardNo(),list);
}
/**
* 根据卡号和密码验证该卡是否注册:
* */
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;
}
/**
* 根据卡号,验证该卡是否注册
* */
public boolean isExistCard(String number){
Set<String> numbers=cards.keySet();
Iterator<String> iterator=numbers.iterator();
while (iterator.hasNext()) {
String str=iterator.next();
if (str.equals(number))
return true;
}
return false;
}
/**
* 生成随机卡号
* */
public String createNumber(){
String zhi = "1234567890";
String alphas = "139";
Random rand = new Random();
// 8位数字
for (int i = 0; i < 8; i++) {
alphas += zhi.charAt(rand.nextInt(10));
}
return alphas;
}
/**
* 生成指定个数的卡号列表
* */
public ArrayList<String> getNewNumber(int count){
ArrayList<String> list = new ArrayList<>();
for(int j = 1; j <= count; j++){
String kaHao = createNumber();
System.out.print(j+"."+kaHao+"\t");
list.add(kaHao);
if(j%3 == 0){
System.out.println();
}
}
return list;
}
/**
* 注册新卡
* */
public void addCard(MobileCard card){
cards.put(card.getCardNumber(),card);
}
/**
* 办理退网
* */
public void delCard(String number){
if (cards.containsKey(number)) {
cards.remove(number); //移除手机号
System.out.println("卡号:"+number+"办理退网成功!");
System.out.println("谢谢使用!");
}else {
System.out.println("手机号码输入有误!");
}
}
/**
* 套餐余额查询
* */
public void showRemainDetail(String number){
MobileCard card = null;//要查询的卡
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;
//话唠套餐,查询套餐内剩余的通话时长和短信条数
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);
}
/**
* 本月账单查询
* */
public void showAmountDetail(String number){
MobileCard card;//要查询的卡
StringBuffer meg = new StringBuffer();
card = cards.get(number);
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);
}
/**
* 添加指定卡号的消费记录
* */
public void addConsumInfo(String number,ConsumInfo info){
if (consumInfos.containsKey(number)) { //若存在指定的键映射的“键值对”,则返回true
consumInfos.get(number).add(info);
}else {
List<ConsumInfo> list = new ArrayList<ConsumInfo>();
list.add(info);
consumInfos.put(number, list);
}
}
/**
* 使用嗖嗖
* */
public void userSoso(String number){
//添加场景Map集合的键值对
scenes.put(0,scene0);
scenes.put(1,scene1);
scenes.put(2,scene2);
scenes.put(3,scene3);
scenes.put(4,scene4);
scenes.put(5,scene5);
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);
System.out.println(ranNum);
switch (ranNum){
//序号为0或1为通话场景
case 0:
case 1:
//判断该卡所属套餐是否支持通话功能
if(pack instanceof CallSevice){
//执行通话方法
System.out.println(scene.getDescription());
CallSevice callSevice = (CallSevice)pack;
try{
temp = callSevice.call(scene.getData(),card);
}catch(Exception e){
e.printStackTrace();
}
//添加一条消费记录
addConsumInfo(number,new ConsumInfo(number,scene.getType(),temp));
break;
}else{
//如果该卡套餐不支持通话功能,则重新生成随机数选择其他场景
continue;
}
case 2:
case 3:
//判断该卡所属套餐是否支持短信功能
if(pack instanceof SendService){
//执行通话方法
System.out.println(scene.getDescription());
SendService sendService = (SendService)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 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);
}
/**
* 资费说明
* */
public void showDescription(){
FileReader fr = null;
BufferedReader br = null;
try{
fr = new FileReader("D:\\俺的文件夹\\北大青鸟S1\\使用JAVA实现面向对象编程\\嗖嗖移动业务大厅\\src\\资费说明.txt");
br = new BufferedReader(fr);
String line = br.readLine();
while(line != null){
System.out.println(line);
line = br.readLine();
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
try{
if(fr != null){
fr.close();
}
if(br != null){
br.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
/**
* 套餐更变
* */
public void changingPack(String number,int packNum){
MobileCard card = cards.get(number);
switch (packNum){
//话唠套餐
case 1:
if(card.getSerPackage() instanceof TalkPackage){
System.out.println("对不起,您已经是该套餐用户,无法更换!");
}else{
Bian(card,packNum);
}
break;
//网虫套餐
case 2:
if(card.getSerPackage() instanceof NetPackage){
System.out.println("对不起,您已经是该套餐用户,无法更换!");
}else{
Bian(card,packNum);
}
break;
//超人套餐
case 3:
if(card.getSerPackage() instanceof SuperPackage){
System.out.println("对不起,您已经是该套餐用户,无法更换!");
}else{
Bian(card,packNum);
}
break;
}
}
/**
* //套餐更变(附属)
* */
public void Bian(MobileCard card,int packNum){//套餐更变(附属)
//如果剩余费用不够支持新的套餐费用
if(card.getMoney() < card.getSerPackage().getPrice()){
System.out.println("余额不足,请充值!");
}else{
card.setRealTalkTime(0);
card.setRealSMSCount(0);
card.setRealFlow(0);
card.setSerPackage(createPack(packNum));
card.setMoney(card.getMoney() - card.getSerPackage().getPrice());
System.out.println("套餐更换成功!");
createPack(packNum).showInfo();
}
}
/**
* 话费充值
* */
public void chargeMoney(String number,double money){
cards.get(number).setMoney(cards.get(number).getMoney()+money);
}
/**
* 打印消费详单
* */
public void printAmountDetail(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<>();//存储指定卡所有消费记录
boolean isExit = false;//现有消费记录中是否存在此卡号消费记录,是:true;不是:false
while (it.hasNext()){
if(it.next().equals(number)){
infos = consumInfos.get(number);
isExit = true;
break;
}
}
if(isExit){
//存在此卡消费记录,写入文本文件
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();
}
}
}
}
/**
* 根据用户选择的套餐序号返回套餐对象
* */
public ServicePackage createPack(int packId){
ServicePackage servicePackage = null;
do {
if(packId == 1){
servicePackage = new TalkPackage();
break;
}else if(packId == 2){
servicePackage = new NetPackage();
break;
}else if(packId == 3){
servicePackage = new SuperPackage();
break;
}else{
System.out.println("对不起,没有该选项!");
continue;
}
}while(true);
return servicePackage;
}
}
消费信息类:ConsumInfo
package cn.Hall;
import javax.swing.*;
/**
* 消费信息类
* */
public class ConsumInfo {
private int talkTime; //通话时长
private int smsCount; //短信条数
private int flow; //上网流量
private double price; //套餐月资费
private String cardNo; //卡号
private String type; //消费类型
private int consumData;//消费数据
public ConsumInfo(String cardNo, String type, int consumData) {
this.cardNo = cardNo;
this.type = type;
this.consumData = consumData;
}
//通话时长
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//卡号
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
//消费类型
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;
}
}
嗖嗖移动卡类:MobileCard
package cn.Hall;
/**
* 嗖嗖移动卡类
* */
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 cardNumber, String username, String password, ServicePackage serPackage, double money) {
this.cardNumber = cardNumber;
this.username = username;
this.password = password;
this.serPackage = serPackage;
this.money = money;
}
public MobileCard(String cardNumber, String username, String password, ServicePackage serPackage, double consumAmount, double money, int realTalkTime, int realSMSCount, int realFlow) {
this.cardNumber = cardNumber;
this.username = username;
this.password = password;
this.serPackage = serPackage;
this.consumAmount = consumAmount;
this.money = money;
this.realTalkTime = realTalkTime;
this.realSMSCount = realSMSCount;
this.realFlow = realFlow;
}
//卡号
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 getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
//当月消费金额
public double getConsumAmount() {
return consumAmount;
}
public void setConsumAmount(double consumAmount) {
this.consumAmount = consumAmount;
}
//当月实际通话时长
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 showMsg(){
System.out.println("卡号:"+this.cardNumber+",用户名:"+this.username+",当前余额:"+this.money+"元");
this.serPackage.showInfo();
}
}
使用场景类:Scene
package cn.Hall;
/**
* 使用场景类
* */
public class Scene {
private String type; //场景类型
private int data; //场景消费数据
private String description;//场景描述
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;
}
}
品牌套餐类(父类)ServicePackage;话痨套餐类(子类)TalkPackage;网虫套餐类(子类)NetPackage;超人套餐类(子类)SuperPackage
package cn.Hall;
/**
* 品牌套餐类(父类)
* */
public class ServicePackage {
private double price;//套餐月消费
//套餐月消费
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void showInfo(){
}
}
package cn.Hall;
import cn.Inter.CallSevice;
import cn.Inter.SendService;
/**
* 话痨套餐类
* */
public class TalkPackage extends ServicePackage implements CallSevice, SendService {
private int talkTime;//通话时长
private int smsCount;//短信条数
//public double price;//套餐月资费
//通话时长
public int getTalkTime() {
return talkTime;
}
//短信条数
public int getSmsCount() {
return smsCount;
}
public void setSmsCount(int smsCount) {
this.smsCount = smsCount;
}
public TalkPackage() {
this.talkTime = 500;
this.smsCount = 30;
super.setPrice(58);
}
public void showInfo(){
System.out.println("超人套餐:通话时长为"+this.talkTime+"分钟/月,短信条数为:"+this.smsCount+"条/月,上网流量为"+0+"GB/月");
}
/**
* @param minCount 通话分钟数
* @param card 超出套餐内的通话时长时需消费那张卡的余额
* */
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);
}else if(card.getMoney() >= 0.2){
//第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付
card.setRealTalkTime(card.getRealTalkTime() + 1); //实际使用通话时长1分钟
//账户余额消费0.2元(1分钟 额外通话)
card.setMoney(card.getMoney() - 0.2);
card.setConsumAmount(card.getConsumAmount() + 0.2); //当月消费金额 + 0.2
}else{
temp = i; //记录实际通话分钟数
throw new Exception("本次已经通话" + i + "分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
public int send(int count, MobileCard card) throws Exception{
int temp = count;
for(int i = 0; i < count; i++){
if(this.smsCount - card.getRealSMSCount() >= 1){
//第一种情况:套餐剩余短信数能够发送一个短信
card.setRealSMSCount(card.getRealSMSCount() + 1);
}else if(card.getMoney() >= 0.1){
//第二种情况:套餐内短信已经用完,剩下话费能够允许发一条短信
card.setRealSMSCount(card.getRealSMSCount() + 1); //实际短信数加 1 条
//账户余额消费0.1元,(一条短信)
card.setMoney(card.getMoney() - 0.1);
card.setConsumAmount(card.getConsumAmount() + 0.1); //当月消费金额 + 0.1
}else{
temp = i; //记录发短信条数
throw new Exception("短信已经发送" + i + "条,您的余额不足,请充值后再使用!");
}
}
return temp;
}
}
package cn.Hall;
import cn.Inter.NetService;
/**
* 网虫套餐类
* */
public class NetPackage extends ServicePackage implements NetService {
private int flow; //上网流量
//private double price;//套餐月资费
//上网流量
public int getFlow() {
return flow;
}
public void setFlow(int flow) {
this.flow = flow;
}
public NetPackage() {
this.flow = 3;
super.setPrice(68);
}
public void showInfo(){
System.out.println("网虫套餐:通话时长为"+0+"分钟/月,短信条数为:"+0+"条/月,上网流量为"+this.flow+"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){
//第一种情况:流量剩余够 1 MB
card.setRealFlow(card.getRealFlow() + 1);
}else if(card.getMoney() >= 0.1){
//第二种情况:套餐内流量已经用完,剩下话费够支付 1 MB的流量
card.setRealFlow(card.getRealFlow() + 1); //实际短信数加 1 条
//账户余额消费0.1元,(1MB流量)
card.setMoney(card.getMoney() - 0.1);
card.setConsumAmount(card.getConsumAmount() + 0.1); //当月消费金额 + 0.1
}else{
temp = i; //记录使用流量多少MB
throw new Exception("流量已经使用" + i + "MB,您的余额不足,请充值后再使用!");
}
}
return temp;
}
}
package cn.Hall;
import cn.Inter.CallSevice;
import cn.Inter.NetService;
import cn.Inter.SendService;
/**
* 超人套餐类
* */
public class SuperPackage extends ServicePackage implements CallSevice, SendService, NetService {
private int talkTime;//通话时长
private int smsCount;//短信条数
private int flow; //上网流量
//private double price;//套餐月资费
//通话时长
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;
super.setPrice(78);
}
public void showInfo(){
System.out.println("超人套餐:通话时长为"+this.talkTime+"分钟/月,短信条数为:"+this.smsCount+"条/月,上网流量为"+this.flow+"GB/月");
}
/**
* @param minCount 通话分钟数
* @param card 超出套餐内的通话时长时需消费那张卡的余额
* */
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);
}else if(card.getMoney() >= 0.2){
//第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付
card.setRealTalkTime(card.getRealTalkTime() + 1); //实际使用通话时长1分钟
//账户余额消费0.2元(1分钟 额外通话)
card.setMoney(card.getMoney() - 0.2);
card.setConsumAmount(card.getConsumAmount() + 0.2); //当月消费金额 + 0.2
}else{
temp = i; //记录实际通话分钟数
throw new Exception("本次已经通话" + i + "分钟,您的余额不足,请充值后再使用!");
}
}
return temp;
}
public int send(int count,MobileCard card) throws Exception{
int temp = count;
for(int i = 0; i < count; i++){
if(this.smsCount - card.getRealSMSCount() >= 1){
//第一种情况:套餐剩余短信数能够发送一个短信
card.setRealSMSCount(card.getRealSMSCount() + 1);
}else if(card.getMoney() >= 0.1){
//第二种情况:套餐内短信已经用完,剩下话费能够允许发一条短信
card.setRealSMSCount(card.getRealSMSCount() + 1); //实际短信数加 1 条
//账户余额消费0.1元,(一条短信)
card.setMoney(card.getMoney() - 0.1);
card.setConsumAmount(card.getConsumAmount() + 0.1); //当月消费金额 + 0.1
}else{
temp = i; //记录发短信条数
throw new Exception("短信已经发送" + i + "条,您的余额不足,请充值后再使用!");
}
}
return temp;
}
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){
//第一种情况:流量剩余够 1 MB
card.setRealFlow(card.getRealFlow() + 1);
}else if(card.getMoney() >= 0.1){
//第二种情况:套餐内流量已经用完,剩下话费够支付 1 MB的流量
card.setRealFlow(card.getRealFlow() + 1); //实际短信数加 1 条
//账户余额消费0.1元,(1MB流量)
card.setMoney(card.getMoney() - 0.1);
card.setConsumAmount(card.getConsumAmount() + 0.1); //当月消费金额 + 0.1
}else{
temp = i; //记录使用流量多少MB
throw new Exception("流量已经使用" + i + "MB,您的余额不足,请充值后再使用!");
}
}
return temp;
}
}
实现类:CreateBusiness
package cn.Hall;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**
* 创建业务类 SosoMgr
* */
public class CreateBusiness {
Scanner input = new Scanner(System.in);
CardUtil cu = new CardUtil();//工具类
Common cm = new Common(); //公共类
MobileCard mc = new MobileCard();//套餐父类
/**
* 主菜单
* */
public void start(){
cu.initScene(); //信息初始化
Common cm = new Common();
do{
System.out.println("----------\n1.用户登录\n2.用户注册\n3.使用嗖嗖\n4.话费充值\n5.资费说明\n6.退出系统");
System.out.print("请输入:");
int value = input.nextInt();
switch (value){
case 1:
System.out.println("用户登录-->");
cardMenu();//二级菜单
continue;
case 2:
System.out.println("用户注册-->");
registCard();//用户注册流程
continue;
case 3:
System.out.println("使用嗖嗖-->");
System.out.println("请输入手机卡号:");
String number=input.next();
cu.userSoso(number);
continue;
case 4:
System.out.println("话费充值-->");
System.out.print("请输入您的手机号:");
String phine = input.next();
System.out.print("请输入要充值的金额:");
double jinE = input.nextDouble();
boolean flag = cu.isExistCard(phine);
if(flag){
cu.chargeMoney(phine,jinE);
System.out.println("充值成功,当前的话费为:"+cu.cards.get(phine).getMoney()+"元");
}else{
System.out.println("您输入的手机号有误!");
}
continue;
case 5:
System.out.println("资费说明-->");
cu.showDescription();
continue;
case 6:
System.out.println("感谢您的的使用,欢迎下次光临!");
System.exit(1);
break;
default:
System.out.println("对不起,没有该选项!");
continue;
}
}while (true);
}
/**
* 二级菜单
* */
public void cardMenu(){
System.out.print("请输入手机卡号:");
String phine = input.next();
System.out.print("请输入卡号密码:");
String phinePwd = input.next();
if(cu.isExistCard(phine,phinePwd)){
System.out.println("登录成功!");
}else{
System.out.println("用户名或密码错误!");
return;
}
do{
System.out.println("**********明宇嗖嗖移动用户菜单**********");
System.out.println("1.本月账单查询\t2.套餐余额查询\t3.打印消费详单\t4.套餐更变\t5.办理退网");
System.out.println("请选择(输入1~5选择功能,其他键返回上一级):");
int zhi = input.nextInt();
switch (zhi){
case 1:
System.out.println("***>>本月账单查询");
cu.showAmountDetail(phine);
continue;
case 2:
System.out.println("***>>套餐余额查询");
cu.showRemainDetail(phine);
continue;
case 3:
System.out.println("***>>打印消费详单");
cu.printAmountDetail(phine);
continue;
case 4:
System.out.println("***>>套餐更变");
System.out.print("1.话唠套餐\t2.网虫套餐\t3.超人套餐,请选择套餐(输入序号):");
int num = input.nextInt();
cu.changingPack(phine,num);
continue;
case 5:
System.out.println("***>>办理退网");
cu.delCard(phine);
continue;
default:
System.out.println("***>>已返回!");
new CreateBusiness().start();
break;
}
}while(true);
}
/**
* 用户注册流程
* */
public void registCard(){//Use使用
System.out.println("******可选的卡号******");
ArrayList<String> list = cu.getNewNumber(9);
System.out.print("请选择卡号(输入1~9的序号):");
int xuHao = input.nextInt();
String number = list.get(xuHao-1); //卡号
int Hao = 0;
ServicePackage servicePackage = null;
do {
System.out.println("1.话唠套餐 2.网虫套餐 3.超人套餐,请选择套餐(输入序号):");
Hao = input.nextInt();
servicePackage = cu.createPack(Hao);
}while(Hao<1 && Hao>3);
mc.setSerPackage(servicePackage);
System.out.print("请输入姓名:");
String name = input.next();
System.out.print("请输入密码:");
String pwd = input.next();
System.out.print("请输入预存话费金额:");
int jinE = input.nextInt();
do {
System.out.print("您预存的话费余额不足以支付本月固定套餐资费,请重新充值:");
jinE = input.nextInt();
}while (jinE < servicePackage.getPrice());
mc.setMoney(jinE - servicePackage.getPrice());
MobileCard card = new MobileCard(number,name,pwd,mc.getSerPackage(),mc.getMoney());
cu.addCard(card);
System.out.print("注册成功!");
card.showMsg();
}
}
测试类:Test
package cn.Hall;
/**
* 测试类
* */
public class Test {
public static void main(String[] args) {
CreateBusiness cb = new CreateBusiness();
System.out.println("**********欢迎使用明宇嗖嗖移动大厅**********");
cb.start();
}
}
制作不易,搜索CSDN老明宇,感谢你们的支持!
原文作者:老明宇.
原文地址: https://blog.csdn.net/qq_44860582/article/details/102971110
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/qq_44860582/article/details/102971110
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
相关文章