Java学习之分层技术编程

根据所学的基础集合类知识来简单模拟出一个注册和登录的小程序。先编写并分析不含有I/O流读写文件操作(即非持久化)的Demo程序,后再根据所学的I/O流知识进行模拟数据库功能,进行注册信息的写入文件操作,以达到持久化的目的,算是对之前所学的集合框架和I/O流部分基础知识的回顾与深入理解。

详细分析:实现用户登录、注册的功能;
用户:
属性:登陆名、密码;
行为:注册、登陆
软件分层:
1.控制层:Demo类中的main方法
2.持久化层:
1).IDAO:注册、登陆
2).UserDao:注册,登陆
3.模型层:JavaBean:POJO
1).UserPojo:
loginName
loginPwd
4.试图层:
1).向控制台输出:UserLogin.java
2).分包:
1.按功能划分;
2.按模块划分;
3.先按模块划分,然后再按功能划分;
1.持久化层:com.mytest.dao–>IDAO.java
com.mytest.user.dao --> UserDao.java
2.模型层: com.mytest.user.model --> UserPojo.java
3.控制层: com.mytest.demo --> Demo.java
4.视图层: com.mytest.user.view --> UserLogin.java

package com.mytest.demo;

import com.mytest.user.view.UserLogin;
/
public class Demo {
	public static void main(String[] args) {
		//启动视图层
		UserLogin uLogin = new UserLogin();
		uLogin.showView();
	}
}

package com.mytest.user.dao;

import java.util.ArrayList;
import com.mytest.dao.IDAO;
import com.mytest.user.model.UserPojo;

public class UserDao implements IDAO {
	//定义一个集合,模拟数据库,存储用户的登陆名和密码信息;
	private static ArrayList<UserPojo> userList = new ArrayList<UserPojo>();
	
	//登陆
	@Override
	public boolean toLogin(UserPojo user) {
		//到数据库中找,是否有此用户名和密码的用户,如果有,返回true,否则返回false
		//数据库我们使用的是userList,所以我们要userList中去找;
		//遍历集合
		for(UserPojo u : userList){
			if(u.getLoginName().equals(user.getLoginName()) &&
					u.getLoginPwd().equals(user.getLoginPwd())){
				return true;
			}
		}
		return false;
	}
	//注册
	@Override
	public boolean toRegist(UserPojo user) {
		//1.先验证数据库中是否有此用户名,如果有,返回false不允许注册,否则将用户信息写入数据库
		//遍历,验证用户名
		for(UserPojo u : userList){
			if(u.getLoginName().equals(user.getLoginName())){
				return false;
			}
		}
		//写入集合
		userList.add(user);
		//返回true
		return true;
	}

}

package com.mytest.dao;

import com.mytest.user.model.UserPojo;


public interface IDAO {
	//一般情况下,此接口中包含一些访问数据库的增删改查的一些方法
	
	//登录
	boolean toLogin(UserPojo user);
	//注册
	boolean toRegist(UserPojo user);
	
}

//当登陆成功之后,自动启动猜字小游戏

package com.mytest.game;

import java.util.Random;
import java.util.Scanner;

public class NumberGame {
	
	public void start(){
		Scanner sc = new Scanner(System.in);
		//产生一个数字:1--100
		Random rdo = new Random();
		int num = rdo.nextInt(100) + 1;
		int count = 0;//计数器
		
		System.out.println("数字已经生成,可以开始了...");
		
		while(true){
			System.out.print("请输入你猜的数字:");
			int uNum = sc.nextInt();
			count++;
			if(uNum > num){
				System.out.println("高了!");
			}else if (uNum < num){
				System.out.println("低了!");
			}else{
				System.out.println("恭喜你,猜到了!数字为:" + num + " 你猜了 : " + count + " 次。");
				break;
			}
		}
	}
}

package com.mytest.user.model;
 //实现:
 //实现模型层:因为其它模块都需要模型层的类.

public class UserPojo {
	private String loginName;
	private String loginPwd;
	public UserPojo(String loginName, String loginPwd) {
		super();
		this.loginName = loginName;
		this.loginPwd = loginPwd;
	}
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getLoginPwd() {
		return loginPwd;
	}
	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}
	
	
}
package com.mytest.user.view;

import java.util.Scanner;

import com.mytest.game.NumberGame;
import com.mytest.user.dao.UserDao;
import com.mytest.user.model.UserPojo;

public class UserLogin {
	public void showView(){
		//向控制台输出
		Scanner sc = new Scanner(System.in);
		do{
			System.out.println("1.注册      2.登录         3.退出");
			int op = sc.nextInt();
			String userName = "";
			String userPwd = "";
			UserPojo user = null;
			switch(op){
				case 1:
				case 2:
					System.out.print("请输入登陆名:");
					userName = sc.next();
					System.out.print("请输入登陆密码:");
					userPwd = sc.next();
					//封装数据模型
					user = new UserPojo(userName,userPwd);
					
					break;
				case 3:
					//自己填充;
					
					break;
			}
			//不论是注册还是登录,都需要使用持久层
			UserDao uDao = new UserDao();
			if(op == 1){
				boolean result = uDao.toRegist(user);
				if(result == true){
					//注册成功了
					System.out.println("注册成功!");
				}else{
					System.out.println("注册失败,登陆名可能已经存在!");
				}
			}
			if(op == 2){
				boolean result = uDao.toLogin(user);
				if(result){
				//	System.out.println("登录成功!");
				//启动一个猜数字小游戏;
					//考虑再加一个小菜单。
					new NumberGame().start();
				}else{
					System.out.println("用户名或密码错误,登录失败!");
				}
			}
		}while(true);
	}
}

– --我们会很直观的感受到上述程序需要每次都进行信息的注册,并且只有先注册才能进行登录查询,一旦停止后,上次所写入的注册信息全部丧失掉,这样的情况往往不适合实际情况。在掌握I/O流知识的基础下,可以进一步将每次注册的信息存入至文本文件,在下一次启动该程序时,点击登陆后可以将文本文件的内容读入集合类中,利用集合自带方法进行匹配。

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

package com.mytest.demo;

import java.io.IOException;
import com.mytest.user.view.UserLogin;

public class Demo {
	public static void main(String[] args) {
		//启动视图层
		UserLogin uLogin = new UserLogin();
		try {
			uLogin.showView();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

package com.mytest.dao;

import java.io.IOException;
import com.mytest.user.model.UserPojo;

public interface IDAO {
	//一般情况下,此接口中包含一些访问数据库的增删改查的一些方法
	
	//登录
	boolean toLogin(UserPojo user) throws IOException;
	//注册
	boolean toRegist(UserPojo user) throws IOException;
	
}
package com.mytest.user.dao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import com.mytest.dao.IDAO;
import com.mytest.user.model.UserPojo;

public class UserDao implements IDAO {
	//定义一个集合,模拟数据库,存储用户的登陆名和密码信息;
//	private static ArrayList<UserPojo> userList = new ArrayList<UserPojo>();
	//写入到一个文件中。
	//定义输入输出流
	private BufferedReader in;
	private BufferedWriter out;
	//登陆
	@Override
	public boolean toLogin(UserPojo user) throws IOException {
		//1.先获取所有用户信息
		ArrayList<String> userList = this.getAllUser();
		//2.循环遍历所有的用户信息,进行匹配
		for(String s : userList){//s = "aaa,123456"
			String[] uArray = s.split("[,]");//uArray[0] = "aaa",uArray[1] = "123456"
			//匹配用户名和密码要全部相同
			if(uArray[0].trim().equals(user.getLoginName()) &&
						uArray[1].trim().equals(user.getLoginPwd())){
				return true;
			}
			
		}
		//如果上面没有return,说明没有找到此用户,直接返回false;
		return false;
	}
	
	//注册
	@Override
	public boolean toRegist(UserPojo user) throws IOException {
		//1.先获取所有的用户信息
		ArrayList<String> userList = this.getAllUser();
		//2.遍历之前的所有用户,判断用户名是否存在
		for(String s : userList){//s = "aaa,1234"
			String[] userArray = s.split("[,]");
			if(userArray[0].trim().equals(user.getLoginName())){//将文件中的用户的登录名和参数user的登录名比较
				return false;
				
			}
		}
		//3.如果之前都没有return,那么说明用户名没有重复,可以写入文件;
		return this.saveUser(user);
	}
	
	//这里定义一个方法,获取文件中的所有用户名和密码信息
	//注意方法的返回值:1.空的集合(size()== 0),现在的例子就是这样的
	//           2.可以返回一个null;
	private ArrayList<String> getAllUser() throws IOException{
		//从文件中读取所有用户信息
		this.in = new BufferedReader(new FileReader("user.txt"));
		String row = null;
		ArrayList<String> userList = new ArrayList<String>();
		while((row = in.readLine()) != null){
			userList.add(row);
		}
		in.close();
		return userList;
		
	}
	
	//定义一个方法,写入一个UserPojo信息,一个登录用户的信息
	private boolean saveUser(UserPojo user) throws IOException{
		//向文件写入一行数据
		this.out = new BufferedWriter(new FileWriter("user.txt",true));//追加写入
		//先将用户的登陆名和密码封装成一个字符串
		String row = user.getLoginName() + "," + user.getLoginPwd();
		//写入文件;
		out.write(row);
		out.newLine();
		out.close();
		return true;
	}

}

package com.mytest.user.view;

import java.io.IOException;
import java.util.Scanner;

import com.mytest.game.NumberGame;
import com.mytest.user.dao.UserDao;
import com.mytest.user.model.UserPojo;

public class UserLogin {
	public void showView() throws IOException{
		//向控制台输出
		Scanner sc = new Scanner(System.in);
		do{
			System.out.println("1.注册      2.登录         3.退出");
			int op = sc.nextInt();
			String userName = "";
			String userPwd = "";
			UserPojo user = null;
			switch(op){
				case 1:
				case 2:
					System.out.print("请输入登陆名:");
					userName = sc.next();
					System.out.print("请输入登陆密码:");
					userPwd = sc.next();
					//封装数据模型
					user = new UserPojo(userName,userPwd);
					
					break;
				case 3:
					//自己填充;
					
					break;
			}
			//不论是注册还是登录,都需要使用持久层
			UserDao uDao = new UserDao();
			if(op == 1){
				boolean result = uDao.toRegist(user);
				if(result == true){
					//注册成功了
					System.out.println("注册成功!");
				}else{
					System.out.println("注册失败,登陆名可能已经存在!");
				}
			}
			if(op == 2){
				boolean result = uDao.toLogin(user);
				if(result){
				//	System.out.println("登录成功!");
				//启动一个猜数字小游戏;
					//考虑再加一个小菜单。
					new NumberGame().start();
				}else{
					System.out.println("用户名或密码错误,登录失败!");
				}
			}
		}while(true);
	}
}

package com.mytest.game;

import java.util.Random;
import java.util.Scanner;

public class NumberGame {
	
	public void start(){
		Scanner sc = new Scanner(System.in);
		//产生一个数字:1--100
		Random rdo = new Random();
		int num = rdo.nextInt(100) + 1;
		int count = 0;//计数器
		
		System.out.println("数字已经生成,可以开始了...");
		
		while(true){
			System.out.print("请输入你猜的数字:");
			int uNum = sc.nextInt();
			count++;
			if(uNum > num){
				System.out.println("高了!");
			}else if (uNum < num){
				System.out.println("低了!");
			}else{
				System.out.println("恭喜你,猜到了!数字为:" + num + " 你猜了 : " + count + " 次。");
				break;
			}
		}
	}
}


package com.mytest.user.model;
public class UserPojo { }          同上述代码

– --通过对比可以很直接的感受到加入了IO流的程序,满足了我们对软件的需求,也使得软件能够更加接近实用,而不仅仅限定在内部测试的范围内。经过整理这两段代码,集合和I/O流的知识脉络更加清晰明了。代码中出现了某些编程的项目框架和分层理念,例如其中的Model层的UserPojo,这些也都是在进阶路上需要积累学习的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值