-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneProblem.java
More file actions
78 lines (65 loc) · 1.91 KB
/
PhoneProblem.java
File metadata and controls
78 lines (65 loc) · 1.91 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
import java.util.*;
class Phone {
int phoneId;
String os;
String brand;
int price;
public Phone(int phoneId, String os, String brand, int price) {
this.phoneId = phoneId;
this.os = os;
this.brand = brand;
this.price = price;
}
public int getPhoneId() {
return phoneId;
}
public String getOs() {
return os;
}
public String getBrand() {
return brand;
}
public int getPrice() {
return price;
}
}
public class PhoneProblem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Phone[] arr = new Phone[4];
for (int i = 0; i < 4; i++) {
int a = sc.nextInt(); sc.nextLine();
String b = sc.nextLine();
String c = sc.nextLine();
int d = sc.nextInt(); sc.nextLine();
arr[i] = new Phone(a, b, c, d);
}
String brand = sc.nextLine();
String os = sc.nextLine();
// First method call
int sum = findpriceForGivenBrand(arr, brand);
System.out.println(sum == 0 ? "The given Brand is not available" : sum);
// Second method call
Phone phone = getPhoneIdBasedOnOs(arr, os);
System.out.println(phone == null ?
"No phones are available with specified os and price range" :
phone.phoneId);
}
public static int findpriceForGivenBrand(Phone[] arr, String brand) {
int sum = 0;
for (Phone p : arr) {
if (p.getBrand().equalsIgnoreCase(brand)) {
sum += p.getPrice();
}
}
return sum;
}
public static Phone getPhoneIdBasedOnOs(Phone[] arr, String os) {
for (Phone p : arr) {
if (p.getOs().equalsIgnoreCase(os) && p.getPrice() >= 50000) {
return p;
}
}
return null;
}
}