import [Link].
ArrayList;
import [Link];
import [Link];
/**
* Simple Online Book Store Console Application in Java
* Features:
* - List available books
* - Add books to cart
* - View cart with total price
* - Checkout and clear cart
* - Exit application
*/
public class OnlineBookStore {
// Book class representing a book
static class Book {
private int id;
private String title;
private String author;
private double price;
public Book(int id, String title, String author, double price) {
[Link] = id;
[Link] = title;
[Link] = author;
[Link] = price;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return [Link]("%d. %s by %s - $%.2f", id, title, author, price);
}
}
// CartItem class representing an item in the cart with quantity
static class CartItem {
private Book book;
private int quantity;
public CartItem(Book book, int quantity) {
[Link] = book;
[Link] = quantity;
}
public Book getBook() {
return book;
}
public int getQuantity() {
return quantity;
}
public void increaseQuantity(int amount) {
[Link] += amount;
}
public double getTotalPrice() {
return [Link]() * quantity;
}
@Override
public String toString() {
return [Link]("%s | Quantity: %d | Total: $%.2f",
[Link](), quantity, getTotalPrice());
}
}
// Store class holding list of books
static class Store {
private List<Book> inventory;
public Store() {
inventory = new ArrayList<>();
loadBooks();
}
private void loadBooks() {
[Link](new Book(1, "The Great Gatsby", "F. Scott Fitzgerald",
10.99));
[Link](new Book(2, "1984", "George Orwell", 8.99));
[Link](new Book(3, "To Kill a Mockingbird", "Harper Lee",
9.99));
[Link](new Book(4, "Pride and Prejudice", "Jane Austen", 7.99));
[Link](new Book(5, "The Hobbit", "J.R.R. Tolkien", 12.50));
[Link](new Book(6, "Moby Dick", "Herman Melville", 11.25));
}
public void listBooks() {
[Link]("\nAvailable Books:");
for (Book book : inventory) {
[Link](book);
}
}
public Book getBookById(int id) {
for (Book book : inventory) {
if ([Link]() == id) {
return book;
}
}
return null;
}
}
// Cart class holding CartItems
static class Cart {
private List<CartItem> items;
public Cart() {
items = new ArrayList<>();
}
public void addBook(Book book, int quantity) {
for (CartItem item : items) {
if ([Link]().getId() == [Link]()) {
[Link](quantity);
[Link](quantity + " more added to existing book in
cart.");
return;
}
}
[Link](new CartItem(book, quantity));
[Link](quantity + " added to cart.");
}
public void viewCart() {
if ([Link]()) {
[Link]("\nYour cart is empty.");
return;
}
[Link]("\nYour Cart:");
double total = 0.0;
for (CartItem item : items) {
[Link](item);
total += [Link]();
}
[Link]("Total price: $%.2f%n", total);
}
public double checkout() {
if ([Link]()) {
[Link]("\nYour cart is empty. Add books before
checkout.");
return 0.0;
}
double total = 0.0;
[Link]("\nCheckout");
for (CartItem item : items) {
[Link](item);
total += [Link]();
}
[Link]("Total amount due: $%.2f%n", total);
[Link]();
[Link]("Thank you for your purchase!\n");
return total;
}
public boolean isEmpty() {
return [Link]();
}
}
// Main method and user interaction
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Store store = new Store();
Cart cart = new Cart();
[Link]("Welcome to the Online Book Store!");
boolean running = true;
while (running) {
[Link]("\nMain Menu:");
[Link]("1. List Books");
[Link]("2. Add Book to Cart");
[Link]("3. View Cart");
[Link]("4. Checkout");
[Link]("5. Exit");
[Link]("Choose an option (1-5): ");
String input = [Link]();
switch (input) {
case "1":
[Link]();
break;
case "2":
[Link]();
[Link]("Enter Book ID to add to cart: ");
try {
int bookId = [Link]([Link]());
Book book = [Link](bookId);
if (book == null) {
[Link]("Invalid Book ID");
break;
}
[Link]("Enter quantity: ");
int quantity = [Link]([Link]());
if (quantity <= 0) {
[Link]("Quantity must be at least 1");
break;
}
[Link](book, quantity);
} catch (NumberFormatException e) {
[Link]("Invalid input, please enter numeric
values.");
}
break;
case "3":
[Link]();
break;
case "4":
[Link]();
break;
case "5":
[Link]("Exiting Online Book Store. Goodbye!");
running = false;
break;
default:
[Link]("Invalid option. Please choose between 1 and
5.");
}
}
[Link]();
}
}