您的位置:首页 > 职场人生

黑马程序员__移动用户资费统计Demo

2013-04-11 12:56 281 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

模拟实现简易的移动用户资费统计系统逻辑,具体需求如下:

1.移动运营商A设置两种类型的用户:普通用户及VIP用户,现该运营商已有5个VIP用户和15个普通用户,共计20个用户。

2.普通用户资费标准如下(不考虑漫游和长途):

【基准资费】

无月租费用。

通话费:0.6元 / 分钟(仅拨打收费,接听免费)

短信费:0.1元 / 条

数据费:5元/ M

【优惠套餐】

话费套餐 :月功能费 20元,最多可拨打60分钟电话,超出时间按照

0.5元 / 分钟计费。

短信套餐 :月功能费10元,最多可发送200条短信,超出条数按照

0.1元 / 条计费。

数据套餐 :月功能费20元,最多可获50M的流量,超出流量按照

3元 / M 计费。

注:用户可以选择多种套餐,各功能(通话、短信、数据)计费时,如已选择对应套餐,则按套餐标准计费;如未选择对应套餐,则按对应的基准资费计费。

VIP用户资费标准如下(不考虑漫游和长途):

【基准资费】

月租费用:按天收取,2元 / 天

通话费:0.4元 / 分钟(仅拨打收费,接听免费)

短信费:0.1元 / 条

数据费:3元/ M

【优惠套餐】

套餐1 :月基本费用 100元(无月租费用),提供如下服务:

① 最多可拨打750分钟电话,超出部分按照0.3元 / 分钟计费。

② 最多可发送200条短信,超出条数按照0.1元 / 条计费。

③ 最多可获得100M数据流量,超出流量按照1元 / M计费。

套餐2 :月基本费用 200元(无月租费用),提供如下服务:

① 最多可拨打2000分钟电话,超出部分按照0.2元 / 分钟计费。

② 最多可发送500条短信,超出条数按照0.1元 / 条计费。

③ 最多可获得300M数据流量,超出流量按照0.5元 / M计费。

注:用户最多只能选择一种套餐,如未选择任何套餐,则按照基准资费计费。

各类型用户只能选择提供给本类型用户的套餐。

新用户入网。

① 对于新入网的普通用户,入网当月赠送如下服务:免费拨打60分钟

电话,免费发送200条短信,免费获得50M流量。超出赠送的部分

按照普通用户基准资费进行计费。

② 对于新入网的VIP用户,入网当月赠送如下服务:免费拨打200分

钟电话,免费发送200条短信,免费获得100M数据流量。超出赠送

的部分按照VIP用户基准资费进行计费(注意:需按入网天数计算月

租费用)。

每月为用户计算一次账单,用户订制的套餐信息和账单信息采用文件方式进行存储(提示:可使用java中的Properties API进行文件操作)。

用户可自由订制或退订所属用户类型的套餐,并从下月起生效。

异步随机生成客户操作如下:

① 拨打电话,每次拨打时长为1至10分钟不等(随机决定,以分钟为

单位)。

② 发送短信,每次发送条数为1至10条不等(随机决定)。

③ 上网获取数据,每次获取数据流量可为50K,100K,200K,

500K,1M(随机决定)。

④ 订制或退订相应套餐。

⑤ 新用户入网(随机决定用户类型)。

注:随机生成客户操作时间间隔自定,可设置。

不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。

运用配置文件存储各项数据:

在配置文件中存储各项数据

采用一种便于程序代码读取的格式:
要存储的数据项有:功能单价费用、功能套餐免费数量、功能套餐月费用、新入网免费数量、整体月基本费或月租费。
一些数据还要随以下类型进行区分:用户类型、套餐类型、功能类型。
在配置文件中通过用点(.)对数据项名称进行分级的方式来区分各个数据项所属的类别和功能,如下所示:
common.normal.phone.price 表示普通用户/非套餐/电话/单价
common.pack1.phone.price 表示普通用户/套餐/电话/单价
common.pack1.phone.free 表示普通用户/套餐/电话/免费数量
common.pack1.phone.rent 表示普通用户/套餐/电话/套餐月功能费用

