-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookStore28.java
169 lines (135 loc) · 4.35 KB
/
BookStore28.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package problem28;
import java.util.Scanner;
class BookStore {
String bookName, authorName;
double price;
int year;
BookStore() {
}
public BookStore(String bookName, String authorName, double price, int year) {
this.bookName = bookName;
this.authorName = authorName;
this.price = price;
this.year = year;
}
public String getBookName() {
return bookName;
}
public boolean setBookName(String bookName) {
// for(int i = 0; i < bookName.length(); i++) {
// if(!(bookName.charAt(i)>='A' && bookName.charAt(i) <='z' || bookName.charAt(i))) {
// System.out.println("Enter correct Book name !");
// return false;
// }
// }
this.bookName = bookName;
return true;
}
public String getAuthorName() {
return authorName;
}
public boolean setAuthorName(String authorName) {
for(int i = 0; i < authorName.length(); i++) {
if(!(authorName.charAt(i)>='A' && authorName.charAt(i) <='z' || authorName.charAt(i) == 32)) {
System.out.println("Enter correct author name !");
return false;
}
}
this.authorName = authorName;
return true;
}
public double getPrice() {
return price;
}
public boolean setPrice(double price) {
this.price = price;
return true;
}
public int getYear() {
return year;
}
public boolean setYear(int year) {
if(year >= 1700 && year <= 2020) {
this.year = year;
return true;
}
System.out.println("Enter correct year!");
return false;
}
void display() {
System.out.println("-----------------------------------------------");
System.out.println("Book details ");
System.out.println("Book Name : " + bookName);
System.out.println("Author Name : " + authorName);
System.out.println("Year of Publication : " + year);
System.out.println("Price : $" + String.format("%.2f", price));
System.out.println("-----------------------------------------------");
}
}
public class BookStore28 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// BookStore b1 = new BookStore("Daniel Defoe", "Robinson Crusoe", 15.50, 1719);
// BookStore b2 = new BookStore("Joseph Conrad", "Heart of Darkness", 12.80, 1902);
// BookStore b3 = new BookStore("Pat Conroy", "Beach Music", 9.50, 1996);
// b1.display();
// b2.display();
// b3.display();
System.out.println("How many books details you want to enter?");
int totalNumber;
while(!sc.hasNextInt()) {
System.out.println("Not a valid input! Try again");
sc.next();
}
totalNumber = sc.nextInt();
BookStore[] book = new BookStore[totalNumber];
System.out.println("-----------------------------------------");
System.out.println("Enter book details..");
for (int i = 0; i < totalNumber; i++) {
boolean isValidDetail = false;
book[i] = new BookStore();
sc.nextLine();
while (isValidDetail == false) {
System.out.print("Author Name :");
String name = sc.nextLine();
isValidDetail = book[i].setAuthorName(name);
}
isValidDetail = false;
while (isValidDetail == false) {
System.out.print("Book title :");
String bookName = sc.nextLine();
isValidDetail = book[i].setBookName(bookName);
}
isValidDetail = false;
while (isValidDetail == false) {
System.out.print("Year of publication :");
while(!sc.hasNextInt()) {
System.out.println("Not a valid input! Try again");
sc.next();
}
int year = sc.nextInt();
isValidDetail = book[i].setYear(year);
}
isValidDetail = false;
while (isValidDetail == false) {
System.out.print("Book price :");
while(!sc.hasNextDouble()) {
System.out.println("Not a valid input! Try again");
sc.next();
}
double price = sc.nextDouble();
isValidDetail = book[i].setPrice(price);
}
}
sc.close();
for (int i = 0; i < totalNumber; i++) {
System.out.println("-----------------------------------------");
System.out.println("\nEntered details : ");
System.out.println("Book name : " + book[i].getBookName());
System.out.println("Author Name : " + book[i].getAuthorName());
System.out.println("Year of publicaation : " + book[i].getYear());
System.out.println("Price : " + book[i].getPrice());
System.out.println("-----------------------------------------");
}
}
}