大学Java实验二
1、假定要为某个公司编写雇员工资支付程序。这个公司有各种类型的雇员,不同类型的雇员按不同的方式支付工资:
经理(Manager)每月获得一份固定的工资;
销售人员(Salesman)在基本工资的基础上每月还有销售提成;
一般工人(Worker)则按他每月工作的天数计算工资。
(提示:设计一个抽象类――雇员(Employee)描述雇员的共同特性,例如姓名(name)等。
这个类还应提供一个计算工资的抽象方法computeSalary(),使得可以通过为个类计算所有雇员的工资。
设计经理(Manager)类、销售人员(Salesman)类、一般工人(Worker)类,再设计一个演示用的类。)
Employee.java
package shiyan2.first;
import java.util.Scanner;
public abstract class Employee {
String name;
String position;
String info = new String();
Scanner _order = new Scanner(System.in);
public abstract void getInfo();
public abstract double computeSalary();
public abstract void setInfo();
}
Manager.java
package shiyan2.first;
public class Manager extends Employee {
double guDingGongZi = 8000;// 设计经理的固定工资
@Override
public double computeSalary() {
return guDingGongZi;
}
@Override
public void setInfo() {
position = "设计经理";
System.out.print("请输入此" + position + "的姓名:");
name = _order.next();
}
@Override
public void getInfo() {
info = "/n/t"
+ position
+ name
+ "的本月薪水为:"
+ computeSalary()
+ "元/n/t其中:固定工资为"
+ computeSalary()
+ "元/n/n/t#########################如果遗漏或疑问请在三个工作日内与财务处联系#########################/n/n/n";
System.out.print(info);
}
}
Salesman.java
package shiyan2.first;
public class Salesman extends Employee {
double saleNum;// 销售金额
double tiCheng = 0.3;// 提成比例
double jiBenGongZi = 3000;// 基本工资
@Override
public double computeSalary() {
return jiBenGongZi + saleNum * tiCheng;
}
@Override
public void setInfo() {
position = "销售人员";
System.out.print("请输入此" + position + "的姓名:");
name = _order.next();
System.out.print("请输入此" + position + "本月的销售金额:");
saleNum = _order.nextDouble();
if(saleNum<0){
System.out.println("/n/t#########一个月的销售额为" + saleNum
+ "元不符合实际!/t#########");
}
}
@Override
public void getInfo() {
info = "/n/t"
+ position
+ name
+ "的本月薪水为:"
+ computeSalary()
+ "元/n/t其中:基本工资为 "
+ jiBenGongZi
+ "元/t销售金额为 "
+ saleNum
+ "元/t提成比例为 "
+ tiCheng
+ "/t提成金额为 "
+ saleNum*tiCheng
+ "元/n/n/t#########################如果遗漏或疑问请在三个工作日内与财务处联系#########################/n/n/n";
System.out.print(info);
}
}
Worker.java
package shiyan2.first;
public class Worker extends Employee {
double workDayNum;// 工作的天数,可以有不足一天的情况
double daySalary = 30;// 每天的工资
@Override
public double computeSalary() {
return workDayNum * daySalary;
}
@Override
public void setInfo() {
position = "一般工人";
System.out.print("请输入此" + position + "的姓名:");
name = _order.next();
System.out.print("请输入此" + position + "本月的工作天数:");
workDayNum = _order.nextDouble();
}
@Override
public void getInfo() {
if (workDayNum < 0 || workDayNum > 31) {
System.out.println("/n/t#########/t一个月工作" + workDayNum
+ "天不符合实际!/t#########/n/n/n");
} else {
info = "/n/t"
+ position
+ name
+ "的本月薪水为:"
+ computeSalary()
+ "元/n/t其中:每日工资为 "
+ daySalary
+ "元/t工作天数为 "
+ workDayNum
+ "天/n/n/t#########################如果遗漏或疑问请在三个工作日内与财务处联系#########################/n/n/n";
System.out.print(info);
}
}
}
SalaryManager.java
package shiyan2.first;
import java.util.InputMismatchException;
import java.util.Scanner;
public class SalaryManager {
static boolean flag = true; // flag用来判断程序是否该结束,false表是该退出,true表示不要退出
int order;
Scanner _order = new Scanner(System.in);// 全局变量
Manager manager = new Manager();
Salesman salesman = new Salesman();
Worker worker = new Worker();
public SalaryManager() {// 构造函数
}
public void isExit() {
System.out
.print("/t*****************************************************************/n");
System.out.print("/t/t/t#/t 1---继续/t #/n");
System.out.print("/t/t/t#/t 0---退出/t/t #/n");
System.out
.print("/t*****************************************************************/n");
System.out.print("请选择你的操作:");
order = _order.nextInt();
if (order == 0) {
flag = false;
} else if (order == 1) {
flag = true;
} else {
System.out.println("/n/t#########/t" + order
+ "是非法命令/t#########/n/n/n");
}
}
public void showTips() {
System.out
.print("/t*****************************************************************/n");
System.out.print("/t/t/t#/t 1---发放设计经理的工资/t #/n");
System.out.print("/t/t/t#/t 2---发放销售人员的工资/t #/n");
System.out.print("/t/t/t#/t 3---发放一般工人的工资/t #/n");
System.out.print("/t/t/t#/t 0---退出程序/t/t #/n");
System.out
.print("/t*****************************************************************/n");
System.out.print("请选择你的操作:");
order = _order.nextInt();
if (order == 1) {
manager.setInfo();
manager.getInfo();
} else if (order == 2) {
salesman.setInfo();
salesman.getInfo();
} else if (order == 3) {
worker.setInfo();
worker.getInfo();
} else if (order == 0) {
flag = false;
System.out.println("/n/n/n/t/t/t# /t成功退出程序!/t/t #/n");
} else {
System.out
.println("/t/t提示:" + order + "不是本系统的操作代号,请确认后重新来过!/n/n/n");
}
}
public static void main(String[] args) {
SalaryManager salarymanager = new SalaryManager();
while (flag) {
try {
salarymanager.showTips();
if (flag == true) {
salarymanager.isExit();
}
} catch (InputMismatchException nfe) {
System.out.println("/n/t#########/t" + salarymanager.order
+ "是非法的,请确认后重新来过!/t#########/n/n");
//salarymanager.isExit();
}
}
}
}
2、
在前面的基础上对雇员工资计算程序进行扩充,即将一般雇员再细分为计时雇员(DailyWorker)和计件雇员(PieceWorker)两类,
计时雇员按天计算工资额,而计件雇员按其生产的产品件数计算工资额。
(提示:设计这两个类,与前面的程序结合在一起,修改一下演示程序,演示这些类的用法。)
Employee.java
package shiyan2.second;
import java.util.Scanner;
public abstract class Employee {
String name;
String position;
String info = new String();
Scanner _order = new Scanner(System.in);
public abstract void getInfo();
public abstract double computeSalary();
public abstract void setInfo();
}
Worker.java
package shiyan2.second;
public abstract class Worker extends Employee {
double workDayNum;// 工作的天数,可以有不足一天的情况
double daySalary = 30;// 每天的工资
@Override
public double computeSalary() {
return workDayNum * daySalary;
}
@Override
public void setInfo() {
position = "一般工人(计时雇员)";
System.out.print("请输入此" + position + "的姓名:");
name = _order.next();
System.out.print("请输入此" + position + "本月的工作天数:");
workDayNum = _order.nextDouble();
}
@Override
public void getInfo() {
if (workDayNum < 0 || workDayNum > 31) {
System.out.println("/n/t#########/t一个月工作" + workDayNum
+ "天不符合实际!/t#########/n/n/n");
} else {
info = "/n/t"
+ position
+ name
+ "的本月薪水为:"
+ computeSalary()
+ "元/n/t其中:每日工资为 "
+ daySalary
+ "元/t工作天数为 "
+ workDayNum
+ "天/n/n/t############如果遗漏或疑问请在三个工作日内与财务处联系############/n/n/n";
System.out.print(info);
}
}
}
DailyWorker.java
package shiyan2.second;
public class DailyWorker extends Worker {
public DailyWorker() {
}
}
PieceWorker.java
package shiyan2.second;
public class PieceWorker extends Worker {
double finshNum;
double singleMonery = 12;
public PieceWorker() {
}
public double computeSalary() {
return finshNum * singleMonery;
}
@Override
public void setInfo() {
position = "一般工人(计件雇员)";
System.out.print("请输入此" + position + "的姓名:");
name = _order.next();
System.out.print("请输入此" + position + "本月生产的产品件数:");
finshNum = _order.nextInt();
}
@Override
public void getInfo() {
if (finshNum < 0) {
System.out.println("/n/t#########/t一个月生产" + workDayNum
+ "件产品不符合实际!/t#########/n/n/n");
} else {
info = "/n/t"
+ position
+ name
+ "的本月薪水为:"
+ computeSalary()
+ "元/n/t其中:生产一件产品的工资为 "
+ singleMonery
+ "元/t生产的产品数量为 "
+ finshNum
+ "元/n/n/t############如果遗漏或疑问请在三个工作日内与财务处联系############/n/n/n";
System.out.print(info);
}
}
}
Salesman.java
package shiyan2.second;
public class Salesman extends Employee {
double saleNum;// 销售金额
double tiCheng = 0.3;// 提成比例
double jiBenGongZi = 3000;// 基本工资
@Override
public double computeSalary() {
return jiBenGongZi + saleNum * tiCheng;
}
@Override
public void setInfo() {
position = "销售人员";
System.out.print("请输入此" + position + "的姓名:");
name = _order.next();
System.out.print("请输入此" + position + "本月的销售金额:");
saleNum = _order.nextDouble();
if(saleNum<0){
System.out.println("/n/t#########一个月的销售额为" + saleNum
+ "元不符合实际!/t#########");
}
}
@Override
public void getInfo() {
info = "/n/t"
+ position
+ name
+ "的本月薪水为:"
+ computeSalary()
+ "元/n/t其中:基本工资为 "
+ jiBenGongZi
+ "元/t销售金额为 "
+ saleNum
+ "元/t提成比例为 "
+ tiCheng
+ "/t提成金额为 "
+ saleNum*tiCheng
+ "元/n/n/t#########################如果遗漏或疑问请在三个工作日内与财务处联系#########################/n/n/n";
System.out.print(info);
}
}
SalaryManager.java
package shiyan2.second;
import java.util.InputMismatchException;
import java.util.Scanner;
public class SalaryManager {
static boolean flag = true; // flag用来判断程序是否该结束,false表是该退出,true表示不要退出
int order;
Scanner _order = new Scanner(System.in);// 全局变量
Manager manager = new Manager();
Salesman salesman = new Salesman();
DailyWorker dailyworker = new DailyWorker();
PieceWorker pieceworker = new PieceWorker();
public SalaryManager() {// 构造函数
}
public void whichWorker() {
System.out
.print("/t*****************************************************************/n");
System.out.print("/t/t/t#/t 1---计时雇员/t #/n");
System.out.print("/t/t/t#/t 2---计件雇员/t #/n");
System.out.print("/t/t/t#/t 0---退出/t/t #/n");
System.out
.print("/t*****************************************************************/n");
System.out.print("请选择你的操作:");
order = _order.nextInt();
if (order == 1) {
dailyworker.setInfo();
dailyworker.getInfo();
} else if (order == 2) {
pieceworker.setInfo();
pieceworker.getInfo();
} else if (order == 0) {
flag = false;
System.out.println("/t/t/t/n/n/n#/t 成功退出程序!/t/t #/n");
} else {
System.out
.println("/t/t提示:" + order + "不是本系统的操作代号,请确认后重新来过!/n/n/n");
}
}
public void isExit() {
System.out
.print("/t*****************************************************************/n");
System.out.print("/t/t/t#/t 1---继续/t #/n");
System.out.print("/t/t/t#/t 0---退出/t/t #/n");
System.out
.print("/t*****************************************************************/n");
System.out.print("请选择你的操作:");
order = _order.nextInt();
if (order == 0) {
flag = false;
System.out.println("/n/n/n/t/t/t# /t成功退出程序!/t/t #/n");
} else if (order == 1) {
flag = true;
} else {
System.out.println("/n/t#########/t" + order
+ "是非法命令/t#########/n/n/n");
}
}
public void showTips() {
System.out
.print("/t*****************************************************************/n");
System.out.print("/t/t/t#/t 1---发放设计经理的工资/t #/n");
System.out.print("/t/t/t#/t 2---发放销售人员的工资/t #/n");
System.out.print("/t/t/t#/t 3---发放一般工人的工资/t #/n");
System.out.print("/t/t/t#/t 0---退出程序/t/t #/n");
System.out
.print("/t*****************************************************************/n");
System.out.print("请选择你的操作:");
order = _order.nextInt();
if (order == 1) {
manager.setInfo();
manager.getInfo();
} else if (order == 2) {
salesman.setInfo();
salesman.getInfo();
} else if (order == 3) {
whichWorker();
} else if (order == 0) {
flag = false;
System.out.println("/n/n/n/t/t/t# /t成功退出程序!/t/t #/n");
} else {
System.out
.println("/t/t提示:" + order + "不是本系统的操作代号,请确认后重新来过!/n/n/n");
}
}
public static void main(String[] args) {
SalaryManager salarymanager = new SalaryManager();
while (flag) {
try {
salarymanager.showTips();
if (flag == true) {
salarymanager.isExit();
}
} catch (InputMismatchException nfe) {
System.out.println("/n/t#########/t" + salarymanager.order
+ "是非法的,请确认后重新来过!/t#########/n/n");
// salarymanager.isExit();
}
}
}
}