-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathfootwearProgram.java
136 lines (123 loc) · 2.49 KB
/
footwearProgram.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
package IPA2;
import java.util.*;
public class footwearProgram
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
Footwear[] ft = new Footwear[5];
for(int i=0; i<5; i++)
{
int a = sc.nextInt();sc.nextLine();
String b = sc.nextLine();
String c = sc.nextLine();
int d = sc.nextInt();sc.nextLine();
ft[i]=new Footwear(a,b,c,d);
}
String ftType = sc.nextLine();
String ftName = sc.nextLine();
int count = getCountByType(ft,ftType);
if(count>0)
{
System.out.println(count);
}
else
{
System.out.println("Footwear not avaliable");
}
Footwear obj = getSecondHighestPriceByBrand(ft, ftName);
if(obj!=null)
{
System.out.println(obj.getId());
System.out.println(obj.getName());
System.out.println(obj.getPrice());
}
else
{
System.out.println("Brand not available");
}
}
public static int getCountByType(Footwear[] ft, String t)
{
int count = 0;
for(int i=0; i<ft.length; i++)
{
if(ft[i].getType().equalsIgnoreCase(t))
{
count++;
}
}
if (count>0)
{
return count;
}
else
{
return 0;
}
}
public static Footwear getSecondHighestPriceByBrand(Footwear[] ft, String name)
{
List<Footwear> matchingFootwear = new ArrayList<>();
// Collect footwears matching the brand name
for (Footwear footwear : ft) {
if (footwear.getName().equalsIgnoreCase(name)) {
matchingFootwear.add(footwear);
}
}
// If there are less than two matching items, return null
if (matchingFootwear.size() < 2) {
return null;
}
// Sort by price in descending order
matchingFootwear.sort((f1, f2) -> Integer.compare(f2.getPrice(), f1.getPrice()));
// Return the second highest priced footwear
return matchingFootwear.get(1);
}
}
class Footwear
{
private int id;
private String name;
private String type;
private int price;
public Footwear(int id, String name, String type, int 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 int getPrice()
{
return price;
}
public void setPrice(int price)
{
this.price = price;
}
}