In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章主要讲解了"Java怎么实现雇员管理小项目",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java怎么实现雇员管理小项目"吧!
工具:记事本,方便编译和演示
环境:将代码放在一个类当中了(这个习惯很不好)
目的:回顾知识,理解面向对象编程
代码实例(分四块,直接用即可)
1.导包
import java.util.ArrayList;import java.io.BufferedReader;import java.io.InputStreamReader;
2.测试类
public class GuanLi { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub // 创建EmpManage对象 EmpManage em = new EmpManage(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 做出一个简单的菜单 while (true) { System.out.println("请输入你要进行的操作:"); System.out.println("1:表示要添加一个雇员"); System.out.println("2:表示要查找一个雇员"); System.out.println("3:表示要修改雇员工资"); System.out.println("4:表示要删除一个雇员"); System.out.println("5:表示要您要退出操作"); String operType = br.readLine(); if (operType.equals("1")) { System.out.println("请输入编号"); String num = br.readLine(); System.out.println("请输入名字"); String name = br.readLine(); System.out.println("请输入工资"); float sal = Float.parseFloat(br.readLine()); // 此时此刻,经过输入信息,对象创建好 Emp2 emp = new Emp2(num, name, sal); // 将它添加进去 em.addEmp(emp); } else if (operType.equals("2")) { System.out.println("请输入编号"); String num = br.readLine(); em.showInfo(num); } else if (operType.equals("3")) { System.out.println("请输入编号"); String num = br.readLine(); System.out.println("请输入工资"); float sal = Float.parseFloat(br.readLine()); em.updateSal(num, sal); } else if (operType.equals("4")) { System.out.println("请输入编号"); String num = br.readLine(); em.delEmp(num); } else if (operType.equals("5")) { System.exit(0); } } }}
3.雇员管理类(包括对雇员信息的动态管理)
// 创建雇员管理类class EmpManage { // 定义集合类(属性) private ArrayList al = null; // 构造函数,初始化成员变量 public EmpManage() { al = new ArrayList(); } // 封装的方法 // 1.加入员工 public void addEmp(Emp2 emp) { al.add(emp); } // 2.显示员工相关信息 public void showInfo(String num) { // 遍历整个ArrayList for (int i = 0; i
< al.size(); i++) { // 取出Emp2对象 Emp2 emp = (Emp2) al.get(i); // 比较编号 if (emp.getNum().equals(num)) { System.out.println("找到该员工,他的信息是:"); System.out.println("编号:" + emp.getNum()); System.out.println("名字:" + emp.getName()); System.out.println("工资:" + emp.getSal()); } } } // 3.修改员工工资(依照编号来修改工资) // 即,第一个参数是编号;第二个是用户传进来"新的"薪水 public void updateSal(String num, float newSal) { // 遍历 for (int i = 0; i < al.size(); i++) { Emp2 emp = (Emp2) al.get(i); // 判断编号 if (emp.getNum().equals(num)) { // 修改薪水 emp.setSal(newSal); } } } // 4.删除某个员工 public void delEmp(String unm) { // 遍历 for (int i = 0; i < al.size(); i++) { Emp2 emp = (Emp2) al.get(i); if (emp.getNum().equals(unm)) { // 按编号删除 al.remove(i); // 按对象删除 // al.remove(emp); } } }} 4.雇员类(包括雇员的基本信息) // 创建雇员类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; } // 学号里面可能出现字母,所以定义为String private String num; private float sal; // 构造函数,一堆初始化的工作 public Emp2(String num, String name, float sal) { this.num = num; this.name = name; this.sal = sal; }} 在这里,雇员类和雇员管理类分开,方法、属性都进行了相应的封装,各司其职。 在测试类中,我们想用做什么,就调用其类中的方法即可,而不用操心方法是咋实现的呀,里面都有哪些函数呀,这即是很好的面向对象编程思想。When compiling in dos, it is found that all classes in.java files are compiled separately into.class files. After using the IDE, this was not noticed.
Thank you for reading, the above is "Java how to achieve employee management small projects" content, after the study of this article, I believe we have a deeper understanding of Java how to achieve employee management small projects, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.