test

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************
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值