java第15周作业_Homework0611[通俗易懂]

java第15周作业_Homework0611[通俗易懂]————————————————————————————————————————————~~~~Cola公司的雇员分为以下若干类:(知识点:多态)[必做题]4.1ColaEmployee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(intmont

大家好,欢迎来到IT知识分享网。

————————————————————————————————————

————————~~~~

Cola公司的雇员分为以下若干类: (知识点:多态)[必做题]

4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法: getSalary(int month)根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。

4.2 SalariedEmployee: ColaEmployee的子类,拿固定工资的员工。属性:且薪gramming Your Future果后作业

4.3 HourlyEmployee : ColaEmployee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数

4.4 SalesEmployee : ColaEmployee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率

4.5定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee数组里,并单元出数组中每个员工当月的T

———–测试类———

package homework0611_01;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class TestCompany {
    public static void main(String[] args) {
        ColaEmployee salariedEmployee = new SalariedEmployee("Kiana", 12, 23333.3);
        ColaEmployee hourlyEmployee = new HourlyEmployee("Mei", 4, 100, 180);
        ColaEmployee salesEmployee = new SalesEmployee("Bronya", 8, 166667.6, 0.12);

        ArrayList<ColaEmployee> colaEmployeeArrayList = new ArrayList<ColaEmployee>();
        colaEmployeeArrayList.add(salariedEmployee);
        colaEmployeeArrayList.add(hourlyEmployee);
        colaEmployeeArrayList.add(salesEmployee);

        Scanner s = new Scanner(System.in);
        while (true) {
            System.out.println("请输入需要查询的月份");
            int month = s.nextInt();
            Iterator<ColaEmployee> iterator = colaEmployeeArrayList.iterator();
            while (iterator.hasNext()) {
                try {
                    Company.printSalary(month, iterator.next());
                }
                catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
            System.out.println("想继续查询么?yes or no");
            String flag = s.next();
            if ("yes".equals(flag)) {
                continue;
            }
            else if ("no".equals(flag)) {
                System.out.println("已退出");
                break;
            }
            else {
                System.out.println("没有检测到有效字符,默认退出");
                break;
            }
        }
    }
}

————–父类—————-

package homework0611_01;

public abstract class ColaEmployee {
    private String name;
    private int month;

    public ColaEmployee() {
    }

    public ColaEmployee(String name, int month) {
        this.name = name;
        this.month = month;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public boolean MyMonthException(int month) {
        return month < 1 || month > 12;
    }
    public abstract double getSalary(int month) throws Exception;

    public abstract double eleSalary();
}

  ————-三个子类————-

1.

package homework0611_01;

public class SalariedEmployee extends ColaEmployee{
    private double monthSalary;

    public SalariedEmployee() {
    }

    public SalariedEmployee(String name, int month, double monthSalary) {
        super(name, month);
        this.monthSalary = monthSalary;
    }

    public double getMonthSalary() {
        return monthSalary;
    }

    public void setMonthSalary(double monthSalary) {
        this.monthSalary = monthSalary;
    }

    @Override
    public double getSalary(int month) throws Exception{
        if(super.MyMonthException(month)){
            throw new Exception("月份输入错误异常");
        }
        return month == super.getMonth() ? this.monthSalary + 100 : this.monthSalary;
    }

    @Override
    public double eleSalary() {
        return this.monthSalary;
    }
}

  2.

package homework0611_01;

public class HourlyEmployee extends ColaEmployee{
    private double hourSalary;
    private int hourMonth;

    public HourlyEmployee() {
    }

    public HourlyEmployee(String name, int month, double hourSalary, int hourMonth) {
        super(name, month);
        this.hourSalary = hourSalary;
        this.hourMonth = hourMonth;
    }

    @Override
    public double getSalary(int month) throws Exception{
        if(super.MyMonthException(month)){
            throw new Exception("月份输入错误异常");
        }
        return month == super.getMonth() ? eleSalary() + 100 : eleSalary();
    }

    @Override
    public double eleSalary() {
        return this.hourMonth > 160 ? this.hourSalary * this.hourMonth + ((this.hourMonth - 160) * this.hourSalary * 0.5) :
                this.hourMonth * this.hourSalary;
    }
}

  3.

package homework0611_01;

public class SalesEmployee extends ColaEmployee{
    private double saleMonth;
    private double rate;

    public SalesEmployee() {
    }

    public SalesEmployee(String name, int month, double saleMonth, double rate) {
        super(name, month);
        this.saleMonth = saleMonth;
        this.rate = rate;
    }

    @Override
    public double getSalary(int month) throws Exception{
        if(super.MyMonthException(month)){
            throw new Exception("月份输入错误异常");
        }
        return month == super.getMonth() ? this.eleSalary() + 100 : this.eleSalary();
    }

    @Override
    public double eleSalary() {
        return this.saleMonth * rate;
    }
}

  ———–公司类————

package homework0611_01;

public class Company {
    public static void printSalary(int month, ColaEmployee colaEmployee) throws Exception{
        System.out.println(colaEmployee.getName() + "\t" + colaEmployee.getMonth() + "月的工资是" + colaEmployee.getSalary(month));
    }
}

  —————-输出结果—————

java第15周作业_Homework0611[通俗易懂]

————————————————————————————————————————

利用接口实现动态的创建对象[选做题]
5.1 创建4个类:
•苹果
•香蕉
•葡萄
•园丁
5.2 在三种水果的构造方法中打印一句话.
•以苹果类为例
class apple{
  public apple(){
    System.out.println(―创建了一个苹果类的对象‖);
  }
}
5.3 要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象
 
—————测试类——————
package homework0611_02;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Gardener gardener = new Gardener();
        Scanner s = new Scanner(System.in);
        while(true) {
            System.out.println("请输入想要创造的水果");
            try {
                gardener.create(s.next());
            }
            catch (Exception e) {
                System.out.println(e.getMessage());
            }
            System.out.println("还想要继续创建么?yes or no");
            String flag = s.next();
            if ("yes".equals(flag)) {
                continue;
            }
            else if ("no".equals(flag)) {
                System.out.println("已退出");
                break;
            }
            else {
                System.out.println("没有检测到有效字符,默认退出");
                break;
            }
        }
    }
}

  

  ————–接口————
package homework0611_02;

public interface Fruit {

}

  ————-三个实现类————

1.

package homework0611_02;

public class Apple implements Fruit{
    public Apple() {
        System.out.println("创建了一个苹果的对象");
    }
}

  2.

package homework0611_02;

public class Oranges implements Fruit{
    public Oranges() {
        System.out.println("创建了一个橘子的对象");
    }
}

  3.

package homework0611_02;

public class Pear implements Fruit{
    public Pear() {
        System.out.println("创建了一个梨的对象");
    }
}

  —————园丁类—————–

package homework0611_02;

public class Gardener {
    public void create(String fruit) throws Exception{
      if("apple".equals(fruit)) {
          Fruit apple = new Apple();
      }else if("oranges".equals(fruit)) {
          Fruit oranges = new Oranges();
      }else if("pear".equals(fruit)) {
          Fruit pear = new Pear();
      }else {
          throw new Exception("没有这个水果");
      }
    }
}

  ———–输出结果————

java第15周作业_Homework0611[通俗易懂]

 

——————————————————————————————————————————————

————————————————————————————
2021年6月11日作业完成

 

 安

 

java第15周作业_Homework0611[通俗易懂]

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/29210.html

(0)
上一篇 2023-09-29 14:33
下一篇 2023-09-29 16:33

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信