赞
踩
创建一个工人类,属性︰姓名、年龄、工资,要求如下︰ (1)为Worker提供无、有参数的构造方法,属性私有,并提供公开的get/set(2)创建多个Worker类,存储在List集合中。 (3)打印输出所有的工人信息。 (4)计算所有工人的平均工资。 (5)打印输出姓名中包含"胡”的所有工人信息。 (6)打印输出所有姓"胡”的工人信息。 (7)键盘输入一个姓名,查找是否存在此员工,存在,则打印输出员工的信息,如果不存在,则输出"查无此人" (8输入一个工资,查询工资大于输入工资的员工信息。
-
- public class Worker {
- private String name;
- private int age;
- private double salary;//工资
-
- public Worker() {
- }
-
- public Worker(String name, int age, double salary) {
- this.name = name;
- this.age = age;
- this.salary = salary;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public double getSalary() {
- return salary;
- }
-
- public void setSalary(double salary) {
- this.salary = salary;
- }
-
- @Override
- public String toString() {
- return "姓名:'" + name + '\'' +
- ", 年龄:" + age +
- ", 工资:" + salary;
- }
- }
-
- class TextWorker {
- public static void main(String[] args) {
- //创建多个Worker对象
- Worker worker1 = new Worker("胡有德", 23, 5500);
- Worker worker2 = new Worker("张胡有", 23, 4000);
- Worker worker3 = new Worker("张三", 23, 6000);
- Worker worker4 = new Worker("李四", 23, 3999);
- //将多个Worker类对象存储在List集合中
- ArrayList<Worker> workers = new ArrayList<>();
- workers.add(worker1);
- workers.add(worker2);
- workers.add(worker3);
- workers.add(worker4);
- //打印输出所有工人信息
- System.out.println("-----所有工人信息为:------");
- for (Worker worker : workers) {
- System.out.println(worker);
- }
- //计算所有工人的平均工资
- System.out.println("-----工人的平均工资为:-----");
- double averageSalary = 0;
- for (Worker worker : workers) {
- averageSalary += worker.getSalary();
- }
- System.out.println(averageSalary / workers.size() + "\t");
- //打印输出姓名中包含”胡“的所有工人信息
- System.out.println("-----姓名中含有胡字的为:-----");
- for (Worker worker : workers) {
- String name = worker.getName();//找到每个工人的姓名
- char[] charsName = name.toCharArray();//将字符串转化为数组
- for (int i = 0; i < charsName.length; i++) {//遍历循环找到胡字
- if (charsName[i] == '胡') {
- System.out.println(worker);
- }
- }
- }
- //打印输出姓”胡“的所有工人信息
- System.out.println("-----姓”胡“的所有工人信息:-----");
- for (Worker worker : workers) {
- String name = worker.getName();//找到每个工人的姓名
- char[] charsName = name.toCharArray();//将字符串转化为数组
- if (charsName[0] == '胡') {
- System.out.println(worker);
- }
- }
- //查询
- System.out.println("请输入要查询的员工:");
- Scanner sc = new Scanner(System.in);
- String na = sc.next();
- int index = -1;
- for (Worker worker : workers) {
- String name = worker.getName();//找到每个工人的姓名
- if (name.equals(na)) {
- System.out.println(worker);
- index = 1;
- }
- }
- if (index == -1) {
- System.out.println("查无此人!");
- }
- //查询工资
- System.out.println("请输入要查询的工资:");
- int index1 = 0;
- //判断用户输入的数据合理性
- try {
- double scal = sc.nextDouble();
- for (Worker worker : workers) {
- double salary = worker.getSalary();//找到每个工人的工资
- if (salary >= scal) {
- System.out.println(worker);
- index1++;
- }
- }
- if (index == 0) {
- System.out.println("没有员工的工资大于" + scal);
- } else {
- System.out.println("共计" + index1 + "条员工信息。");
- }
- } catch (Exception e) {
- System.out.println("你输入的不是数字!");
- }
-
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。