-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataEntry.java
More file actions
81 lines (71 loc) · 1.99 KB
/
DataEntry.java
File metadata and controls
81 lines (71 loc) · 1.99 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Scanner;
class Person {
private String name;
private String salary;
private String role;
Person(String name, String salary, String role) {
this.name = name;
this.salary = salary;
this.role = role;
}
void display(int id) {
System.out.printf("%-8s%-25s%-10s%-10s", (id + 1), this.name, this.salary, this.role);
}
}
public class DataEntry {
int i;
Person[] people = new Person[5];
public static void main(String[] args) {
DataEntry dataEntry = new DataEntry();
dataEntry.doDataEntry();
}
// Check if there is still place for new person and then, if user will input
// data
void doDataEntry() {
Scanner scanner = new Scanner(System.in);
if (i < 5) {
System.out.print("\nData Entry? (Y/n) ... ");
String willEntry = scanner.nextLine();
if ("y".equalsIgnoreCase(willEntry)) {
getInput(scanner);
} else if ("n".equalsIgnoreCase(willEntry)) {
doResult();
} else {
doDataEntry();
}
} else {
scanner.close();
doResult();
}
}
// If user choose to input data, this functions get inputs from user
void getInput(Scanner scanner) {
// Scanner scanner = new Scanner(System.in);
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Salary: ");
String salary = scanner.nextLine();
System.out.print("Role: ");
String role = scanner.nextLine();
addNewPerson(name, salary, role);
}
// this function add user with infromation from input data
void addNewPerson(String name, String salary, String role) {
people[i] = new Person(name, salary, role);
i++;
doDataEntry();
}
// after data entry is done, display result
void doResult() {
if (i > 0) {
System.out.println("\n----Result----");
System.out.printf("%-8s%-25s%-10s%-10s", "id", "name", "salary", "role");
for (int j = 0; j < i; j++) {
people[j].display(j);
}
System.out.println("\n----End----");
} else {
System.out.println("No Input");
}
}
}