package employee;
public class Employee {
//员工私有属性
private String name; //员工姓名
private int age; //员工年龄
private String position; //员工职位
private int salary; //工资
/**
* 给所有的属性指定初始值
* @param name
* @param age
* @param position
* @param salary
*/
public Employee(String name,int age,String position,int salary){
this.name = name;
this.age =age;
this.position = position;
this.salary = salary;
}
/**
* 给name属性赋值
* @param name
*/
public void setName(String name){
this.name = name;
}
public String getName() {
return name;
}
/**
* 给age属性赋值
* @param age
*/
public void setAge(int age){
if(age<18){
this.age=18;
System.out.println("当年龄无效时,默认为18");
}else{
this.age =age;
}
}
public int getAge() {
return age;
}
/**
* 给position属性赋值
* @param position
*/
public void setPosition(String position){
if(position.equals("售后服务") || position.equals("销售员") ){
this.position = position;
}else{
this.position = "售后服务";
System.out.println("输入不符合要求,默认为售后服务");
}
}
public String getPosition() {
return position;
}
/**
* 给员工工资赋值
* @param salary
*/
public void setSalary(){
if(age>=18 && age<=20){
this.salary = 1000;
}else if(age>=21 && age<=25){
this.salary = 1500;
}else if(age>=26 && age<=30){
this.salary = 2000;
}else if(age>=31 && age<=40){
this.salary = 3000;
}else if(age>=41 && age<=50){
this.salary = 3500;
}else if(age>=51){
this.salary = 4000;
}else{
System.out.println("没有设置年龄或者年龄无效");
}
}
public int getSalary() {
return salary;
}
}
测试类
package employee;
import java.util.Scanner;
public class TestEmployee {
public static void main(String[] args){
Employee emp1 = new Employee(null, 0, null, 0) ;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个员工姓名");
//获取输入的名字
String name1 = sc.next();
emp1.setName(name1);
System.out.println("请输入第一个员工年龄");
//获取输入的年龄
int age1 = sc.nextInt();
emp1.setAge(age1);
System.out.println("请输入第一个员工职位");
//获取输入的职位
String position1 = sc.next();
emp1.setPosition(position1);
emp1.setSalary();
System.out.println("---------------------------------");
System.out.println("员工1姓名为:"+emp1.getName());
System.out.println("年龄:"+emp1.getAge());
System.out.println("工作为:"+emp1.getPosition());
System.out.println("工资为:"+emp1.getSalary());
}
}
第一次回答问题,玩玩而已但是还是希望采纳
温馨提示:答案为网友推荐,仅供参考