⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cellphone.java

📁 移动联通通信Java代码小司
💻 JAVA
字号:
/** 以下代码放于 CellPhone.java 中 **/

class Message{  //专门处理短信的类
  private String direction; //方向 发出send 还是接收from
  private String content; //内容
  private String name; //对方的名字
  private CellPhone theOther; //对方手机
  private boolean readed; //对方是否已读取本信息
  private boolean sended; //是否已发送读取确认信息

  public Message(CellPhone theOther,String direction,String content){ //构造函数
    this.direction=direction;
    this.content=content;
    this.name=theOther.getName();
    this.theOther=theOther;
    readed=sended=false;
  }

  public int compareTo(String s){  //比较函数
    return s.compareTo(content);
  }

  public void markReaded(){ //标记已读取
    readed=true;
  }

  public String toString(){ //显示本信息的详细内容
    String s=direction+" "+name+":"+content;
    if (readed) s+="\t对方已读";
    else if (!sended && direction.compareTo("from")==0) {
      theOther.markReaded(content);	//发送信息给对方手机表明本信息已读取
      sended=true;
    }
    return s;
  }
}



public class CellPhone{ //手机类
   public static final double MoneyPerMinuteOut = 0.4;  //常量 呼叫电话收费
   public static final double MoneyPerMinuteIn = 0.2;   //常量 接听电话收费
   public static final double MoneyPerMessage = 0.1;    //常量 短信收费
   
   String name; //户主名字
   double money, totalSpend;  //费用
   Message msg[]; //短信息
   String log; int cntLog; //纪录

   CellPhone(String name, double money){ //手机开户
     this.name=name; this.money=money; totalSpend=0.0;
     log=""; cntLog=0;
     msg=new Message[10];
   }

   public String getName(){return name;} //返回手机用户名字

   private void doLog(String s){ //使用纪录,这里没有使用数组,直接保存为一长串字符串
     cntLog++;
     log+=cntLog+". "+s+"\t余:"+(int)(money*100+0.5)/100.0+"\n";
   }

   private void storeMessage(CellPhone c, String direction, String content){ //保存c发来的消息
     int i;
     for (i=0; i<10; i++){
       if (null==msg[i]) break;
     }
     if (i==10){
       for (i=0; i<9; i++){
       if (null==msg[i+1]) break;
         msg[i]=msg[i+1];
       }
     }
     msg[i]=new Message(c,direction,content);
   }

   public void markReaded(String content){ //做出信息已读的标记
     int i;
     for (i=0; i<10; i++){ //查找是哪条信息已读取
       if (msg[i].compareTo(content)==0){
         msg[i].markReaded();
         break;
       }
     }
   }

   public double getTotalMoney(){ //本手机的花费总金额
     return totalSpend;
   }

   public void callTo(CellPhone c, long t){ //打电话给c用了t分钟时间
     String s="";
     if (t/60>0) s+=" "+(t/60)+" 小时";
     if (t%60>0) s+=" "+(t%60)+" 分钟";
     money-=t*MoneyPerMinuteOut;
     totalSpend+=t*MoneyPerMinuteOut;
     doLog("呼叫 "+c.name+" 电话"+s);
     c.money-=t*MoneyPerMinuteIn;
     c.totalSpend+=t*MoneyPerMinuteIn;
     c.doLog("接听 "+name+" 电话"+s);
   }

   public void messageTo(CellPhone c, String s){ //发短信给c,长度最多为10字符如短信长度超过10字符则自动拆分为多条
     int cnt=0;
     while (s.length()>10){
       storeMessage(c,"send", s.substring(0,9)+">");
       c.storeMessage(this,"from", s.substring(0,9)+">");
       s=s.substring(9); cnt++;
     }
     storeMessage(c,"send", s);
     c.storeMessage(this,"from", s);
     cnt++;
     money-=cnt*MoneyPerMessage;
     totalSpend+=cnt*MoneyPerMessage;
     doLog("发短信给 "+c.name+",分为"+cnt+"次发送");
   }

   public String getMessage(){ //返回本手机的短信,并自动给发信手机回复确认收到的消息,确认回复不收费(这个没那么简单,不要想当然) //显示后的短信不要删除,一直保留,最多10条
     int i; String s="";
     for (i=0;i<10;i++){
       if (msg[i]==null) break;
       s+= (i+1)+". " + msg[i] + "\n";
     }
     s="尊敬的用户"+name+",您共有"+i+"条短信\n"+s;
     return s;
   }

   public String toString(){ //显示本手机的花费情况,每次打电话的纪录,发短信的纪录,不要使用数组纪录
     String s="尊敬的用户"+name+",您的话费余额为"+(int)(money*100+0.5)/100.0+"元,详单如下:\n";
     return s+log;     
   }

}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -