java--面向对象综合案例二(图书馆)

Book:

package library;


public class Book {
private int id;
private String name;
private String author;
private double price;

public Book(int id, String name, String author, double price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}


public String toString(){
return "书号:"+this.id+"书名:"+this.name+"作者:"+this.author+"价格:"+this.price;
}




public int getId() {
return id;
}




public void setId(int id) {
this.id = id;
}




public String getName() {
return name;
}




public void setName(String name) {
this.name = name;
}




public String getAuthor() {
return author;
}




public void setAuthor(String author) {
this.author = author;
}




public double getPrice() {
return price;
}




public void setPrice(double price) {
this.price = price;
}
}

Manager:

package library;


import java.util.Arrays;


public class Manager {
Book[] books=new Book[0];
int index=0;

public void add(Book book){
books=Arrays.copyOf(books, books.length+1);
books[index]=book;
index++;
}
public void update(String name,double price){
for(int i=0;iif(books[i].getName().equals(name)){
books[i].setPrice(price);
}
}
}

public Book queryid(int id){
Book book=null;
for(int i=0;iif(books[i].getId()==id){
book=books[i];
}
}
return book;
}

public Book[] queryname(String author){
Book[] bk={};
int index=0;
for(int i=0;iif(author.equals(books[i].getAuthor())){
bk=Arrays.copyOf(bk, bk.length+1);
bk[index]=books[i];
index++;
}
}
return books;
}


public void showAll(){
for(int i=0;iBook book=books[i];
System.out.println(book.getId()+" "+book.getName()+book.getAuthor()+book.getPrice());
}
}
/* //private Book[] books = {};//数组
Book[] books=new Book[0];
int index=0;
public void add(Book book){
books= Arrays.copyOf(books, books.length+1);
books[index]=book;
index++;
// for(int i=0;i // books=Arrays.copyOf(books, books.length+1);
// books[books.length-1]=book;
// }
//System.out.println("书号:"+super.getId()+"书名:"+super.getName()+"作者:"+super.getAuthor()+"价格:"+super.getPrice());
}
public void update(String name,double price){
for(int i=0; iBook book =books[i];
if(name.equals(book.getName())){
book.setPrice(price);
}
 
}
// for(int i=0;i // if(name.equals(books[i])){
//
// }
// }
}
public Book queryid(int id){
Book book =null;
for(int i=0; iif(books[i].getId()==id){
book=books[i];
}
}
return book;
}
public Book[] queryname(String author){
Book[] books1 ={};
int inr = 0;
for(int i=0; iif(author.equals(books[i].getAuthor())){
Book book =books[i];
books1 = Arrays.copyOf(books1, books1.length+1);
books1[inr]=book;
inr++;
 
}
}
return books1;
}
public void showAll(){
for(int i=0; iBook book =books[i];
//System.out.println(books[i].getId()+books[i].getName()+books[i].getAuthor()+books[i].getPrice());
System.out.println(book.getId()+" "+book.getName()+" "+book.getAuthor()+" "+book.getPrice());
}
//System.out.println("书号:"++"书名:"+super.getName()+"作者:"+super.getAuthor()+"价格:"+super.getPrice());
}*/
}

Test:

package library;


import java.util.Arrays;


import org.junit.Test;




public class jTest {


@Test
public void test(){
Manager manager=new Manager();
Book book1=new Book(1001,"红楼梦","张三",200);
Book book2=new Book(2002,"西游记","李四",140.50);
Book book3=new Book(3003,"三国演义","王五",130);
Book book4=new Book(4004,"水浒传","赵六",83);
Book book5=new Book(5005,"三个和尚","李四",50);

manager.add(book1);
manager.add(book2);
manager.add(book3);
manager.add(book4);
manager.add(book5);

manager.showAll();//第一次测试输出

manager.update("西游记", 170);//修改图书价格


Book b3003 =manager.queryid(3003); //修改图书编号
System.out.println(b3003.getName());

Book[] totle = manager.queryname("李四");
System.out.println(Arrays.toString(totle));

manager.showAll(); //修改后等显示全部



/* Manager manager =new Manager();
// Manager[] mg=new Manager[0]; 
// Manager mg1=new Manager(0, null, null, 0);
Book book1=new Book(1001,"红楼梦","张三",200);
Book book2=new Book(2002,"西游记","李四",140.50);
Book book3=new Book(3003,"三国演义","王五",130);
Book book4=new Book(4004,"水浒传","赵六",83);
Book book5=new Book(5005,"三个和尚","李四",50);
manager.add(book1);
manager.add(book2);
manager.add(book3);
manager.add(book4);
manager.add(book5);


manager.update("西游记", 170);//修改图书价格


Book b3003 =manager.queryid(3003); //修改图书编号
System.out.println(b3003.getName());

Book[] totle = manager.queryname("李四");
System.out.println(Arrays.toString(totle));

manager.showAll(); //显示全部
*/
//mg1.add(book1);
//manager.add(1001,"红楼梦","张三",200);
/*for(Book bk:book){
System.out.println("书号:"+bk.getId()+"  书名:"+bk.getName()+"  作者:"+bk.getAuthor()+"  价格:"+bk.getPrice());
}*/

// Book[] books={manager1,manager2,manager3,manager4,manager5};
// manager1.add();
// manager2.add();
// manager3.add();
// manager4.add();
// manager5.add();
// manager2.update("西游记", 170);
}
}

你可能感兴趣的:(java)