Skip to content

Commit a10fb32

Browse files
adding more exercises
objects and classes
1 parent 2437878 commit a10fb32

File tree

13 files changed

+754
-0
lines changed

13 files changed

+754
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package Lesson06_ObjectsAndClasses.MoreExercises.P01_CompanyRoster;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Department {
7+
private String name;
8+
private List<Employee> employeeList=new ArrayList<>();
9+
10+
Department(){
11+
12+
}
13+
public Department(String name,Employee employee ) {
14+
this.name = name;
15+
employeeList.add(employee);
16+
}
17+
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public void setEmployeeToList(Employee employee) {
28+
this.employeeList.add(employee);
29+
}
30+
31+
public List<Employee> getEmployeeList() {
32+
return employeeList;
33+
}
34+
35+
public void printSortedDescendingBySalary() {
36+
this.employeeList.stream().sorted((e1,e2)->Double.compare(e2.getSalary(), e1.getSalary())
37+
).forEach(e -> System.out.println(e.toString()));
38+
39+
}
40+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package Lesson06_ObjectsAndClasses.MoreExercises.P01_CompanyRoster;
2+
3+
public class Employee {
4+
private String name;
5+
private double salary;
6+
private String position;
7+
private String department;
8+
private String email;
9+
private int age;
10+
11+
Employee() {
12+
}
13+
14+
public Employee(String name, double salary, String position, String department, String email, int age) {
15+
this.name = name;
16+
this.salary = salary;
17+
this.position = position;
18+
this.department = department;
19+
this.email = email;
20+
this.age = age;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
public double getSalary() {
32+
return salary;
33+
}
34+
35+
public void setSalary(double salary) {
36+
this.salary = salary;
37+
}
38+
39+
public String getPosition() {
40+
return position;
41+
}
42+
43+
public void setPosition(String position) {
44+
this.position = position;
45+
}
46+
47+
public String getDepartment() {
48+
return department;
49+
}
50+
51+
public void setDepartment(String department) {
52+
this.department = department;
53+
}
54+
55+
public String getEmail() {
56+
return email;
57+
}
58+
59+
public void setEmail(String email) {
60+
this.email = email;
61+
}
62+
63+
public int getAge() {
64+
return age;
65+
}
66+
67+
public void setAge(int age) {
68+
this.age = age;
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return String.format("%s %.2f %s %d", name, salary, email, age);
74+
}
75+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package Lesson06_ObjectsAndClasses.MoreExercises.P01_CompanyRoster;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
import java.util.regex.Pattern;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
Scanner scanner = new Scanner(System.in);
11+
int rows = Integer.parseInt(scanner.nextLine());
12+
List<Department> departments = new ArrayList<>();
13+
14+
for (int i = 0; i < rows; i++) {
15+
String inputLine = scanner.nextLine();
16+
String[] command = inputLine.split("\\s+");
17+
Employee currentEmployee = new Employee();
18+
19+
if (command.length == 6) {
20+
currentEmployee = new Employee(
21+
command[0],
22+
Double.parseDouble(command[1]),
23+
command[2],
24+
command[3],
25+
command[4],
26+
Integer.parseInt(command[5]));
27+
} else if (command.length == 5) {
28+
if (isInteger(command[command.length - 1])) {
29+
currentEmployee = new Employee(
30+
command[0],
31+
Double.parseDouble(command[1]),
32+
command[2],
33+
command[3],
34+
"n/a",
35+
Integer.parseInt(command[4]));
36+
} else {
37+
currentEmployee = new Employee(
38+
command[0],
39+
Double.parseDouble(command[1]),
40+
command[2],
41+
command[3],
42+
command[4],
43+
-1);
44+
}
45+
46+
} else if (command.length == 4) {
47+
currentEmployee = new Employee(
48+
command[0],
49+
Double.parseDouble(command[1]),
50+
command[2],
51+
command[3],
52+
"n/a",
53+
-1);
54+
}
55+
56+
57+
Department department = new Department(command[3], currentEmployee);
58+
if (!isDepartmentExist(departments, command[3])) {
59+
departments.add(department);
60+
} else {
61+
for (Department dp : departments) {
62+
if (dp.getName().equals(command[3])) {
63+
dp.setEmployeeToList(currentEmployee);
64+
break;
65+
}
66+
}
67+
}
68+
}
69+
double maxAverageSalary = Double.MIN_VALUE;
70+
String departmentName = "";
71+
for (Department dp : departments) {
72+
double averageSalary = getAverageSalary(dp);
73+
if (averageSalary > maxAverageSalary) {
74+
maxAverageSalary = averageSalary;
75+
departmentName = dp.getName();
76+
}
77+
}
78+
79+
80+
System.out.println("Highest Average Salary: " + departmentName);
81+
for (Department dp : departments) {
82+
if (dp.getName().equals(departmentName)) {
83+
dp.printSortedDescendingBySalary();
84+
}
85+
}
86+
}
87+
88+
public static boolean isInteger(String input) {
89+
Pattern pattern = Pattern.compile("[+-]?[0-9][0-9]*");
90+
return pattern.matcher(input).matches();
91+
}
92+
93+
public static boolean isDepartmentExist(List<Department> departments, String name) {
94+
for (Department dp : departments) {
95+
if (dp.getName().equals(name)) {
96+
return true;
97+
}
98+
}
99+
return false;
100+
}
101+
102+
public static double getAverageSalary(Department department) {
103+
double averageSalary = 0;
104+
105+
for (Employee employee : department.getEmployeeList()) {
106+
averageSalary += employee.getSalary();
107+
}
108+
averageSalary = averageSalary / department.getEmployeeList().size();
109+
return averageSalary;
110+
}
111+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Lesson06_ObjectsAndClasses.MoreExercises.P02_RawData;
2+
3+
import java.util.List;
4+
5+
public class Car {
6+
private String model;
7+
private Engine engine;
8+
private Cargo cargo;
9+
private List<Tire> tires;
10+
11+
public Car(String model, Engine engine, Cargo cargo, List<Tire> tires) {
12+
this.model = model;
13+
this.engine = engine;
14+
this.cargo = cargo;
15+
this.tires = tires;
16+
}
17+
18+
public Engine getEngine() {
19+
return engine;
20+
}
21+
22+
public String getModel() {
23+
return model;
24+
}
25+
26+
public Cargo getCargo() {
27+
return this.cargo;
28+
}
29+
30+
public List<Tire> getTires() {
31+
return this.tires;
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Lesson06_ObjectsAndClasses.MoreExercises.P02_RawData;
2+
3+
public class Cargo {
4+
private int cargoWeight;
5+
private String cargoType;
6+
7+
public Cargo(int cargoWeight, String cargoType) {
8+
this.cargoWeight = cargoWeight;
9+
this.cargoType = cargoType;
10+
}
11+
12+
public int getCargoWeight() {
13+
return cargoWeight;
14+
}
15+
16+
public void setCargoWeight(int cargoWeight) {
17+
this.cargoWeight = cargoWeight;
18+
}
19+
20+
public String getCargoType() {
21+
return cargoType;
22+
}
23+
24+
public void setCargoType(String cargoType) {
25+
this.cargoType = cargoType;
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Lesson06_ObjectsAndClasses.MoreExercises.P02_RawData;
2+
3+
public class Engine {
4+
private int engineSpeed;
5+
private int enginePower;
6+
7+
public Engine(int engineSpeed, int enginePower) {
8+
this.engineSpeed = engineSpeed;
9+
this.enginePower = enginePower;
10+
}
11+
12+
public int getEngineSpeed() {
13+
return engineSpeed;
14+
}
15+
16+
public void setEngineSpeed(int engineSpeed) {
17+
this.engineSpeed = engineSpeed;
18+
}
19+
20+
public int getEnginePower() {
21+
return enginePower;
22+
}
23+
24+
public void setEnginePower(int enginePower) {
25+
this.enginePower = enginePower;
26+
}
27+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package Lesson06_ObjectsAndClasses.MoreExercises.P02_RawData;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
public class Main {
8+
public static void main(String[] args) {
9+
Scanner scanner = new Scanner(System.in);
10+
int rows = Integer.parseInt(scanner.nextLine());
11+
List<Car> cars = new ArrayList<>();
12+
13+
for (int i = 1; i <= rows; i++) {
14+
String[] input = scanner.nextLine().split(" ");
15+
String model = input[0];
16+
int engineSpeed = Integer.parseInt(input[1]);
17+
int enginePower = Integer.parseInt(input[2]);
18+
int cargoWeight = Integer.parseInt(input[3]);
19+
String cargoType = input[4];
20+
Engine engine = new Engine(engineSpeed, enginePower);
21+
Cargo cargo = new Cargo(cargoWeight, cargoType);
22+
List<Tire> tireList = new ArrayList<>();
23+
for (int tireItems = 5; tireItems <= 12; tireItems += 2) {
24+
Tire currentTire = new Tire(Double.parseDouble(input[tireItems]), Integer.parseInt(input[tireItems + 1]));
25+
tireList.add(currentTire);
26+
}
27+
Car currentCar = new Car(model, engine, cargo, tireList);
28+
cars.add(currentCar);
29+
}
30+
31+
String command = scanner.nextLine();
32+
switch (command) {
33+
case "fragile": //print all cars whose Cargo Type is "fragile" with a tire whose pressure is < 1
34+
for (Car car : cars) {
35+
Cargo cargo = car.getCargo();
36+
String cargoType = cargo.getCargoType();
37+
if (cargoType.equals("fragile")) {
38+
List<Tire> tireList = car.getTires();
39+
for (Tire tire : tireList) {
40+
double pressure = tire.getPressure();
41+
if (pressure < 1) {
42+
System.out.println(car.getModel());
43+
break;
44+
}
45+
}
46+
}
47+
}
48+
break;
49+
50+
case "flamable"://"flammable" print all cars whose Cargo Type is "flammable" and have Engine Power > 250.
51+
for (Car car : cars) {
52+
Cargo cargo = car.getCargo();
53+
String cargoType = cargo.getCargoType();
54+
if (cargoType.equals("flamable")) {
55+
Engine engine = car.getEngine();
56+
int enginePower = engine.getEnginePower();
57+
if (enginePower > 250) {
58+
System.out.println(car.getModel());
59+
60+
}
61+
}
62+
}
63+
64+
break;
65+
}
66+
67+
68+
}
69+
}

0 commit comments

Comments
 (0)