forked from gmrpr321/Abstract_class_and_Interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx4_1.java
76 lines (65 loc) · 2.27 KB
/
Ex4_1.java
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
package Abstract_class_and_Interface;
import java.util.Scanner;
import java.util.Scanner;
abstract class Book {
String title = null, author = null;
public int price = 0;
abstract void setdetails();
abstract String gettitle();
}
class Mybook extends Book {
void setdetails() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter book details");
System.out.println("Enter title : ");
super.title = scan.next();
System.out.println("Enter author name : ");
super.author = scan.next();
System.out.println("Enter price of book ");
price = scan.nextInt();
}
String gettitle() {
return super.title;
}
}
public class Ex4_1 {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int nos, temp, t1;
System.out.println("Enter the number of book objects :");
nos = scan.nextInt();
Mybook arr[] = new Mybook[nos];
for (int x = 0; x < nos; x++) {
arr[x] = new Mybook();
}
while (true) {
System.out.println("1.Enter data\n2.Get title\n3.Get Total price");
temp = scan.nextInt();
try {
switch (temp) {
case 1:
System.out.println("Enter book number : ");
t1 = scan.nextInt();
arr[t1 - 1].setdetails();
break;
case 2:
System.out.println("Enter Book number : ");
t1 = scan.nextInt();
System.out.println(arr[t1 - 1].gettitle());
break;
case 3:
int tot = 0;
for (Mybook x : arr) {
tot += x.price;
}
System.out.println("Total prices of all books is : " + tot);
break;
default:
System.out.println("Enter a valid option");
}
} catch (ArrayIndexOutOfBoundsException obj) {
System.out.println("Please Enter a book number within specified range");
}
}
}
}