catalogue
(3) Overall code implementation
(4) Functional module diagram analysis
(1) Basic thought
- OOP thought
Starting from the object-oriented idea, in order to realize the simulation of user login interface, specifically, there should be user and user management system, user characteristic attributes and implementation methods in the use class, statistics of user login and offline in the user management system, and finally presented in the user service class.
- Concrete implementation
The definition of basic classes is realized by user class and userloginserve. Finally, the two classes are connected by user service class (UserManage)
User class
1. Attribute characteristics: name and password
2. Method: password check, user login, online prompt
public void checkpwd(String m_pwd){} public void logout(){} public boolean isonline(){}
User login service class
1. Feature attribute: creation of user class array
User use[]=new User(10);
2. Method: check whether the user is online and record the number of people, and the user logs in
public void checkonline(){} public void login(String m_name,String pwd){}
User management interface
Main function: simulate and generate multiple user objects
public static void main(String[] args){}
(2) Implementation of method
- make a concrete analysis
1. Reasons why the checkpwd method belongs to the user class:
In the user class, an object will be created every time a user logs in. Therefore, you can judge whether the user is online only by determining whether the user exists and judging whether its password is right or wrong (the corresponding online is true and false respectively). If checkpwd is in the user login service class, you cannot obtain the user's login status after the user class is created.
The specific implementation is as follows:
public void checkpwd(String m_pwd) { if(m_pwd.equals(pwd)) //Judge whether the password is right or wrong { online=true; System.out.println("The user is online"); }eles { System.out.println("Password verification error"); } }
2. Why is the logout method in the user class and the login method in the user login service class
A user class needs to be created in the user login service. Since the object of the user class is created first, if the login method is in the user class, it has not been determined whether there is this user. This user may not exist, but it is still online. The correctness of the password is judged by the user class, and the correctness of the user name is judged by the user service class. The login method and need to be determined by the user, so it should be in the user class
The specific implementation is as follows:
public void login(String m_name,String m_pwd) //Authenticate one user at a time { int count=0; for(int i;i<use.length;i++) //Traversal user { if(use[i]!=null) //Check whether the pointer is null { if(use[i].name.equals(m_name)) { count++; User us=use[i]; us.checkpwd(m_pwd); } } } if(count==0) { System.out.println(name+"User does not exist, please try again!"); }else{ System.out.println(name+"User presence") } } public void logout() //Realize offline function { if(online) { online=false; System.out.println("User offline"); }else{ System.out.println(name+"Not online"); } }
3. Reason for the online method: there are only two login States, offline and online, that is, it is more appropriate to use bool data type
public boolean isonlinne() { if(online) { System.out.println(name+"on-line"); }else{ System.out.println(name+"off-line"); } }
4. Check the reasons for the number of people in the user service category:
The online number of users can be counted only after the user is online. Therefore, the online number of users can be counted only after the user class object is created
int count=0; for(int i;i<use.length;i++) //Traversal user { if(use[i]!=null) //Check whether the pointer is null { if(use[i].name.equals(m_name)) { count++; User us=use[i]; us.checkpwd(m_pwd); } } }
(3) Overall code implementation
User class
public class User { String name; String pwd; boolean online=false; public void checkpwd(String m_pwd) //Check whether the password is correct { if(m_pwd.equals(pwd)) { online=true; System.out.println(name+"User online"); }else{ System.out.println("Password verification error"); } } public boolean isonline() //Implementation of whether the user is online { if(online) { System.out.println(name+"User online"); }else{ System.out.println(name+"User offline"); } return online; } public void logout() { if(online) { online=false; System.out.println(name+"Logout"); }else{ Syetem.out.println.(name+"Not online"); } } }
Implementation of user login service class
public class UserloginServe() { static User []users=new User[20]; //static is called by the class name and has only one copy public void login(String m_name,String m_pwd) //User login { int count=0; //Count the number of cycles for(int i=0,i<use.length;i++) { if(users[i]!=null) { if(m_name.equals(name)) { count++; System.out.println("User presence"); User user=users[i]; user.checkpwd(m_pwd); } } } if(count==0) { System.out.println("This user does not exist"); }else{ System.out.println(name+"User presence"); } } public static void checkNum() //Query the number of people online { int count=0; for(int i=0;i<use.length;i++) { if(users[i]!=null) { if(users[i].isline()) //Verify Online { count++; } } } System.out.println("The number of online users is"+count); } }
user management
public class Usremanager() { public static void main(String []args) { for(int i=0;i<20;i++) { User user=new User(); user.name="user"+i; user.pwd="admin"+i; UserloginSeve.users[i]=user; } for(int i=0;i<20;i++) { UserloginServe.login("user"+i,"admin"+i); } UserloginSeve.user[8].logout(); UserloginServe.checkNum(); } }
(4) Functional module diagram analysis
Specific explanation:
In the user class, the checkpwd and logout methods obtain the user's online status from the true and false values of online and feed it back. In the user login service class, The login method calls the checkpwd method to verify the existence of the user. In the checkUseNum method, call isoline through the array to obtain whether the user is online and count the number of people. Finally, assign a value to the array in the main function and call the login and checkNum methods