When learning the collection framework, it is easy for beginners to practice exercises such as student management system and employee management system. Before learning the collection framework, you have basically learned the basic Java grammar, and the collection framework also tests the understanding of the previous learning from the side. The following is a practice question I have done before, review the collection framework and briefly introduce the idea of object-oriented programming. I found that when you look back, you will understand the old knowledge more thoroughly. Recently consolidated the foundation.
Tools: Notepad for easy compilation and demonstration
Environment: Put the code in a class (this habit is very bad)
Purpose: Review knowledge and understand object-oriented programming
Code instance (divided into four pieces, just use it directly)
1. Guide packet
import java.util.ArrayList;import java.io.BufferedReader;import java.io.InputStreamReader;
2. Testing class
public class GuanLi { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub // Create an EmpManage object EmpManage em = new EmpManage(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // Make a simple menu while (true) { System.out.println("Please enter the operation you want to perform:"); System.out.println("1: means you want to add an employee"); System.out.println("2: means to find an employee"); System.out.println("3: means to modify employee salary"); System.out.println("4: means to delete an employee"); System.out.println("5: means to ask you to exit the operation"); String operatorType = br.readLine(); if (operType.equals("1")) { System.out.println("Please enter a number"); String num = br.readLine(); System.out.println("Please enter a name"); String name = br.readLine(); System.out.println("Please enter salary"); float sal = Float.parseFloat(br.readLine()); // At this moment, after entering information, the object is created Emp2 emp = new Emp2(num, name, sal); // Add it to em.addEmp(emp); } else if (operType.equals("2")) { System.out.println("Please enter number"); String num = br.readLine(); em.showInfo(num); } else if (operType.equals("3")) { System.out.println("Please enter number"); String num = br.readLine(); System.out.println("Please enter salary"); float sal = Float.parseFloat(br.readLine()); em.updateSal(num, sal); } else if (operType.equals("4")) { System.out.println("Please enter number"); String num = br.readLine(); em.delEmp(num); } else if (operType.equals("5")) { System.exit(0); } } }}3. Employee management category (including dynamic management of employee information)
// Create employee management class EmpManage { // Define the collection class (properties) private ArrayList<Emp2> al = null; // Constructor, initialize member variable public EmpManage() { al = new ArrayList<Emp2>(); } // Encapsulated method// 1. Join employee public void addEmp(Emp2 emp) { al.add(emp); } // 2. Show employee-related information public void showInfo(String num) { // Traverse the entire ArrayList for (int i = 0; i < al.size(); i++) { // Take out the Emp2 object Emp2 emp = (Emp2) al.get(i); // Compare number if (emp.getNum().equals(num)) { System.out.println("Finding the employee, his message is: "); System.out.println("Note: " + emp.getNum()); System.out.println("Name: " + emp.getName()); System.out.println("Sal: " + emp.getSal()); } } } // 3. Modifying the employee's salary (modifying the salary according to the number) // That is, the first parameter is the number; the second is the "new" salary sent by the user public void updateSal(String num, float newSal) { // Traversal for (int i = 0; i < al.size(); i++) { Emp2 emp = (Emp2) al.get(i); // Judgment number if (emp.getNum().equals(num)) { // Modify salary emp.setSal(newSal); } } } // 4. Delete an employee public void delEmp(String unm) { // Traversal for (int i = 0; i < al.size(); i++) { Emp2 emp = (Emp2) al.get(i); if (emp.getNum().equals(unm)) { // Delete al.remove(i); // Delete by object// al.remove(emp); } } }}4. Employee category (including basic information about employees)
// Create employee class class Emp2 { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public float getSal() { return sal; } public void setSal(float sal) { this.sal = sal; } // Letters may appear in the student number, so it is defined as String private String num; private float sal; // Constructor, a bunch of initialization work public Emp2(String num, String name, float sal) { this.num = num; this.name = name; this.sal = sal; }}Here, the employee class and the employee management class are separated, and the methods and attributes are encapsulated accordingly, and each performs its own duties.
In the test class, if we want to use it, we can just call the methods in the class, without worrying about how the methods are implemented, and what functions are there. This is a good object-oriented programming idea.
When compiling in dos, it is found that all classes in the .java file will be compiled into .class files separately. After using the IDE, I didn't pay attention to this.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.