1. 程式人生 > >Java作業聊天記錄的提取

Java作業聊天記錄的提取

題目:根據附件中的QQ群聊天檔案,讀取其中的聊天記錄,實現如下的方法:1)List<Message> getMessagesById(String id, Date beginTime, Date endTime):根據引數的id返回id對應的qq號碼或者郵箱地址標識的聊天者在指定時間段內(beginTime與Date endTime之間)的所有聊天記錄;2)getChatter():列印除了教師外的話嘮(發言最多的)同學的QQ號(或郵箱)、名字(QQ群中的名字)。

 

LittleProject.java程式碼如下:

package homework5;
import
java.util.*; import java.util.Map.Entry; import java.io.*; public class LittleProject { List<Message> list = new ArrayList<>(); //Set<Person> personSet = new HashSet<>(); Map<Person,Integer> personMap = new HashMap<>(); public void execute(){ File file
= new File("山科大Java學習交流群.txt"); try { InputStream is = new FileInputStream(file); BufferedReader br; try { br = new BufferedReader(new InputStreamReader(is,"UTF-8")); try { String str0 = br.readLine(); String content
= ""; String last = str0; while(true) { String str1 = br.readLine(); if(!(str1==null)&&!str1.startsWith("2018")) {//"!(str1==null)&&!str1.startsWith("2018")"順序不能錯,害我debug好久 content+=str1; content+="\r\n"; } else if(str1==null){ list.add(dealingMessage(last,content)); break; } else { list.add(dealingMessage(last,content)); content = ""; last = str1; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("無該檔案!"); e.printStackTrace(); } } public Message dealingMessage(String str,String content) { String[]ss = str.split(" "); String[]dateString = ss[0].split("-"); String[]timeString = ss[1].split(":"); Date date = null; String name = null; String mail = null; Date date1 = new Date(); Calendar calendar = Calendar.getInstance(); int year = Integer.parseInt(dateString[0]); int month = Integer.parseInt(dateString[1]) - 1; int day = Integer.parseInt(dateString[2]); int hour = Integer.parseInt(timeString[0]); int minute = Integer.parseInt(timeString[1]); int second = Integer.parseInt(timeString[2]); calendar.set(year,month,day,hour,minute,second); date1 = calendar.getTime(); date = date1; String left; if(calendar.get(Calendar.HOUR_OF_DAY)<10&&calendar.get(Calendar.HOUR_OF_DAY)>=0) { left = str.substring(4+2*2+2+1 + 2*3+2+1-1, str.length()); } else{ left = str.substring(4+2*2+2+1 + 2*3+2+1, str.length()); } StringBuilder sb = new StringBuilder(left); sb.reverse(); String sReverse = new String(sb); if(left.endsWith(")")) { int index = sReverse.indexOf('('); int mailLength = index+1-2;//不包含括號 String mail1 = left.substring(left.length()-1-mailLength,left.length()-1); String name1 = left.substring(0,left.length()-1-mailLength-1); mail = mail1; name = name1; } else if(left.endsWith(">")) { int index = sReverse.indexOf('<'); int mailLength = index+1-2;//不包含括號 String mail1 = left.substring(left.length()-1-mailLength,left.length()-1); String name1 = left.substring(0,left.length()-mailLength-1-1); mail = mail1; name = name1; } return new Message(date,name,mail,content); } public List<Message> getMessagesById(String id,Date beginTime,Date endTime){ List<Message> thisList = new ArrayList<>(); for(Message message:list) { if(message.getMail().equals(id)&&message.getDate().before(endTime)&&message.getDate().after(beginTime)) { thisList.add(message); } } return thisList; } public void getChatter() { for(Message message:list) { Person s = new Person(message.getMail(),message.getName()); if(personMap.containsKey(s)){ personMap.put(s, personMap.get(s)+1); } else { personMap.put(s, 1); } } List<Map.Entry<Person, Integer>> mapList = new ArrayList<>(personMap.entrySet()); Collections.sort(mapList,new Comparator<Map.Entry<Person, Integer>>(){ public int compare(Entry<Person,Integer> o1,Entry<Person,Integer> o2) { return -o1.getValue().compareTo(o2.getValue()); } }); int i=0; for(Map.Entry<Person, Integer> entry:mapList) { if(i==1) { System.out.println("Chatter is:"+entry.getKey().getName()+""+entry.getKey().getMail()+" 發言次數:"+entry.getValue()); break; } i++; } } }

Message程式碼如下:

package homework5;

import java.util.Date;

public class Message implements Comparable{
    private Date date;
    private String name;
    private String mail;
    private String content;
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMail() {
        return mail;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    @Override
    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        return date.compareTo((Date)arg0);
    }
    public Message(Date date, String name, String mail, String content) {
        super();
        this.date = date;
        this.name = name;
        this.mail = mail;
        this.content = content;
    }
}

Person.java程式碼如下:

package homework5;
import java.util.*;
public class Person {
    private String mail;
    private String name;
    private List<Message> list = new ArrayList<>();
    public String getMail() {
        return mail;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Message> getList() {
        return list;
    }
    public void setList(List<Message> list) {
        this.list = list;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((mail == null) ? 0 : mail.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (mail == null) {
            if (other.mail != null)
                return false;
        } else if (!mail.equals(other.mail))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    public Person(String mail, String name) {
        super();
        this.mail = mail;
        this.name = name;
    }
    
}

Test.java程式碼如下:

package homework5;
import java.util.*;
public class Test {
    public static void main(String[] args) {
        LittleProject lp = new LittleProject();
        lp.execute();//儲存messages
        System.out.println("請輸入ID:");
        Scanner input = new Scanner(System.in);
        String id = input.next();
        int []time = new int[12];
        System.out.println("請輸入開始時間(格式:年 月 日 時 分 秒)");
        for(int i=0;i<6;i++) {
            time[i] = input.nextInt();
        }
        System.out.println("請輸入結束時間(格式:年 月 日 時 分 秒)");
        for(int i=6;i<12;i++) {
            time[i] = input.nextInt();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(time[0], time[1],time[2],time[3],time[4],time[5]);
        Date begin = calendar.getTime();
        calendar.set(time[6], time[7],time[8],time[9],time[10],time[11]);
        Date end = calendar.getTime();
        List<Message> list = new ArrayList<Message>();
        list = lp.getMessagesById(id, begin, end);
        for(Message message:list) {
            System.out.println(message.getContent());
        }
        //lp.storeStudent();
        lp.getChatter();
    }
}