0% found this document useful (0 votes)
6 views2 pages

Java Book Purchase System Code

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Java Book Purchase System Code

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Import Scanner class to take user input from the console

import [Link];

class Book {
// Private variables -cannot be accessed directly outside this class.
// Eg: the Customer class wont be able print or display the details like the
title,author cos they are private
private String title; // Title of the book
private String author; // Author name
private double price; // Price of the book
private int quantity; // Number of books available in stock

// Constructor to initialize the book object with title, author, price, and
quantity
// class concepts do not let you directly enter values to variables like
functions do , so we Constructors

public Book(String title, String author, double price, int quantity) {


[Link] = title; // Assign parameter 'title' to this object's
'title'
[Link] = author; // Assign parameter 'author' to this object's
'author'
[Link] = price; // Assign parameter 'price' to this object's
'price'
[Link] = quantity; // Assign parameter 'quantity' to this object's
'quantity'
}

// Method to handle book purchase


void purchaseBook(int q, String cname) {
// Check if requested quantity is available
if (q <= quantity) {
// Purchase successful
[Link]("Customer name: " + cname + " | Book: " + title + "
| Purchase Successful! Qty:" + q);
quantity -= q; // Reduce stock by purchased quantity
} else {
// Not enough stock
[Link]("Book out of stock! Only " + quantity + " left");
}
}

// Method to display book details


void displayDetails() {
[Link]("Title: " + title); // Print title
[Link]("Author: " + author);
[Link]("Price: " + price);
[Link]("Quantity: " + quantity + "\n");
}
}

class Customer {
// Private fields for customer information
private String name; // Customer's name
private int age; // Customer's age

// Constructor to initialize customer object


Customer(String name, int ageee) {
[Link] = name; // Assign parameter 'name' to this object's 'name'
[Link] = ageee; // Assign parameter 'ageee' to this object's 'age'
}

// Method for customer to buy a book


void buyBook(Book b1, int q) {
// Call purchaseBook method of Book class and pass customer's name and
quantity
[Link](q, name);
}
}

// Main class - the name should be same as the .java file name
class Books {
public static void main(String[] args) {

// Create book objects with title, author, price, and quantity


Book b1 = new Book("Fault in Our Stars", "John Green", 249.99, 6);
Book b2 = new Book("Atomic Habits", "James Clear", 199.99, 7);

// Display stock of books before purchase


[Link]("--Books Stock--\n ");
[Link](); // Display details of first book
[Link](); // Display details of second book

// we are creating a customer object


Customer c1 = new Customer("Karan", 19);

// customer buys 4 copies of the first book


[Link](b1, 4);

// display book details after purchase to see updated quantity


[Link]("\nBook details after purchase:");
[Link]();

// Customer tries to buy 5 copies of the first book (but only 2 left)
[Link](b1, 5); // Will print "Book out of stock! Only 2 left"

// This is different from above program, this is just for you understand
how to get input
// Create Scanner object to read input from console
Scanner sc = new Scanner([Link]);

[Link]("Enter name:"); // Prompt user to enter name


String name = [Link](); // Read the entered name as a
string

[Link]("name: " + name); // Print the entered name

[Link](); // Close Scanner to free


resources
}
}

Common questions

Powered by AI

The software design ensures accurate reflection of customer purchases in book stock through the 'purchaseBook' method. This handles the decrement of the 'quantity' variable of the 'Book' object each time a purchase is made, contingent on verifying stock availability first. By reducing quantity only when sufficient stock exists, the design prevents overselling, directly linking customer transactions with inventory updates and maintaining the accuracy of stock information .

The 'displayDetails' method in the 'Book' class plays a significant role in enhancing the application's usability by providing a clear and concise display of book information. It outputs the title, author, price, and available quantity, offering necessary information for decision-making by users or customers. This implementation supports quick accessibility of relevant data without revealing internal states or logic, promoting user-focused design. Yet, it could be improved with custom formatting or additional detail, depending on user needs .

Omitting stock checks in the purchasing process could lead to several issues. Primarily, it would result in the ability to purchase more copies of a book than are available, causing over-promising and subsequent customer dissatisfaction. This failure to verify stock before purchase would result in negative customer experiences and financial discrepancies due to inconsistent inventory records. Consequently, the business could suffer reputational damage and operational inefficiencies as resources become misallocated, highlighting the importance of robust stock management in software design .

Enhancements could include implementing a user-friendly graphical interface for better interaction, incorporating error-checking and feedback mechanisms for user input, and utilizing database systems for persistent storage of book and customer data. Additionally, providing recommendations based on purchase history, integrating digital receipts, and implementing a feature to notify users of low stock levels could offer a more comprehensive experience. Such improvements would elevate both the functionality and user engagement of the software .

The stock levels in the book store application are managed by the 'purchaseBook' method within the 'Book' class. When a purchase is initiated, this method checks if the requested quantity is available against the current stock. If sufficient, the purchase proceeds, and the quantity attribute of the Book object is decremented by the purchased amount. This stock update is critical for maintaining accurate inventory records and ensuring that stock levels reflect actual availability, thus preventing overselling and improving operational efficiency .

The 'purchaseBook' method is crucial for the inventory management of books. It checks if the requested quantity is available before allowing a purchase. If the stock suffices, the purchase is approved, the quantity is reduced accordingly, and a confirmation message is displayed. If insufficient stock is available, a message informs the customer of the remaining quantity. This ensures accurate tracking of inventory levels and prevents over-selling, which is vital for maintaining customer satisfaction and operational efficiency .

The use of the 'Scanner' class in the 'Books' class allows the program to read user input from the console, facilitating dynamic interaction between the user and the software. This is crucial for scenarios where real-time data input is required, like gathering customer details. The implementation involves creating a 'Scanner' object, reading inputs such as strings for names, and closing the scanner to free resources. This implementation supports the user interface by enabling the program to adapt according to user-provided data, making the system interactive and user-friendly .

Constructors in the 'Book' class are used to initialize new objects with specific values for the title, author, price, and quantity fields as soon as the object is created. This is distinct from regular methods, which require explicit calling and can be used multiple times on the same object to perform operations or modify data. Constructors ensure that an object starts its life in a valid state with necessary parameters set, preventing potential logic errors during later usage .

During a book purchase, the 'Customer' class interacts with the 'Book' class by invoking the 'buyBook' method. This method, in turn, calls the 'purchaseBook' method of the 'Book' class, passing along the customer's name and desired quantity for the transaction. This interaction employs object-oriented principles such as encapsulation, where direct manipulation of the book’s stock is avoided; instead, it's managed through methods. Furthermore, it follows abstraction by keeping the complex logic of purchase management hidden within the 'Book' class, simplifying usage for other classes or users .

Encapsulation is demonstrated in the 'Book' and 'Customer' classes through the use of private variables. This ensures these variables cannot be accessed directly outside their respective classes. Instead, methods such as 'purchaseBook' in the 'Book' class and 'buyBook' in the 'Customer' class are provided to manipulate these private variables safely and appropriately. This protects the internal state of objects and promotes data integrity .

You might also like