code.java

package AQSLogs;

import java.io.BufferedReader;
import java.io.File;
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.Map.Entry;
import java.util.Set;

public class AQSLogsUtility {

    static Map dailyUpdatedMap = new HashMap();
    static Map dailyShutdownMap = new HashMap();

    public static void main(final String[] args) throws IOException {
        fileTraversal("Data");
    }

    /**
     * Checks whether the path contains any files.
     *
     * @param pathName
     * @throws IOException
     */
    private static void fileTraversal(final String pathName) throws IOException {
        File file = new File(pathName);
        File[] path = null;
        ArrayList listOfFiles = new ArrayList();
        if (file.isDirectory()) {
            path = file.listFiles();
            for (int i = 0; i < path.length; i++) {

                if (path[i].isDirectory()) {
                    System.out.println("The Month :: " + path[i].getName());
                    fileTraversal(path[i].getPath());
                }
                // If the path refers to a s.txt file.
                if (path[i].isFile()) {
                    String name = path[i].getName().substring(path[i].getName().lastIndexOf(".") - 1);

                    if (name.equalsIgnoreCase("s.txt")) {
                        listOfFiles.add(path[i]);

                    }
                }
            }
        }
        File newFile = null;
        String dailyServiceType = null;
        String dailyTransactionType = null;
        Map dailyEntryMap = new HashMap();

        Map monthlyEntryMap = new HashMap();

        /**
         * Reads the content of each entry in the text file to find out the
         * daily count of transactions.
         */
        for (int i = 0; i < listOfFiles.size(); i++) {

            newFile = (File) listOfFiles.get(i);
            String fileName = (newFile.getName()).substring(0, 11);
            System.out.println("Daily Report ::" + fileName);
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(newFile)));
            String content;
            while ((content = in.readLine()) != null) {
                if (content.startsWith("0/")) {
                    // Declares Process
                    dailyServiceType = content.substring(2, content.lastIndexOf("=") - 1);

                    dailyTransactionType = content.substring(content.lastIndexOf("=") + 2);

                    // Declares Transaction
                    int dailyTransactionValue = Integer.parseInt(dailyTransactionType);

                    /**
                     * Updates the entry in a temporary daily Map.
                     */
                    if (dailyEntryMap.containsKey(dailyServiceType)) {
                        Integer actualValue = (Integer) dailyEntryMap.get(dailyServiceType);
                        int existingValue = actualValue.intValue();
                        if (dailyTransactionValue >= existingValue) {
                            // dailyShutdownMap.clear();
                            Integer newIntObj = new Integer(dailyTransactionValue);
                            dailyEntryMap.put(dailyServiceType, newIntObj);
                            int newDailyValue = (dailyTransactionValue - existingValue);
                            AQSLogsUtility.dailyUpdatedMap = updateNewMap(dailyServiceType, Math.abs(newDailyValue));

                        } else {
                            Integer newIntObj = new Integer(dailyTransactionValue);
                            dailyEntryMap.put(dailyServiceType, newIntObj);
                            Integer value = (Integer) AQSLogsUtility.dailyUpdatedMap.get(dailyServiceType);
                            // dailyShutdownMap.put(dailyServiceType, value);
                            AQSLogsUtility.dailyShutdownMap.put(dailyServiceType, value + newIntObj);
                            AQSLogsUtility.dailyUpdatedMap.remove(dailyServiceType);

                        }

                    } else {
                        Integer intObj = new Integer(Math.abs(dailyTransactionValue));
                        dailyEntryMap.put(dailyServiceType, intObj);
                    }

                }

            }

            Set set = AQSLogsUtility.dailyUpdatedMap.entrySet();
            Iterator it = set.iterator();

            while (it.hasNext()) {
                /**
                 * View the daily report.
                 */

                Map.Entry me = (Entry) it.next();
                System.out.println(me.getKey() + ":" + me.getValue());

                /**
                 * Updating the data to get a monthly report.
                 */
                if (monthlyEntryMap.containsKey(me.getKey())) {
                    Integer value = (Integer) monthlyEntryMap.get(me.getKey());
                    int oldValue = value.intValue();
                    int newValue = ((Integer) me.getValue()).intValue();
                    int latestValue = oldValue + newValue;
                    Integer newIntObj = new Integer(latestValue);
                    monthlyEntryMap.put(me.getKey(), newIntObj);
                } else {
                    monthlyEntryMap.put(me.getKey(), me.getValue());
                }
            }

            dailyEntryMap.clear();
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

        }

        /**
         * View the monthly report.
         */
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("Monthly Report for ::");
        Set monthlySet = monthlyEntryMap.entrySet();
        Iterator it01 = monthlySet.iterator();
        while (it01.hasNext()) {
            Map.Entry monthData = (Entry) it01.next();
            System.out.println(monthData.getKey() + ":" + monthData.getValue());
        }
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

    }

    /**
     * CAlculated and updated the correct entry for the Daily Report.
     *
     * @param dailyServiceType
     * @param newDailyValue
     * @return
     */
    private static Map updateNewMap(final String dailyServiceType, final int newDailyValue) {
        int shutValue = 0;
        if (AQSLogsUtility.dailyUpdatedMap.containsKey(dailyServiceType)) {

            Integer existingValue = (Integer) AQSLogsUtility.dailyUpdatedMap.get(dailyServiceType);
            int actualValue = existingValue.intValue();
            Integer intObj = new Integer(Math.abs(actualValue + newDailyValue + shutValue));
            AQSLogsUtility.dailyUpdatedMap.put(dailyServiceType, intObj);

        } else {

            Integer intObj = new Integer(Math.abs(newDailyValue));
            AQSLogsUtility.dailyUpdatedMap.put(dailyServiceType, intObj);
            if (AQSLogsUtility.dailyShutdownMap.containsKey(dailyServiceType)) {
                Integer shutDownValue = (Integer) AQSLogsUtility.dailyShutdownMap.get(dailyServiceType);
                shutValue = shutDownValue.intValue();
                Integer intObj01 = new Integer(Math.abs(newDailyValue + shutValue));
                AQSLogsUtility.dailyUpdatedMap.put(dailyServiceType, intObj01);
            }


        }

        return AQSLogsUtility.dailyUpdatedMap;

    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值