虽然是个名称是手机话费计费系统,但是这只是个我的课程实验,没有实用性,呵呵。
不过可以作为学习参考的实例。
大家都知道,计费功能:

一,计费方式(这个好办,获得通话时间后,通过数学公式能够解决)

二,就是如何获得通话时间,因为是日期,而不是简单的 结束 起始时间相减能解决的。

三,就是Java类库里面的date和Calendar的使用和相互转换(还要考虑实际日期的情况,比如闰年,平年,2月份,30/31日的月,月末与月初的交接,还有时间格式SimpleDateFormat 以及异常处理),这些问题相信很多同学都和我一样遇到过的。呵呵。

 

使用方法:可以上我上传的资源下载下载后,直接解压导入eclipse 就OK。

也可以直接把以下代码拷贝到自己新建的项目,类中。

 

test01.java:
______________________________________________________________
package test01;
import java.text.ParseException;
import java.util.Scanner;public class test01 {
 public static void main(String[] args) throws ParseException {
  client ldl = new client();
  while (true) {
   System.out.println("-----          用户话费系统           ------");
   System.out.println("-----          1.新建通话             ------");
   System.out.println("-----        2.计算具体某个月话费      ------");
   System.out.println("-----     3.显示某年所有的话费账单信息  ------");
   System.out.println("-----            4.退出                ------");
   System.out.println(" ");
   
   Scanner stdin=new Scanner(System.in);
   int i=stdin.nextInt();
   switch (i) {
   case 1:
    //System.out.println(1);
    ldl.addBill();
    break;
   case 2:
    //System.out.println(2);
    System.out.println("请输入年:");
    int y2=stdin.nextInt();
    System.out.println("请输入月:");
    int m2=stdin.nextInt();
    ldl.disCertainBill(y2, m2);
    break;
   case 3:
    //System.out.println(3);
    System.out.println("请输入年:");
    int y3=stdin.nextInt();
    ldl.disYearBill(y3);
    break;
   case 4:
    System.out.println("退出系统");
    return ;
   default:
    System.out.println("你输入的不对");
    break;
   }
  }
 }
}_____________________________________
client.java
_____________________________________
package test01;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*;public class client {
 ArrayList<bill> arrayB = new ArrayList<bill>();
 DecimalFormat df=new DecimalFormat("######00.00"); 
 // Vector v = new Vector(4); void addBill() throws ParseException {
  bill b1 = new bill();
  b1.addBill();
  if(b1.ifOverstep(b1.begin, b1.end)){
   arrayB.add(b1.handleOverstep());
   System.out.println("测试");
  }
  arrayB.add(b1);
  // v.add(b1);
 } // 通话时间<=20分钟时,每分钟收费0.05美元,通话时间不到1分钟时按1分钟计算。
 // 通话时间>20分钟时,收费1.00美元,超过20分钟的部分,每分钟0.10美元,不到1分钟时按1分钟计算。 float Charging(int minute) {
  float totalCharges = 0;
  if (minute <= 20) {
   totalCharges = (float) (0.05 * minute);
  } else {
   totalCharges = (float) (0.10 * (minute - 20) + 0.05 * 20);
  }
  return totalCharges;
 } // 显示出某一个具体月的账单
 void disCertainBill(int year, int month) {
  float total = 0;
  int n = 1;
  for (Iterator iter = arrayB.iterator(); iter.hasNext();) {
   bill b = (bill) iter.next();
   if (b.getBeginYear() == year && b.getBeginMonth() == (month-1)) {  
    System.out.println("该月的通话" + n + "为:");
    b.display();
    n++;
    total += Charging(b.getContinue());
   }
  }
 
    //四舍五入  
    //value   =   Double.parseDouble(df.format(value));
  System.out.println("该月的账单为:" + df.format(total));
 } // 显示该年的所有月账单
 void disYearBill(int year) {
  for (int i = 1; i <= 12; i++) {
   float total = 0;
   for (Iterator iter = arrayB.iterator(); iter.hasNext();) {
    bill b = (bill) iter.next();
    if (b.getBeginYear() == year && b.getBeginMonth() == (i-1)) {
     total += Charging(b.getContinue());
    }
   }
   System.out.println("第" + i + "月的账单为:" + df.format(total));
  }
 } public static void main(String[] args) throws ParseException {
  client ldl = new client();
  //System.out.println(ldl.Charging(10));
  //System.out.println(ldl.Charging(20));  bill b1 = new bill("2010-04-22 22:00:00:00", 10,10);
  bill b2 = new bill("2010-04-22 22:00:00:00", 10,10);
  bill b3 = new bill("2010-04-22 22:00:00:00", 10,10);
  bill b4 = new bill("2010-04-22 22:00:00:00", 10,10);
  bill b5 = new bill("2010-04-22 22:00:00:00", 10,10);
  bill b6 = new bill("2010-04-22 22:00:00:00", 10,10);
  bill b7 = new bill("2010-04-22 22:00:00:00", 10,10);
  bill b8 = new bill("2010-04-22 22:00:00:00", 10,10);
  bill b9 = new bill("2010-04-22 22:00:00:00", 10,10);
  bill b10 = new bill("2010-04-22 22:00:00:00", 10,10);
  
  bill b11 = new bill("2010-05-22 22:00:00", 30,30);
  bill b12 = new bill("2010-05-22 22:00:00", 30,30);
  bill b13 = new bill("2010-05-22 22:00:00", 30,30);
  bill b14 = new bill("2010-05-22 22:00:00", 30,30);
  bill b15 = new bill("2010-05-22 22:00:00", 30,30);
  bill b16 = new bill("2010-05-22 22:00:00", 30,30);
  bill b17 = new bill("2010-05-22 22:00:00", 30,30);
  bill b18 = new bill("2010-05-22 22:00:00", 30,30);
  bill b19 = new bill("2010-05-22 22:00:00", 30,30);
  bill b20 = new bill("2010-05-22 22:00:00", 30,30);
  
  bill b21 = new bill("2010-06-30 22:50:00", 50,30);
  
  ldl.arrayB.add(b1);
  ldl.arrayB.add(b2);
  ldl.arrayB.add(b3);
  ldl.arrayB.add(b4);
  ldl.arrayB.add(b5);
  ldl.arrayB.add(b6);
  ldl.arrayB.add(b7);
  ldl.arrayB.add(b8);
  ldl.arrayB.add(b9);
  ldl.arrayB.add(b10);
  ldl.arrayB.add(b11);
  ldl.arrayB.add(b12);
  ldl.arrayB.add(b13);
  ldl.arrayB.add(b14);
  ldl.arrayB.add(b15);
  ldl.arrayB.add(b16);
  ldl.arrayB.add(b17);
  ldl.arrayB.add(b18);
  ldl.arrayB.add(b19);
  ldl.arrayB.add(b20);
  ldl.arrayB.add(b21);
  ldl.disYearBill(2010);
  //ldl.disCertainBill(2010, 04); }
}
_______________bill.java 这个类是重点,呵呵________________________________package test01;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Pattern;public class bill {
 java.text.DateFormat df4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aaa");// 显示的格式
 java.text.DateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 输出的格式
 Calendar begin = Calendar.getInstance();
 Calendar end = Calendar.getInstance();
 Date starttime;
 int iContinue = 0;
 int iContinue2 = 0;
 public bill() {
 }
 public bill(Calendar c1,Calendar c2){
  this.begin=c1;
  this.end=c2;
 }
 public bill(String s, int time, int second) throws ParseException {
  starttime = df3.parse(s);
  begin.setTime(starttime);
  end.setTime(starttime);
  
  iContinue = time;
  iContinue2 = second;
  
  end.add(Calendar.MINUTE, iContinue);
  end.add(Calendar.SECOND, iContinue2);
 } public void addBill() throws ParseException {
  addBegin();
  addContinue();
  System.out.println("开始时间:" + df3.format(begin.getTime()));
  System.out.println("结束时间:" + df3.format(end.getTime()));
 } void addBegin() throws ParseException {
  boolean flag = true;
  while (flag) {
   System.out.println("-----输入当前时间(yyyy-MM-dd HH:mm:ss)------");
     Pattern p = Pattern
       .compile("^((//d{2}(([02468][048])|([13579][26]))[s]?((((0?" +
         "[13578])|(1[02]))[s]?((0?[1-9])|([1-2][0-9])|(3[01])))" +
         "|(((0?[469])|(11))[s]?((0?[1-9])|([1-2][0-9])|(30)))|" +
         "(0?2[s]?((0?[1-9])|([1-2][0-9])))))|(//d{2}(([02468][12" +
         "35679])|([13579][01345789]))[s]?((((0?[13578])|(1[02]))" +
         "[s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))" +
         "[s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[s]?((0?[" +
         "1-9])|(1[0-9])|(2[0-8]))))))");
         //(//s(((0?[0-9])|([1-2][0-3]))//:([0-5]?[0-9])((//s)|(//:([0-5]?[0-9])))))?$ 时间部分
     
    //String s = "2010/02/28";
    //System.out.println(s + " " + p.matcher(s).matches());
   Scanner stdin = new Scanner(System.in);
   String sBegin = stdin.nextLine();
   
   StringBuffer s=new StringBuffer(sBegin);
   String sA=s.substring(0, 4)+"/"+s.substring(5, 7)+"/"+s.substring(8, 10);   if (!p.matcher(sA).matches()) {// 判断其 输入的日期是否为日期格式!
    System.out.println("-----这个日子不存在!------");
    System.out.println("-----(yyyy-MM-dd HH:mm:ss)------");
   } else {
    starttime = df3.parse(sBegin);
    begin.setTime(starttime);
    System.out.println(df3.format(begin.getTime()));
    flag = false;
   }
  }
 } // 账单持续时间
 void addContinue() {
  System.out.println("-----输入电话持续时间(分:秒)------");
  Scanner stdin = new Scanner(System.in);  boolean flag = true;
  boolean flag2 = true;  while (flag) {
   System.out.println("-----分------");
   iContinue = stdin.nextInt();
   if (iContinue > 60) {
    System.out.println("对不起,最长允许通话为60分钟");
   } else {
    end.setTime(starttime);
    end.add(Calendar.MINUTE, iContinue);
    flag = false;
   }
  }
  while (flag2) {
   System.out.println("-----秒------");
   iContinue2 = stdin.nextInt();
   if (iContinue2 > 60 || iContinue2 < 0) {
    System.out.println("对不起,秒范围为0-60");
   } else {
    end.add(Calendar.SECOND, iContinue2);
    if(ifOverstep(begin,end)){
     
    }
    flag2 = false;
   }
  }
 }
 boolean ifOverstep(Calendar c1,Calendar c2){
  int month=this.getBeginMonth();
  int month2=this.getEndMonth();
  if(month2>month){
   //此时该通话处于月末与月初交互出   System.out.println("测试:月末月初交界处OK");
   return true;
  }else{
   return false;
  }
 }
 bill handleOverstep(){
  int minute1=60-this.getBeginMinute();
  int second1=(60-this.getBeginSecond())%60;
  this.setContinue(minute1, second1);
  int minute2=this.getEndMinute();
  int second2=this.getEndSecond();  Calendar end2 = Calendar.getInstance();
  end.set(Calendar.MINUTE, 0);
  end.set(Calendar.SECOND, 0);
  end2.set(this.getEndYear(),this.getEndMonth(),this.getEndDate(),this.getEndHour(),minute2,second2);
  /*
  end2.set(Calendar.YEAR, end.get(Calendar.YEAR));
  end2.set(Calendar.MONTH, end.get(Calendar.MONTH));
  end2.set(Calendar.DATE, end.get(Calendar.DATE));
  end2.set(Calendar.HOUR, end.get(Calendar.HOUR));
  end2.set(Calendar.HOUR, end.get(Calendar.YEAR));
  end2.set(Calendar.YEAR, end.get(Calendar.YEAR));
*/
  bill b=new bill(end,end2);
  b.setContinue(minute2, second2);
  return b;  
  
 } public Calendar getBegin() {
  return begin;
 } public Calendar getEnd() {
  return end;
 } public int getContinue() {
  if (iContinue2 > 0) {
   return iContinue + 1;
  } else {
   return iContinue;
  }
 }
 
 public void setContinue(int minute,int second) {
  this.iContinue=minute;
  this.iContinue2=second;
 }
 
 // 开始时间
 public int getBeginYear() {
  return begin.get(Calendar.YEAR);
 } public int getBeginMonth() {
  return begin.get(Calendar.MONTH);
 } public int getBeginDate() {
  return begin.get(Calendar.DATE);
 } public int getBeginHour() {
  return begin.get(Calendar.HOUR);
 } public int getBeginMinute() {
  return begin.get(Calendar.MINUTE);
 }
 public int getBeginSecond() {
  return begin.get(Calendar.SECOND);
 } // 结束时间
 public int getEndYear() {
  return end.get(Calendar.YEAR);
 } public int getEndMonth() {
  return end.get(Calendar.MONTH);
 } public int getEndDate() {
  return end.get(Calendar.DATE);
 } public int getEndHour() {
  return end.get(Calendar.HOUR);
 } public int getEndMinute() {
  return end.get(Calendar.MINUTE);
 }
 public int getEndSecond() {
  return end.get(Calendar.SECOND);
 }
 
 void display() {
  System.out.println("开始时间:" + df4.format(begin.getTime()));
  System.out.println("通话时间:" + iContinue + "分" + iContinue2 + "秒");
  System.out.println("结束时间:" + df4.format(end.getTime()));
 } public static void main(String[] args) throws Exception {
  /*
   * if (!(new bill().addBill())) { System.out.println("测试OK"); }
   */
  // bill b1 = new bill();
  // b1.addBill();
  // b1.display();
  bill b2 = new bill("2010-04-22 22:00:00", 10, 10);
  System.out.println(b2.getBeginMonth());
  b2.display();
 }
}