package com.sosoyad.com;
//话唠套餐
public class TalkPackage extends ServicePackage implements ICallService,ISendService {
private int talkTime;
private int smsCount;
public int getTalkTime() {
return talkTime;
}
public void setTalkTime(int talkTime) {
this.talkTime = talkTime;
}
public int getSmsCount() {
return smsCount;
}
public void setSmsCount(int smsCount) {
this.smsCount = smsCount;
}
public TalkPackage() {
setPrice(58);
this.talkTime = 500;
this.smsCount = 30;
}
@Override
//保存要发送短信的条数,如果套餐内剩余分钟数减要使用的分钟数大于等于1的话,
//就给card中的realtalktime赋值加1,如果话费大于等于0.1的话,话费减0.1 打一分钟
public int call(int Count, MobileCard card) throws Exception {
int temp=Count;
for (int i = 0; i <Count ; i++) {
if (talkTime-card.getRealTalkTime()>=1){
card.setRealTalkTime(card.getRealTalkTime()+1);
}else if (card.getMoney()>=0.2){
card.setRealTalkTime(card.getRealTalkTime()+1);
card.setMoney(card.getMoney()-0.2);
card.setConsumAmount(card.getConsumAmount()+1);
}else {
temp=i;
throw new Exception("本次通话"+i+"分钟,余额不足");
}
}
return temp;
}
@Override
public int send(int count, MobileCard card) throws Exception {
int temt=count;
for (int i = 0; i <count ; i++) {
if (smsCount-card.getRealSMSCount()>=1) {
smsCount-=count;
card.setRealSMSCount(card.getRealSMSCount()+1);
}else if (card.getMoney()>=0.1){
card.setRealSMSCount(card.getRealSMSCount()+1);
card.setConsumAmount(card.getConsumAmount()+0.1);
card.setMoney(card.getMoney()-0.1);
}else {
temt=i;
throw new Exception("已经发送"+i+"条,余额不足");
}
}
return 0;
}
@Override
public void showInfo() {
System.out.println("话痨套餐:通话时长:" +
""+talkTime+"分钟/月"+"短信条数"+smsCount+"条");
}
}
package com.sosoyad.com;
//超人套餐
public class SuperPackage extends ServicePackage implements
ICallService,ISendService,INetService{
private int talkTime;
private int smsCount;
private int flow;
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;
}
SuperPackage() {
setPrice(78);
this.talkTime=200;
this.smsCount=50;
this.flow=1024;
}
@Override
public int call(int Count, MobileCard card) throws Exception {
int temp=Count;
for (int i = 0; i <Count; i++) {
if (talkTime-card.getRealTalkTime()>=1){
card.setRealTalkTime(card.getRealTalkTime()+1);
}else if (card.getMoney()>=0.2){
card.setRealTalkTime(card.getRealTalkTime()+1);
card.setMoney(card.getMoney()-0.2);
card.setConsumAmount(card.getConsumAmount()+0.1);
}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.getRealFlow()>=1){
card.setRealFlow(card.getRealSMSCount()+1);
}else if (card.getMoney()>=0.1){
card.setRealFlow(card.getRealSMSCount()+1);
card.setMoney(card.getMoney()-0.1);
card.setConsumAmount(card.getConsumAmount()+0.1);
}else {
temp=i;
throw new Exception("已经使用"+i+"M流量,余额不足");
}
}
return temp;
}
@Override
public int send(int count, MobileCard card) throws Exception {
int temp=count;
for (int i = 0; i <count ; i++) {
if (smsCount-card.getRealSMSCount()>=1){
card.setRealSMSCount(card.getRealSMSCount()+1);
}else if (card.getMoney()>=0.1){
card.setRealSMSCount(card.getRealSMSCount()+1);
card.setConsumAmount(card.getConsumAmount()+0.1);
card.setMoney(card.getMoney()-0.1);
}else {
temp=i;
throw new Exception("已经发送"+i+"条信息,余额不足");
}
}
return temp;
}
@Override
public void showInfo() {
System.out.println("超人卡:通话时长:"+talkTime+"分钟/月"+
"短信条数"+smsCount+"条"+"上网流量"+flow+"GB");
}
}
package com.sosoyad.com;
public abstract class ServicePackage {
private double Price;
public double getPrice() {
return Price;
}
public void setPrice(double price) {
Price = price;
}
public abstract void showInfo();
}
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;
}
}
package com.sosoyad.com;
//网虫
public class NetPackage extends ServicePackage implements INetService {
private int flow;
public int getFlow() {
return flow;
}
public void setFlow(int flow) {
this.flow = flow;
}
NetPackage(){
setPrice(68);
this.flow=3072;
}
@Override
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){
card.setRealFlow(card.getRealSMSCount()+1);
}else if (card.getMoney()>=0.1){
card.setRealFlow(card.getRealSMSCount()+1);
card.setMoney(card.getMoney()-0.1);
card.setConsumAmount(card.getConsumAmount()+0.1);
}else {
temp=i;
throw new Exception("已经使用"+i+"M流量,余额不足");
}
}
return temp;
}
@Override
public void showInfo() {
System.out.println("网虫套餐:上网流量"+(flow/1024)+"GB");
}
}
package com.sosoyad.com;
//卡类
public class MobileCard {
private String cardNumber;
private String userWord;
private String password;
private ServicePackage serPackage;
private double consumAmount;
private double money;
private int realTalkTime;
private int realSMSCount;
private int realFlow;
public MobileCard(String cardNumber, String userWord, String password,
ServicePackage serPackage, double consumAmount, double money) {
this.cardNumber = cardNumber;
this.userWord = userWord;
this.password = password;
this.serPackage = serPackage;
this.consumAmount = consumAmount;
this.money = money;
}
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
public String getUserWord() {
return userWord;
}
public void setUserWord(String userWord) {
this.userWord = userWord;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public ServicePackage getSerPakage() {
return serPackage;
}
public ServicePackage getSerPackage() {
return serPackage;
}
public void setSerPackage(ServicePackage serPackage) {
this.serPackage = serPackage;
}
public double getConsumAmount() {
return consumAmount;
}
public void setConsumAmount(double consumAmount) {
this.consumAmount = consumAmount;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public int getRealTalkTime() {
return realTalkTime;
}
public void setRealTalkTime(int realTalkTime) {
this.realTalkTime = realTalkTime;
}
public int getRealSMSCount() {
return realSMSCount;
}
public void setRealSMSCount(int realSMSCount) {
this.realSMSCount = realSMSCount;
}
public int getRealFlow() {
return realFlow;
}
public void setRealFlow(int realFlow) {
this.realFlow = realFlow;
}
MobileCard() {
}
public void showMeg(){
System.out.println("注册成功 卡号:"+cardNumber+"用户名"+
userWord+"当前余额"+money);
}
}
import java.text.DecimalFormat;
//消费记录
public class ConsumInfo {
private String cardNmber;
private String tybe;
private int consumData;
public ConsumInfo(String cardNmber, String tybe, int consumData) {
this.cardNmber = cardNmber;
this.tybe = tybe;
this.consumData = consumData;
}
public static String dataformat(double data){
DecimalFormat formatData=new DecimalFormat("#.0");
return formatData.format(data);
}
public String getCardNmber() {
return cardNmber;
}
public void setCardNmber(String cardNmber) {
this.cardNmber = cardNmber;
}
public String getTybe() {
return tybe;
}
public void setTybe(String tybe) {
this.tybe = tybe;
}
public int getConsumDate() {
return consumData;
}
public void setConsumData(int consumData) {
this.consumData = consumData;
}
}
public interface ICallService {
int call(int Count,MobileCard card)throws Exception;
}
public interface INetService{
int netPlay(int flow,MobileCard card)throws Exception;
}
public interface ISendService{
int send(int Count,MobileCard card)throws Exception;
}
import java.io.*;
import java.util.*;
import static sun.plugin.security.JDK11ClassFileTransformer.init;
public class CardUtil {
Scanner sc=new Scanner(System.in);
MobileCard moc=new MobileCard();
Map<String ,MobileCard>cards=new HashMap<String, MobileCard>();
Map<String,List<ConsumInfo>>consumInfo=new HashMap<String, List<ConsumInfo>>();
List<Scene>scenelist=new ArrayList<Scene>();
List<ConsumInfo>listcon=new ArrayList<ConsumInfo>();
//场景初始化
public void initScene(){
scenelist.add(new Scene("通话", 90, "电话忘了挂,通话90分钟"));
scenelist.add(new Scene("短信", 50, "发送短信50条"));
scenelist.add(new Scene("短信", 5, " 发送短信5条"));
scenelist.add(new Scene("上网", 1024, "与朋友视频通话,使用流量1GB"));
scenelist.add(new Scene("通话", 30, "与朋友打电话,通话30分钟"));
scenelist.add(new Scene("上网", 2048, "使用手机看视频,使用流量2GB"));
}
// 判断账号密码
public boolean isExistCard(String number,String passWord){
if (cards.containsKey(number)&&passWord.equals(cards.get(number).getPassword())){
return true;
}else {
return false;
}
}
// 判断手机号是否注册过
public boolean isExistCard(String number){
if (cards.containsKey(number)){
return false;
}else {
return true;
}
}
public String createNumber(){
StringBuffer sb=new StringBuffer("136");
sb.append((int) (Math.random() * (10000000 - 99999999) + 99999999));
String number=sb.toString();
return number;
}
public String[]getNewNumbers(int count){
String[]newnumber=new String[count];
for (int i = 0; i <count ;) {
String newnumber1=createNumber();
if (isExistCard(newnumber1)){
newnumber[i]=newnumber1;
i++;
}else {
continue;
}
}
int a=0;
for (int i = 0; i <3 ; i++) {
for (int j = 0; j <3 ; j++) {
System.out.println((a+1)+","+newnumber[a++]+"\t");
}
System.out.println();
}
return newnumber;
}//注册功能
public void addCard(MobileCard card){
System.out.println("*****卡号*******");
String[]num=getNewNumbers(9);
System.out.println("*****选择卡号*******");
int cardid;
while (true){
cardid=sc.nextInt();
if (cardid>0&&cardid<10){
card.setCardNumber(num[cardid-1]);
break;
}else {
continue;
}
}
System.out.println("1、话痨套餐 2、网虫套餐 3、超人套餐");
int taoid;
while (true){
taoid=sc.nextInt();
switch (taoid){
case 1:
card.setSerPackage(new TalkPackage());
break;
case 2:
card.setSerPackage(new NetPackage());
break;
case 3:
card.setSerPackage(new SuperPackage());
break;
default:
System.out.println();
continue;
}
break;
}
System.out.println("请输入姓名");
card.setUserWord(sc.next());
System.out.println("请输入密码");
card.setPassword(sc.next());
System.out.println("充值金额");
int moneys=0;
int money1=0;
while (true){
money1=sc.nextInt();
switch (taoid){
case 1:
moneys=58;
break;
case 2:
moneys=68;
break;
case 3:
moneys=78;
break;
}
if (money1>=moneys){
card.setMoney(money1-moneys);
break;
}else {
System.out.println("钱不够");
continue;
}
}
cards.put(num[cardid-1],card);
card.showMeg();
cards.get(card.getCardNumber()).getSerPakage().showInfo();
}
public void delCard(String number){
cards.remove(number);
System.out.println("考号:"+number+"退网成功");
}
//t套餐余额
public void showRemainDetail(String number){
MobileCard card=cards.get(number);
int smscount=0;
int flow=0;
int time=0;
if (card.getSerPackage()instanceof TalkPackage){
TalkPackage type=(TalkPackage)card.getSerPackage();
if (type.getTalkTime()>card.getRealTalkTime()){
time=type.getTalkTime()-card.getRealTalkTime();
}else {
time=0;
}
if (type.getSmsCount()>card.getRealSMSCount()){
smscount=type.getSmsCount()-card.getRealSMSCount();
}else {
smscount=0;
}
}
if (card.getSerPackage()instanceof SuperPackage){
SuperPackage type=(SuperPackage)card.getSerPackage();
if (type.getTalkTime()>card.getRealTalkTime()){
time=type.getTalkTime()-card.getRealTalkTime();
}else {
time=0;
}
if(type.getSmsCount()>card.getRealSMSCount()){
smscount=type.getSmsCount()-card.getRealSMSCount();
}else {
smscount=0;
}
if (type.getFlow()>card.getRealFlow()){
flow=type.getFlow()-card.getRealFlow();
}else {
flow=0;
}
}
if (card.getSerPackage()instanceof NetPackage){
NetPackage type=(NetPackage)card.getSerPackage();
if (type.getFlow()>card.getRealFlow()){
flow=type.getFlow()-card.getRealFlow();
}else {
flow=0;
}
}
System.out.println("卡号是"+card.getCardNumber()+"套餐余额");
System.out.println("通话时长"+time);
System.out.println("短信条数"+smscount);
System.out.println("上网流量"+flow);
}
//本月套餐
public void showAmountDetail(String number){
System.out.println("本月账单明细");
MobileCard card=cards.get(number);
System.out.println("你的卡号:"+number);
System.out.println("套餐资费"+card.getSerPackage().getPrice());
System.out.println("合计"+(card.getConsumAmount()+card.getSerPakage().getPrice()));
System.out.println("余额"+card.getMoney());
}
//添加消费清单
public void addConsumInfo(String number,ConsumInfo info){
listcon.add(info);
consumInfo.put(number,listcon);
}//使用搜搜
public void useSoso(String number){
MobileCard card1=cards.get(number);
ServicePackage pack=card1.getSerPackage();
int temp=0;
do {
int ran=(int)(Math.random()*6);
Scene scene=scenelist.get(ran);
switch (ran){
case 0:
case 4:
if (pack instanceof ICallService){
System.out.println(scene.getDescription());
ICallService callservice=(ICallService)pack;
try {
temp=callservice.call(scene.getData(),card1);
} catch (Exception e) {
e.printStackTrace();
}
addConsumInfo(number,new ConsumInfo(number,
scene.getDescription(),temp));
break;
}else {
continue;
}
case 1:
case 2:
if (pack instanceof ISendService){
System.out.println(scene.getDescription());
ISendService callservice=(ISendService)pack;
try {
temp=callservice.send(scene.getData(),card1);
} catch (Exception e) {
e.printStackTrace();
}
addConsumInfo(number,new ConsumInfo(number,scene
.getDescription(),temp));
break;
}else {
continue;
}
case 3:
case 5:
if (pack instanceof INetService){
System.out.println(scene.getDescription());
INetService callservice=(INetService)pack;
try {
temp=callservice.netPlay(scene.getData(),card1);
} catch (Exception e) {
e.printStackTrace();
}
addConsumInfo(number,new ConsumInfo(number,
scene.getDescription(),temp));
break;
}else {
continue;
}
}
break;
}while (true);
}//资费
public void showDescription(){
FileReader fis=null;
BufferedReader br=null;
try {
fis=new FileReader("D:\\资费说明.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
br=new BufferedReader(fis);
String s=null;
try {
while ((s=br.readLine())!=null){
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
if (br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void changingPack(String number,int packNum){
MobileCard card=cards.get(number);
ServicePackage type=null;
switch (packNum){
case 1:
if (!(card.getSerPackage()instanceof TalkPackage)){
type=new TalkPackage();
}
break;
case 2:
if (!(card.getSerPackage()instanceof NetPackage)){
type=new NetPackage();
}
break;
case 3:
if (!(card.getSerPackage()instanceof SuperPackage)){
type=new SuperPackage();
}
break;
}
if (type==null){
System.out.println("无需更换套餐");
return;
}
if (card.getMoney()<type.getPrice()){
System.out.println("余额不足");
}else {
card.setSerPackage(type);
System.out.println("更换成功");
showRemainDetail(number);
}
}
//打印清单
public void printConsumInfo(String number) {
Iterator<ConsumInfo>it=listcon.iterator();
FileWriter fw=null;
try {
fw=new FileWriter("D:\\消费清单.txt");
fw.write("————————"+number+"的消费记录————————————\r\n");
fw.write("序号\t类型\t数据(通话)/流量/短信\r\n");
int count=0;
while (it.hasNext()) {
ConsumInfo c = it.next();
String s = ++count + "\t" + c.getTybe() + "\t" + c.getConsumDate() + "\r\n";
fw.write(s);
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//充值
public void chargeMoney(String number,double money){
if (!isExistCard(number)){
double money1=cards.get(number).getMoney();
cards.get(number).setMoney(money+money1);
System.out.println("充值成功,当前话费:"+cards.get(number).getMoney());
}else {
System.out.println("不存在的手机号");
}
}
public void showone(){
initScene();
init();
MobileCard card=new MobileCard();
System.out.println("********欢迎使用搜搜移动*******");
System.out.println("1、用户登录 2、用户注册 3、使用嗖嗖 4、话费充值 5、资费说明 6、退出系统");
System.out.println("请选择");
int choose=sc.nextInt();
switch (choose){
case 1:
test();
break;
case 2:
addCard(card);
break;
case 3:
System.out.println("输入手机号");
String number=sc.next();
useSoso(number);
break;
case 4:
System.out.println("输入手机号");
String s=sc.next();
System.out.println("输入充值金额");
int m=sc.nextInt();
chargeMoney(s,m);
break;
case 5:
showDescription();
break;
case 6:
System.out.println("再见!!!");
return;
}
showone();
}
public void test() {
System.out.println("输入手机号");
String number=sc.next();
System.out.println("输入密码");
String key=sc.next();
if (isExistCard(number,key)){
System.out.println("------搜搜移动用户------");
System.out.print("1、本月账单查询\n2、套餐余量查询\n3、" +
"打印消费详单\n4、套餐变更\n5、办理退网\n请选择" +
"(输入1~5选择功能,其他键返回上一级):");
int num=sc.nextInt();
switch (num){
case 1:
showAmountDetail(number);
test();
break;
case 2:
showRemainDetail(number);
test();
break;
case 3:
printConsumInfo(number);
test();
break;
case 4:
System.out.println("套餐变更");
System.out.println("1、话痨套餐 2、网虫套餐 3、超人套餐");
int packnum=sc.nextInt();
changingPack(number,packnum);
test();
break;
case 5:
delCard(number);
break;
default:
showone();
break;
}
}else {
System.out.println("账号密码错误");
test();
}
}
}
public class Main {
public static void main(String[] args) {
CardUtil c=new CardUtil();
c.showone();
c.test();
}
}