📄 library.java
字号:
import java.io.*;
public class Library {
private Book[] books;
private int noOfBooks;
public Library()
{
books=new Book[0];
noOfBooks=0;
}
public Library(Book books[])
{
noOfBooks=books.length;
for(int i=0;i<noOfBooks;i++)
{
this.books[i]=books[i];
}
}
public void addBook(Book book)
{
Book temp[]=new Book[noOfBooks];
if(noOfBooks==0)
{
books=new Book[1];
books[0]=(Book)book.clone();
noOfBooks++;
}
else if(noOfBooks>0)
{
for(int i=0;i<noOfBooks;i++)
{
temp[i]=(Book)books[i].clone();
}
books=new Book[noOfBooks+1];
for(int i=0;i<noOfBooks;i++)
{
books[i]=(Book)temp[i].clone();
}
books[noOfBooks]=book;
noOfBooks++;
}
}
public Book getBook(int index)
{
return this.books[index];
}
public String toString()
{
String out="";
for(int i=0;i<noOfBooks;i++)
{
out+="The "+(i+1)+"th book"+" id="+books[i].getID_number()+" author: "+books[i].getAuthor_of_book()+"\n";
out+=" title:"+books[i].getTitle_of_book()+"\n";
out+=" year ="+books[i].getYear()+" price="+books[i].getPrice()+"$"+"\n";
}
return out;
}
public void Sort_title()
{
int i,j;
Book temp;
for(i=0;i<noOfBooks;i++)
{
if(this.books[i]==null)
break;
for(j=i+1;j<noOfBooks;j++)
{
if(this.books[j]==null)
break;
if(this.books[j].Smaller_title(this.books[i]))
{
temp=(Book)this.books[i].clone();
this.books[i]=(Book)this.books[j].clone();
this.books[j]=(Book)temp.clone();
}
}
}
}
public void Sort_author()
{
int i,j;
Book temp;
for(i=0;i<noOfBooks;i++)
{
if(this.books[i]==null)
break;
for(j=i+1;j<noOfBooks;j++)
{
if(this.books[j]==null)
break;
if(this.books[j].Smaller_author(this.books[i]))
{
temp=(Book)this.books[i].clone();
this.books[i]=(Book)this.books[j].clone();
this.books[j]=(Book)temp.clone();
}
}
}
}
public void Sort_id_number()
{
int i,j;
Book temp;
for(i=0;i<noOfBooks;i++)
{
if(this.books[i]==null)
break;
for(j=i+1;j<noOfBooks;j++)
{
if(this.books[j]==null)
break;
if(this.books[j].Smaller_id_number(this.books[i]))
{
temp=(Book)this.books[i].clone();
this.books[i]=(Book)this.books[j].clone();
this.books[j]=(Book)temp.clone();
}
}
}
}
public void Sort_Year()
{
int i,j;
Book temp;
for(i=0;i<noOfBooks;i++)
{
if(this.books[i]==null)
break;
for(j=i+1;j<noOfBooks;j++)
{
if(this.books[j]==null)
break;
if(this.books[j].Smaller_year(this.books[i]))
{
temp=(Book)this.books[i].clone();
this.books[i]=(Book)this.books[j].clone();
this.books[j]=(Book)temp.clone();
}
}
}
}
public void Sort_price()
{
int i,j;
Book temp;
for(i=0;i<noOfBooks;i++)
{
if(this.books[i]==null)
break;
for(j=i+1;j<noOfBooks;j++)
{
if(this.books[j]==null)
break;
if(this.books[j].Smaller_price(this.books[i]))
{
temp=(Book)this.books[i].clone();
this.books[i]=(Book)this.books[j].clone();
this.books[j]=(Book)temp.clone();
}
}
}
}
public void WriteToFile()throws IOException
{
try{
BufferedWriter out=new BufferedWriter(new FileWriter("library.txt"));
out.write(noOfBooks+"\n");
for(int i=0;i<noOfBooks;i++)
{
books[i].WriteToFile(out);
}
out.close();
}catch(IOException e){}
}
public void ReadFromFile()throws IOException
{
try{
BufferedReader in=new BufferedReader(new FileReader("library.txt"));
noOfBooks=Integer.parseInt(in.readLine());
for(int i=0; i<noOfBooks; i++)
books[i].ReadFromFile(in);
in.close();
}catch(IOException e){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -