-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathIPA20.java
127 lines (122 loc) · 3.01 KB
/
IPA20.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
package IPA20;
import java.util.*;
public class IPA20 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Engine[] eng = new Engine[4];
for (int i = 0; i < eng.length; i++) {
int a = sc.nextInt();sc.nextLine();
String b = sc.nextLine();
String c = sc.nextLine();
double d = sc.nextDouble();sc.nextLine();
eng[i]=new Engine(a, b, c, d);
}
String t = sc.nextLine();
String n = sc.nextLine();
int ans1 = findAvgEnginePriceByType(eng,t);
if(ans1!=0)
{
System.out.println(ans1);
}
else
{
System.out.println("There are no engine with given type");
}
Engine[] ans2 = searchEngineByName(eng,n);
if(ans2!=null)
{
for (int i = 0; i < ans2.length; i++) {
System.out.println(ans2[i].getId());
}
}
}
public static int findAvgEnginePriceByType(Engine[]e, String t)
{
int sum=0, count=0;
for (int i = 0; i < e.length; i++) {
if(e[i].getType().equalsIgnoreCase(t))
{
sum+=e[i].getPrice();
count++;
}
}
if(count>0)
{
int avg = sum/count;
return avg;
}
else
{
return 0;
}
}
public static Engine[] searchEngineByName(Engine[]e, String n)
{
Engine[] arr = new Engine[0];
for (int i = 0; i < e.length; i++) {
if(e[i].getName().equalsIgnoreCase(n))
{
arr = Arrays.copyOf(arr,arr.length+1);
arr[arr.length-1]=e[i];
}
}
Engine data;
for (int i = 0; i < arr.length-1; i++)
{
for (int j = i+1; j < arr.length; j++)
{
if(arr[i].getId()>arr[j].getId())
{
data = arr[i];
arr[i] = arr[j];
arr[j] = data;
}
}
}
if(arr.length>0)
{
return arr;
}
else
{
return null;
}
}
}
class Engine
{
private int id;
private String name;
private String type;
private double price;
public Engine(int id, String name, String type, double price) {
this.id = id;
this.name = name;
this.type = type;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}