1 回答
TA贡献1856条经验 获得超11个赞
我从您的代码中确定了导致错误的 2 件事:
BufferedReader 扫描是您库的一部分,因此您尝试对其进行序列化/反序列化,因此会收到错误消息。
您似乎永远不会增加 bookid。
话虽如此,你应该对你的图书馆建模如下:
图书馆包含书籍。
public class Book implements Serializable
{
private static final long serialVersionUID = 24913L;
static int bookId=0; // Stores ID of Books
double bookPrice; // Stores Price of Book
int availableCopies; // Stores Nos. of available copies
String bookTitle; // Stores title of Books
String bookAuthor; // Stores author of Book
String bookSubject; // Stores Subject of Book
//StringBuffer studentName; // Stores Name of Student which issued/Returned Book.
StringBuffer issuedTo[] = new StringBuffer[1000];
//Stores all names of students which issued same book.
//Constructors
public Book()
{
generateBookId();
}
public Book(String bookTitle, String bookAuthor, String bookSubject, double bookPrice, int availableCopies)
{
generateBookId();
this.bookTitle=bookTitle;
this.bookAuthor=bookAuthor;
this.bookSubject=bookSubject;
this.bookPrice=bookPrice;
this.availableCopies=availableCopies;
}
//Functions
public static int generateBookId(){
bookId++; //Incrementing Book Id
return bookId; // Returning the incremented book Id
}
public int getBookId() throws IOException{
return bookId;
}
public double getBookPrice() throws IOException{
return bookPrice;
}
public String getBookTitle() throws IOException{
return bookTitle;
}
public String getBookAuthor() throws IOException{
return bookAuthor;
}
public String getBookSubject() throws IOException{
return bookSubject;
}
public int getAvailableCopies() throws IOException{
return availableCopies;
}
/*public void setBookId() throws IOException{
bookId = Integer.parseInt(scan.readLine());
}*/
public void setBookPrice(double bookPrice) throws IOException{
this.bookPrice = bookPrice;
}
public void setBookTitle(String bookTitle) throws IOException{
this.bookTitle = bookTitle;
}
public void setBookAuthor(String bookAuthor) throws IOException{
this.bookAuthor = bookAuthor;
}
public void setBookSubject(String bookSubject) throws IOException{
this.bookSubject = bookSubject;
}
public void setNoOfCopies(int availableCopies) throws IOException{
this.availableCopies = availableCopies;
}
public void addBookCopies() throws IOException{
availableCopies++;
}
public void removeBookCopies() throws IOException{
availableCopies--;
}
}
和图书馆如下:
public class Library implements Serializable
{
private static final long serialVersionUID = 24913L; //used to verify sender and reciever
public static ArrayList<Book> bookData = new ArrayList<Book>();
public void addBook(Book book)
{
try{
objCount++;
bookData.add(book);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("Database.ser");
oos = new ObjectOutputStream(fos);
oos.flush();
oos.writeObject(bookData);
oos.close();
fos.close();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}finally{
try{
File file = new File("Books_Index.txt");
FileWriter fw = new FileWriter(file, true);
int count=getObjCount();
fw.write("\nBook Index ["+count+"] Contains Book Named: ["+book.getBookTitle()+"]");
fw.close();
//saveData();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
}
}
public void deleteBook(int index ,String title)
{
//fetchData();
FileInputStream fis = null;
ObjectInputStream ois = null;
//ArrayList<Books> bookData = null;
try {
fis = new FileInputStream("Database.ser");
ois = new ObjectInputStream(fis);
bookData = (ArrayList<Book>) ois.readObject();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
int status=0;
try{
for(int i=0;i<bookData.size();i++)
{
if(title.equalsIgnoreCase(bookData.get(i).getBookTitle()))
{
bookData.set(bookData.indexOf(i),null);
//changing the value to null so that index of books would be maintained
// ...to avoid left shift
break;
}
else{
status=1;
}
}
if(status == 1)
{
System.out.println("Book Not Found.");
}
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("Database.ser");
oos = new ObjectOutputStream(fos);
oos.flush();
oos.writeObject(bookData);
oos.close();
fos.close();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}finally{
try{
File file = new File("Books_Index.txt");
FileWriter fw = new FileWriter(file, true);
int count = getObjCount();
fw.write("\n Book Index ["+count+"] got deleted. Book Named: ["+title+"]");
fw.close();
System.out.println("Delete Operation Finished.");
//saveData();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
}
}
public void modifyBook(int index,String title,double price,int availableCopies)
{
//fetchData();
FileInputStream fis = null;
ObjectInputStream ois = null;
//ArrayList<Books> bookData = null;
try {
fis = new FileInputStream("Database.ser");
ois = new ObjectInputStream(fis);
bookData = (ArrayList<Book>) ois.readObject();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
int status=0;
try{
for(int i=0;i<bookData.size();i++)
{
if(title.equalsIgnoreCase(bookData.get(i).getBookTitle()))
{
bookData.get(index).setBookPrice(price);
bookData.get(index).setNoOfCopies(availableCopies);
}
else{
status=1;
}
}
if(status == 1)
{
System.out.println("Book Not Found.");
}
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("Database.ser");
oos = new ObjectOutputStream(fos);
oos.flush();
oos.writeObject(bookData);
oos.close();
fos.close();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}finally{
System.out.println("Modify Operation Finished.");
//saveData();
}
}
public void searchBook(int index,String title)
{
//fetchData();
FileInputStream fis = null;
ObjectInputStream ois = null;
//ArrayList<Books> bookData = null;
try {
fis = new FileInputStream("Database.ser");
ois = new ObjectInputStream(fis);
bookData = (ArrayList<Book>) ois.readObject();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
int status=0;
try{
for(int i=0;i<bookData.size();i++)
{
if(title.equalsIgnoreCase(bookData.get(i).getBookTitle()))
{
if(bookData.get(i).getBookId() == getObjCount())
{
System.out.println("Book Found at ["+i+"]");
System.out.println("Displaying Book Data....");
System.out.println("Book Title : "+bookData.get(i).getBookTitle());
System.out.println("Book Author : "+bookData.get(i).getBookAuthor());
System.out.println("Book Subject : "+bookData.get(i).getBookSubject());
System.out.println("Book Price : "+bookData.get(i).getBookPrice());
System.out.println("No. of Copies : "+bookData.get(i).getAvailableCopies());
break;
}else{
System.out.println("Internal Error Caught.[1] Index Mismatch");
//Library.main();
}
}
else{
status=1;
}
}
if(status == 1)
{
System.out.println("Book Not Found.");
}
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("Database.ser");
oos = new ObjectOutputStream(fos);
oos.flush();
oos.writeObject(bookData);
oos.close();
fos.close();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}finally{
System.out.println("Search Operation Finished.");
//saveData();
}
}
public void issueBook(int index,String title,String studName)
{
int status = 0;
//fetchData();
FileInputStream fis = null;
ObjectInputStream ois = null;
//ArrayList<Books> bookData = null;
try {
fis = new FileInputStream("Database.ser");
ois = new ObjectInputStream(fis);
bookData = (ArrayList<Book>) ois.readObject();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
try{
for(int i=0;i<bookData.size();i++)
{
if(title.equalsIgnoreCase(bookData.get(i).getBookTitle()))
{
if(bookData.get(i).getBookId() == getObjCount())
{
//
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
//
//Format formatter = new SimpleDateFormat("yyyy-MM-dd");
//String currentDate = formatter.format(date);
//i++;
bookData.get(i).removeBookCopies();
//i++;
File file = new File("issueLogs.txt");
FileWriter fw = new FileWriter(file, true);
fw.write("\n["+dtf.format(now)+"] : Book Title : "+bookData.get(i).getBookTitle()+" was Issued by Student Name"+studName);
fw.close();
break;
}else{
System.out.println("Internal Error Caught.[1] Index Mismatch");
//Library.main();
}
}
else{
status=1;
}
if(status == 1)
{
System.out.println("353");
}
}
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("Database.ser");
oos = new ObjectOutputStream(fos);
oos.flush();
oos.writeObject(bookData);
oos.close();
fos.close();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}finally{
System.out.println("Issue Operation Finished.");
//saveData();
}
}
public void returnBook(int index,String title,String studName)
{
int status=0;
//fetchData();
FileInputStream fis = null;
ObjectInputStream ois = null;
//ArrayList<Books> bookData = null;
try {
fis = new FileInputStream("Database.ser");
ois = new ObjectInputStream(fis);
bookData = (ArrayList<Book>) ois.readObject();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
try{
for(int i=0;i<bookData.size();i++)
{
if(title.equalsIgnoreCase(bookData.get(i).getBookTitle()))
{
if(bookData.get(i).getBookId() == getObjCount())
{
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
//Format formatter = new SimpleDateFormat("yyyy-MM-dd");
//String currentdate = formatter.format(date);
//i++;
bookData.get(i).addBookCopies();
//i++; //are yaar he ka kelela mi??
File file = new File("returnLogs.txt");
FileWriter fw = new FileWriter(file, true);
fw.write("\n["+dtf.format(now)+"] : Book Title : "+bookData.get(i).getBookTitle()+" was Returned by Student Name"+studName);
fw.close();
}else{
System.out.println("Internal Error Caught.[1] Index Mismatch");
//Library.main();
}
}
else{
status=1;
}
if(status == 1)
{
System.out.println("Line no 402");
}
}
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("Database.ser");
oos = new ObjectOutputStream(fos);
oos.flush();
oos.writeObject(bookData);
oos.close();
fos.close();
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}
}catch(Exception e){
System.out.println("Exception Caught: "+e);
}finally{
System.out.println("Return Operation Finished.");
//saveData();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
public static int objCount=0;
public static int getObjCount()
{
return objCount;
}
}
最后,您可以拥有执行所有代码的主运行程序类:
public class CreateLibrary {
static final String username = "";
static final String password = "";
public static void main(String args[]) throws Exception
{
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter User Name: ");
String enterUser= scan.readLine();
System.out.println("Enter Password: ");
String enterPass =scan.readLine();
if(enterUser.equals(username) && enterPass.equals(password))
{
System.out.println("Login Successful...\n\n");
Library library = new Library();
int choice=0;
while(choice!=7)
{
System.out.println("Select From Below Options: ");
System.out.println("1. Add Book\n2. Modify Book\n3. Delete Book\n4. Search Book\n5. Issue Book\n6. Return Book\n7. LogOut");
System.out.println("Enter Your Choice: ");
choice = Integer.parseInt(scan.readLine());
switch(choice)
{
case 1:
{
Book book = new Book();
System.out.println("_________________Enter Book Details_________________");
System.out.println(" Enter title of the Book : ");
book.setBookTitle(scan.readLine());
System.out.println(" Enter the Name of Author : ");
book.setBookAuthor(scan.readLine());
System.out.println(" Enter the Subject of Book : ");
book.setBookSubject(scan.readLine());
System.out.println(" Enter the Price of Book : ");
book.setBookPrice(Integer.parseInt(scan.readLine()));
System.out.println(" Enter Number of Copies :");
book.setNoOfCopies(Integer.parseInt(scan.readLine()));
library.addBook(book);
break;
}
case 2:
{
System.out.println("Enter Index of Book you want to Modify: ");
int index=Integer.parseInt(scan.readLine());
System.out.println("_________________Modify Book_________________");
System.out.println("Enter Title of Book to Modify: ");
String title = scan.readLine();
System.out.println("Enter New Price of Book: ");
double price = Double.parseDouble(scan.readLine());
System.out.println("Enter New Available copies of Book");
int availableCopies = Integer.parseInt(scan.readLine());
library.modifyBook(index,title,price,availableCopies);
break;
}
case 3:
{
System.out.println("Enter Index of Book you want to Delete: ");
int index=Integer.parseInt(scan.readLine());
System.out.println("_________________Delete Book_________________");
System.out.println("Enter Title of Book to delete: ");
String title = scan.readLine();
library.deleteBook(index,title);
break;
}
case 4:
{
System.out.println("Enter Index of Book you want to Search: ");
int index=Integer.parseInt(scan.readLine());
System.out.println("_________________Search Book_________________");
System.out.println("Enter Title of Book to Search: ");
String title = scan.readLine();
library.searchBook(index,title);
break;
}
case 5:
{
System.out.println("Enter Index of Book you want to Issue: ");
int index=Integer.parseInt(scan.readLine());
System.out.println("_________________Issue Book_________________");
System.out.println("Enter Title of Book : ");
String title = scan.readLine();
System.out.println("Enter Name of Student: ");
String studName = scan.readLine();
library.issueBook(index,title,studName);
break;
}
case 6:
{
System.out.println("Enter Index of Book you want to Return: ");
int index=Integer.parseInt(scan.readLine());
System.out.println("_________________Return Book_________________");
System.out.println("Enter Title of Book : ");
String title = scan.readLine();
System.out.println("Enter Name of Student: ");
String studName = scan.readLine();
library.returnBook(index,title,studName);
break;
}
case 7:
{
System.exit(0);
break;
}
}
}
} //if bracket
else
{
System.out.println("Invalid Login Credentials. It's not that easy to get in.");
System.out.print("Quiting...");
Thread.sleep(800);
}
}
}
希望有帮助。
添加回答
举报