-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
130 lines (112 loc) · 3.6 KB
/
Book.java
File metadata and controls
130 lines (112 loc) · 3.6 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
130
package com.kushal.LibraryManagement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Book {
private int book_id;
private String title;
private String author;
private String publisher;
private int quantity;
public int getBook_id() {
return book_id;
}
public void setBook_id(int book_id) {
this.book_id = book_id;
}
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 String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
static void displayAllBooks() throws SQLException {
ResultSet rs = Database.getBooks();
String format = "| %-8s | %-25s | %-20s | %-20s | %-8s |\n";
String separator = "+----------+---------------------------+----------------------+----------------------+----------+\n";
System.out.println(separator);
System.out.printf(format, "Book ID", "Title", "Author", "Publisher", "Quantity");
System.out.println(separator);
assert rs != null;
if (rs.next()) {
while (rs.next()) {
System.out.printf(format,
rs.getInt("book_id"),
rs.getString("title"),
rs.getString("author"),
rs.getString("publisher"),
rs.getInt("quantity")
);
}
} else {
System.out.println("| No books found in the database. |");
}
System.out.println(separator);
}
static void addBook(){
Book b = new Book();
Scanner sc = new Scanner(System.in);
System.out.println("--Add a new Book--");
System.out.println("Enter Book Details");
System.out.print("Enter Title: ");
b.setTitle(sc.nextLine());
System.out.print("Enter Author: ");
b.setAuthor(sc.nextLine());
System.out.print("Enter Publisher: ");
b.setPublisher(sc.nextLine());
System.out.print("Enter Quantity: ");
b.setQuantity(sc.nextInt());
System.out.println(b.getBook_id());
try {
Database.addBook(b);
} catch (SQLException e){
e.printStackTrace();
}
}
static void updateBookQuantity(){
Book b = new Book();
Scanner sc = new Scanner(System.in);
System.out.println("--Update Quantity of a Book--");
System.out.println("Enter Book Details");
System.out.print("Enter Book ID: ");
b.setBook_id(sc.nextInt());
System.out.print("Enter Updated Quantity: ");
b.setQuantity(sc.nextInt());
try {
Database.updateBookQuantity(b);
} catch (SQLException e){
e.printStackTrace();
}
}
public static void deleteBook() {
Book b = new Book();
Scanner sc = new Scanner(System.in);
System.out.println("--Delete a Book--");
System.out.println("Enter Book Details");
System.out.print("Enter Book ID: ");
b.setBook_id(sc.nextInt());
try {
Database.deleteBook(b);
} catch (SQLException e){
e.printStackTrace();
}
}
}