Skip to content

Commit 29ed473

Browse files
committed
Book (IPA-35)
1 parent 01d7ea7 commit 29ed473

File tree

2 files changed

+202
-201
lines changed

2 files changed

+202
-201
lines changed
Lines changed: 128 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,129 @@
1-
import java.util.*;
2-
3-
public class IPA9 {
4-
public static void main(String[] args) {
5-
Scanner sc = new Scanner(System.in);
6-
Book[] bk = new Book[4];
7-
for (int i = 0; i < bk.length; i++) {
8-
int a = sc.nextInt();sc.nextLine();
9-
int b = sc.nextInt();sc.nextLine();
10-
String c = sc.nextLine();
11-
String d = sc.nextLine();
12-
double e = sc.nextDouble();sc.nextLine();
13-
14-
bk[i] = new Book(a,b,c,d,e);
15-
}
16-
String name = sc.nextLine();
17-
18-
Book[] ans1 = findBookWithMaximumPrice(bk);
19-
if(ans1!=null)
20-
{
21-
for (int i = 0; i < ans1.length; i++) {
22-
System.out.println(ans1[i].getId()+" "+ans1[i].getTitle());
23-
}
24-
}
25-
else
26-
{
27-
System.out.println("No Book found with mentioned attribute.");
28-
}
29-
Book ans2 = searchBookByTitle(bk, name);
30-
if(ans2!=null)
31-
{
32-
System.out.println(ans2.getId());
33-
System.out.println(ans2.getPages());
34-
}
35-
}
36-
public static Book[] findBookWithMaximumPrice(Book[] b)
37-
{
38-
Book[] details = new Book[0];
39-
double max = 0;
40-
for (int i = 0; i < b.length; i++)
41-
{
42-
if(b[i].getPrice()>=max)
43-
{
44-
max = b[i].getPrice();
45-
}
46-
}
47-
for (int i = 0; i < b.length; i++) {
48-
if(b[i].getPrice()==max)
49-
{
50-
details = Arrays.copyOf(details, details.length+1);
51-
details[details.length-1]=b[i];
52-
}
53-
}
54-
if(details.length>0)
55-
{
56-
return details;
57-
}
58-
else{
59-
return null;
60-
}
61-
}
62-
public static Book searchBookByTitle(Book[] b, String n)
63-
{
64-
for (int i = 0; i < b.length; i++) {
65-
if(b[i].getTitle().equalsIgnoreCase(n))
66-
{
67-
return b[i];
68-
}
69-
}
70-
return null;
71-
}
72-
}
73-
class Book
74-
{
75-
private int id, pages;
76-
private String title, author;
77-
private double price;
78-
79-
public Book(int id, int pages, String title, String author, double price)
80-
{
81-
this.id = id;
82-
this.pages = pages;
83-
this.title = title;
84-
this.author = author;
85-
this.price = price;
86-
}
87-
88-
public int getId()
89-
{
90-
return id;
91-
}
92-
public void setId(int id)
93-
{
94-
this.id = id;
95-
}
96-
public int getPages()
97-
{
98-
return pages;
99-
}
100-
public void setPages(int pages)
101-
{
102-
this.pages = pages;
103-
}
104-
public String getTitle()
105-
{
106-
return title;
107-
}
108-
public void setTitle(String title)
109-
{
110-
this.title = title;
111-
}
112-
public String getAuthor()
113-
{
114-
return author;
115-
}
116-
public void setAuthor(String author)
117-
{
118-
this.author = author;
119-
}
120-
public double getPrice()
121-
{
122-
return price;
123-
}
124-
public void setPrice(double price)
125-
{
126-
this.price = price;
127-
}
1+
package IPA9;
2+
import java.util.*;
3+
4+
public class IPA9 {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
Book[] bk = new Book[4];
8+
for (int i = 0; i < bk.length; i++) {
9+
int a = sc.nextInt();sc.nextLine();
10+
int b = sc.nextInt();sc.nextLine();
11+
String c = sc.nextLine();
12+
String d = sc.nextLine();
13+
double e = sc.nextDouble();sc.nextLine();
14+
15+
bk[i] = new Book(a,b,c,d,e);
16+
}
17+
String name = sc.nextLine();
18+
19+
Book[] ans1 = findBookWithMaximumPrice(bk);
20+
if(ans1!=null)
21+
{
22+
for (int i = 0; i < ans1.length; i++) {
23+
System.out.println(ans1[i].getId()+" "+ans1[i].getTitle());
24+
}
25+
}
26+
else
27+
{
28+
System.out.println("No Book found with mentioned attribute.");
29+
}
30+
Book ans2 = searchBookByTitle(bk, name);
31+
if(ans2!=null)
32+
{
33+
System.out.println(ans2.getId());
34+
System.out.println(ans2.getPages());
35+
}
36+
}
37+
public static Book[] findBookWithMaximumPrice(Book[] b)
38+
{
39+
Book[] details = new Book[0];
40+
double max = 0;
41+
for (int i = 0; i < b.length; i++)
42+
{
43+
if(b[i].getPrice()>=max)
44+
{
45+
max = b[i].getPrice();
46+
}
47+
}
48+
for (int i = 0; i < b.length; i++) {
49+
if(b[i].getPrice()==max)
50+
{
51+
details = Arrays.copyOf(details, details.length+1);
52+
details[details.length-1]=b[i];
53+
}
54+
}
55+
if(details.length>0)
56+
{
57+
return details;
58+
}
59+
else{
60+
return null;
61+
}
62+
}
63+
public static Book searchBookByTitle(Book[] b, String n)
64+
{
65+
for (int i = 0; i < b.length; i++) {
66+
if(b[i].getTitle().equalsIgnoreCase(n))
67+
{
68+
return b[i];
69+
}
70+
}
71+
return null;
72+
}
73+
}
74+
class Book
75+
{
76+
private int id, pages;
77+
private String title, author;
78+
private double price;
79+
80+
public Book(int id, int pages, String title, String author, double price)
81+
{
82+
this.id = id;
83+
this.pages = pages;
84+
this.title = title;
85+
this.author = author;
86+
this.price = price;
87+
}
88+
89+
public int getId()
90+
{
91+
return id;
92+
}
93+
public void setId(int id)
94+
{
95+
this.id = id;
96+
}
97+
public int getPages()
98+
{
99+
return pages;
100+
}
101+
public void setPages(int pages)
102+
{
103+
this.pages = pages;
104+
}
105+
public String getTitle()
106+
{
107+
return title;
108+
}
109+
public void setTitle(String title)
110+
{
111+
this.title = title;
112+
}
113+
public String getAuthor()
114+
{
115+
return author;
116+
}
117+
public void setAuthor(String author)
118+
{
119+
this.author = author;
120+
}
121+
public double getPrice()
122+
{
123+
return price;
124+
}
125+
public void setPrice(double price)
126+
{
127+
this.price = price;
128+
}
128129
}

0 commit comments

Comments
 (0)