-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelcomUser.java
96 lines (87 loc) · 3.25 KB
/
TelcomUser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
class TelcomUser {
private String phoneNumber;
private String callTo;
private StringBuffer communicationRecords;
public TelcomUser(String phoneNumber) {
this.phoneNumber = phoneNumber;
this.communicationRecords = new StringBuffer();
}
//模拟通话记录的生成
void generateCommunicateRecord() {
//随机生成通话记录数目
int recordNum = new Random().nextInt(10);
for(int i = 0; i <= recordNum; i++) {
//随机生成第i条通话记录
//开始时间,当前时间之前的某个随机时间
/* 用System.currentTimeMillis()精确到毫秒
long timeStart = System.currentTimeMillis() - new Random().nextInt(36000000);
//结束时间开始后的十分钟内随机的一个时间,至少一分钟
long timeEnd = timeStart + 60000 + new Random().nextInt(600000);
*/
//用Calendar获取当前时间
Calendar cal = Calendar.getInstance();
//随机减去若干小时(10小时以内)
cal.add(Calendar.HOUR, - new Random().nextInt(10));
//获得对应毫秒
long timeStart = cal.getTimeInMillis();
//结束时间开始后的十分钟内随机的一个时间,至少一分钟
long timeEnd = timeStart + 60000 + new Random().nextInt(600000);
//被叫号码
this.callTo = getCallToPhoneNumber();
//插入通话记录
this.communicationRecords.append(this.phoneNumber +
"," + timeStart +
"," + timeEnd +
"," + this.callTo+
";");
}
}
//随机生成被叫号码(后四位随机)并返回
private String getCallToPhoneNumber() {
return "1380372" + String.valueOf(new Random().nextInt(10))
+ String.valueOf(new Random().nextInt(10))
+ String.valueOf(new Random().nextInt(10))
+ String.valueOf(new Random().nextInt(10));
}
//模拟计费办法,以字符串的形式返回保留4位小数的计费结果
private String accountFee(long timeStart, long timeEnd) {
//每分钟收费*元
double feePerMinute = 0.2;
//通话分钟数按四舍五入计算
int minutes = Math.round((timeEnd - timeStart)/60000);
double feeTotal = feePerMinute * minutes;
return String.format("%.4f", feeTotal);
}
//打印通话记录
void printDetails() {
//获取全部通话记录
String allRecords = this.communicationRecords.toString();
//分割通话记录
String [] recordArray = allRecords.split(";");
//循环分割记录内的每一项并输出
for(int i = 0; i < recordArray.length; i++) {
System.out.println("---------通话记录分割线---------");
String [] recordField = recordArray[i].split(",");
System.out.println("主叫:" + recordField[0]);
System.out.println("被叫:" + recordField[3]);
Date timeStart = new Date(Long.parseLong(recordField[1]));
Date timeEnd = new Date(Long.parseLong(recordField[2]));
DateFormat mediumFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
//DateFormat
/*
System.out.println("通话开始时间:" + mediumFormat.format(timeStart));
System.out.println("通话结束时间:" + mediumFormat.format(timeEnd));
*/
//SimpleDateFormat
System.out.println("通话开始时间:" + simpleDateFormat.format(timeStart));
System.out.println("通话结束时间:" + simpleDateFormat.format(timeEnd));
System.out.println("计费:"
+ accountFee(Long.parseLong(recordField[1]), Long.parseLong(recordField[2]))
+ " 元。");
}
}
}