Java案例实战
一.Java实战之生成电信公司的通话记录
1.代码
package com.enmonster.LittleLawson;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/*
1.this class is in order to get the data (analog the data source)
2.the right format is following:
number1 number2 daytime specificTime duration flag
3.the teleNumber is static,so how to avoid using 'static' identifier?
*/
public class DataProducer {
static ArrayList teleNumber = new ArrayList();//store telephone number;
static Map numberAndName = new HashMap();//telephone number --> name
public static void main(String[] args) {
if(args[0]==null || args[0]==""){
return;
}
DataProducer dataProducer = new DataProducer();
dataProducer.initMetadata();
for(int i = 0;i<1000000;i++){
String result = dataProducer.produce(teleNumber,"2017-01-01","2017-12-12");
System.out.println(result);
dataProducer.writeLog(args[0],result+"\n");
}
}
//init the metadata
public void initMetadata(){
teleNumber.add("17802596779");
teleNumber.add("18907263863");
teleNumber.add("19188980695");
teleNumber.add("13320266126");
teleNumber.add("19048828124");
teleNumber.add("13653454072");
teleNumber.add("13135279938");
teleNumber.add("18281704261");
teleNumber.add("17035534749");
teleNumber.add("19834669675");
teleNumber.add("19417467461");
teleNumber.add("19772366326");
teleNumber.add("18283449398");
teleNumber.add("16005439091");
teleNumber.add("14924565399");
teleNumber.add("14218140347");
teleNumber.add("17782151215");
teleNumber.add("17340248510");
teleNumber.add("19961057493");
teleNumber.add("19724655139");
numberAndName.put("17802596779","李雁");
numberAndName.put("18907263863","卫艺");
numberAndName.put("19188980695","仰莉");
numberAndName.put("13320266126","陶欣悦");
numberAndName.put("19048828124","施梅梅");
numberAndName.put("13653454072","金虹霖");
numberAndName.put("13135279938","魏明艳");
numberAndName.put("18281704261","华贞");
numberAndName.put("17035534749","华啟倩");
numberAndName.put("19834669675","仲采绿");
numberAndName.put("19417467461","卫丹");
numberAndName.put("19772366326","戚丽红");
numberAndName.put("18283449398","何翠柔");
numberAndName.put("16005439091","钱溶艳");
numberAndName.put("14924565399","钱琳");
numberAndName.put("14218140347","缪静欣");
numberAndName.put("17782151215","焦秋菊");
numberAndName.put("17340248510","吕访琴");
numberAndName.put("19961057493","沈丹");
numberAndName.put("19724655139","褚美丽");
}
//produce access log
public String produce(ArrayList<String> teleNum,String startTime,String endTime){
int callerIndex = (int)(Math.random()*teleNum.size() );//get a random number
String callerName = teleNum.get(callerIndex);//get the caller Name
int calleeIndex = 0;
do{
calleeIndex = (int)(Math.random()*teleNum.size());//get a random number
}
while(callerIndex == calleeIndex);//if get the same index ,continue
String calleeName =teleNum.get(calleeIndex);//get the calleeName
//for example,you could use yyyy-mm-dd,but this pattern represent year-minute-day
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//should care the format,or you could meet an error
//the startTime and endTime only a string,so you should transform them to a variables of Date
Date startDate = null;
Date endDate = null;
try {
startDate = sdf.parse(startTime);
endDate = sdf.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
}
//get a random distance to form a date
long randomTs = startDate.getTime() + (long)((endDate.getTime() - startDate.getTime()) * Math.random());
Date resultDate = new Date(randomTs);
String resultTimeString = sdf.format(resultDate);
//the following is in order to get a specific time
int hour = (int) (Math.random() * 24);//get hour
int minute = (int) (Math.random() * 60);
int second = (int) (Math.random() * 60);
String specificTime = Integer.toString(hour)+":"+Integer.toString(minute)+":"+Integer.toString(second);
//The following procedure is to generate a duration of communication
int duration = (int) (Math.random()*3600);//Units are seconds
String result = callerName+" "+calleeName+" "+resultTimeString+" "+specificTime+" "+duration ;
return result;
}
//write result to text file
public void writeLog(String filePath,String result){
OutputStreamWriter osw = null;
try {
//you should use append not override,so you should use FileOutputStream(filePath,true),the true denote append
osw = new OutputStreamWriter(new FileOutputStream(filePath,true), "utf-8");
osw.write(result);
osw.flush();//must
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.涉及到的细节
- getTime()中是获取当前日期00:01:00到1970:08:00的毫秒数。验证输出结果如下:
笔者传入的日期是1970-01-01,输出的startDate却是1970-01-01 00:01:00的时间,然后getTime(),得到的结果却是负数。进一步验证。
- SimpleDateFormat中的参数也需要注意大小写。
- FileOutputStream中注意是否是追加,需要添加“追加”对应的参数【true/false】
- 将数字转换成字符串:String str = Integer.toString(i);