forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Array_of_objects_inheritance.java
158 lines (146 loc) · 5.05 KB
/
Array_of_objects_inheritance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Here, the concept of java inheritance is implied by using constructors,objects and using
array of objects.
Most of the code available on inheritance and not based on array of objects method and
hence is not sufficient to cover the overall concepts related to inheritance method. This
will also help you in familiarising array of objects method which is very beneficial for
working with java.
*/
import java.util.Scanner;
class Employee
{
String Name,Phonenumber,Address;
int Age,Basicpay,HouseRentAllowance,TravelAllowance,Bonus,PensionFund,Insurance,IncomeTax;
int deduction,addition,totalsalary,netsalary;
Scanner sc = new Scanner(System.in);
Employee()
{
System.out.print("\nName : ");
this.Name = sc.nextLine();
System.out.print("\nAge : ");
this.Age = sc.nextInt();
sc.nextLine();
System.out.print("\nPhone number : ");
this.Phonenumber = sc.nextLine();
/*System.out.print("\nBasic pay : ");
this.Basicpay = sc.nextInt();*/
System.out.print("\nAddress : ");
this.Address = sc.nextLine();
}
void calculate_salary()
{
HouseRentAllowance = Basicpay * 5/100;
TravelAllowance = Basicpay * 3/100;
Bonus = Basicpay * 2/100;
PensionFund = Basicpay * 10/100;
Insurance = Basicpay * 3/100; //Insurance amount is user choice.Here considered as 3% of Basic pay.
IncomeTax = Basicpay * 5/100;
deduction = PensionFund+IncomeTax+Insurance;
addition = HouseRentAllowance+TravelAllowance+Bonus;
totalsalary = Basicpay+addition;
netsalary = totalsalary-deduction;
}
void print_salary()
{
System.out.println("\nSalary details of "+Name+"\n");
System.out.println("Basic pay : "+Basicpay);
System.out.println("House Rent Allowance : "+HouseRentAllowance);
System.out.println("Travel Allowance : "+TravelAllowance);
System.out.println("Bonus : "+Bonus);
System.out.println("Pension Fund : "+PensionFund);
System.out.println("Insurance : "+Insurance);
System.out.println("Income Tax : "+IncomeTax);
System.out.println("Salary Addition: "+addition);
System.out.println("Salary Deduction: "+deduction);
System.out.println("Total Salary: "+totalsalary);
System.out.println("Net Salary: "+netsalary);
}
void display()
{
System.out.println("Name : "+this.Name);
System.out.println("Age : "+this.Age);
System.out.println("Phone number : "+this.Phonenumber);
System.out.println("Address : "+this.Address);
}
}
class Officer extends Employee
{
String Specialization;
Officer()
{
super();
System.out.print("\nBasic pay : ");
Basicpay = sc.nextInt();
sc.nextLine();
System.out.print("\nSpecialization : ");
Specialization = sc.nextLine();
}
void display()
{
super.display();
System.out.println("Specialization : "+this.Specialization);
super.calculate_salary();
}
}
class Manager extends Employee
{
String Department;
Manager()
{
super();
System.out.print("\nBasic pay : ");
Basicpay = sc.nextInt();
sc.nextLine();
System.out.print("\nDepartment : ");
Department = sc.nextLine();
}
void display()
{
super.display();
System.out.println("Department : "+this.Department);
super.calculate_salary();
}
}
public class Main
{
public static void main(String args[])
{
int i,n,m;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of Officers : ");
n = sc.nextInt();
Officer []officers = new Officer[n];
System.out.println("\n Enter Details of officers...");
for(i=0;i<n;i++)
{
System.out.println("\nDetails of officer "+(i+1));
Officer temp = new Officer();
officers[i] = temp;
}
System.out.println("\nEnter the number of Managers : ");
m = sc.nextInt();
Manager []managers = new Manager[m];
System.out.println("\n Enter Details of managers...");
for(i=0;i<m;i++)
{
System.out.println("\nDetails of manager "+(i+1));
Manager temp = new Manager();
managers[i] = temp;
}
System.out.println("\n\tEmployee Profile...");
System.out.println("\n Officer Details...\n");
for(i=0;i<n;i++)
{
System.out.println("\nOfficer "+(i+1));
officers[i].display();
officers[i].print_salary();
}
System.out.println("\n Manager Details...\n");
for(i=0;i<m;i++)
{
System.out.println("\nManager "+(i+1));
managers[i].display();
managers[i].print_salary();
}
}
}