-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_cricket_names_with_User_Defined_Arrays.java
More file actions
206 lines (171 loc) · 4.82 KB
/
Java_cricket_names_with_User_Defined_Arrays.java
File metadata and controls
206 lines (171 loc) · 4.82 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package placements;
import java.util.*;
import java.io.*;
/*
* Create a class Player with the below attributes:
* int playerId;
String playerName;
int runs;
String playerType;
String matchType;
The above attributes must be private and use a getter-setter method to fetch the values and use the parameterized constructor as
required to assign the values.
Implement 2 static methods:
findPlayerWithLowestRuns - this method will take the array of Player objects and a String value of player type as input parameters
This method will return the lowest run of the given player type
If no such player of that player type is found print "No such Player" in the output
*
findPlayerByMatchType- this method will take the array of Player objects and a String value of match type as input parameters
This method will return the id of the players of given match type in descending order.
If no such player of that match type is found print "No Player with given match type" in the output
These methods should be called from the main method and before calling these methods take
2 inputs of String type from user-> String PlayerType and String Match Type.
Input 1:
11
sachin
100
international
odi
12
shewag
133
international
test
13
varun
78
state
test
14
ashwin
67
state
odi
state <- PlayerType user input
odi <- Match Type user input
Output:
67
14
11
Input 2:
11
sachin
100
international
odi
12
shewag
133
international
test
13
varun
78
state
test
14
ashwin
67
state
odi
districy <- PlayerType user input
t20 <- Match Type user input
output:
No Such Player
No player with given match type */
class Player
{
private int playerId;
private String playerName;
private int runs;
private String playerType;
private String matchType;
//Constructor to initialize all the values
Player(int playerId,String playerName,int runs, String playerType , String matchType)
{
this.playerId=playerId;
this.playerName=playerName;
this.runs=runs;
this.playerType=playerType;
this.matchType=matchType;
}
//Getter-Setter method for all values
public void set_playerId(int playerId) {this.playerId=playerId;}
public int get_playerId(){return playerId;}
public void set_playerName(String playerName) {this.playerName=playerName;}
public String get_playerName(){return playerName;}
public void set_runs(int runs) {this.runs=runs;}
public int get_runs(){return runs;}
public void set_playerType(String playerType) {this.playerType=playerType;}
public String get_playerType(){return playerType;}
public void set_playerId(String matchType) {this.matchType=matchType;}
public String get_matchType(){return matchType;}
}
//Solution class
public class Solutions {
//method to return the lowest run
public static int findPlayerWithLowestRuns(Player [] p , String pt)
{
int min= Integer.MAX_VALUE;
for(int i =0;i<p.length;i++)
{
if(p[i].get_playerType().equals(pt))
{
if(p[i].get_runs()<min)
min=p[i].get_runs();
}
}
if (min==Integer.MAX_VALUE)
return 0;
else
return min;
}
//method to return the player with the same match type
public static int[] findPlayerByMatchType(Player[]p,String mt)
{
int []id = new int[4];
int j=0,flag=0;
for(int i =0;i<p.length;i++)
{
if(p[i].get_matchType().equals(mt))
{
id[j++]=p[i].get_playerId();
flag++;
}
}
if(flag!=0)
{
Arrays.sort(id);
return id;
}
else
return null;
}
public static void main(String [] args)throws IOException
{
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
Player p [] = new Player[4];
for(int i =0;i<4;i++)
{
p[i]=new Player(Integer.parseInt(inp.readLine()),inp.readLine(),Integer.parseInt(inp.readLine()),inp.readLine(),inp.readLine());
}
//Taking the match type and player type from the user.
String pt = inp.readLine();
String mt= inp.readLine();
if(findPlayerWithLowestRuns(p ,pt)==0)
System.out.println("No Such Player");
else
System.out.println(findPlayerWithLowestRuns(p ,pt));
if(findPlayerByMatchType(p ,mt)==null)
System.out.println("No player with given match type");
else
{
int [] res = findPlayerByMatchType(p ,mt);
for(int i =res.length-1;i>=0;i--)
{
if(res[i]!=0)
System.out.println(res[i]);
}
}
}
}