java基础综合练习(嗖嗖移动)
Posted 呛水de鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java基础综合练习(嗖嗖移动)相关的知识,希望对你有一定的参考价值。
功能列表:
服务器端代码:
1 package cn.luoxue.server; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6 import java.net.InetAddress; 7 8 public class UDPServer { 9 public static void main(String[] args) throws IOException { 10 DatagramSocket socket = new DatagramSocket(8800); 11 DatagramPacket packet = null; 12 byte[] data = null; 13 int count = 0; 14 System.out.println("***客服静候您的咨询***"); 15 while(true){ 16 data = new byte[1024];//创建字节数组,指定接收的数据包的大小 17 packet = new DatagramPacket(data, data.length); 18 socket.receive(packet);//此方法在接收到数据报之前会一直阻塞 19 Thread thread = new Thread(new UDPThread(socket, packet)); 20 thread.start(); 21 count++; 22 System.out.println("服务器端被连接过的次数:"+count); 23 InetAddress address = packet.getAddress(); 24 System.out.println("当前客户端的IP为:"+address.getHostAddress()); 25 26 } 27 28 } 29 }
多线程代码:
package cn.luoxue.server; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPThread implements Runnable{ DatagramSocket socket = null; DatagramPacket packet = null; public UDPThread(DatagramSocket socket,DatagramPacket packet) { this.socket = socket; this.packet = packet; } @Override public void run() { String info = null; InetAddress address = null; int port = 8800; byte[] data2 = null; DatagramPacket packet2 = null; try { info = new String(packet.getData(), 0, packet.getLength()); System.out.println("我是服务器,客户端说:"+info); address = packet.getAddress(); port = packet.getPort(); if(info.equals("886")){ data2 = "好的,回聊".getBytes(); }else if(info.equals("美女你好问个问题")){ data2 = "先生,你想问什么呢".getBytes(); }else if(info.equals("你认为明天是什么天气")){ data2 = "那还用说,当然是晴天!".getBytes(); }else if(info.equals("适合爬山吗")){ data2 = "毫无争议绝对适合".getBytes(); }else if(info.equals("最高的山峰是哪个")){ data2 = "珠穆朗玛峰!".getBytes(); }else{ data2 = "你说啥?风大听不到!".getBytes(); } packet2 = new DatagramPacket(data2, data2.length, address, port); socket.send(packet2); } catch (IOException e) { e.printStackTrace(); } //socket.close();不能关闭 } }
实体类代码:
package cn.luoxue.entityclass; import cn.luoxue.interfaces.NetService; import cn.luoxue.search_The_mobile_lobby.MobileCard; /** * 网虫套餐类 * @author 三文鱼的回流 * */ public class NetPackage extends ServicePackage implements NetService { private int flow; //上网流量 public NetPackage() { super(); // TODO Auto-generated constructor stub } public NetPackage(double price, int flow) { super(price); this.flow = flow; } public int getFlow() { return flow; } public void setFlow(int flow) { this.flow = flow; } @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){ //第一种情况:流量剩余够 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,您的余额不足,请充值后再使用!"); //System.err.println("流量已经使用" + i + "MB,您的余额不足,请充值后再使用!"); } } return temp; } @Override public void showInfo() { System.out.println("网虫套餐:上网流量为:" + (this.flow * 1.0 / 1024) + "GB/月," + "资费为:" + super.getPrice() + "元/月"); } }
package cn.luoxue.entityclass; import cn.luoxue.interfaces.CallService; import cn.luoxue.interfaces.NetService; import cn.luoxue.interfaces.SendService; import cn.luoxue.search_The_mobile_lobby.MobileCard; /** * 超人套餐类 * @author 三文鱼的回流 * */ public class SuperPackage extends ServicePackage implements SendService, NetService, CallService { private int talkTime; //通话时长 private int smsCount; //短信条数 private int flow; //上网流量 public SuperPackage() { super(); // TODO Auto-generated constructor stub } 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; } @Override //返回通话时长 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 + "分钟,您的余额不足,请充值后再使用!"); //System.err.println("本次已经通话" + 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){ //第一种情况:流量剩余够 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; } @Override //返回实际发送短信条数 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; } @Override public void showInfo() { System.out.println("超人套餐:通话时长为:" + this.talkTime + "分钟/月," + "短信条数为:" + this.smsCount + "条/每月," + "上网流量为:" + (this.flow * 1.0 / 1024) + "GB/月," + "资费为:" + super.getPrice() + "元/月" ); } }
package cn.luoxue.entityclass; import cn.luoxue.interfaces.CallService; import cn.luoxue.interfaces.SendService; import cn.luoxue.search_The_mobile_lobby.MobileCard; /** * 话唠套餐类 * @author 三文鱼的回流 * */ 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; } @Override //返回通话时长 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; } @Override //返回实际发送短信条数 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 + "条,您的余额不足,请充值后再使用!"); //System.err.println("短信已经发送" + i + "条,您的余额不足,请充值后再使用!"); } } return temp; } @Override public void showInfo() { System.out.println("话唠套餐:通话时长为:" + this.talkTime + "分钟/月,短信条数为:" + this.smsCount + "条/月,资费为:" + super.getPrice() + "元/月" ); } }
package cn.luoxue.entityclass; /** * 资费套餐类 ,是话唠、网虫、超人套餐的父类 * @author 三文鱼的回流 * */ public abstract class ServicePackage { private double price; //月资费 public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public ServicePackage() { super(); // TODO Auto-generated constructor stub } public ServicePackage(double price) { super(); this.price = price; } public abstract void showInfo(); }
接口代码:
package cn.luoxue.interfaces; import cn.luoxue.search_The_mobile_lobby.MobileCard; /** * 接口 上网服务 * @author 三文鱼的回流 * */ public interface NetService { public abstract int netPlay(int flow, MobileCard card) throws Exception; }
package cn.luoxue.interfaces; import cn.luoxue.search_The_mobile_lobby.MobileCard; /** *接口 通话服务 * @author 三文鱼的回流 * */ public interface CallService { public abstract int call (int minCount, MobileCard card) throws Exception; }
package cn.luoxue.interfaces; import cn.luoxue.search_The_mobile_lobby.MobileCard; /** * 接口 短信服务 * @author 三文鱼的回流 * */ public interface SendService { public abstract int send(int count, MobileCard card) throws Exception; }
工具类:
package cn.luoxue.search_The_mobile_lobby; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; 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 cn.luoxue.entityclass.NetPackage; import cn.luoxue.entityclass.ServicePackage; import cn.luoxue.entityclass.SuperPackage; import cn.luoxue.entityclass.TalkPackage; import cn.luoxue.interfaces.CallService; import cn.luoxue.interfaces.NetService; import cn.luoxue.interfaces.SendService; /** * 工具类 * @author Administrator * */ public class CardUtil { //Common common = new Common(); protected static Map<String, MobileCard> cards = new HashMap<String, MobileCard>(); //已注册嗖嗖移动用户列表 protected static Map<String, List<ConsumInfo>> consumInfos = new HashMap<String, List<ConsumInfo>>(); //所有卡号的消费记录列表 protected static Map<Integer, Scene> scenes = new HashMap<Integer, Scene>();//使用场景列表 protected static Scene scene0 = new Scene("通话", 90, "问候客户,谁知其如此难缠,通话90分钟"); protected static Scene scene1 = new Scene("通话", 30, "询问妈妈身体状况,本地通话30分钟"); protected static Scene scene2 = new Scene("短信", 5, "参与环境保护实施方案问卷调查,发送短信5条"); protected static Scene scene3 = new Scene("短信", 50, "通知朋友手机换号,发送短信50条"); protected static Scene scene4 = new Scene("上网", 1024, "和女朋友用微信视频聊天,使用流量1GB"); protected static Scene scene5 = new Scene("上网", 2 * 1024, "晚上手机在线看韩剧,不留神睡着啦!使用2GB"); protected static Scanner input = new Scanner(System.in); public CardUtil() { super(); // TODO Auto-generated constructor stub } /** *注册新卡 * @param card */ public static void addCard(MobileCard card){ cards.put(card.getCardNumber(), card); } /** *话费充值 * @param number */ 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("充值成功,当前话费余额为" + dataFormat(cards.get(number).getMoney())); break; } } } /** *使用嗖嗖 * @param number */ public static 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); Scene scene = scenes.get(ranNum); //获取该序号所有对应的场景 switch(ranNum){ case 0: case 1: //序号为0或1的通话场景 //获取该卡所属套餐是否支持通话功能 if(pack instanceof CallService){ //执行通话方法 System.out.println(scene.getDescription()); CallService callService = (CallService) pack; try { temp = callService.call(scene.getData(), card); } catch (Exception e) { e.printStackTrace(); } //添加一条消费记录 addConsumInfo(number, new ConsumInfo(number, scene.getType(), temp)); break; }else{ //如果该卡套餐不支持通话功能,则重新生成随机数选择其他场景 continue; } case 2: case 3: //序号2或3发短信场景 //获取该卡所属套餐是否支持短信 if(pack instanceof SendService){ 以上是关于java基础综合练习(嗖嗖移动)的主要内容,如果未能解决你的问题,请参考以下文章