需求一:
构造出多个话单,话单中包含主叫号码,被叫号码,通话时长,通话时间,将多个话单持久化到文件中,文件名叫billing.txt。
格式如下
主叫号码|被叫号码|通话时长(单位s)|通话时间(yyyy-MM-dd HH:mm:ss)
例如:
13888888888|13666666666|30|2022-03-30 17:05:57
13888888888|13777777777|25|2022-03-31 17:07:57
13999999999|13888888888|50|2022-03-28 17:05:57
......
需求二:
根据话单文件统计出每个号码主叫通话总时长并输出。
例如:
13888888888 x秒
13999999999 y秒
需求一:
(1)准备一个工具类DateUtil,负责将日期转换成字符串,字符串转换成日期
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
/**
*
* @param date 代转换的日期
* @param format 代换格式 yyyy年 MM:月 dd:日 HH: 24小时制 mm:分 ss:秒
* @return 指定格式的日期字符串
*/
public static String dateToStr(Date date, String format){
//日期转换器
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
*
* @param dateStr 日期格式的字符串
* @param format 日期格式
* @return 按照日期格式转换成日期对象
*/
public static Date strToDate(String dateStr,String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}
(2)设计一个话单类
import java.util.Date;
/**
* 话单类
* */
public class Billing {
//主叫号
private String callingNumber;
//被叫号
private String calledNumber;
//通话时长
private int times;
//通话开始时间
private Date startTime;
public Billing(String callingNumber, String calledNumber, int times, Date startTime) {
this.callingNumber = callingNumber;
this.calledNumber = calledNumber;
this.times = times;
this.startTime = startTime;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(callingNumber)
.append("|")
.append(calledNumber)
.append("|")
.append(times)
.append("|")
.append(DateUtil.dateToStr(startTime,"yyyy-MM-dd HH:mm:ss"));
return sb.toString();
}
}
(3)输出话单到文件中
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class TestBilling {
public static void main(String[] args) {
//构造出多个话单
List<Billing> billings = new ArrayList<Billing>();
Billing billing1 = new Billing("13888888888","13666666666",30,new Date());
Billing billing2 = new Billing("13888888888","13777777777",25,new Date());
Billing billing3 = new Billing("13999999999","13888888888",50,new Date());
Billing billing4 = new Billing("13777777777","13666666666",120,new Date());
billings = Arrays.asList(billing1,billing2,billing3,billing4);
//数组和集合之间的转换
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("src/com/njwbhz/part0330/billings.txt"));
//写话单
Iterator<Billing> it = billings.iterator();
while (it.hasNext()){
String billingStr = it.next().toString();
writer.write(billingStr);
writer.newLine();
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null != writer){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
需求二:
按照行读取话单文件,将每一条话单的主叫号+通话时长输出。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class ReadBilling {
public static void main(String[] args) {
Map<String,Integer> billingMap = new HashMap<String, Integer>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(ReadBilling.class.getResourceAsStream("billing.txt")));
String lineData = reader.readLine();
String[] datas = null;
while (null != lineData){
datas = lineData.split("\\|");
if (billingMap.containsKey(datas[0])){
billingMap.put(datas[0],billingMap.get(datas[0]) + Integer.valueOf(datas[2]));
}else {
billingMap.put(datas[0],Integer.valueOf(datas[2]));
}
lineData = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (null != reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//遍历
billingMap.entrySet().forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue() + "秒"));
}
}