package com.soso.enty;
public class Account {
private String type;//账户类型
private String name;
private String telNo;
private int money;//账户余额
private int pwd;
private taocan taocan;//套餐
private int sumoney;//充值金额
public int getSumoney() {
return sumoney;
}
public void setSumoney(int sumoney) {
this.sumoney = sumoney;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelNo() {
return telNo;
}
public void setTelNo(String telNo) {
this.telNo = telNo;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getPwd() {
return pwd;
}
public void setPwd(int pwd) {
this.pwd = pwd;
}
public taocan getTaocan() {
return taocan;
}
public void setTaocan(taocan taocan) {
this.taocan = taocan;
}
public Account(String type, String name, String telNo, int money, int pwd,
com.soso.enty.taocan taocan) {
super();
this.type = type;
this.name = name;
this.telNo = telNo;
this.money = money;
this.pwd = pwd;
this.taocan = taocan;
}
public Account() {
super();
}
}
package com.soso.enty;
public abstract class taocan {
private int speakTime;//通话时间
private int speakTimeLeft;//剩余通话时间
private int text;//短信条数
private int textLeft;//剩余短信条数
private int flow;//流量
private int flowLeft;//剩余流量
private int price;//套餐价格
private String recordes;//消费记录
public String getRecordes() {
return recordes;
}
public void setRecordes(String recordes) {
this.recordes = recordes;
}
public int getSpeakTime() {
return speakTime;
}
public void setSpeakTime(int speakTime) {
this.speakTime = speakTime;
}
public int getSpeakTimeLeft() {
return speakTimeLeft;
}
public void setSpeakTimeLeft(int speakTimeLeft) {
this.speakTimeLeft = speakTimeLeft;
}
public int getText() {
return text;
}
public void setText(int text) {
this.text = text;
}
public int getTextLeft() {
return textLeft;
}
public void setTextLeft(int textLeft) {
this.textLeft = textLeft;
}
public int getFlow() {
return flow;
}
public void setFlow(int flow) {
this.flow = flow;
}
public int getFlowLeft() {
return flowLeft;
}
public void setFlowLeft(int flowLeft) {
this.flowLeft = flowLeft;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public taocan(int speakTime, int text,
int flow, int price) {
super();
this.speakTime = speakTime;
this.text = text;
this.flow = flow;
this.price = price;
}
public taocan() {
super();
}
public abstract void printof();
//消费
public void consume(String TelNo) {
}
}
话痨套餐:
package com.soso.enty;
import com.soso.util.Util;
public class HuaLao extends taocan {
public HuaLao(){
super.setPrice(58);
super.setSpeakTime(200);
super.setText(50);
super.setFlow(0);
}
@Override
public void printof() {
// TODO Auto-generated method stub
System.out.println("话唠套餐: \r\n" +
"通话时长:200分钟 \r\n" +
"短信条数:50条\r\n" +
"月资费:58元");
}
//消费方法
public void consume(String telNo) {
int choice=(int)(Math.random()*2)+1;
switch (choice) {
case 1:
call(telNo);
break;
case 2:
text(telNo);
break;
default:
break;
}
}
//打电话
public void call(String telNo) {
int money=0;//本次消费金额
//消费记录
System.out.println("通知朋友手机换号,打电话50分钟");
if(this.getRecordes()!=null) {
this.setRecordes(this.getRecordes()+" "+"通知朋友手机换号,打电话50分钟");
}else {
this.setRecordes("通知朋友手机换号,打电话50分钟");
}
//剩余通话时间
if(this.getSpeakTime()-50>=0) {
this.setSpeakTime(this.getSpeakTime()-50);
}else {
money=(50-this.getSpeakTime())*2;
this.setSpeakTime(0);
for (int j = 0; j < Util.list.size(); j++) {
if(Util.list.get(j).getTelNo().equals(telNo)) {
Util.list.get(j).setMoney(Util.list.get(j).getMoney()-money);
if(Util.list.get(j).getMoney()<0) {
System.out.println("余额不足,请及时缴费");
}
break;
}
}
}
//计费
}
//发短信
public void text(String telNo) {
int money=0;//本次消费金额
//消费记录
System.out.println("通知朋友手机换号,发了50条短信");
if(this.getRecordes()!=null) {
this.setRecordes(this.getRecordes()+" "+"通知朋友手机换号,发了50条短信");
}else {
this.setRecordes("通知朋友手机换号,发了50条短信");
}
//剩余通话时间
if(this.getTextLeft()-50>=0) {
this.setTextLeft(this.getTextLeft()-50);
}else {
money=(50-this.getTextLeft())*1;
this.setTextLeft(0);
for (int j = 0; j < Util.list.size(); j++) {
if(Util.list.get(j).getTelNo().equals(telNo)) {
Util.list.get(j).setMoney(Util.list.get(j).getMoney()-money);
if(Util.list.get(j).getMoney()<0) {
System.out.println("余额不足,请及时缴费");
}
break;
}
}
}
//计费
}
}
网虫套餐:
package com.soso.enty;
import com.soso.util.Util;
public class WangChong extends taocan {
public WangChong(){
super.setPrice(68);
super.setFlow(5000);
super.setSpeakTime(0);
super.setText(0);
super.setFlowLeft(super.getFlow());
}
@Override
public void printof() {
// TODO Auto-generated method stub
System.out.println("网虫套餐: \r\n" +
"上网流量:5GB\r\n" +
"月资费:68元");
}
public void consume(String telNo) {
useFlow(telNo);
}
//使用流量
public void useFlow(String telNo) {
int money=0;//本次消费金额
//消费记录
System.out.println("通知朋友手机换号,用了300M流量");
if(this.getRecordes()!=null) {
this.setRecordes(this.getRecordes()+" "+"通知朋友手机换号,用了300M流量");
}else {
this.setRecordes("通知朋友手机换号,用了300M流量");
}
//剩余通话时间
if(this.getFlowLeft()-300>=0) {
this.setFlowLeft(this.getFlowLeft()-300);
}else {
money=(300-this.getTextLeft())*2;
this.setFlowLeft(0);
for (int j = 0; j < Util.list.size(); j++) {
if(Util.list.get(j).getTelNo().equals(telNo)) {
Util.list.get(j).setMoney(Util.list.get(j).getMoney()-money);
if(Util.list.get(j).getMoney()<0) {
System.out.println("余额不足,请及时缴费");
}
break;
}
}
}
//计费
}
}
超人套餐:
package com.soso.enty;
import com.soso.util.Util;
public class SuperTaoCan extends taocan {
public SuperTaoCan(){
super.setPrice(78);
super.setFlow(1000);
super.setSpeakTime(200);
super.setText(100);
}
@Override
public void printof() {
// TODO Auto-generated method stub
System.out.println("超人套餐\r\n" +
"通话时长:200分钟 \r\n" +
"短信条数:100条\r\n" +
"上网流量:1GB\r\n" +
"月资费:78元");
}
//消费方法
public void consume(String telNo) {
int choice=(int)(Math.random()*3)+1;
switch (choice) {
case 1:
call(telNo);
break;
case 2:
text(telNo);
break;
case 3:
useFlow(telNo);
break;
default:
break;
}
}
//打电话
public void call(String telNo) {
int money=0;//本次消费金额
//消费记录
System.out.println("通知朋友手机换号,打电话50分钟");
if(this.getRecordes()!=null) {
this.setRecordes(this.getRecordes()+" "+"通知朋友手机换号,打电话50分钟");
}else {
this.setRecordes("通知朋友手机换号,打电话50分钟");
}
//剩余通话时间
if(this.getSpeakTime()-50>=0) {
this.setSpeakTime(this.getSpeakTime()-50);
}else {
money=(50-this.getSpeakTime())*2;
this.setSpeakTime(0);
for (int j = 0; j < Util.list.size(); j++) {
if(Util.list.get(j).getTelNo().equals(telNo)) {
Util.list.get(j).setMoney(Util.list.get(j).getMoney()-money);
if(Util.list.get(j).getMoney()<0) {
System.out.println("余额不足,请及时缴费");
}
break;
}
}
}
//计费
}
//发短信
public void text(String telNo) {
int money=0;//本次消费金额
//消费记录
System.out.println("通知朋友手机换号,发了50条短信");
if(this.getRecordes()!=null) {
this.setRecordes(this.getRecordes()+" "+"通知朋友手机换号,发了50条短信");
}else {
this.setRecordes("通知朋友手机换号,发了50条短信");
}
//剩余通话时间
if(this.getTextLeft()-50>=0) {
this.setTextLeft(this.getTextLeft()-50);
}else {
money=(50-this.getTextLeft())*1;
this.setTextLeft(0);
for (int j = 0; j < Util.list.size(); j++) {
if(Util.list.get(j).getTelNo().equals(telNo)) {
Util.list.get(j).setMoney(Util.list.get(j).getMoney()-money);
if(Util.list.get(j).getMoney()<0) {
System.out.println("余额不足,请及时缴费");
}
break;
}
}
}
//计费
}
//使用流量
public void useFlow(String telNo) {
int money=0;//本次消费金额
//消费记录
System.out.println("通知朋友手机换号,用了300M流量");
if(this.getRecordes()!=null) {
this.setRecordes(this.getRecordes()+" "+"通知朋友手机换号,用了300M流量");
}else {
this.setRecordes("通知朋友手机换号,用了300M流量");
}
//剩余通话时间
if(this.getFlowLeft()-300>=0) {
this.setFlowLeft(this.getFlowLeft()-300);
}else {
money=(300-this.getTextLeft())*2;
this.setFlowLeft(0);
for (int j = 0; j < Util.list.size(); j++) {
if(Util.list.get(j).getTelNo().equals(telNo)) {
Util.list.get(j).setMoney(Util.list.get(j).getMoney()-money);
if(Util.list.get(j).getMoney()<0) {
System.out.println("余额不足,请及时缴费");
}
break;
}
}
}
//计费
}
}
方法:
package com.soso.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import com.soso.enty.Account;
import com.soso.enty.HuaLao;
import com.soso.enty.SuperTaoCan;
import com.soso.enty.WangChong;
import com.soso.enty.taocan;
public class Util {
public static List<Account> list = new ArrayList<Account>();
static taocan tc;
static Scanner input = new Scanner(System.in);
public Boolean isright(String str) {
String reg = "^\\d?";
if (str.matches(reg)) {
return true;
}
return false;
}
// 初始化list
public void init() { // 新建一个账户,放到集合中
Account ac = new Account();
ac.setTelNo("18790275702");
ac.setType("网虫套餐");
ac.setMoney(25);
ac.setName("ljc");
ac.setPwd(123);
ac.setSumoney(100);
ac.setTaocan(new WangChong());
list.add(ac);
}
public void regist() {// 注册
System.out.println("*******可选择的卡号*******");
Map map = new HashMap();
Account ac = new Account();
for (int i = 0; i < 9; i++) {
String card = "139";
for (int j = 0; j < 8; j++) {
int a = (int) (Math.random() * 10);
card = card + a;
}
map.put(i + 1, card);
System.out.print(i + 1 + "." + card + "\t");
if ((i + 1) % 3 == 0) {
System.out.println();
}
}
System.out.println("请选择卡号:");
int kh = input.nextInt();
ac.setTelNo((String) map.get(kh));
System.out.println("你选择的卡号是:" + ac.getTelNo());
System.out.println("1.话唠套餐 2.网虫套餐 3.超人套餐\n 请选择套餐(输入序号):");
int num = input.nextInt();
switch (num) {
case 1:
ac.setType("话唠套餐 ");
tc = new HuaLao();
tc.setSpeakTimeLeft(tc.getSpeakTime());
tc.setTextLeft(tc.getText());
ac.setTaocan(tc);
// String hl="话唠套餐:通话时长:200分钟 ,短信条数:50条,月资费:58元";
ac.getTaocan().printof();
break;
case 2:// 网虫套餐
// String wc="网虫套餐:上网流量:5GB,月资费:68元";
ac.setType("网虫套餐");
tc = new WangChong();
tc.setFlowLeft(tc.getFlow());
ac.setTaocan(tc);
ac.getTaocan().printof();
break;
case 3:// 超人套餐
ac.setType("超人套餐");
tc = new SuperTaoCan();
tc.setSpeakTimeLeft(tc.getSpeakTime());
tc.setTextLeft(tc.getText());
tc.setFlowLeft(tc.getFlow());
ac.setTaocan(tc);
ac.getTaocan().printof();
}
System.out.println("请输入姓名");
String name = input.next();
ac.setName(name);
System.out.println("请输入密码");
int pwd = input.nextInt();
System.out.println("请输入预存话费金额:");
int ycmoney = input.nextInt();
ac.setSumoney(ac.getSumoney()+ycmoney);
int money = ycmoney - tc.getPrice();
ac.setMoney(money);
ac.setPwd(pwd);
System.out.print("注册成功!");
System.out.print("卡号:" + ac.getTelNo() + "用户名:" + ac.getName()
+ "当前余额:" + ac.getMoney() + "元\n");
list.add(ac);
// System.out.println(list.size());
}
public boolean Login() throws Exception {
System.out.println("请输入手机卡号:");
String cardNo = input.next();
System.out.println("请输入密码:");
int strpwd = input.nextInt();
for (int i = 0; i < list.size(); i++) {
if (cardNo.equals(list.get(i).getTelNo())
&& strpwd == (list.get(i).getPwd())) {
meal(cardNo);
return true;
}
}
return false;
}
public void charge() { // 充值
System.out.println("请输入电话卡号");
String telno = input.next();
System.out.println("请输入充值金额");
int money = input.nextInt();
boolean flag = false;
for (int i = 0; i < list.size(); i++) {
if (telno.equals(list.get(i).getTelNo())) {
list.get(i).setMoney(list.get(i).getMoney()+money);
flag = true;
System.out.println("充值成功");
break;
}
}
if (!flag) {
System.out.println("手机号不存在,充值失败。");
}
}
public void meal(String telNo) throws Exception { // 嗖嗖移动用户菜单
System.out.println("1.本月账单查询2.套餐余量查询3.打印消费详单 4.套餐变更5.办理退网");
System.out.println("请选择(输入1~5选择功能,其他键返回上一级):");
Scanner sca = new Scanner(System.in);
int num = sca.nextInt();
String tn = telNo;// 登录的电话号
switch (num) {
case 1:// 本月账单查询
System.out.println("*****本月账单查询******");
for (int i = 0; i < list.size(); i++) {
if ((list.get(i).getTelNo()).equals(tn)) {
System.out.println("您的卡号:" + Util.list.get(i).getTelNo()
+ ",当月账单:");
System.out.println("套餐资费:" + Util.list.get(i).getTaocan().getPrice()
+ "元");
System.out.println("合计消费:"
+ (Util.list.get(i).getSumoney() - Util.list.get(i).getMoney()) + "元");
System.out.println("账户余额:" + Util.list.get(i).getMoney()
+ "元");
break;
}
}
meal(tn);
break;
case 2:// 套餐余量查询
System.out.println("*****套餐余量查询******");
for (int i = 0; i < list.size(); i++) {
if (tn.equals(list.get(i).getTelNo())) {
System.out.println("您的卡号是" + list.get(i).getTelNo());
if(list.get(i).getTaocan().getPrice()==58){
System.out.println("套餐内剩余:\n免费通话时长剩余:"
+ list.get(i).getTaocan().getSpeakTime()
+ "分钟\n免费短信条数剩余:"
+ list.get(i).getTaocan().getTextLeft()
+ "条");
break;
}
if(list.get(i).getTaocan().getPrice()==68){
System.out.println("套餐内剩余:\n套餐流量剩余:"
+ list.get(i).getTaocan().getFlowLeft() + "M");
break;
}
if(list.get(i).getTaocan().getPrice()==78){
System.out.println("套餐内剩余:\n免费通话时长剩余:"
+ list.get(i).getTaocan().getSpeakTime()
+ "分钟\n免费短信条数剩余:"
+ list.get(i).getTaocan().getTextLeft()
+ "条\n套餐流量剩余:"
+ list.get(i).getTaocan().getFlowLeft() + "M");
break;
}
break;
}
}
meal(tn);
break;
case 3:// 打印消费详单
String tr = null;
for (int i = 0; i < list.size(); i++) {
if (telNo.equals(list.get(i).getTelNo())) {
tr = list.get(i).getTaocan().getRecordes();
}
}
if(tr!=null){
System.out.println(tr);
String[] str = tr.split(" ");
File file = new File("D:\\d\\record.txt");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < str.length; i++) {
bw.write(str[i]);
bw.newLine();
}
bw.close();
System.out.println("套餐消费记录打印完毕!");
}else{
System.out.println("占时没有套餐消费记录");
}
meal(tn);
break;
case 4:// 套餐变更
changtc(tn);
break;
case 5:// 办理退网
for (int i = 0; i < list.size(); i++) {
if (tn.equals(list.get(i).getTelNo())) {
list.remove(i);
}
}
System.out.println("*****办理退网******\n卡号" +tn+" 办理退网成功!");
break;
default:
break;
}
}
public void changtc(String telNo) throws Exception{
String str=telNo;
System.out.println("********套餐变更******");
System.out.println("1.话唠套餐 2.网虫套餐 3.超人套餐\n 请选择套餐(输入序号):");
int num = input.nextInt();
for (int i = 0; i < list.size(); i++) {
if (telNo.equals(list.get(i).getTelNo())) {
System.out.println("当前套餐为:"+list.get(i).getType());
switch (num) {
case 1:
if(list.get(i).getType().equals("话唠套餐")||list.get(i).getMoney()-58<0){
System.out.println("无法选择");
break;
}else{
list.get(i).setType("话唠套餐 ");
tc = new HuaLao();
tc.setSpeakTimeLeft(tc.getSpeakTime());
tc.setTextLeft(tc.getText());
list.get(i).setTaocan(tc);
list.get(i).setMoney(list.get(i).getMoney()-tc.getPrice() );
list.get(i).getTaocan().printof();
System.out.println("变更套餐成功");
meal(str);
}
break;
case 2:// 网虫套餐
if(list.get(i).getType().equals("网虫套餐")||list.get(i).getMoney()-68<0){
System.out.println("无法选择");
}else{
list.get(i).setType("网虫套餐");
tc = new WangChong();
tc.setFlowLeft(tc.getFlow());
list.get(i).setTaocan(tc);
list.get(i).setMoney(list.get(i).getMoney()-tc.getPrice() );
list.get(i).getTaocan().printof();
System.out.println("变更套餐成功");
meal(str);
}
break;
case 3:// 超人套餐
if(list.get(i).getType().equals("超人套餐")||list.get(i).getMoney()-78<0){
System.out.println("无法选择");
}else{
list.get(i).setType("超人套餐");
tc = new SuperTaoCan();
tc.setSpeakTimeLeft(tc.getSpeakTime());
tc.setTextLeft(tc.getText());
tc.setFlowLeft(tc.getFlow());
list.get(i).setTaocan(tc);
list.get(i).setMoney(list.get(i).getMoney()-tc.getPrice() );
list.get(i).getTaocan().printof();
System.out.println("变更套餐成功");
meal(str);
}
}
break;
}
}
}
}
测试类:
package com.soso.enty;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.Scanner;
import com.soso.util.Util;
public class test {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
test t = new test();
Util u=new Util();
u.init();
t.begin();
}
public void begin() throws Exception {
Util util = new Util();
String a = "";
do {
System.out.println("*************欢迎使用嗖嗖移动业务大厅***************");
System.out
.println("1.用户登录 2.用户注册 3.使用嗖嗖 4.话费充值 5.资费说明 6.退出系统");
System.out.println("请选择:");
String num = input.next();
while (util.isright(num) == false) {
System.out.println("请选择:");
num = input.next();
}
int chose = Integer.parseInt(num);
switch (chose) {
case 1:// 用户登录
boolean flag = false;
for (int i = 0; i < 3; i++) {
flag = util.Login();
if (flag == true) {
break;
}
System.out.println("登录失败还剩" + (2 - i) + "次机会");
}
if (flag == true) {
begin();
}
if (flag == false) {
System.out.println("登录次数已用完");
}
break;
case 2:// 用户注册
util.regist();
break;
case 3: // 使用嗖嗖
System.out.println("输入手机号");
String telNo = input.next();
for (int i = 0; i < Util.list.size(); i++) {
if (telNo.equals(Util.list.get(i).getTelNo())) {
Util.list.get(i).getTaocan().consume(telNo);
}
}
break;
case 4:// 话费充值
util.charge();
break;
case 5:// 资费说明
File file = new File("D:\\soso.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String str = "";
while ((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
break;
case 6:// 退出系统
System.out.println("成功退出嗖嗖移动业务大厅");
System.exit(0);
break;
}
System.out.println("是否返回主菜单y\\n");
a = input.next();
} while (a.equals("y"));
}
}
*************欢迎使用嗖嗖移动业务大厅***************
1.用户登录 2.用户注册 3.使用嗖嗖 4.话费充值 5.资费说明 6.退出系统
请选择:
4
请输入电话卡号
2313
请输入充值金额
100
手机号不存在,充值失败。
是否返回主菜单y\n
。。。。。