-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookStoreApp.java
More file actions
248 lines (205 loc) · 9.2 KB
/
BookStoreApp.java
File metadata and controls
248 lines (205 loc) · 9.2 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.company;
import java.awt.*;
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
abstract class Item {
protected String name;
protected String author;
protected int availablequantity;
protected int price;
public Item(String name, String author, int availablequantity, int price) {
this.name = name;
this.author = author;
this.availablequantity = availablequantity;
this.price = price;
}
public abstract void displayItemDetails();
public abstract int calculatePrice(int quantity);
}
class Book extends Item {
public Book(String name, String author, int availablequantity, int price) {
super(name, author, availablequantity, price);
}
@Override
public void displayItemDetails() {
System.out.println("Book Name: " + name);
System.out.println("Author: " + author);
System.out.println("Quantity Available: " + availablequantity);
System.out.println("Price per Book: " + price);
}
@Override
public int calculatePrice(int quantity) {
return this.price * quantity;
}
}
class InventoryManager {
public static ArrayList<Book> loadInventory() throws FileNotFoundException {
ArrayList<Book> books = new ArrayList<>();
try {
File bookFile = new File("BooksName.txt");
File authorFile = new File("AuthorsName.txt");
File quantityFile = new File("Quantity.txt");
File priceFile = new File("Price.txt");
// Check if the files exist before attempting to read
if (!bookFile.exists() || !authorFile.exists() || !quantityFile.exists() || !priceFile.exists()) {
throw new FileNotFoundException("One or more required inventory files are missing.");
}
Scanner bookScanner = new Scanner(bookFile);
Scanner authorScanner = new Scanner(authorFile);
Scanner quantityScanner = new Scanner(quantityFile);
Scanner priceScanner = new Scanner(priceFile);
while (bookScanner.hasNextLine() && authorScanner.hasNextLine() &&
quantityScanner.hasNextLine() && priceScanner.hasNextLine()) {
String name = bookScanner.nextLine();
String author = authorScanner.nextLine();
int availablequantity = Integer.parseInt(quantityScanner.nextLine());
int price = Integer.parseInt(priceScanner.nextLine());
// Ensure quantity and price are positive numbers
if (availablequantity < 0 || price < 0) {
throw new IllegalArgumentException("Quantity and price must be non-negative.");
}
Book book = new Book(name, author, availablequantity, price);
books.add(book);
}
bookScanner.close();
authorScanner.close();
quantityScanner.close();
priceScanner.close();
} catch (IOException | IllegalArgumentException e) {
System.out.println("Error loading inventory: " + e.getMessage());
throw new RuntimeException("Failed to load inventory", e);
}
return books;
}
}
class Transaction {
private static final Scanner in = new Scanner(System.in);
public static void initiatePurchase(ArrayList<Book> Books){
System.out.print("Enter the Name of the Book you want to buy: ");
String selectedBookName = in.nextLine();
// Find the book by name
Book selectedBook = null;
for (Book book : Books) {
if (book.name.equalsIgnoreCase(selectedBookName)) {
selectedBook = book;
break;
}
}
if (selectedBook != null) {
// Process the purchase if book found
Transaction.processPurchase(selectedBook);
} else {
System.out.println("Sorry, the book you requested is not available.");
}
}
public static void processPurchase(Book book) {
System.out.print("Enter the Number of Copies you want to buy: ");
int quantity = in.nextInt();
if (quantity > book.availablequantity) {
System.out.println("Sorry, only " + book.availablequantity + " copies are available.");
} else {
int totalPrice = book.calculatePrice(quantity);
System.out.println("Total Amount to be paid: " + totalPrice);
// completeTransaction(book, quantity, totalPrice);
System.out.println("Would you like to proceed with the transaction? (yes/no): ");
String answer = in.next();
if ("yes".equalsIgnoreCase(answer)) {
System.out.print("Enter your name: ");
String customerName = in.next();
System.out.print("Enter your mobile number: ");
long phone = in.nextLong();
System.out.println("Transaction Successful. Generating Receipt...");
generateReceipt(customerName, phone, book, quantity, totalPrice);
updateInventory(book, quantity);
}
}
}
private static void generateReceipt(String customerName, long phone, Book book, int quantity, int totalPrice) {
try {
File receipt = new File("Receipt.txt");
if (receipt.createNewFile()) {
FileWriter writer = new FileWriter(receipt);
writer.write("----- Book Store Receipt -----\n");
writer.write("Name: " + customerName + "\n");
writer.write("Mobile Number: " + phone + "\n");
writer.write("Book: " + book.name + "\n");
writer.write("Author: " + book.author + "\n");
writer.write("Quantity: " + quantity + "\n");
writer.write("Total Price: " + totalPrice + "\n");
writer.close();
Desktop.getDesktop().open(receipt);
receipt.deleteOnExit();
}
} catch (IOException e) {
System.out.println("Error generating receipt: " + e.getMessage());
}
}
private static void updateInventory(Book book, int quantitySold) {
// Update the inventory and write back to the text files
book.availablequantity -= quantitySold; // Update book's quantity
try {
// Read the existing inventory data
ArrayList<String> books = readFile("BooksName.txt");
ArrayList<String> authors = readFile("AuthorsName.txt");
ArrayList<String> quantities = readFile("Quantity.txt");
ArrayList<String> prices = readFile("Price.txt");
// Find the index of the book that was purchased
int index = books.indexOf(book.name);
if (index != -1) {
quantities.set(index, String.valueOf(book.availablequantity));
writeToFile("Quantity.txt", quantities);
// Additional handling if you want to update other files (like prices, etc.)
}
} catch (IOException e) {
System.out.println("Error updating inventory: " + e.getMessage());
}
}
private static ArrayList<String> readFile(String fileName) throws IOException {
ArrayList<String> lines = new ArrayList<>();
try (Scanner scanner = new Scanner(new File(fileName))) {
while (scanner.hasNextLine()) {
lines.add(scanner.nextLine());
}
}
return lines;
}
private static void writeToFile(String fileName, ArrayList<String> data) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
for (String line : data) {
writer.write(line);
writer.newLine();
}
}
}
}
public class BookStoreApp {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
ArrayList<Book> books = InventoryManager.loadInventory();
System.out.println("Welcome to the Book Store");
System.out.println("Do you want to see the catalogue:");
String ans = scanner.nextLine();
if( ans.equalsIgnoreCase("yes")) {
System.out.println("Available Books:");
// Display all books in inventory
for (Book book : books) {
book.displayItemDetails();
System.out.println("--------------------------");
}
}
System.out.println("Do you want to buy any booke: ");
ans = scanner.nextLine();
if( ans.equalsIgnoreCase("yes")) {
// Ask user to select a book to purchase
Transaction.initiatePurchase(books);
}
else{
System.out.println("Thankyou for visiting");
}
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}