首页 > 代码库 > java之登录实例(面向对象,静态,scanner等)

java之登录实例(面向对象,静态,scanner等)

分析:抽取用户类,提取出用户的属性,方法

          用户类测试:判断登陆,注册等

User : 用户类   

package cn.my.login;

import java.util.Scanner;

/**
 *  分析:用户的基本信息
 *  用户名,密码,邮箱,电话,地址
 *  登录,注册方法
 *
 */
public class User {

 private String username;
 private String password;
 private String email;
 private String phone;
 private String address;
 public User(String username, String password, String email, String phone,
   String address) {
  super();
  this.username = username;
  this.password = password;
  this.email = email;
  this.phone = phone;
  this.address = address;
 }
 public User() {
  super();
  // TODO Auto-generated constructor stub
 }
 @Override
 public String toString() {
  return "User [username=" + username + ", password=" + password
    + ", email=" + email + ", phone=" + phone + ", address="
    + address + "]";
 }
 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;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public String getPhone() {
  return phone;
 }
 public void setPhone(String phone) {
  this.phone = phone;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 
 public static boolean login()
 { 
 Scanner sc=new Scanner(System.in);
 System.out.println("请输入用户名");
 String userName=sc.nextLine();
 System.out.println("请输入密码");
 String password=sc.nextLine();
 //静态类可以直接调用静态方法和属性和对象引用
 for (int i = 0; i < UserTest.users.length; i++) {
  if (UserTest.users[i]==null) {
   continue;
  }
  if(UserTest.users[i].getUsername().equals(userName)&&UserTest.users[i].getPassword().equals(password)){
   return true;
  } 
 }
 return false;
 }

 public static void regist(){
  Scanner sc=new Scanner(System.in);
  User user=new User();
  System.out.println("请输入用户名");
  user.setUsername(sc.nextLine());
  System.out.println("请输入密码");
  user.setPassword(sc.nextLine());
  System.out.println("请输入邮箱");
  user.setEmail(sc.nextLine());
  System.out.println("请输入电话");
  user.setPassword(sc.nextLine());
  System.out.println("请输入地址");
  user.setAddress(sc.nextLine());
 //将用户添加到数组中
  UserTest.users[UserTest.index++]=user;
 } 
}


UserTest测试类:

package cn.my.login;

import java.util.Scanner;

public class UserTest {
 /**
  *对登录用户的测试
  *
  *1 先提示不同选项
  *2 不同的选项对应的不同操作,
  * 不同的判断,去调用不同的方法
  *
  */
 static int index=0;
 static User[] users =new User[20];
 public static void main(String[] args) {

  //User user=new User();
  printMenu(); 
 while(true){
  Scanner sc=new Scanner(System.in);
  String str=sc.nextLine();
  switch (str) {
  case "1":
   for (int i = 0; i <3; i++) {
    if(User.login()){
     System.out.println("登录成功");
        System.exit(0); 
    }else {
     System.out.println("用户名或者密码错误");
    } 
   }
              System.exit(0);
  case "2":
   User.regist();
   System.out.println("恭喜你注册成功");
   printMenu();
   break;
  case "3":
   System.out.println("退出成功,欢迎下次再来");
   System.exit(0);
  case "4": 
   printMenu();
   break;
  default:
   System.out.println("输入错误,请重新输入");
   break;
  } 
 } 
  
 }
 public static void printMenu(){
System.out.println("**登录请输入1***注册请输入2***");
System.out.println("**退出请输入3***重新选择请输入4***"); 
 }

}