Java employees manage small projects and Java employees manage projects

Source: Internet
Author: User

Java employees manage small projects and Java employees manage projects

When learning the collection framework, it is easy for beginners to practice exercises such as the student management system and employee management system. Before learning the collection framework, basically all the basic Java syntaxes have been completed, and the Collection framework also tests its understanding of the previous learning. Next we will use an exercise that we have done, review the collection framework, and briefly introduce the object-oriented programming ideas. I found the old knowledge. when you look back, you will have a better understanding. Recently consolidated basic ing.

Tool: notepad for easy compilation and demonstration

Environment: Put the code in a class (this is a bad habit)

Objective: To review knowledge and understand Object-Oriented Programming

Code instance (in four parts, use it directly)

1. Import package

import java.util.ArrayList;import java.io.BufferedReader;import java.io.InputStreamReader;

2. Test class

Public class GuanLi {public static void main (String [] args) throws Exception {// TODO Auto-generated method stub // create EmpManage object EmpManage em = new EmpManage (); bufferedReader br = new BufferedReader (new InputStreamReader (System. in); // make a simple menu while (true) {System. out. println ("Enter the operation you want to perform:"); System. out. println ("1: indicates to add an employee"); System. out. println ("2: Indicates finding an employee"); System. out. println ("3: indicates the employee's salary to be modified"); System. out. println ("4: indicates to delete an employee"); System. out. println ("5: indicates that you want to exit the operation"); String operType = br. readLine (); if (operType. equals ("1") {System. out. println ("Enter the number"); String num = br. readLine (); System. out. println ("Enter the name"); String name = br. readLine (); System. out. println ("Enter the salary"); float sal = Float. parseFloat (br. readLine (); // at this moment, after entering the information, the object creates Emp2 emp = new Emp2 (num, name, sal); // Add it to em. addEmp (emp);} else if (operType. equals ("2") {System. out. println ("Enter the number"); String num = br. readLine (); em. showInfo (num);} else if (operType. equals ("3") {System. out. println ("Enter the number"); String num = br. readLine (); System. out. println ("Enter the salary"); float sal = Float. parseFloat (br. readLine (); em. updateSal (num, sal);} else if (operType. equals ("4") {System. out. println ("Enter the number"); String num = br. readLine (); em. delEmp (num);} else if (operType. equals ("5") {System. exit (0 );}}}}

3. Employee management (including dynamic management of employee information)

// Create the employee management class EmpManage {// define the collection class (attribute) private ArrayList <Emp2> al = null; // constructor, initialize the member variable public EmpManage () {al = new ArrayList <Emp2> () ;}// encapsulated method // 1. add employee public void addEmp (Emp2 emp) {al. add (emp);} // 2. show employee information public void showInfo (String num) {// traverse the entire ArrayList for (int I = 0; I <al. size (); I ++) {// retrieves the Emp2 object Emp2 emp = (Emp2) al. get (I); // compare the number if (emp. getNum (). equals (num) {System. out. println ("find this employee. His information is:"); System. out. println ("No.:" + emp. getNum (); System. out. println ("name:" + emp. getName (); System. out. println ("Salary:" + emp. getSal () ;}}// 3. modify the employee's salary (change the employee's salary according to the number) // that is, the first parameter is the number, and the second parameter is the "new" salary public void updateSal (String num, float newSal) {// traverse for (int I = 0; I <al. size (); I ++) {Emp2 emp = (Emp2) al. get (I); // judge the number if (emp. getNum (). equals (num) {// modify the salary of emp. setSal (newSal) ;}}// 4. delete an employee public void delEmp (String unm) {// traverse for (int I = 0; I <al. size (); I ++) {Emp2 emp = (Emp2) al. get (I); if (emp. getNum (). equals (unm) {// Delete al by number. remove (I); // Delete by object // al. remove (emp );}}}}

4. Employees (including basic information about employees)

// Create employee 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;} // The student ID may contain letters, 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 is separated from the employee management class, and the methods and attributes are encapsulated to perform their respective duties.

In the test class, we can call the methods in the class to do what we want, instead of worrying about how the methods are implemented. What functions are in the class, this is a good idea of object-oriented programming.

During compilation in dos, we found that all classes in the. java file will be compiled into the. class file separately. After using the IDE, I did not pay attention to this feature.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.