JAVA项目-嗖嗖移动
/** * 移动卡类 */ 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() { //super(); } //接受新注册移动卡用户的名称,和手机号,密码 public MobileCard(String cardNumber, String userName, String passWord){ this.cardNumber=cardNumber; this.userName=userName; this.passWord=passWord; } public MobileCard(String cardNumber, String userName, String passWord, ServicePackage serPackage, double consumAmount, double money, int realTalkTime, int realSMSCount, int realFlow) { //super(); 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 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; } /** * 显示移动卡的信息 */ public void showMeg(){ } }
/** * 三个移动套餐----父类 */ public abstract class ServicePackage { //三个套餐的共同属性就是每月要交月租,不交钱还想用卡? private double price; public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public ServicePackage() { super(); } public ServicePackage(double price) { super(); this.price = price; } //定义一个抽象的控制台输出方法,三个套餐子类会用到 public abstract void showInfo(); }
/** * 短信---接口 */ public interface SendService { public abstract int send(int count, MobileCard card) throws Exception; }
/** * 上网流量---接口 */ public interface NetService { public abstract int netPlay(int flow, MobileCard card) throws Exception; }
/** * 通话--接口 */ public interface CallService { public abstract int call (int minCount, MobileCard card) throws Exception; }
/** * 超人套餐 * 此套餐继承套餐总类,并实现发送短信,上网流量,语音通话的三个接口 */ public class SuperPackage extends ServicePackage implements SendService, NetService, CallService { private int talkTime; //通话时长 private int smsCount; //短信数量 private int flow; //上网流量 public SuperPackage() { super(); } public SuperPackage(double price, int talkTime, int smsCount, int flow) { super(price); this.talkTime = talkTime; this.smsCount = smsCount; this.flow = 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; } //通话时长---暂时未实现具体功能 public int call(int minCount, MobileCard card) throws Exception{ int temp = minCount; return temp; } //使用流量数---暂时未实现具体功能 public int netPlay(int flow, MobileCard card) throws Exception { int temp = flow; return temp; } //发送短信数量---暂时未实现具体功能 public int send(int count, MobileCard card) throws Exception { int temp = count; return temp; } //重写套餐父类showInfo输出方法 public void showInfo() { } }
/** * 话唠套餐 * 此套餐继承套餐总类,并实现发送短信,语音通话的二个接口 * 戒除网瘾,从此套餐开始 */ public class TalkPackage extends ServicePackage implements CallService, SendService { private int talkTime;//通话时长 private int smsCount;//短信条数 public TalkPackage() { super(); } public TalkPackage(double price, int talkTime, int smsCount) { //把此套餐的价格传给父类 super(price); this.talkTime = talkTime; this.smsCount = 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 int call(int minCount, MobileCard card) throws Exception { int temp = minCount; return temp; } //发送短信数量------未实现此功能 public int send(int count, MobileCard card) throws Exception { int temp = count; return temp; } //重写套餐父类showInfo输出方法 public void showInfo() { } }
/** * 网虫套餐---网瘾少年,从此套餐做起~~~ * 此套餐继承餐套父类并继承上网流量接口 */ public class NetPackage extends ServicePackage implements NetService { private int flow; //上网流量 public NetPackage() { super(); } public NetPackage(double price, int flow) { super(price); this.flow = flow; } public int getFlow() { return flow; } public void setFlow(int flow) { this.flow = flow; } //使用流量---未实现此功能 public int netPlay(int flow, MobileCard card) throws Exception { int temp = flow; return temp; } //重写套餐父类showInfo输出方法 public void showInfo() { } }
/** * 实例化三个套餐类 */ public class Common { //实例化话唠套餐 //括号参数分别对应(价格,通话时间,短信数量) static TalkPackage talkPackage = new TalkPackage(58, 500, 30); //实例化网虫套餐 //括号参数分别对应(价格,上网流量) static NetPackage netPackage = new NetPackage(68, 3 * 1024); //实例化超人套餐 //括号参数分别对应(价格,通话时间,短信数量,上网流量) static SuperPackage superPackage = new SuperPackage(78, 200, 50, 1 * 1024); }
import java.io.*; import java.util.*; /** * 工具类 */ public class CardUtil { //初始用户集合 protected static Map<String, MobileCard> cards = new HashMap<String, MobileCard>(); //卡号消费记录列表 protected static Map<String, List<ConsumInfo>> consumInfos = new HashMap<String, List<ConsumInfo>>(); //Scanner方法,后面的方法会多次用到 protected static Scanner input = new Scanner(System.in); public CardUtil() { //super(); } /** * 添加用户 */ public static void addCard(MobileCard card) { cards.put(card.getCardNumber(), card); } /** * 话费充值 */ public static void chargeMoney(String number) { System.out.println("请输入要充值的金额(不少于50元):"); while (true) { double money = 0.0; while (true) { Scanner input = new Scanner(System.in); if (input.hasNextDouble() == true) { money = input.nextDouble(); break; } else { System.out.print("输入错误!请重新输入:"); } } if (money < 50) { System.out.println("输入金额少于50元请重新输入:"); continue; } else { cards.get(number).setMoney(cards.get(number).getMoney() + money); System.out.println("充值成功,当前话费余额为" + cards.get(number).getMoney()); break; } } } /** * 套餐费用简介 */ public static void introduce() throws IOException { //该文件地址需按照自己的文件地址格式来写 File file = new File("E:\\JavaLog/logs/2.txt"); //指定读取编码格式为bgk(解决读取出现中文乱码) BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk")); try { String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (FileNotFoundException e) { System.out.println("没有找到套餐费用简介文件"); } catch (IOException e) { e.printStackTrace(); } finally { //关闭流 br.close(); } } /** * 本月账单查询 */ public static void showAmountDetail(String number) { MobileCard mobileCard = cards.get(number); System.out.println("您的卡号为:" + mobileCard.getCardNumber()); System.out.println("当月账单如下:"); System.out.println("套餐资费:" + mobileCard.getSerPackage().getPrice() + "元" + "\t合计消费:" + mobileCard.getConsumAmount() + "元" + "\t账户余额:" + mobileCard.getMoney() + "元"); } /** * 套餐余量查询 */ public static void showRemainDetail(String searchNumber) { MobileCard mobileCard = cards.get(searchNumber); MobileCard card; int remainTalkTime; int remainSmsCount; int remainFlow; StringBuffer meg = new StringBuffer(); card = cards.get(searchNumber); meg.append("您的手机号码是" + searchNumber + "餐套内剩余:\n"); ServicePackage pack = card.getSerPackage(); //向下转型超人类型 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 + "条"); //查询套餐内剩余的流量 remainFlow = cardPack.getFlow() > card.getRealFlow() ? cardPack.getFlow() - card.getRealFlow() : 0; meg.append("通话时长:" + remainFlow + "分钟\n"); System.out.println("您的手机号是:" + mobileCard.getCardNumber() + ",套餐内剩余:"); System.out.println("通话时长:" + remainTalkTime); System.out.println("短信条数:" + remainSmsCount); System.out.println("上网流量:" + remainFlow); //向下转型上网套餐 } else if (pack instanceof NetPackage) { NetPackage cardPack = (NetPackage) pack; //查询套餐内剩余的流量 remainFlow = cardPack.getFlow() > card.getRealFlow() ? cardPack.getFlow() - card.getRealFlow() : 0; meg.append("通话时长:" + remainFlow + "分钟\n");
System.out.println("您的手机号是:" + mobileCard.getCardNumber() + ",套餐内剩余:"); System.out.println("上网流量:" + remainFlow); //向下转型话痨 } else { TalkPackage cardPack = (TalkPackage) pack; //查询套餐内剩余的通话时长 remainTalkTime = cardPack.getTalkTime() > card.getRealTalkTime() ? cardPack.getTalkTime() - card.getRealTalkTime() : 0; meg.append("通话时长:" + remainTalkTime + "分钟\n"); System.out.println("您的手机号是:" + mobileCard.getCardNumber() + ",套餐内剩余:"); System.out.println("通话时长:" + remainTalkTime); } } /** * 打印消费详单--未实现此功能 */ public static void printAmountDetail(String number) { } /** * 套餐变更---未实现此功能 */ public static void changingPack(String number) { } /** * 办理退网 */ public static void delCard(String number) { Set<String> numberKeys = cards.keySet(); Iterator<String> it = numberKeys.iterator(); while (it.hasNext()) { String numberKey = it.next(); //如果在集合中找到此号码,则删除他,完成退网 if (numberKey.equals(number)) { //删除 cards.remove(numberKey); System.out.println("卡号" + number + "办理退网成功\n谢谢使用!"); } else { System.out.println("没有找到您的手机号码,退卡失败!"); } } } /** * 根据手机号码和密码验证该卡是否已经是移动用户 * 实行登陆操作 */ public static boolean isExistCard(String number, String passWord) { if (cards.size() != 0) { Set<String> numberKeys = cards.keySet(); Iterator<String> its = numberKeys.iterator(); while (its.hasNext()) { String numberKey = its.next();
//根据key取出对应的值 MobileCard mobileCard = cards.get(numberKey);
//trim()作用是无视开始于结束的空格 if (number.trim().equals(numberKey.trim()) && passWord.trim().equals(mobileCard.getPassWord().trim())) { System.out.println("尊敬的☆" + mobileCard.getUserName() + "☆五星级会员,恭喜您登陆成功!\n"); return true; } else if (number.trim().equals(numberKey.trim()) == true && passWord.trim().equals(mobileCard.getPassWord().trim()) == false) { System.out.println("尊敬的☆" + mobileCard.getUserName() + "☆五星级会员,很遗憾您密码输入错误哦~"); return false; } } System.out.println("对不起您还没有注册会员呢~"); return false; } else { System.out.println("对不起您还没有注册会员呢~"); return false; } } /** * 手机号码验证是否已经注册 */ public static boolean isExistCard(String number) { if (cards.size() != 0) { Set<String> numberKeys = cards.keySet(); Iterator<String> its = numberKeys.iterator(); while (its.hasNext()) { if (number.equals(its.next())) { return true; } } System.out.println("对不起此号码还不是移动会员~"); return false; } else { System.out.println("对不起此号码还不是移动会员~"); return false; } } /** * 生成随机卡号 */ public static String createNumber() { Random random = new Random(); boolean isExist = false; String number = ""; int temp = 0; do { isExist = false; //生成的随机数是8位 do { temp = random.nextInt(100000000); } while (temp < 10000000); //生成之前,前面加“139” number = "139" + temp; //和已经注册的用户的卡号对比,重复则无法注册 if (cards != null) { //价格判断 Set<String> cardNumbers = cards.keySet(); for (String cardNumber : cardNumbers) { if (number.equals(cardNumber)) { isExist = true; break; } } } } while (isExist); return number; } /** * 生成指定个数的卡号列表 */ public static String[] getNewNumbers(int count) { String[] strs = new String[count]; for (int i = 0; i < count; i++) { strs[i] = createNumber(); } return strs; } /** * 添加指定卡号的消费记录 */ public static void addConsumInfo(String number, ConsumInfo info) { if (consumInfos.containsKey(number)) { consumInfos.get(number).add(info); } else { List<ConsumInfo> list = new ArrayList<ConsumInfo>(); list.add(info); consumInfos.put(number, list); } } }
import java.io.IOException; import java.util.Scanner; /** * 业务类 */ class SosoMgr { Scanner input = new Scanner(System.in); /** * 实现主菜单 */ public void mainMenu() throws IOException { String number = ""; boolean flag = true; 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.客户咨询 ***********"); System.out.println("*********** 7.退出系统 ***********"); System.out.println("*****************国庆钜惠走过路过不要错过***************"); System.out.print("请输入功能编号:"); String num = input.next(); switch (num) { case "1": System.out.println("*****************************《用户登录》*****************************"); System.out.print("请输入您的手机号:"); number = input.next(); if (number.length() == 11) { System.out.print("请输入您的密码:"); String passWord = input.next(); //通过手机号和密码判断该用户是否存在 if (CardUtil.isExistCard(number, passWord)) { cardMenu(number); } flag = true; } else { System.out.println("您输入的手机号码格式不正确!"); } break; case "2": System.out.println("*****************************《用户注册》*****************************"); registCard(); flag = true; break; case "3": System.out.println("*****************************《使用嗖嗖》*****************************"); flag = true; break; case "4": System.out.println("*****************************《话费充值》*****************************"); System.out.println("请输入充值手机号码:"); String refill_Number = input.next(); if (CardUtil.isExistCard(refill_Number)) { CardUtil.chargeMoney(refill_Number); } flag = true; break; case "5": CardUtil.introduce(); flag = true; break; case "6": System.out.println("*****************************《客户咨询》*****************************"); //客户咨询功能未实现,可在CardUtil类写出ask方法来实现 //CardUtil.ask() flag = true; break; case "7": System.out.println("*****************************《退出系统》*****************************"); System.out.println("谢谢使用"); flag = false; break; default: System.out.println("输入错误,请重新输入!"); flag = true; break; } } while (flag); } /** * 二级菜单 */ public void cardMenu(String number) { boolean flag = true; while (flag) { 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选择功能,其它键返回上一级):"); //num定义为String类型可以防止输入数字过大而导致的InputMismatchException异常 String num = input.next(); switch (num) { case "1": System.out.println("*****************************《本月账单查询》*****************************"); CardUtil.showAmountDetail(number); flag = true; break; case "2": System.out.println("*****************************《套餐余量查询》*****************************"); CardUtil.showRemainDetail(number); flag = true; break; case "3": System.out.println("*****************************《打印消费详情》*****************************"); CardUtil.printAmountDetail(number); flag = true; break; case "4": System.out.println("*****************************《套餐变更》*****************************"); CardUtil.changingPack(number); flag = true; break; case "5": System.out.println("*****************************《办理退网》*****************************"); CardUtil.delCard(number); flag = true; break; default: flag = false; break; } if (flag) { System.out.print("输入0返回上一层,输入其他键返回首页:"); int strNum = input.nextInt(); if (strNum == 1) { continue; } else { flag = false; } } } } /** * 用户注册 */ public void registCard() { MobileCard mobileCard = new MobileCard(); System.out.println("***************************可选择的手机号码***************************"); //通过循环遍历输出卡号,要输出10个号码 String[] cardNumbers = CardUtil.getNewNumbers(9); for (int i = 0; i < cardNumbers.length; i++) { System.out.print((i + 1) + "." + cardNumbers[i] + "\t"); if (2 == i || 5 == i || 8 == i) { System.out.println(); } } System.out.print("请选择卡号:"); while (true) { Scanner input = new Scanner(System.in); if (input.hasNextInt() == true) { int num = input.nextInt(); if (0 < num && num < 10) { mobileCard.setCardNumber(cardNumbers[num - 1]); break; } else { System.out.print("输入错误!请输入(1~9)的数字:"); continue; } } else { System.out.print("输入错误!请输入(1~9)的整数:"); continue; } } System.out.println("1.话唠套餐 2.网虫套餐 3.超人套餐 , 请选择套餐(输入序号):"); boolean bol = true; while (bol) { //core定义为String类型可以防止输入数字过大而导致的InputMismatchException异常 String core = input.next(); switch (core) { case "1": //从此段注释开始,Case1、Case2、Case3 是相同代码块,只有变量不同 //提取出去写成方法在Case直接调用,无法正确获取注册的手机号码 mobileCard.setSerPackage(Common.superPackage); System.out.println("请输入姓名:"); String userName = input.next(); mobileCard.setUserName(userName); System.out.println("请输入密码:"); String passWord = input.next(); mobileCard.setPassWord(passWord); System.out.println("请输入预存话费:"); int money = input.nextInt(); mobileCard.setMoney(money); System.out.println("注册成功!手机号:" + mobileCard.getCardNumber() + ",用户名:" + mobileCard.getUserName() + ",当前余额为:" + mobileCard.getMoney() + "元"); //到此段注释结束 //把注册的值给集合 MobileCard newmobileCard = new MobileCard(mobileCard.getCardNumber(), mobileCard.getUserName(), mobileCard.getPassWord(), Common.talkPackage); CardUtil.addCard(newmobileCard); bol = false; break; case "2": //从此段注释开始,Case1、Case2、Case3 是相同代码块,只有变量不同 //提取出去写成方法在Case直接调用,无法正确获取注册的手机号码 mobileCard.setSerPackage(Common.superPackage); System.out.println("请输入姓名:"); String userName2 = input.next(); mobileCard.setUserName(userName2); System.out.println("请输入密码:"); String passWord2 = input.next(); mobileCard.setPassWord(passWord2); System.out.println("请输入预存话费:"); int money2 = input.nextInt(); mobileCard.setMoney(money2); System.out.println("注册成功!手机号:" + mobileCard.getCardNumber() + ",用户名:" + mobileCard.getUserName() + ",当前余额为:" + mobileCard.getMoney() + "元"); //到此段注释结束 //把注册的值给集合 MobileCard newmobileCard2 = new MobileCard(mobileCard.getCardNumber(), mobileCard.getUserName(), mobileCard.getPassWord(), Common.netPackage); CardUtil.addCard(newmobileCard2); bol = false; break; case "3": //从此段注释开始,Case1、Case2、Case3 是相同代码块,只有变量不同 //提取出去写成方法在Case直接调用,无法正确获取注册的手机号码 mobileCard.setSerPackage(Common.superPackage); System.out.println("请输入姓名:"); String userName3 = input.next(); mobileCard.setUserName(userName3); System.out.println("请输入密码:"); String passWord3 = input.next(); mobileCard.setPassWord(passWord3); System.out.println("请输入预存话费:"); int money3 = input.nextInt(); mobileCard.setMoney(money3); System.out.println("注册成功!手机号:" + mobileCard.getCardNumber() + ",用户名:" + mobileCard.getUserName() + ",当前余额为:" + mobileCard.getMoney() + "元"); //到此段注释结束 //把注册的值给集合 MobileCard newmobileCard3 = new MobileCard(mobileCard.getCardNumber(), mobileCard.getUserName(), mobileCard.getPassWord(), Common.superPackage); CardUtil.addCard(newmobileCard3); bol = false; break; default: System.out.println("输入错误,请重新选择:"); bol = true; break; } } } public static void main(String[] args) throws IOException { //给MobMobileCard的带参方法传入相应的值 MobileCard mobileCard = new MobileCard("36949889123", "张余生", "123", Common.talkPackage, 0, 100, 0, 0, 0); MobileCard mobileCard2 = new MobileCard("12345678912", "飙车司机王", "123", Common.superPackage, 0, 100, 200, 100, 1); //把这些值传给CardUtil方法里会进入集合 CardUtil.addCard(mobileCard); CardUtil.addCard(mobileCard2); SosoMgr sosoMgr = new SosoMgr(); sosoMgr.mainMenu(); } }
//本地套餐家介绍文本如下,可以直接用system.out.println输出,也可以用File方法读取本地txt文件
******************************《套餐介绍》******************************
*********************** ********************** ***********************
** 套餐类型:话唠套餐 * * * * 套餐类型:超人套餐 **
** 通话时长:400分钟 * * 套餐类型:网虫套餐 * * 通话时长:200分钟 **
** 短信条数:200条 * * 上网流量:3GB * * 短信条数:100条 **
** 月租费:38元 * * 月资费:66元 * * 上网流量:1GB **
** * * * * 月资费:88元 **
*********************** ********************** ***********************
******************************《国庆钜惠》******************************