preface:
After many days, we have finished learning the object-oriented phase of Java. After all, we need to put it into practice. This small project is our first java object-oriented solution project. Next, let's enter the project world together
catalogue
3, Implementation of specific modules
1, Project requirements
2, Function realization
UML class diagram of the whole project. Sorry for the nonstandard production.
System flow
3, Implementation of specific modules
1. Realize the login module.
Idea: to realize the login module, we should first think that our usual login account is to enter the account and password, and the login can be successful by matching the account and password you registered before. How can we match the account and password we entered with the account and password we registered in advance? Here we should think that the account and password we entered are String types, Since it is a String, we can use the equals method of String class for matching. If the matching is successful, the login is successful.
Well, in our project, before logging in, we must match whether he exists in our registered account. If so, log in directly. If not, we should let the user register first. Of course, we can also add a verification code composed of random numbers to further strengthen our login module.
Example code
package com.Team.loginAndRegister; /** * @author WangWei * @version 1.0 */ public class LoadingAndRegister { public String user; public String password; public LoadingAndRegister() { } public LoadingAndRegister(String user,String password) { this.user = user; this.password = password; } public void setUser(String user) { this.user = user; } public String getUser() { return user; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } } package com.Team.loginAndRegister; import java.util.Scanner; import java.util.ArrayList; /** * @author WangWei * @version 1.0 */ public class LoadingAndRegisterTest { /** * - ArrayList<LoadingAndRegister> a A collection of stored accounts and passwords * - LoadingAndRegister lr Object of account password class */ Scanner sc = new Scanner(System.in); private ArrayList<LoadingAndRegister> a = new ArrayList<LoadingAndRegister>(); private LoadingAndRegister lr = new LoadingAndRegister(); /** * Registration method * @throws InterruptedException */ public void loginAccount() throws InterruptedException { TSUtility.loadSpecialEffects(); System.out.println("Start registration:"); System.out.println("Please enter your registered account name:"); String loginUserName = TSUtility.readKeyBoard(6, false); lr.setUser(loginUserName); System.out.println("Please enter your login password:"); String loginPassword = TSUtility.readKeyBoard(8, false); lr.setPassword(loginPassword); a.add(lr); System.out.println("Registration succeeded, please login!"); } /** * Login method * @throws InterruptedException */ public void loading() throws InterruptedException { Scanner sc = new Scanner(System.in); boolean flag = true; int count = 5; while (flag) { System.out.println("********************🐱"); System.out.println("*** <Login interface> ***"); System.out.println("*** (: ***🐱"); System.out.println("********************🐱"); System.out.println("Please enter your login account name:"); String userName = sc.next(); System.out.println("Please enter your login password:"); String password = sc.next(); //unregistered if (lr.getUser() == null || lr.getPassword() == null) { System.out.println("Your account is not detected, please register first!"); loginAccount(); } else if (userName.equals(lr.getUser()) && password.equals(lr.getPassword())) { System.out.println("Login succeeded! Welcome:" + lr.getUser()); flag = false; } else { if (count == 1) { System.out.println("Your account has been locked and forced to exit!"); System.exit(0); } else { count--; System.out.println("Login failed! The user name or password does not match. Do you have any more" + (count) + "A chance!\n Please log in again:"); } } } } /** * Method of modifying user information * @throws InterruptedException */ public void updateAccount() throws InterruptedException { boolean flag = true; while (flag) { System.out.println("=====Please select a function====="); System.out.println("1,Change Password"); System.out.println("2,Modify user name"); System.out.println("3,Modify user name and password"); System.out.println("4,Exit without modification"); System.out.println("==================="); System.out.print("Please select:"); char c = TSUtility.readMenuSelection(); TSUtility.loadSpecialEffects(); switch (c) { case '1': System.out.println("Please enter the original password:"); String oldPassword = sc.next(); if (lr.getPassword().equals(oldPassword)) { System.out.println("The password is correct. Please enter your new password:"); String newPassword = sc.next(); lr.setPassword(newPassword); System.out.println("Set successfully, please remember the new password!"); break; } else { System.out.println("Wrong password, please select again!"); } break; case '2': System.out.println("Please enter the original password:"); String oldPassword1 = sc.next(); if (lr.getPassword().equals(oldPassword1)) { System.out.println("The password is correct. Please enter your new user name:"); String newUserName = sc.next(); lr.setPassword(newUserName); System.out.println("Set successfully!"); } else { System.out.println("Wrong password, please select again!"); } break; case '3': System.out.println("Please enter the original password:"); String oldPassword2 = sc.next(); if (lr.getPassword().equals(oldPassword2)) { System.out.println("The password is correct. Please enter your new user name:"); String newUserName = sc.next(); lr.setPassword(newUserName); System.out.println("Please enter your new password:"); String newPassword = sc.next(); lr.setPassword(newPassword); System.out.println("Set successfully!"); } else { System.out.println("Wrong password, please select again!"); } break; case '4': System.out.println("Exiting"); TSUtility.loadSpecialEffects(); System.out.println("Exit successful"); flag = false; break; default: System.out.println("Input error, please re-enter!"); break; } } } }
After we register, we will enter the system and carry out several operations.
1. Complete the addition, deletion, modification and query of developers.
2. Complete the operations of adding, deleting and modifying developers to the development team.
3. Complete the project module and hand over the project to the development team for development. And the previous deletion and modification of the project.
According to our UML class diagram design, we need to write at least nine classes and one interface.
There is an inheritance relationship between the employee class, programmer class, designer class and architect class in order to inherit the properties of the parent class. There are three other classes, PC, notebook and printer. They all implement an interface, equipment. After these classes are completed, we begin to write the NameListService class. The function of this class is to add, delete, modify and query our developers.
The main problem to pay attention to in the NamlistService class is the developer's ID. to increase or delete the developer's ID with our increase or deletion, we can define a global variable to control the change of ID. By adding a developer, we set his ID to increase automatically. When we delete, we start from the location where he deletes, because we use the collection to store the developer. After his previous element is deleted, his next element should go to the location of the deleted element, and the subscript in the collection will move forward. What we need to do is to subtract the ID of the element by one. Then we will use the loop to find the element corresponding to the subscript, and then set its ID. The example code is as follows
boolean flag = false; for (int i = 0; i < employees.size(); i++) { if (id == employees.get(i).getId()) { System.out.println( "Employees:"+ employees.get(i).getName() + "Delete succeeded!"); employees.remove(i); for (i = id; i <= employees.size(); i++) { //This code indicates that after deleting our ith object from the collection, the ID of our next element should be reduced by one accordingly. employees.get(i - 1).setId(employees.get(i - 1).getId() - 1); }
This needs to be understood in combination with the subscript of the set. You can draw a graph to assist in understanding.
After this, we need to be the module of the development team. What we need to pay attention to in the module of the development team is the problem of data exchange.
When we do this part, we will find a problem during the test. We can't take the modified data in our namelist class for use. First, the data changes due to the re creation of the object. So how to prevent this kind of situation? There are two solutions:
1: We can change the parameters of the method we need to use the data in the NameListService class to the object of the NameListService class, so that we will not create the object and change the data.
example:
public ArrayList<ArrayList<Programmer>> getManyTeam(NameListService nls) throws TeamException {
2: We can change the collection of developers in NameListService class to static decoration, so that their data can be shared. When we do so, we will find that when we test, he prints the content in our code block and the content in our modified collection, which does not meet our requirements. This is because, When the class is loaded, the content of the code block will be printed by default. Then we can judge the content of the code block to judge whether the set where we store developers is empty. If it is empty, the content of the code block will be printed. If it is not empty, the content of the set will be printed.
example:
{ /* if (employees.isEmpty()) { }*/ employees.add(new Employee(count, "Jack Ma ", 22, 3000)); employees.add(new Architect(++count, "pony ", 32, 18000, new NoteBook("association T4", 6000), 60000, 5000)); employees.add(new Programmer(++count, "Robin Li", 23, 7000, new PC("Dale", "NEC 17 inch"))); employees.add(new Programmer(++count, "Qiang Dong Liu", 24, 7300, new PC("Dale", "Samsung 17 inches"))); employees.add(new Designer(++count, "Lei Jun ", 50, 10000, new Printer("laser", "Canon 2900"), 5000)); employees.add(new Programmer(++count, "Ren Zhiqiang", 30, 16800, new PC("ASUS", "Samsung 17 inches"))); employees.add(new Designer(++count, "Liu Chuanzhi", 45, 35500, new PC("ASUS", "Samsung 17 inches"), 8000)); employees.add(new Architect(++count, "Yang Yuanqing", 35, 6500, new Printer("Needle type", "Epson 20 k"), 15500, 1200)); employees.add(new Designer(++count, "Shi Yuzhu", 27, 7800, new NoteBook("HP m6", 5800), 1500)); employees.add(new Programmer(++count, "Ding Lei ", 26, 6600, new PC("Dale", "NEC17 inch"))); employees.add(new Programmer(++count, "Chao Yang Zhang ", 35, 7100, new PC("ASUS", "Samsung 17 inches"))); employees.add(new Designer(++count, "Zhi Yuan Yang", 38, 9600, new NoteBook("HP m6", 5800), 3000)); }
What the project module needs to pay attention to is the addition of the project. You need to make a judgment. For example, there is no team in the project, but you add it.
4, Summary
It is really difficult for novices to complete this development project, but it can be completed as long as it takes time.
Next, I'd like to share my experience in this project.
First of all, before getting the project, we should be familiar with the process, clarify its logic, clarify the relationship between each module, and then complete the content of each module, because we use object-oriented knowledge, so we should better understand the relationship, inheritance, implementation, etc. between various classes.
After completing each module, you must do a good job in testing and try to find the bugs you can find. Otherwise, waiting for the final master module to find the bugs can drive you crazy.
After completing this project, you will have a further understanding of object-oriented knowledge and become more and more familiar with object-oriented features. And all kinds of little knowledge we have learned before.
The above is my understanding of this project. Welcome to discuss. Please forgive me for the shortcomings. The code is as follows:
Link: https://pan.baidu.com/s/1gdlwmVpnhhqJDMCPBHQ4Rg
Extraction code: e6kz