vip.normal.phone.price 表示VIP用户/非套餐/电话/单价
vip.pack1.phone.price 表示VIP用户/套餐1/电话/单价
vip.pack2.phone.price 表示VIP用户/套餐2/电话/单价

common.new.phone.free 表示普通用户/新开户/电话/免费数量
vip.new.phone.free 表示VIP用户/新开户/电话/免费数量
对于值为0的数据项,不用在配置文件中存储,这样,当程序代码从配置文件中没有读取到该数据项时,即认为该值为0。
对于vip用户的整体月基本费或月租费,由于计费单位不一样,采用配置文件方式存储将增加程序的复杂度,所以,决定直接在程序代码中硬编码。

编码读取配置文件中的各项数据:

ConfigManager类中的方法:
公有:getPrice、getFree、getRent、getNewCustomerFree。
私有:makePrefix、getNumber
makePrefix方法:
private static String getPrefix(int customerType,int packType,int entryType){
String packTitles = {"normal","pack1","pack2“,”new”};
String entryTitles ={"phone","message","data"};

String customerTitle = customerType==0?"common":"vip";
String packTitle = packTitles[packType];
String entryTitle = entryTitles[entryType];
return customerTitle + "." + packTitle + "." + entryTitle;
}
getNumber方法:
private static int getNumber(String key){
String value = config.getProperty(key);
try{
return Integer.parseInt(value);
}catch(Exception e){
return 0;
}
}

代表移动公司的类:

public class MobileCorporation {
private ArrayList<Customer> customers = new ArrayList<Customer>();
public MobileCorporation(){
for(int i=1;i<=15;i++){
customers.add(new CommonCustomer(i+"号普通客户",new Date(108,10,1)));}
for(int i=1;i<=5;i++){
customers.add(new VIPCustomer(i+"号VIP客户",new Date(108,10,1)));}
System.out.println("程序创建了运营商已有的5个VIP用户和15个普通用户,并设置他们的入网日期为2008年10月1日.");
}
//模拟某个月的业务活动
public void simulateBusiness(Date month){
for(Customer customer : customers){
customer.monthBegin();}
System.out.println("--------being simulating " + DateUtil.formatDateToMonth(month) + "--------------");
for(int i=0;i<500/*30*/;i++){
randDoOneThing(month);}
System.out.println(DateUtil.formatDateToMonth(month)+"的计费汇总清单:");
//汇总所有人的账单
for(int i=0;i<customers.size();i++){
customers.get(i).countMonthMoney(month);}
}


移动公司的randDoOneThing方法

private void randDoOneThing(Date month){
Calendar calendar = Calendar.getInstance();
calendar.setTime(month);
calendar.add(Calendar.MONTH, 1);
Date monthOfOrderPack = calendar.getTime();

int rand = new Random().nextInt(30);
switch(rand){
case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:
callPhone();break;
case 9:    case 10:case 11:case 12:case 13:case 14:case 15:case 16:case 17:
sendMessage();break;
case 18:case 19:case 20:case 21:case 22:case 23:case 24:case 25:case 26:
transferData();break;
/*让orderPack、cancelPack、joinNewCustomer的出现概率是其他操作的1/9。*/
case 27:
orderPack(monthOfOrderPack);break;
case 28:
cancelPack(monthOfOrderPack);break;
case 29:
joinNewCustomer(month);break;
}
}


移动公司类的joinNewCustomer方法:

private void joinNewCustomer(Date month){
Calendar calendar = Calendar.getInstance();
calendar.setTime(month);
int maxDay = calendar.getMaximum(Calendar.DAY_OF_MONTH);
int randDay = new Random().nextInt(maxDay) + 1;
/*下面的复制过程很重要,不能直接修改date,当然最好是对Calendar直接操作
* 这里是为了演示要注意clone而保留的。
*/
Date joinTime = (Date)month.clone();
joinTime.setDate(randDay);
int randType = (new Random().nextInt(10))%2;
Customer customer = null;
if(randType == 0){
int commonId = IdGenerator.getInstance().nextCommonId();
customer = new CommonCustomer(commonId+"号普通客户",joinTime);
customers.add(customer);
}else{
int vipId = IdGenerator.getInstance().nextVipId();
customer = new VIPCustomer(vipId+"号VIP客户",joinTime);
customers.add(customer);
}
System.out.println(DateUtil.formatDateToDay(joinTime) + "新注册了" + customer);
}


为新用户生成Id编号的方法:

public class IdGenerator {
private IdGenerator(){}
private static IdGenerator instance = new IdGenerator();
public static IdGenerator getInstance(){
return instance;}

private int lastCommonId = 15;
private int lastVipId = 5;

public synchronized int nextCommonId(){
return ++lastCommonId;}
public synchronized int nextVipId(){
return ++lastVipId;}
}


主运行类:

public static void main(String[] args) {
MobileCorporation corp = new MobileCorporation();
//设置要模拟的起始月份
Date month = new Date(109,0,1);
System.out.println("程序开始模拟从2009年1月1日开始,连续15个月的运行情况.");
//总共模拟15个连续的月份
for(int i=0;i<15/*3*/;i++){
corp.simulateBusiness(month);
Calendar calendar = Calendar.getInstance();
calendar.setTime(month);
calendar.add(Calendar.MONTH, 1);
month = calendar.getTime();
}
}


普通用户与VIP用户的抽象父类:

public abstract class Customer {
protected String name; private int customerType; private Date joinTime;//用户入网的时间
//下面集合中积累的结果只表示当月的记录,不代表所有历史记录
protected List<ActionRecord> actionRecords = new ArrayList<ActionRecord>();private ArrayList phoneRecords = new ArrayList();
private ArrayList messageRecords = new ArrayList();private ArrayList dataRecords = new ArrayList();
protected PackStrategy packStrategy;
public void monthBegin(){
phoneRecords.clear();messageRecords.clear();dataRecords.clear();actionRecords.clear();
}
public Customer(String name,int customerType,Date joinTime){
this.name = name;this.customerType = customerType;this.joinTime = joinTime;
}
public String toString(){  return name;
}
public void callPhone(int times){
phoneRecords.add(times); actionRecords.add(new ActionRecord("打电话",times + "分钟"));
}
public void sendMessage(int numbers){
messageRecords.add(numbers);  actionRecords.add(new ActionRecord("发短信",numbers + "条"));
}
public  void transferData(int size){
dataRecords.add(size);  actionRecords.add(new ActionRecord("传数据",size + "k"));
}
private int gatherRecords(ArrayList records){
int sum = 0;
for(int i=0;i<records.size();i++){
sum +=  (Integer)(records.get(i));
}
return sum;
}
public abstract void randomCancelPack(Date month);
public abstract void randomOrderPack(Date month);


用户类中计算某月费用的方法:

public int countMonthMoney(Date currentMonth){
boolean newcome = !joinTime.before(currentMonth);//joinTime.after(currentMonth);
int totalPhone = gatherRecords(phoneRecords);int totalMessage = gatherRecords(messageRecords);int totalData = gatherRecords(dataRecords);
int freePhone = 0;int freeMessage = 0;int freeData = 0;
if(newcome){
freePhone = ConfigManager.getNewUserFree(customerType,0);  freeMessage = ConfigManager.getNewUserFree(customerType,1);
freeData = ConfigManager.getNewUserFree(customerType,2);
}
int chargePhone = totalPhone>freePhone?totalPhone-freePhone:0;int chargeMessage = totalMessage>freeMessage?totalMessage-freeMessage:0;
int chargeData = totalData>freeData?totalData-freeData:0;
//汇总打印:包括姓名,入网日期,统计月份,通话清单,费用清单,总费用。
System.out.println(name + "," + DateUtil.formatDateToDay(joinTime) + "入网.");
System.out.println(" 操作清单如下-----");
for(int i=0;i<actionRecords.size();i++){ System.out.println("  " + actionRecords.get(i)); }
System.out.println(" 统计清单如下-----");
System.out.println("  通话:" + phoneRecords + "分钟:短信" + messageRecords + "条:数据" + dataRecords + "k");
System.out.println("  通话累计:" + totalPhone + "分钟,减除新开户" + freePhone + "分钟,实际收费" + chargePhone + "分钟");
System.out.println("  短信累计:" + totalMessage + "条,减除新开户" + freeMessage + "条,实际收费" + chargeMessage + "条");
System.out.println("  数据累计:" + totalData + "k,减除新开户" + freeData + "k,实际收费" + chargeData + "k");
ComputeStrategy phoneStrategy = packStrategy.getValidComputeStrategy(currentMonth,0);
ComputeStrategy messageStrategy = packStrategy.getValidMessageComputeStrategy(currentMonth,1);
ComputeStrategy dataStrategy = packStrategy.getValidDataComputeStrategy(currentMonth,2);
int sum = 0; Rent rent = packStrategy.getValidRent(currentMonth); //VIP用户才有月租金或基本费
if(rent != null){
int rentMoney = rent.coputeRent(joinTime,currentMonth);sum +=  rentMoney;
System.out.println("  月租费或基本费:" + rentMoney + "分钱");
}
sum += phoneStrategy.computeMoney(chargePhone)+messageStrategy.computeMoney(chargeMessage)+ dataStrategy.computeMoney(chargeData);
System.out.println("  总计:" + sum + "分钱");
return sum;
}


普通用户类的 randomOrderPack方法:

public void randomOrderPack(Date month){
int rand = new Random().nextInt(3);
switch(rand){
case 0:
if(packStrategy.getOrderedPack(0)==null || packStrategy.getOrderedPack(0).getPackType() == 0){
packStrategy.orderPack(month,0);
System.out.println(name + "订购了" + "电话套餐" + "(从" + DateUtil.formatDateToMonth(month) + "开始)");
actionRecords.add(new ActionRecord("定电话套餐",""));
}	break;
case 1:
if(packStrategy.getOrderedPack(1) == null || packStrategy.getOrderedPack(1).getPackType() == 0){
packStrategy.orderPack(month,1);
System.out.println(name + "订购了" + "短信套餐" + "(从" + DateUtil.formatDateToMonth(month) + "开始)");
actionRecords.add(new ActionRecord("定短信套餐",""));
}		break;
case 2:
if(packStrategy.getOrderedPack(2) == null || packStrategy.getOrderedPack(2).getPackType() == 0){
packStrategy.orderPack(month,2);
System.out.println(name + "订购了" + "数据套餐" + "(从" + DateUtil.formatDateToMonth(month) + "开始)");
actionRecords.add(new ActionRecord("定数据套餐",""));
}	break;
}
}


普通用户类的 randomCancelPack方法

public void randomCancelPack(Date orderedMonth){
int rand = new Random().nextInt(3);
switch(rand){
case 0:
if(packStrategy.getOrderedPack(0) == null || packStrategy.getOrderedPack(0).getPackType() == 0){
System.out.println(name + "试图退订根本就没有订过的电话套餐");		return;
}				packStrategy.cancelPack(orderedMonth,0);
System.out.println(name + "退订了" + "电话套餐" + "(从" + DateUtil.formatDateToMonth(orderedMonth) + "开始)");
actionRecords.add(new ActionRecord("退订电话套餐",""));	break;
case 1:
if(packStrategy.getOrderedPack(1) ==null || packStrategy.getOrderedPack(1).getPackType() == 0){
System.out.println(name + "试图退订根本就没有订过的短信套餐");		return;
}				packStrategy.cancelPack(orderedMonth,1);
System.out.println(name + "退订了" + "短信套餐" + "(从" + DateUtil.formatDateToMonth(orderedMonth) + "开始)");
actionRecords.add(new ActionRecord("退订短信套餐",""));          break;
case 2:
if(packStrategy.getOrderedPack(2)==null || packStrategy.getOrderedPack(2).getPackType() == 0){
System.out.println(name + "试图退订根本就没有订过的数据套餐");		return;
}				packStrategy.cancelPack(orderedMonth,2);
System.out.println(name + "退订了" + "数据套餐" + "(从" + DateUtil.formatDateToMonth(orderedMonth) + "开始)");
actionRecords.add(new ActionRecord("退订数据套餐",""));		break;
}
}


Vip用户的方法与以上方法类似。

计算某项功能费用的策略类:

public class ComputeStrategy {
//此处省略成员变量的定义….
public ComputeStrategy(int customerType, int packType,int entryType) {
this.customerType = customerType;
this.packType = packType;
this.entryType = entryType;
switch(entryType){
case 0:entryName = "电话";break;
case 1:entryName = "短信";break;
case 2:entryName = "数据";break;
}
}

public int computeMoney(int quantity){
int unitPrice = ConfigManager.getUnitPrice(customerType, packType,entryType);
int freeQuantity = ConfigManager.getPackFree(customerType, packType,entryType);
int packCharge = ConfigManager.getPackCharge(customerType, packType,entryType);
int charQuantity = quantity - freeQuantity;
if(chargeQuantity < 0){
chargeQuantity = 0;
}
int money = packCharge + price * chargeQuantity;
System.out.println("  " + entryName + "月功能费为:" + packCharge + "分钱,");
System.out.println("        收费数量:" + quantity + "-" + freeQuantity + "=" + chargeQuantity + ",");
System.out.println("        使用收费:"    + chargeQuantity + "*" + price + "=" + price * chargeQuantity +"分钱,");
System.out.println("        小计收费:"    + packCharge + "+" + price * chargeQuantity + "分钱");
return money;
}
}


计算VIP用户月租费或月基本费的类

public class Rent {
//此处省略成员变量和构造方法的定义
public int coputeRent(Date startTime,Date currentMonth){
//首先应该想到去找开源的日期运算类
if(unit == RentUnit.DAY){
Calendar start = Calendar.getInstance();
start.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(currentMonth);
//将日期设置为当月的最后一天
end.set(Calendar.DAY_OF_MONTH, 1);
end.add(Calendar.MONTH, 1);
end.add(Calendar.DAY_OF_MONTH, -1);
int days = end.get(Calendar.DAY_OF_MONTH) ;
if(end.get(Calendar.MONTH) == start.get(Calendar.MONTH)){
days -= start.get(Calendar.DAY_OF_MONTH) + 1;
}
return  price*days;
}
else{
return price;
}
}
}


存储用户的各项业务套餐策略的类

public class PackStrategy {
private int customerType;

private Rent rent;
private ComputeStrategy computeStrategy[] = new  ComputeStrategy[3];

private OrderedStrategyHolder<Rent> orderedRent = new OrderedStrategyHolder<Rent>();
private OrderedStrategyHolder<ComputeStrategy>[] orderedStrategyHolder = new OrderedStrategyHolder<ComputeStrategy>[3];

public PackStrategy(int customerType,int packType,Rent rent){
this.customerType = customerType;
this.rent = rent;

for(int i=0;i<3;i++){
computeStrategy[i] = new ComputeStrategy(customerType,packType,i);
orderedStrategyHolder[i] = new OrderedStrategyHolder<ComputeStrategy>();
}
public Rent getValidRent(Date month){
Rent holderRent = orderedRent.getValidStrategy(month);
return holderRent==null?rent:holderRent;
}

public void orderRent(Date orderedMonth,Rent rent){
Rent oldRent = orderedRent.order(orderedMonth,rent);
if(oldRent != null){
this.rent = oldRent;
}
}

public void cancelRent(Date orderedMonth){
orderRent(orderedMonth,null);
}
}
public ComputeStrategy getValidComputeStrategy(Date month,int entryType){
ComputeStrategy orderedStrategy = orderedStrategyHolder[entryType].getValidStrategy(month);
return orderedStrategy==null?computeStrategy[entryType]:orderedStrategy;
}

public void orderPack(Date orderedMonth,int packType,entryType){
ComputeStrategy orderedStrategy = orderedStrategyHolder[entryType].order(
orderedMonth,new ComputeStrategy(customerType,packType,entryType));
if(orderedStrategy != null){
computeStrategy[entryType] = orderedStrategy;
}
}

public void cancelPack(Date orderedMonth,int entryType){
orderPack(orderedMonth, 0,entryType);
}


套餐订购记录类 (存储一项业务套餐订购信息)

public class OrderedStrategyHolder<T> {
private Date orderedMonth;
private T strategy;

public T order(Date orderingMonth,T strategy){
T oldStrategy = null;
if((this.orderedMonth!=null) && this.orderedMonth.before(orderingMonth)){
oldStrategy = this.strategy;
}

this.orderedMonth = orderingMonth;
this.strategy = strategy;
return oldStrategy;
}

public T getValidStrategy(Date month){
if(orderedMonth!=null && (!orderedMonth.after(month))){
return strategy;
}
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: