1. 程式人生 > >Java中簡單的註冊、登陸例項

Java中簡單的註冊、登陸例項

1、功能: 實現使用者的註冊,並能根據註冊的資訊正常登陸。
2、分析:
 a) 具體類
  i. 使用者類
   1. 使用者基本類
   2. 使用者操作類
  ii. 測試類
 b) 每個具體類的內容
  i. 使用者基本類
   1. 成員變數:使用者名稱、密碼
   2. 構造方法:無參構造方法
   3. 成員方法:getXxx()/setXxx()
  ii. 使用者操作類
   1. 登陸:對儲存使用者名稱和密碼的集合進行遍歷。
   2. 註冊:將註冊使用者名稱密碼存入到集合中。
  iii. 測試類
   1. main方法
 c) 類與類之間的關係:測試類建立使用者物件,並使用使用者操作類的功能
 d) 分包(按模組劃分)
  i. 使用者基本類包
  ii. 使用者操作類介面包
  iii. 使用者操作類包
  iv. 測試類包
4、 程式編寫順序:

使用者基本類、使用者操作類介面、使用者操作類、測試類
5、程式例項
 a、使用者基本類

package cn.itcast.pojo;
//使用者基本類
public class UserInformation {
	private String username;
	
	private String passworld;
	
	public UserInformation(){
		
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this
.username = username; } public String getPassworld() { return passworld; } public void setPassworld(String passworld) { this.passworld = passworld; } }

 b、使用者操作類介面

package cn.itcast.dao;

import cn.itcast.pojo.UserInformation;

//對使用者進行操作的介面
public interface UserInformationDao {
	//登陸是否成功
public abstract boolean isLogin(String username,String passworld); //使用者註冊功能 public abstract void regist(UserInformation userinformation); }

 c、使用者操作類

package cn.itcast.dao.impl;

import java.util.ArrayList;

import cn.itcast.dao.UserInformationDao;
import cn.itcast.pojo.UserInformation;

//使用者操作的具體類
public class UserInformationDaoImpl implements UserInformationDao {

	//注意此處的變數必須是靜態的,
	//因為成員變數是隨著物件而存在的,而我們註冊和登陸時建立並使用了兩個物件,
	//登陸和註冊必須要使用同一個集合才有意義(這樣註冊後的賬號才能被查詢到)
	private static ArrayList<UserInformation> array=new ArrayList<UserInformation>();
	
	public boolean isLogin(String username, String passworld) {
		
		boolean flag=false;
		
		for (UserInformation ui:array){
			if(ui.getUsername().equals(username)&&
					ui.getPassworld().equals(passworld)){
				flag=true;
				break;
			}
		}
		
		return flag;
	}

	
	public void regist(UserInformation userinformation) {
		
		array.add(userinformation);

	}

}

 d、測試類

package cn.itcast.test;

import java.util.Scanner;

import cn.itcast.dao.UserInformationDao;
import cn.itcast.dao.impl.UserInformationDaoImpl;
import cn.itcast.pojo.UserInformation;

public class UserInformationTest {
	public static void main(String[] args) {
		while(true) {
			System.out.println("----------歡迎使用----------");
			System.out.println("1、登陸	  2、註冊	   3、退出");	
			System.out.println("請輸入您的選擇");
			Scanner sc=new Scanner(System.in);
			UserInformationDao uid=new UserInformationDaoImpl();
			String choice = sc.nextLine();
			switch (choice) {
			
			//登陸
			case "1":
				System.out.println("----------登陸----------");
				
				System.out.println("請輸入登陸的使用者名稱");
				String name=sc.nextLine();
				System.out.println("請輸入登陸的密碼");
				String password=sc.nextLine();
				
				boolean flag=uid.isLogin(name, password);
				if(flag) {
					System.out.println("登陸成功");
					System.exit(0);
				}else {
					System.out.println("使用者名稱或密碼有誤,登陸失敗");
				}
				break;
				
			//註冊
			case "2":
				System.out.println("----------註冊----------");
				
				System.out.println("請輸入註冊的使用者名稱");
				String newname=sc.nextLine();
				System.out.println("請輸入註冊的密碼");
				String newpassword=sc.nextLine();
				
				UserInformation u=new UserInformation();
				u.setPassworld(newpassword);
				u.setUsername(newname);
			
				uid.regist(u);
				
				System.out.println("註冊成功");
				break;
					
			//退出	
			case "3":
				System.out.println("感謝您的使用");
				sc.close();
				System.exit(0);
				break;
			default:
				System.out.println("輸入有誤,請重新輸入");
				break;
			
			}
			
		}
		
	}
	
	
}

 e、輸出結果顯示
在這裡插入圖片描述