Java Program for employee and manager salary - IProgramX

Q.  Define a class Employee having private members – id, name, department, salary. Define default and parameterized constructors. Create a subclass called “Manager” with private member bonus. Define methods accept and display in both the classes. Create n objects of the Manager class and display the details of the manager having the maximum total salary (salary+bonus)


Program:
import java.io.*;
class Emp{
private int id;
private double salary;
private String name,dept;
double total;
double sal=salary;
public Emp(){
id=0;
salary=0.0;
name="";
dept="";
}
public Emp(int id,double salary, String name, String dept){
this.id=id;
this.salary=salary;
this.name=name;
this.dept=dept;
}
public void accept() throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter id of employee: ");
id=Integer.parseInt(br.readLine());
System.out.println("Enter name of employee: ");
name=br.readLine();
System.out.println("Enter salary of employee: ");
salary=Double.parseDouble(br.readLine());
System.out.println("Enter department of employee: ");
dept=br.readLine();
}
public double total(){
total=total+salary;
return total;
}
public void display(){
System.out.println("Emp Id: "+id);
System.out.println("Name: "+name);
System.out.println("Salary: "+salary);
System.out.println("Department: "+dept);
}
}

class Manager extends Emp{
private double bonus;
public void accept() throws IOException{
super.accept();
System.out.println("Enter Bonus of employee: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
bonus=Double.parseDouble(br.readLine());
super.total=bonus;
}
public void display(){
super.display();
System.out.println("Bonus: "+bonus);
System.out.println("Total Salary: "+total);
}
}

public class sa1 {
public static void main(String[] args) throws IOException{
Manager[] m=new Manager[10];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of employees: ");
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++){
m[i]=new Manager();
m[i].accept();
m[i].total();
}
for(int i=0;i<n;i++){
m[i].display();
System.out.println("---------------------------------");
}
double src=m[0].total;
int temp=0;
for(int i=1;i<n;i++){
if(src<m[i].total) 
{
src=m[i].total;
temp=i;
}
}
System.out.println("The Employee having the maximum Total salary is :");
m[temp].display();
}

}

Output:

Emp Id: 1
Name: Zuck
Salary: 30000.0
Department: HR
Bonus: 3000.0
Total Salary: 33000.0
---------------------------------
Emp Id: 2
Name: shrikant
Salary: 40000.0
Department: HH
Bonus: 4000.0
Total Salary: 44000.0
---------------------------------
Emp Id: 3
Name: madhvan
Salary: 10000.0
Department: JJ
Bonus: 1000.0
Total Salary: 11000.0
---------------------------------
The Employee having the maximum Total salary is :
Emp Id: 2
Name: shrikant
Salary: 40000.0
Department: HH
Bonus: 4000.0

Total Salary: 44000.0

Post a Comment

1 Comments


  1. Slip11_2: Write a program to accept the username and password from user if username and
    password are not same then raise "Invalid Password" with appropriate msg.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class InvalidPasswordException extends Exception
    {}
    class Userpassword extends JFrame implements ActionListener
    {
    JLabel name, pass;
    JTextField nameText;
    JPasswordField passText;
    JButton login, end;
    static int cnt=0;
    Userpassword()
    {
    name = new JLabel("Name : ");
    pass = new JLabel("Password : ");
    nameText = new JTextField(20);
    passText = new JPasswordField(20);
    login = new JButton("Login");
    end = new JButton("End");
    login.addActionListener(this);
    end.addActionListener(this);
    setLayout(new GridLayout(3,2));
    add(name);
    add(nameText);
    add(pass);
    add(passText);
    add(login);
    add(end);
    setTitle("Login Check");
    setSize(300, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    }
    public void actionPerformed (ActionEvent e)
    {
    if(e.getSource()==end)
    {
    System.exit(0);
    }
    if(e.getSource()==login)
    {
    try
    {
    String user = nameText.getText();
    String pass = new String(passText.getPassword());
    if(user.compareTo(pass)==0)
    {
    JOptionPane.showMessageDialog(null, "Login Successful",
    "Login", JOptionPane. INFORMATION_MESSAGE);
    System.exit(0);
    }
    else
    {
    throw new InvalidPasswordException();
    }
    }
    catch(Exception e1)

    {
    cnt++;
    JOptionPane.showMessageDialog(null, "Login Failed", "Login",
    JOptionPane.ERROR_MESSAGE);
    nameText.setText("");
    passText.setText("");
    nameText.requestFocus();
    if(cnt==3)
    {
    JOptionPane.showMessageDialog(null,"3 Attempts Over",
    "Login", JOptionPane.ERROR_MESSAGE);
    System.exit(0);
    }
    }
    }
    }
    public static void main(String args[])
    {
    new Userpassword();
    }
    }

    ReplyDelete