Java IO流(7)之登录注册IO版本案例

登录注册IO版本案例

要求,对着写一遍。

cn.itcast.pojo User

package cn.itcast.pojo;

/**
 * 这是用户基本描述类
 * 
 * @author 风清扬
 * @version V1.1
 * 
 */
public class User {
	// 用户名
	private String username;
	// 密码
	private String password;

	public User() {
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

cn.itcast.dao UserDao

package cn.itcast.dao;

import cn.itcast.pojo.User;

/**
 * 这是针对用户进行操作的接口
 * 
 * @author 风清扬
 * @version V1.1
 * 
 */
public interface UserDao {
	/**
	 * 这是用户登录功能
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @return 返回登录是否成功
	 */
	public abstract boolean isLogin(String username, String password);

	/**
	 * 这是用户注册功能
	 * 
	 * @param user
	 *            要注册的用户信息
	 */
	public abstract void regist(User user);
}

cn.itcast.dao.impl UserDaoImpl

package cn.itcast.dao.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import cn.itcast.dao.UserDao;
import cn.itcast.pojo.User;

/**
 * 这是用户操作的具体实现类(IO版)
 * 
 * @author 风清扬
 * @version V1.1
 * 
 */
public class UserDaoImpl implements UserDao {
	// 为了保证文件一加载就创建
	private static File file = new File("user.txt");

	static {
		try {
			file.createNewFile();
		} catch (IOException e) {
			System.out.println("创建文件失败");
			// e.printStackTrace();
		}
	}

	@Override
	public boolean isLogin(String username, String password) {
		boolean flag = false;

		BufferedReader br = null;
		try {
			// br = new BufferedReader(new FileReader("user.txt"));
			br = new BufferedReader(new FileReader(file));
			String line = null;
			while ((line = br.readLine()) != null) {
				// 用户名=密码
				String[] datas = line.split("=");
				if (datas[0].equals(username) && datas[1].equals(password)) {
					flag = true;
					break;
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("用户登录找不到信息所在的文件");
			// e.printStackTrace();
		} catch (IOException e) {
			System.out.println("用户登录失败");
			// e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					System.out.println("用户登录释放资源失败");
					// e.printStackTrace();
				}
			}
		}

		return flag;
	}

	@Override
	public void regist(User user) {
		/*
		 * 为了让注册的数据能够有一定的规则,我就自己定义了一个规则: 用户名=密码
		 */
		BufferedWriter bw = null;
		try {
			// bw = new BufferedWriter(new FileWriter("user.txt"));
			// bw = new BufferedWriter(new FileWriter(file));
			// 为了保证数据是追加写入,必须加true
			bw = new BufferedWriter(new FileWriter(file, true));
			bw.write(user.getUsername() + "=" + user.getPassword());
			bw.newLine();
			bw.flush();
		} catch (IOException e) {
			System.out.println("用户注册失败");
			// e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					System.out.println("用户注册释放资源失败");
					// e.printStackTrace();
				}
			}
		}
	}
}

cn.itcast.game GuessNumber

package cn.itcast.game;

import java.util.Scanner;

/**
 * 这是猜数字小游戏
 * 
 * @author 风清扬
 * @version V1.1
 * 
 */
public class GuessNumber {
	private GuessNumber() {
	}

	public static void start() {
		// 产生一个随机数
		int number = (int) (Math.random() * 100) + 1;

		// 定义一个统计变量
		int count = 0;

		while (true) {
			// 键盘录入一个数据
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入数据(1-100):");
			int guessNumber = sc.nextInt();

			count++;

			// 判断
			if (guessNumber > number) {
				System.out.println("你猜的数据" + guessNumber + "大了");
			} else if (guessNumber < number) {
				System.out.println("你猜的数据" + guessNumber + "小了");
			} else {
				System.out.println("恭喜你," + count + "次就猜中了");
				break;
			}
		}
	}
}

cn.itcast.test UserTest

package cn.itcast.test;

import java.util.Scanner;

import cn.itcast.dao.UserDao;
import cn.itcast.dao.impl.UserDaoImpl;
import cn.itcast.game.GuessNumber;
import cn.itcast.pojo.User;

/**
 * 用户测试类
 * 
 * @author 风清扬
 * @version V1.1
 * 
 */
public class UserTest {
	public static void main(String[] args) {
		// 为了能够回来
		while (true) {
			// 欢迎界面,给出选择项
			System.out.println("--------------欢迎光临--------------");
			System.out.println("1 登录");
			System.out.println("2 注册");
			System.out.println("3 退出");
			System.out.println("请输入你的选择:");
			// 键盘录入选择,根据选择做不同的操作
			Scanner sc = new Scanner(System.in);
			// 为了后面的录入信息的方便,我所有的数据录入全部用字符接收
			String choiceString = sc.nextLine();

			// switch语句的多个地方要使用,我就定义到外面
			UserDao ud = new UserDaoImpl();

			// 经过简单的思考,我选择了switch
			switch (choiceString) {
			case "1":
				// 登录界面,请输入用户名和密码
				System.out.println("--------------登录界面--------------");
				System.out.println("请输入用户名:");
				String username = sc.nextLine();
				System.out.println("请输入密码:");
				String password = sc.nextLine();

				// 调用登录功能
				// UserDao ud = new UserDaomImpl();

				boolean flag = ud.isLogin(username, password);
				if (flag) {
					System.out.println("登录成功,可以开始玩游戏了");

					System.out.println("你玩吗?y/n");
					while (true) {
						String resultString = sc.nextLine();
						if (resultString.equalsIgnoreCase("y")) {
							// 玩游戏
							GuessNumber.start();
							System.out.println("你还玩吗?y/n");
						} else {
							break;
						}
					}
					System.out.println("谢谢使用,欢迎下次再来");
					System.exit(0);
					// break; //这里写break,结束的是switch
				} else {
					System.out.println("用户名或者密码有误,登录失败");
				}
				break;
			case "2":
				// 欢迎界面,请输入用户名和密码
				System.out.println("--------------注册界面--------------");
				System.out.println("请输入用户名:");
				String newUsername = sc.nextLine();
				System.out.println("请输入密码:");
				String newPassword = sc.nextLine();

				// 把用户名和密码封装到一个对象中
				User user = new User();
				user.setUsername(newUsername);
				user.setPassword(newPassword);

				// 调用注册功能
				// 多态
				// UserDao ud = new UserDaoImpl();
				// 具体类使用
				// UserDaoImpl udi = new UserDaoImpl();

				ud.regist(user);
				System.out.println("注册成功");
				break;
			case "3":
			default:
				System.out.println("谢谢使用,欢迎下次再来");
				System.exit(0);
				break;
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/hc1151310108/article/details/80281541