If P is pressed, ask the user to type his rate (pay) per hour and the number of hours he worked for the entire month separated by a space. Then, display his name and wage.
import java.io.IOException;
import java.util.Scanner;
class Employee{
String name;
Employee(String name){
this.name = name;
}
}
class FullTimeEmployee{
public void output(Employee emp, double monthlySalary) {
System.out.print( "Name: " + emp.name +
"\nMonthly salary: " + monthlySalary);
}
}
class PartTimeEmployee{
public void output(Employee emp, double monthlySalary) {
System.out.print( "Name: " + emp.name +
"\nMonthly salary: " + monthlySalary);
}
}
public class RunEmployee {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.print("Enter name employee: ");
String name = in.nextLine();
Employee empl = new Employee(name);
System.out.print("Select \"f\" (Full-time) / \"p\" (Part-time): ");
char ch = (char) System.in.read();
FullTimeEmployee fte = new FullTimeEmployee();
PartTimeEmployee pte = new PartTimeEmployee();
if(ch == 'f' | ch == 'F'){
System.out.print("Enter monthly salary: ");
int monthlySalary = in.nextInt();
fte.output(empl, monthlySalary);
} else if(ch == 'p' | ch == 'P'){
System.out.println("Enter the rate per hour and the number of hours worked per month separated by a space: ");
String ent1 = in.nextLine();
String ent = in.nextLine();
String []nums = ent.split(" ");
double rate = Integer.parseInt(nums[0]);
double salary = Integer.parseInt(nums[1]);
double monthlySalary = rate * salary;
pte.output(empl, monthlySalary);
}
}
}
Comments
Leave a comment