package AQSLogs;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class test {
public static Map Transaction(String fileName)throws IOException {
//int flag = 0;
String transactionMsg;
int preAmount,curAmount,totalAmount;
String serviceCode = null, transactionAmount = null;
Map<String, Integer> transactionMap = new HashMap();
Map<String, Integer> preTransactionMap = new HashMap();
Map<String, Integer> restartTransactionMap = new HashMap();
BufferedReader msg = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
while((transactionMsg = msg.readLine()) != null){
int flag = 0;
if(transactionMsg.startsWith("0/")){
serviceCode = transactionMsg.substring(2, transactionMsg.lastIndexOf("=") - 1);
transactionAmount = transactionMsg.substring(transactionMsg.lastIndexOf("=") + 2);
curAmount = Integer.parseInt(transactionAmount);
if(preTransactionMap.containsKey(serviceCode)){
int preKeyValue = preTransactionMap.get(serviceCode);
preAmount = preKeyValue;
}else{
preAmount = curAmount;
preTransactionMap.put(serviceCode, preAmount);
}
if(flag == 0){
if(transactionMap.containsKey(serviceCode)){
int keyValue = transactionMap.get(serviceCode);
if(curAmount > preAmount){
totalAmount = keyValue + (curAmount - preAmount);
transactionMap.put(serviceCode, totalAmount);
preAmount = curAmount;
preTransactionMap.put(serviceCode, preAmount);
}
}else {
transactionMap.put(serviceCode, 0);
}
//test
System.out.println("*********transacionMap******");
for(String obj:transactionMap.keySet()){
String key=obj.toString();
int value=(Integer) transactionMap.get(obj); //通过key,拿到value
System.out.println(key+" = "+value);
}
}else if(flag == 1){
if(restartTransactionMap.containsKey(serviceCode)){
int keyValue = restartTransactionMap.get(serviceCode);
if(curAmount >= keyValue){
//keyValue = curAmount;
restartTransactionMap.put(serviceCode, curAmount);
}
}else {
restartTransactionMap.put(serviceCode, curAmount);
}
//test
System.out.println("*********restartTransactionMap******");
for(String obj:restartTransactionMap.keySet()){
String key=obj.toString();
int value=(Integer) restartTransactionMap.get(obj); //通过key,拿到value
System.out.println(key+" = "+value);
}
}
}else if(transactionMsg.startsWith("##")){
flag = 1;
preTransactionMap.clear();
//preTransactionMap.putAll(transactionMap);
//transactionMap.clear();
//System.out.println("flag = " + flag);//test
}
}
msg.close();
for(String restartServiceCode:restartTransactionMap.keySet()){ //直接拿到key值
//String storeKey = preServiceCode.toString();
int restartValue = restartTransactionMap.get(restartServiceCode); //通过key,拿到value
if(transactionMap.containsKey(restartServiceCode)){
int curValue = transactionMap.get(restartServiceCode);
int totalValue = restartValue + curValue;
transactionMap.put(restartServiceCode, totalValue);
}else{
transactionMap.put(restartServiceCode, restartValue);
}
}
return transactionMap;
}
public static void main(String args[]) throws IOException{
String fileName = "Data\\04-Oct-2013s.txt";
//test************
Map<String, Integer> map= Transaction(fileName);
for(Object obj:map.keySet()){ //直接拿到key值
String key=obj.toString();
int value=(Integer) map.get(obj); //通过key,拿到value
System.out.println(key+" = "+value);
}
//test************
}
}