Java面向对象和高级特性 项目实战
Posted 小余上岸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java面向对象和高级特性 项目实战相关的知识,希望对你有一定的参考价值。
一、项目简介
-
项目名:嗖嗖移动业务大厅
- 技能点:
二、技能点
三、系统概述
四、整体开发思路
五、实体类和接口开发
六、 创建工具类
七、使用集合存储数据
八、开发计划
九、代码实现
1.项目目录
2.具体代码
- 测试类
package cn.soso.biz; import java.util.Scanner; import cn.soso.entity.MobileCard; import cn.soso.entity.ServicePackage; import cn.soso.utils.CardUtil; /** * 业务类 * * @author yu * */ public class SosoMgr { Scanner input = new Scanner(System.in); CardUtil utils = new CardUtil(); public static void main(String[] args) { SosoMgr soso = new SosoMgr(); soso.mainMenu(); System.out.println("谢谢使用!"); } /** * 主流程 */ public void mainMenu() { int menuChoose = 0; String mobileNumber= ""; String password = ""; utils.init(); utils.initScenes(); //Common.typesInit(); do { System.out.println("\\n*************欢迎使用嗖嗖移动业务大厅***************"); System.out.println("1.用户登录 2.用户注册 3.使用嗖嗖 4.话费充值 5.资费说明 6.退出系统"); System.out.print("请选择:"); menuChoose = input.nextInt(); // 分支语句:根据功能编号执行相应功能 switch (menuChoose) { case 1: //用户登录 System.out.print("请输入手机卡号:"); mobileNumber = input.next(); System.out.print("请输入密码:"); password = input.next(); if (utils.isExistCard(mobileNumber, password)) { cardMenu(mobileNumber); }else{ System.out.println("对不起,您输入的信息有误,无法登录!"); } continue; case 2: //用户注册 registCard(); continue; case 3: //使用嗖嗖 System.out.print("请输入手机卡号:"); mobileNumber = input.next(); if (utils.isExistCard(mobileNumber)) { try { /*System.out.println("****使用之前****"); cn.soso.utils.showRemainDetail(mobileNumber); cn.soso.utils.showAmountDetail(mobileNumber);*/ utils.userSoso(mobileNumber); } catch (Exception e) { System.err.println(e.getMessage()); } }else{ System.out.println("对不起,该卡号未注册,不能使用!"); } /*System.out.println("****使用之后****"); cn.soso.utils.showRemainDetail(mobileNumber); cn.soso.utils.showAmountDetail(mobileNumber);*/ continue; case 4: //话费充值 System.out.print("请输入充值卡号:"); mobileNumber = input.next(); if (utils.isExistCard(mobileNumber)) { System.out.print("请输入充值金额:"); double money = input.nextDouble(); utils.chargeMoney(mobileNumber, money); }else{ System.out.println("对不起,要充值的卡号未注册,无法充值!"); } continue; case 5: System.out.println("\\n*****资费说明******"); utils.showDescription(); continue; case 6: //退出系统 break; default: //选择其他数字退出系统 break; } break; } while (true); } /** * 手机卡功能菜单 * * @param number * @return */ public int cardMenu(String mobileNumber) { int menuChoose = 0; do { System.out.println("\\n*****嗖嗖移动用户菜单*****"); System.out.println("1.本月账单查询"); System.out.println("2.套餐余量查询"); System.out.println("3.打印消费详单"); System.out.println("4.套餐变更"); System.out.println("5.办理退网"); System.out.print("请选择(输入1~5选择功能,其他键返回上一级):"); menuChoose = input.nextInt(); switch (menuChoose) { case 1: System.out.println("\\n*****本月账单查询******"); utils.showAmountDetail(mobileNumber); continue; case 2: System.out.println("\\n*****套餐余量查询******"); utils.showRemainDetail(mobileNumber); continue; case 3: System.out.println("\\n*****消费详单查询******"); utils.printConsumInfo(mobileNumber); continue; case 4: System.out.println("\\n*****套餐变更******"); System.out.print("1.话唠套餐 2.网虫套餐 3.超人套餐 请选择(序号):"); utils.changingPack(mobileNumber, input.next()); continue; case 5: System.out.println("\\n*****办理退网******"); utils.delCard(mobileNumber); System.out.println("谢谢使用!"); System.exit(1); //办理退网后退出系统 } break; } while (true); return menuChoose; } /** * 注册新卡流程 */ public void registCard(){ String[] newNumbers = utils.getNewNumbers(9); //显示可供选择的手机号列表 System.out.println("*****可选择的卡号*****"); for(int i=0;i<9;i++){ System.out.print((i+1)+"."+newNumbers[i]+"\\t\\t"); if((i+1)%3==0){ System.out.println(); } } //选择手机号 System.out.print("请选择卡号(输入1~9的序号):"); String number = newNumbers[input.nextInt()-1]; //选择套餐类型 System.out.print("1.话唠套餐 2.网虫套餐 3.超人套餐, "); System.out.print("请选择套餐(输入序号):"); //cn.soso.utils.getPackList(); //获取套餐对象 ServicePackage pack = utils.createPack(input.nextInt()); //输入用户名 System.out.print("请输入姓名:"); String name = input.next(); //输入密码 System.out.print("请输入密码:"); String password = input.next(); //输入预存话费金额 double money = 0; System.out.print("请输入预存话费金额:"); money = input.nextDouble(); while(money<pack.getPrice()){ System.out.print("您预存的话费金额不足以支付本月固定套餐资费,请重新充值:"); money = input.nextDouble(); } //创建新卡对象并添加 MobileCard newCard = new MobileCard(name,password,number,pack,pack.getPrice(),money-pack.getPrice()); utils.addCard(newCard); } }
- 公共类
package cn.soso.common; import java.text.DecimalFormat; import java.util.HashMap; import java.util.Map; /** * 公共类 * @author yu * */ public class Common { /** * double类型格式化 * @param data * @return */ public static String dataFormat(double data) { DecimalFormat formatData = new DecimalFormat("#.0"); return formatData.format(data); } /** * double类型两数相减 * @param num1 * @param num2 * @return */ public static double sub(double num1,double num2){ return (num1*10-num2*10)/10; } }
package cn.soso.common; /** * 消费类型 * @author rong.zhou * */ public enum ConsumType { TALK,SMS,NETWORK }
- 实体类
package cn.soso.entity; import cn.soso.common.ConsumType; /** * 消费信息 * @author yu * */ public class ConsumInfo { private String cardNumber; //卡号 private String type; //消费类型:通话、发短信、上网 private int consumData; //消费数据 通话:分钟 发短信:条 上网:MB public ConsumInfo(){} public ConsumInfo(String cardNumber, String type, int consumData) { super(); this.cardNumber = cardNumber; this.type = type; this.consumData = consumData; } public String getCardNumber() { return cardNumber; } public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; } 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; } }
package cn.soso.entity; /** * 手机卡 * @author yu * */ 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 userName, String passWord, String cardNumber, ServicePackage serPackage, double consumAmount, double money) { super(); this.userName = userName; this.passWord = passWord; this.cardNumber = cardNumber; this.serPackage = serPackage; this.consumAmount = consumAmount; this.money = money; } 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 String getCardNumber() { return cardNumber; } public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; } 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(){ System.out.println("卡号:"+this.cardNumber+" 用户名:"+this.userName+" 当前余额:"+this.money+"元。"); this.serPackage.showInfo(); } }
package cn.soso.entity; import cn.soso.common.Common; import cn.soso.service.NetService; /** * 网虫套餐 * * @author yu * */ public class NetPackage extends ServicePackage implements NetService { private int flow; // 上网流量(MB) public NetPackage() { //套餐数据初始化 this.flow = 1024 * 3; this.price = 68.0; } public NetPackage(int flow) { super(); this.flow = flow; } public int getFlow() { return flow; } public void setFlow(int flow) { this.flow = flow; } @Override public void showInfo() { System.out.println("网虫套餐:上网流量是" + flow / 1024 + "GB/月,月资费是" + this.price + "元/月。"); } /** * 提供上网服务 */ public void netPlay2(int flow, MobileCard card) throws Exception { int reminFlow = this.flow - card.getRealFlow(); //卡中可支付的免费流量 // 判断套餐中的上网流量是否足够支付本次上网服务 if (this.flow <= reminFlow) { // 套餐中上网流量足够:修改该卡实际上网流量数据 card.setRealFlow(card.getRealFlow() + flow); } else { // 套餐中上网流量不够:额外消费需按0.1元/条付费,额外消费金额=0.1*(该卡实际消费上网流量+本次消费上网流量-套餐包含的上网流量) double consumeMoney = 0.1 * (flow-reminFlow); // 该卡账户余额足够支付:修改该卡实际使用的上网流量、账户余额、当月消费金额 if (card.getMoney() >= consumeMoney) { //消耗的流量增加 card.setRealFlow(card.getRealFlow() + flow); // 当前账户余额=当前账户余额-额外消费金额 card.setMoney(card.getMoney() - consumeMoney); // 当月消费金额=当月消费金额+额外消费金额 card.setConsumAmount(card.getConsumAmount() + consumeMoney); } else { int temp = (int)(card.getMoney()/0.1); //当前余额够大 throw new Exception("您的余额不足,请充值后再使用!"); } } } /** * 提供上网服务 */ 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){ //第一种情况:套餐剩余流量可以支持使用1M流量 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB }else if(card.getMoney()>=0.1){ //第二种情况:套餐流量已用完,账户余额可以支付1M流量,使用账户余额支付 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1M流量费用) card.setConsumAmount(card.getConsumAmount() + 0.1); }else{ temp = i; throw new Exception("本次已使用流量"+i+"MB,您的余额不足,请充值后再使用!"); } } return temp; } }
package cn.soso.entity; /** * 使用场景 * @author yu * */ public class Scene { private String type; //场景消费类型 private int data; //消费数据 private String description;//场景描述 public Scene(){} 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 cn.soso.entity; /** * 嗖嗖移动卡套餐 * @author yu * */ public abstract class ServicePackage { protected double price; //套餐月资费(元) public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } //显示套餐数据 public abstract void showInfo(); }
package cn.soso.entity; import cn.soso.common.Common; import cn.soso.service.CallService; import cn.soso.service.NetService; import cn.soso.service.SendService; /** * 超人套餐 * @author yu * */ public class SuperPackage extends ServicePackage implements CallService, SendService,NetService { private int talkTime; //通话时长(分钟) private int smsCount; //短信条数(条) private int flow; //上网流量(MB) 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*1024; this.price = 78.0; } @Override public void showInfo() { System.out.println("超人套餐:通话时长为"+this.talkTime+"分钟/月,短信条数为"+this.smsCount+"条/月,上网流量为"+this.flow/1024+"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){ //第一种情况:套餐剩余流量可以支持使用1M流量 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB }else if(card.getMoney()>=0.1){ //第二种情况:套餐流量已用完,账户余额可以支付1M流量,使用账户余额支付 card.setRealFlow(card.getRealFlow()+1); //实际使用流量加1MB card.setMoney(Common.sub(card.getMoney(),0.1)); //账户余额消费0.1元(1M流量费用) card.setConsumAmount(card.getConsumAmount() + 0.1); }else{ temp = i; throw new Exception("本次已使用流量"+i+"MB,您的余额不足,请充值后再使用!"); } } return temp; } /** * 提供通话服务 */ 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); //实际通话数据加1 }else if(card.getMoney()>=0.2){ //第二种情况:套餐通话时长已用完,账户余额可以支付1分钟通话,使用账户余额支付 card.setRealTalkTime(card.getRealTalkTime()+1); //实际使用通话时长1分钟 card.setMoney(Common.sub(card.getMoney(),0.2)); //账户余额消费0.2元(1分钟额外通话) card.setConsumAmount(card.getConsumAmount() + 0.2); }else{ temp = i; //记录实现通话分钟数 throw new Exception("本次已通话"+i+"分钟,您的余额不足,请充值后再使用!"); } } return temp; } /** * 提供短信服务 */ 大数据必学Java基础(四十六):内部类和面向对象项目实战