-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathIPA9.java
More file actions
129 lines (124 loc) · 2.95 KB
/
IPA9.java
File metadata and controls
129 lines (124 loc) · 2.95 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
package IPA9;
import java.util.*;
public class IPA9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Book[] bk = new Book[4];
for (int i = 0; i < bk.length; i++) {
int a = sc.nextInt();sc.nextLine();
int b = sc.nextInt();sc.nextLine();
String c = sc.nextLine();
String d = sc.nextLine();
double e = sc.nextDouble();sc.nextLine();
bk[i] = new Book(a,b,c,d,e);
}
String name = sc.nextLine();
Book[] ans1 = findBookWithMaximumPrice(bk);
if(ans1!=null)
{
for (int i = 0; i < ans1.length; i++) {
System.out.println(ans1[i].getId()+" "+ans1[i].getTitle());
}
}
else
{
System.out.println("No Book found with mentioned attribute.");
}
Book ans2 = searchBookByTitle(bk, name);
if(ans2!=null)
{
System.out.println(ans2.getId());
System.out.println(ans2.getPages());
}
}
public static Book[] findBookWithMaximumPrice(Book[] b)
{
Book[] details = new Book[0];
double max = 0;
for (int i = 0; i < b.length; i++)
{
if(b[i].getPrice()>=max)
{
max = b[i].getPrice();
}
}
for (int i = 0; i < b.length; i++) {
if(b[i].getPrice()==max)
{
details = Arrays.copyOf(details, details.length+1);
details[details.length-1]=b[i];
}
}
if(details.length>0)
{
return details;
}
else{
return null;
}
}
public static Book searchBookByTitle(Book[] b, String n)
{
for (int i = 0; i < b.length; i++) {
if(b[i].getTitle().equalsIgnoreCase(n))
{
return b[i];
}
}
return null;
}
}
class Book
{
private int id, pages;
private String title, author;
private double price;
public Book(int id, int pages, String title, String author, double price)
{
this.id = id;
this.pages = pages;
this.title = title;
this.author = author;
this.price = price;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public int getPages()
{
return pages;
}
public void setPages(int pages)
{
this.pages = pages;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
}