Online BookStore
unknown
java
a month ago
5.0 kB
1
Indexable
Never
1. Define User Interface Create an interface that defines user-related operations. java Copy code // User interface for defining user properties and actions public interface User { String getUsername(); String getPassword(); void setUsername(String username); void setPassword(String password); } 2. Implement the User Interface Create a SimpleUser class that implements the User interface. java Copy code // Implementation of the User interface public class SimpleUser implements User { private String username; private String password; public SimpleUser(String username, String password) { this.username = username; this.password = password; } @Override public String getUsername() { return username; } @Override public String getPassword() { return password; } @Override public void setUsername(String username) { this.username = username; } @Override public void setPassword(String password) { this.password = password; } } 3. Update the Bookstore Interface Extend the Bookstore interface to include user management functionality. java Copy code // Extended Bookstore interface with user management public interface Bookstore { void addBook(Book book); void listBooks(); double checkout(); void registerUser(User user); User login(String username, String password); } 4. Implement User Management in the Bookstore Update the OnlineBookstore class to support user registration and login. java Copy code import java.util.HashMap; import java.util.Map; // Implementation of the Bookstore interface public class OnlineBookstore implements Bookstore { private List<Book> books; private List<Book> cart; private Map<String, User> users; // To store registered users private User currentUser; // To keep track of the currently logged-in user public OnlineBookstore() { books = new ArrayList<>(); cart = new ArrayList<>(); users = new HashMap<>(); currentUser = null; } @Override public void addBook(Book book) { books.add(book); } @Override public void listBooks() { System.out.println("Available Books:"); for (Book book : books) { System.out.println(book); } } public void addToCart(Book book) { if (currentUser != null) { cart.add(book); } else { System.out.println("Please log in to add items to the cart."); } } @Override public double checkout() { if (currentUser != null) { double total = 0; for (Book book : cart) { total += book.getPrice(); } cart.clear(); // Empty the cart after checkout return total; } else { System.out.println("Please log in to checkout."); return 0; } } @Override public void registerUser(User user) { if (!users.containsKey(user.getUsername())) { users.put(user.getUsername(), user); System.out.println("User registered successfully."); } else { System.out.println("Username already exists."); } } @Override public User login(String username, String password) { User user = users.get(username); if (user != null && user.getPassword().equals(password)) { currentUser = user; System.out.println("Login successful."); return user; } else { System.out.println("Invalid username or password."); return null; } } } 5. Update the Main Class Demonstrate how to use the user management functionality. java Copy code public class Main { public static void main(String[] args) { OnlineBookstore bookstore = new OnlineBookstore(); // Add some books to the bookstore bookstore.addBook(new SimpleBook("Book One", "Author A", 19.99)); bookstore.addBook(new SimpleBook("Book Two", "Author B", 29.99)); bookstore.addBook(new SimpleBook("Book Three", "Author C", 9.99)); // Register users User user1 = new SimpleUser("user1", "password1"); bookstore.registerUser(user1); User user2 = new SimpleUser("user2", "password2"); bookstore.registerUser(user2); // Login as a user User loggedInUser = bookstore.login("user1", "password1"); if (loggedInUser != null) { // List available books bookstore.listBooks(); // Add books to cart bookstore.addToCart(new SimpleBook("Book One", "Author A", 19.99)); bookstore.addToCart(new SimpleBook("Book Three", "Author C", 9.99)); // Checkout double total = bookstore.checkout(); System.out.println("Total amount for checkout: $" + total); } } }
Leave a Comment