⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatservice.java

📁 Ajax 聊天小程序.想学习Ajax技术的朋友,可不要错过啊.
💻 JAVA
字号:
package lee;

import java.util.*;
import java.io.*;

/*
 * @author  yeeku.H.lee kongyeeku@163.com
 * @version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date: 
 */
public class ChatService 
{
    private static ChatService cs;
    private Properties userList;

    private LinkedList<String> chatMsg;

    private ChatService()
    {
    }

    public static ChatService instance()
    {
        if (cs == null)
        {
            cs = new ChatService();
        }
        return cs;
    }

    public boolean validLogin(String user , String pass) 
        throws IOException
    {
        if (loadUser().getProperty(user) == null)
        {
            return false;
        }
        if (loadUser().getProperty(user).equals(pass))
        {
            return true;
        }
        return false;
    }

    public boolean addUser(String name , String pass)
        throws Exception
    {
        if (userList == null)
        {
            userList = loadUser();
        }
        if (userList.containsKey(name))
        {
            throw new Exception("用户名已经存在,请重新选择用户名");
        }
        userList.setProperty(name , pass);
        saveUserList();
        return true;
    }


    public String getMsg()
    {
        if (chatMsg == null)
        {
            chatMsg = new LinkedList<String>();
            return "";
        }
        String result = "";
        for (String tmp : chatMsg)
        {
            result += tmp + "\n";
        }
        return result;
    }

    public void addMsg(String user , String msg)
    {
        if (chatMsg == null)
        {
            chatMsg = new LinkedList<String>();
        }
        if (chatMsg.size() > 40)
        {
            chatMsg.removeFirst();
        }
        chatMsg .add(user + "说:" + msg);
    }

    //////////////////////////////////////////////////////////////
    //        下面是系统的工具方法
    /////////////////////////////////////////////////////////////

    private Properties loadUser()throws IOException
    {
        if (userList == null)
        {
            File f = new File("userFile.properties");
            if (!f.exists())
                f.createNewFile() ;            
            userList = new Properties();
            userList.load(new FileInputStream(f)); 
        }
        return userList;
    }

    private boolean saveUserList()throws IOException
    {
        if (userList == null)
        {
            return false;
        }
        userList.store(new FileOutputStream("userFile.properties"), "userList");
        return true;
    }
}

⌨️ 快捷键说明

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