-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance2.java
More file actions
69 lines (67 loc) · 2.46 KB
/
Inheritance2.java
File metadata and controls
69 lines (67 loc) · 2.46 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
/*
Create a class Book with following members:
B.No, Title, Author, Publisher, Price, number of books sold
Derive a class Hard Copy from Book with following members:
Number of pages
Use constructors for initialization of data members.
a)Number of books sold should get updated for each hard Copy is sold
b)For a given author find the number of books sold.
c)Write function to find the book with highest price
*/
import java.util.Scanner;
class Book {
static int noOfBooksSold = 0;
int bno;
String title, author, publisher;
double price;
public Book(int bno, String title,String author, String publisher, double price) {
this.bno = bno;
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}
}
class HardCopy extends Book {
private int noOfPages;
public HardCopy(int bno, String title,String author, String publisher, double price, int noOfPages) {
super(bno, title, author, publisher, price);
this.noOfPages = noOfPages;
noOfBooksSold++;
}
}
public class Inheritance2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HardCopy[] bArr = new HardCopy[100];
for(int i = 0; ; i++) {
System.out.println("Enter bno, title, author, publisher, price, noOfPages... -1 to exit");
int bno = sc.nextInt();
if(bno == -1)
break;
else
bArr[i] = new HardCopy(bno, sc.next(), sc.next(), sc.next(), sc.nextDouble(), sc.nextInt());
}
System.out.println("No of books sold: " + Book.noOfBooksSold);
System.out.println("Enter an author name to find out the no of books sold");
String author = sc.next();
int count = 0;
for(int i = 0; i < Book.noOfBooksSold; i++) {
if(bArr[i].author.equals(author))
count++;
}
System.out.println("No of books sold for author " + author + " is " + count);
System.out.println("Bno " + costliestBook(bArr) + " has the highest price");
}
public static int costliestBook(HardCopy[] b) {
double maxPrice = 0;
int bno = -1;
for(int i = 0; i < Book.noOfBooksSold; i++) {
if(b[i].price > maxPrice) {
maxPrice = b[i].price;
bno = b[i].bno;
}
}
return bno;
}
}