-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee.java
More file actions
59 lines (45 loc) · 1.63 KB
/
Copy pathEmployee.java
File metadata and controls
59 lines (45 loc) · 1.63 KB
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
import java.util.Scanner;
class Address {
String address;
}
class Person {
int id;
String name;
Address address; // Each person has an Address object
void printStatement() {
System.out.println("The name of the person is: " + this.name +
" & the id no is " + this.id +
" & the address is " + this.address.address);
}
}
class Employee {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Creating person objects
Person p1 = new Person();
Person p2 = new Person();
// Assigning address objects to each person
p1.address = new Address();
p2.address = new Address();
// Taking input for the first person
System.out.print("Enter ID for person 1: ");
p1.id = in.nextInt();
in.nextLine(); // Clear buffer
System.out.print("Enter Name for person 1: ");
p1.name = in.nextLine();
System.out.print("Enter Address for person 1: ");
p1.address.address = in.nextLine();
// Taking input for the second person
System.out.print("Enter ID for person 2: ");
p2.id = in.nextInt();
in.nextLine(); // Clear buffer
System.out.print("Enter Name for person 2: ");
p2.name = in.nextLine();
System.out.print("Enter Address for person 2: ");
p2.address.address = in.nextLine();
// Printing the details
p1.printStatement();
p2.printStatement();
in.close(); // Close the scanner
}
}