-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnlineBookstoreManagementSystem.java
109 lines (91 loc) · 3.24 KB
/
OnlineBookstoreManagementSystem.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
import java.util.ArrayList;
import java.util.List;
class Book {
private String title;
private String author;
private double price;
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
}
class Bookstore {
private List<Book> books;
public Bookstore() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public void displayBooks() {
if (books.isEmpty()) {
System.out.println("No books available in the bookstore.");
} else {
System.out.println("Books in the Bookstore:");
System.out.println("-------------------------");
for (Book book : books) {
System.out.println("Title: " + book.getTitle());
System.out.println("Author: " + book.getAuthor());
System.out.println("Price: $" + book.getPrice());
System.out.println("-------------------------");
}
}
}
public List<Book> searchByTitle(String keyword) {
List<Book> searchResults = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().toLowerCase().contains(keyword.toLowerCase())) {
searchResults.add(book);
}
}
return searchResults;
}
public void purchaseBook(Book book) {
if (books.contains(book)) {
System.out.println("Purchasing book: " + book.getTitle());
System.out.println("Price: $" + book.getPrice());
books.remove(book);
System.out.println("Book purchased successfully!");
} else {
System.out.println("Book not found in the bookstore.");
}
}
}
public class OnlineBookstoreManagementSystem {
public static void main(String[] args) {
Bookstore bookstore = new Bookstore();
// Adding books to the bookstore
bookstore.addBook(new Book("Java Programming", "John Smith", 29.99));
bookstore.addBook(new Book("Python for Beginners", "Alice Johnson", 19.99));
bookstore.addBook(new Book("Data Structures and Algorithms", "Michael Brown", 39.99));
// Displaying all books in the bookstore
bookstore.displayBooks();
// Searching books by title
System.out.println("Searching books by title: 'java'");
List<Book> searchResults = bookstore.searchByTitle("java");
for (Book book : searchResults) {
System.out.println("Title: " + book.getTitle());
System.out.println("Author: " + book.getAuthor());
System.out.println("Price: $" + book.getPrice());
System.out.println("-------------------------");
}
// Purchasing a book
Book bookToPurchase = new Book("Java Programming", "John Smith", 29.99);
bookstore.purchaseBook(bookToPurchase);
// Displaying updated list of books
bookstore.displayBooks();
}
}