Object oriented Day01

Posted by gorskyLTD on Wed, 29 Dec 2021 23:29:47 +0100

Classification thought ## subcontracting thought ## dark horse information management system

  1. Classification thought
  2. Package overview and definition
  3. Package considerations and class access
  4. Requirements description and effect demonstration
  5. Environment construction
  6. Menu building
  7. Basic added functions - thought analysis
  8. Basic add function - code implementation
  9. Add function - add student number judgment
  10. Add function - problem analysis
  11. static keyword features
  12. static precautions - student number problems and Solutions
  13. View student - code implementation
  14. Delete function - code implementation
  15. Modification function - code implementation
  16. Code optimization of student management system

  • Teacher management system - environment construction


Through the knowledge we have mastered, first implement a case, and then find out some problems in the case
Improve the reusability, readability and maintainability of the code through new knowledge points
Existing problems:
1. Code redundancy (complex)
2. Poor readability (multiple functions are placed in one module / class)
3. Poor maintainability (high coupling)

Code analysis of student management system:
The business logic is in a java file
There are many repeated codes (index judgment, encapsulating the code of Student object)
Insert picture description here

Please add a picture description
------------------------------------------------------------------------------------------------------

  1. Classification thought
    Classification idea:
    Division of labor and cooperation, special personnel to do special work

Boss Management:
Customer service reception - > salesman - > warehouse management - > warehouse

Classification of dark horse information management system:
Student class:
The standard class encapsulates the student information (id, name, age, birthday) entered by the keyboard
StudentDao class: library management
Data Access Object, which is used to access containers (arrays, collections, databases) that store data
StudentService class: Salesman
Process business logic (for example, judge whether the entered id exists)
StudentController class: customer reception
Dealing with users (receiving requests, collecting information, printing data to the console, etc.)
------------------------------------------------------------------------------------------------------
2. Overview and definition of package
Since there are many classified files, we need to manage the files in the following ways:
Subcontracting (sub folder)

Subcontracting idea:
If all class files are in the same package, it is not conducive to management and later maintenance
Therefore, class files with different functions can be managed under different packages

Package: the essence is folder

Package creation:
Single level package and multi-level package can be created
Use between multi - level packages separate
The naming standard of the package is the reverse writing of the company domain name after removing www (com.baidu)
Package names are all lowercase letters

package keyword:
Use package to define a package: text documents are added manually and automatically by the development tool (in the first line of the code)
Format: package name Package name Class name;

3. Package considerations and class access
Package considerations:
The package statement must be the first line of code in the file
package keyword. There can only be one in a java file
If there is no package, it means no package name by default (it can be placed in the root directory scr in the IDEA)

Access between classes:
Access to classes under the same package:
There is no need to guide the package, and you can access it directly
Access to classes under different packages:
1. Access through the full class name (package name + class name), separated by
2. Access after importing packages (common)

Application scenarios through full category name Guide Package:
When a class with the same name appears under multiple packages
To use these two classes with the same name in another class
If we use import to import the first class, the second class cannot use import

Note the relationship between the writing positions of import, package and class Keywords:
package: must be the first line of code in the file
import: write it under package and on class
class: under import

  1. Requirements description and effect demonstration
    [dark horse information management system] includes:
    1. Student management system
      Add: the entered information includes id, name, age and birthday; Use array to store student information. id cannot be repeated
      Delete: enter the student id to delete. If the entered id does not exist, you need to re-enter it; Remove from array if present
      Modify: enter the student id to modify. If the entered id does not exist, you need to re-enter it; If it exists, continue to enter other information and modify the old information in the array
      Query: print the information of all objects in the array on the console

    2. Teacher management system
      Add: (ibid.)
      Delete: (ibid.)
      Change: (ibid.)
      Check: (ibid.)

Case requirements: complete with classification idea and subcontracting idea

code:
Add (in StudentController class: customer reception):

private void addStudent(){
        //Scope of promotion id
        String id;
        //Receive student information
        //Judge whether the student number is repeated
        while (true) {
            System.out.println("Please enter student ID:");
            id = sc.next();
            //Assign the isExists method judgment of the business layer
            boolean result = studentService.isExists(id);
            //Judge the return result
            if (result) {
                //If yes, an error message appears
                System.out.println("Student number already exists,Please re-enter!");
            } else {
                //Release
                break;
            }
        }
    }
(stay StudentDao Class: warehouse management)
public boolean addStudent(Student stu){
/*
            Add students' ideas:
                Where the index is null, it is added
                Define index as - 1, assuming that the array is full and there are no null elements
                Traverse to get each element and judge whether it is null
                If it is null, let the index record the location index and end the loop to get the index of the location to be stored
         */
        int index=-1;
        for (int i = 0; i < stuArray.length; i++) {
            // If null is found
            if(stuArray[i] == null){
                // Record index
                index = i;
                // End cycle
                break;
            }
        }
        // Judgment index
        if (index == -1) {
            // If it is still - 1, it means that there is no null, the memory is full, and false is returned
            return false;
        } else {
            // If there is a position, add the student object to the array and return true
            stuArray[index] = stu;
            return true;
        }
    }

Delete (in StudentController class: customer reception):

/*
           Delete student
        */
    private void deleteStudentById(){
        // Assign the Service layer and call findAllStudent() to get the student array
        Student[] stuArray = studentService.findAllStudent();
        // Determine array address
        if (stuArray == null) {
            System.out.println("No students have been added to the array, please add them first");
            //Stop method
            return;
        }
        //Scope of promotion id
        String id;
        while (true) {
            System.out.println("Please enter the student ID to delete:");
            id = sc.next();
            //Judge student number
            boolean result = studentService.isExists(id);
            if (!result) {
                //If it does not exist, an error message will appear (it cannot be deleted if it does not exist)
                System.out.println("The student number entered does not exist,Please re-enter");
            } else {
                //Release
                break;
            }
        }

    }
(stay StudentDao Class: warehouse management)

    /*
        Delete student
    */
    public void deleteStudentById(String id) {
        // Find the index of the object according to the student number
        int index = getIndex(id);
        // Replace the object at the index position with null
        stuArray[index] = null;
    }

Change (in StudentController class: customer reception):

   /*
           Modify student
        */
    private void updateStudentById() {
        // Assign the Service layer and call findAllStudent() to get the student array
        Student[] stuArray = studentService.findAllStudent();
        // If the returned value is null, it means there is no data and cannot be deleted
        if (stuArray == null) {
            System.out.println("No students have been added to the array,Please add first");
            // Stop method
            return;
        }
        //Scope of promotion id
        String id;
        while (true) {
            System.out.println("Please enter the student ID to be modified:");
            id = sc.next();
            //Judge student number
            boolean result = studentService.isExists(id);
            if (!result) {
                //If it does not exist, an error message will appear (it cannot be modified if it does not exist)
                System.out.println("The student number entered does not exist,Please re-enter");
            } else {
                //Release
                break;
            }
        }
    }
(stay StudentDao Class: warehouse management)

Check (in StudentController class: customer reception):

Insert the code slice here
Insert the code slice here

–5. Environment construction

  1. Menu building
  1. Basic added functions - thought analysis
  1. Basic add function - code implementation
  1. Add function - add student number judgment
  1. Add function - problem analysis
  1. static keyword features
  1. static precautions - student number problems and Solutions
  1. View student - code implementation
  1. Delete function - code implementation
  1. Modification function - code implementation
  1. Code optimization of student management system

Topics: Java