-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program6Driver.java
160 lines (101 loc) · 3.26 KB
/
Program6Driver.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
159
160
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class P6Driver{
public static void main(String [] args) throws FileNotFoundException {
System.out.println("\nHello and welcome to your personal accountant.");
Scanner scan = new Scanner(new File("data.txt"));
Employee emp1 = new Employee();
Employee[] emp = new Employee[20];
for(int i =0; i<emp.length; i++){
emp[i] = new Employee();
}
int index = 0;
while(scan.hasNextLine()){
String line = scan.nextLine();
if(line.length()>7){
emp[index].setName(line);
index++;
if(index > 12){
index = 0;
}
}
else if(line.length()==7){
emp[index].setId(line);
index++;
if(index>12){
index = 0;
}
}
else{
emp[index].setSalary(Integer.parseInt(line));
index++;
}
}
Scanner keyboard = new Scanner(System.in);
System.out.println("\nChose from the following:\n");
System.out.println("\t1. Print all the data.");
System.out.println("\t2. Print the average salary");
System.out.println("\t3. Find the boss with the highest salary");
System.out.println("\t4. Find the newest employee with the lowest salary");
System.out.println("\nEnter a number or -1 to exit\n");
int response = keyboard.nextInt();
while(response != -1){
if(response == 1){
Print(emp, index);
}
if(response == 2){
Average(emp, index);
}
if(response == 3){
Boss(emp, index);
}
if(response == 4){
Newest(emp, index);
}
System.out.println("\nIf you would like to make another selection, enter a number or -1 to exit\n");
response = keyboard.nextInt();
}
System.out.println("Goodbye and have a wonderful day!");
}
public static void Print(Employee[] emp, int length){
for(int i = 0; i<length; i++){
System.out.println("Name: " + emp[i].getName() + " ID#: " + emp[i].getId() + " Salary: " + emp[i].getSalary());
}
}
public static void Average(Employee[] emp, int length){
int sum = 0;
for(int i = 0; i < length; i++){
sum += emp[i].getSalary();
}
int average;
average = sum/length;
System.out.println("The average salary is " + average + " dollars.");
}
public static void Boss(Employee[] emp, int length){
int highest;
highest = emp[0].getSalary();
int index =0;
for(int i = 0; i<length; i++){
if(emp[i].getSalary()>highest){
highest = emp[i].getSalary();
index = i;
}
}
System.out.println("The Boss with the highest salary: ");
System.out.println("Name: " + emp[index].getName() + " ID#: " + emp[index].getId() + " Salary: " + emp[index].getSalary());
}
public static void Newest(Employee[] emp, int length){
int lowest;
lowest = emp[0].getSalary();
int index=0;
for(int i =0; i<length; i++){
if(emp[i].getSalary()<lowest){
lowest = emp[i].getSalary();
index = i;
}
}
System.out.println("The newest employee with the lowest salary: ");
System.out.println("Name: " + emp[index].getName() + " ID#: " + emp[index].getId() + " Salary: " + emp[index].getSalary());
}
}