webcountimpl.java

来自「一个完整的网络订餐系统」· Java 代码 · 共 123 行

JAVA
123
字号
package com.util;

import java.io.File;
import java.io.PrintStream;
import javax.servlet.ServletContext;


public class WebCountImpl
{

    static WebCount webcount = null;
    private String fileName;
    private String bakFileName;

    public WebCountImpl()
    {
        fileName = "webcount1.wzp";
        bakFileName = "webcount_bak.wzp";
    }

    public WebCountImpl(ServletContext context)
    {
        fileName = "webcount1.wzp";
        bakFileName = "webcount_bak.wzp";
        String filePath = context.getRealPath("WEB-INF");
        fileName = filePath + File.separator + fileName;
        bakFileName = filePath + File.separator + bakFileName;
    }

    public WebCountImpl(String fileName, String bakfileName)
    {
        this.fileName = "webcount1.wzp";
        bakFileName = "webcount_bak.wzp";
        this.fileName = fileName;
        bakFileName = bakfileName;
    }

    public WebCount getNewWebCount()
    {
        return new WebCount();
    }

    public void initWebCount(WebCount webCount)
    {
        WebCountImpl _tmp = this;
        webcount = webCount;
        initWebCount();
    }

    public void initWebCount()
    {
        WebCountImpl _tmp = this;
        if(webcount == null)
            webcount = new WebCount();
        Serial serial = new Serial();
        File file1 = new File(fileName);
        File file2 = new File(bakFileName);
        try
        {
            serial.writeObjectToFile(file1, webcount);
            serial.writeObjectToFile(file2, webcount);
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }

    public WebCount getWebCount()
    {
        WebCountImpl _tmp = this;
        if(webcount == null)
        {
            Serial serial = new Serial();
            try
            {
                webcount = (WebCount)serial.readObjectFromFile(new File(fileName));
            }
            catch(Exception ex)
            {
                try
                {
                    webcount = (WebCount)serial.readObjectFromFile(new File(bakFileName));
                    serial.writeObjectToFile(new File(fileName), webcount);
                }
                catch(Exception ex1)
                {
                    initWebCount();
                }
            }
        }
        return webcount;
    }

    public void addCount()
    {
        _addCount();
    }

    private void _addCount()
    {
        WebCountImpl _tmp = this;
        if(webcount == null)
            webcount = getWebCount();
        webcount.addCount();
        if(webcount.getSumcount() % 10 == 0)
        {
            Serial serial = new Serial();
            try
            {
                serial.writeObjectToFile(new File(fileName), webcount);
                if(webcount.getSumcount() % 100 == 0)
                    serial.writeObjectToFile(new File(bakFileName), webcount);
            }
            catch(Exception ex)
            {
                System.out.println(ex.getMessage());
            }
        }
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?