对象数组实现图书管理系统

 实现:

  1. 增删改查

  2. ID和ISBN只可读不可改

  3. 输出规范。如:用户输入无效空格,利用正则,使结果不输出无效空格。

  4. 利用关键字查找书籍信息

  5. 价格需要由字符串转换为浮点型,如果发生异常,说明用户的输入有错误,让用户重新输入

  6. 数据输入时如果不符合要求必须重新输入。如:书名号规范,价格必须为数字

package bean.gcu;

public class Book {
	private String Name;
	private String Author;
	private Double Price;
	private String ISBN;
	private String ID;
	private String Describe;
	public Book() {
	}
	public Book(String id,String isbn) {
		this.ID=id;
		this.ISBN=isbn;	
	}	//ID和ISBN只可赋初值,只可读不可改
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getAuthor() {
		return Author;
	}
	public void setAuthor(String author) {
		Author = author;
	}
	public Double getPrice() {
		return Price;
	}
	public void setPrice(Double price) {
		Price = price;
	}
	public String getISBN() {
		return ISBN;
	}
	public String getID() {
		return ID;
	}
	public String getDescribe() {
		return Describe;
	}
	public void setDescribe(String describe) {
		Describe = describe;
	}	
}
package db.gcu;

import bean.gcu.Book;
public class Library {
	public static Book[] book;
	public Library(int n){
		book=new Book[n];
	}
}
package service.gcu;

import bean.gcu.Book;
public interface IBookDao {
	public abstract int add(Book book,int i);
	public abstract void del(String isbn);
	public abstract void amend(String isbn);
	public abstract int cheak(String isbn);
}
package dao.gcu;

import service.gcu.IBookDao;
import java.util.Scanner;
import bean.gcu.Book;
import db.gcu.Library;
public class Booklmpl implements IBookDao {
	public int add(Book b,int i) {
		for(int j=0;j<Library.book.length;j++) {
			if(Library.book[j]==null) {
				Library.book[j]=b;
				System.out.println("添加成功!");
				i++;
				return i;
			}
		}
		System.out.println("无法添加!");
		return i;
	}
	public void del(String id) {
		for(int i=0;i<Library.book.length;i++) {
			if(Library.book[i]!=null&&Library.book[i].getID().equals(id)) {
				Library.book[i]=null;
				System.out.println("已删除");
				return;
			}
		}
		System.out.println("并无该ID!");
	}
	public void amend(String s) {
		Scanner sc=new Scanner(System.in);
		for(int i=0;i<Library.book.length;i++) {
			if(Library.book[i]!=null&&(Library.book[i].getName().contains(s)||Library.book[i].getAuthor().equals(s)||Library.book[i].
					getISBN().equals(s)||Library.book[i].getID().equals(s)||Library.book[i].getDescribe().contains(s))) {
				System.out.print("请输入您要修改书的何信息(如书名、作者、价格、描述 (ISBN、ID不可修改)):");
				String str=sc.nextLine();
				if(str.equals("书名")) {
					System.out.print("请输入修改后的书名:");	
					String name=sc.nextLine().trim();					//去掉前后空格
					for(;name.startsWith("《")==false||name.endsWith("》")==false;) {
						System.out.print("请重新输入正确书名格式:");				//格式要求,错误则重新输入
						name=sc.nextLine().trim();
					}
					Library.book[i].setName(name);
				}
				else if(str.equals("作者")) {
					System.out.print("请输入修改后的作者:");
					Library.book[i].setAuthor(sc.nextLine().replace(" ",""));
				}
				else if(str.equals("价格")) {
					System.out.print("请输入修改后的价格:");
					double price=-1;
					while(price<0) {
						try {																		//捕获异常
							price= Double.parseDouble(sc.nextLine().replace(" ",""));		//将String类型转换成Double类型,并且去掉所有空格
							if(price<0) {
								System.out.printf("发生异常:价格不能为负数:%5.0f\n",price);
								System.out.print("需重新输入正确价格:"); 
							}
							else{
								Library.book[i].setPrice(price);
							}					
						}
						catch(NumberFormatException e) {							//处理异常
							System.out.println("发生异常:价格需为数字且不能为负数"+e.getMessage()); 
							System.out.print("需重新输入正确价格:"); 
						}
					}
				}
				else if(str.equals("ISBN")) {
					System.out.println("ISBN不可修改!");
					return;
				}
				else if(str.equals("ID")) {
					System.out.println("ID不可修改!");
					return;
				}
				else if(str.equals("描述")) {
					System.out.print("请输入修改后的描述:");
					Library.book[i].setDescribe(sc.nextLine());
				}
				else {
					System.out.println("并无此信息!");
					return;				
				}	
				System.out.println("修改成功!");
				return;
			}		
		}
		System.out.println("并无该信息");
		return;
	}
	public int cheak(String s) {
		int f=0;
		for(int i=0;i<Library.book.length;i++) {
			if(Library.book[i]!=null&&(Library.book[i].getName().contains(s)||Library.book[i].getAuthor().equals(s)||Library.
					book[i].getISBN().equals(s)||Library.book[i].getID().equals(s)||Library.book[i].getDescribe().contains(s))){
				System.out.println("书名:"+Library.book[i].getName()+",作者:"+Library.book[i].getAuthor()+",价格:"+Library.book[i].getPrice()+
					",IBSN:"+Library.book[i].getISBN()+",ID:"+Library.book[i].getID()+",描述:"+Library.book[i].getDescribe());
				f++;
			}
		}
		return f;			//若返回值f为0说明没有有关的书籍
	}
}
package test.gcu;

import java.util.Scanner;
import bean.gcu.Book;
import dao.gcu.Booklmpl;
import db.gcu.Library;
public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Library library=new Library(10);
		Booklmpl booklmpl=new Booklmpl();
		Scanner sc=new Scanner(System.in);	
		System.out.print("添加请按1;删除请按2;修改请按3;查询请按4;程序结束请按0:");
		int a=sc.nextInt();
		int i=0;
		while(a>=0&&a<=4) {
			if(a==1) {
				Book book=new Book();			
				if(i<Library.book.length) {
					System.out.print("请输入书的ID:");
					sc.nextLine();			//读取a赋值留下的回车
					String id=sc.nextLine().replace(" ","");		//去掉所有空格
					for(int j=0;j<Library.book.length;j++) {
						if(Library.book[j]!=null&&Library.book[j].getID().equals(id)) {
							System.out.println("该ID已存在,添加失败!");	
							break;	
						}					
						System.out.print("请输入书的ISBN:");						
						String isbn=sc.nextLine().replace(" ","");					//去掉所有空格
						book=new Book(id,isbn);
						System.out.print("请输入书的书名(带书名号):");
						String name=sc.nextLine().trim();								//去掉前后空格
						for(;name.startsWith("《")==false||name.endsWith("》")==false;) {
							System.out.print("请重新输入正确书名格式:");				//格式要求,错误则重新输入
							name=sc.nextLine().trim();
						}
						book.setName(name);
						System.out.print("请输入书的作者:");							
						book.setAuthor(sc.nextLine().replace(" ",""));					//去掉所有空格
						System.out.print("请输入书的价格:");		
						double price=-1;
						while(price<=0) {	
							try {																	//捕获异常
								price= Double.parseDouble(sc.nextLine().replace(" ",""));		//将String类型转换成Double类型,并且去掉所有空格
								if(price<0) {
									System.out.printf("发生异常:价格不能为负数:%5.0f\n",price);
									System.out.print("需重新输入正确价格:"); 
								}
								else {
									book.setPrice(price);
								}
							}
							catch(NumberFormatException e) {							//处理异常
								System.out.println("发生异常:价格需为数字且不能为负数"+e.getMessage()); 
								System.out.print("需重新输入正确价格:"); 
							}
						}
						System.out.print("请输入书的描述:");
						book.setDescribe(sc.nextLine());			
						booklmpl.add(book,i);
						i++;
						break;
					}
				}
			}				
			if(a==2) {
				sc.nextLine();			//读取a赋值留下的回车
				System.out.print("请输入您要删除书的ID:");
				booklmpl.del(sc.nextLine().replace(" ",""));			//删除一本书需用唯一值查找
			}
			if(a==3) {
				sc.nextLine();			//读取a赋值留下的回车
				System.out.print("请输入您要修改的书(可以是书名、作者、ISBN、ID、关键字):");
				booklmpl.amend(sc.nextLine().replace(" ",""));
			}
			if(a==4) {
				sc.nextLine();			//读取a赋值留下的回车
				System.out.print("请输入您要查找的书(可以是书名、作者、ISBN、ID、关键字):");
				int hhh=booklmpl.cheak(sc.nextLine().replace(" ",""));
				if(hhh==0) {
					System.out.println("查无此书");	
				}
			}
			if(a==0) {
				System.out.println("程序结束!");
				return;
			}
			System.out.println("--------------------------------------------------------------");
			System.out.print("添加请按1;删除请按2;修改请按3;查询请按4;程序结束请按0:");
			a=sc.nextInt();
		}
	}
}

测试结果:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:为了方便图书馆管理,我们可以使用对象数组来构造图书借阅系统。首先需要定义一个Book类,表示一本书的属性和行为,如书名、作者、出版社、ISBN号等,并且可以实现借出和归还两个方法。然后定义一个Borrower类,表示借书人的属性和行为,如姓名、电话、借阅的书籍等,并且可以实现借书和还书两个方法。 接下来定义一个Library类,表示图书馆的属性和行为,如存储所有书籍的数组books,存储所有借阅人的数组borrowers,以及借阅和归还书籍的方法。在借阅书籍时,需要检查该书籍是否存在且未被借出,若满足条件则将其标记为已被借出,并将借阅人的信息添加到borrowers数组中;在归还书籍时,需要检查该书籍是否存在且已被借出,若满足条件则将其标记为未被借出,并将该借阅人的信息从borrowers数组中移除。 示例代码: class Book { constructor(name, author, publisher, isbn) { this.name = name; this.author = author; this.publisher = publisher; this.isbn = isbn; this.borrowed = false; } borrow() { this.borrowed = true; } return() { this.borrowed = false; } } class Borrower { constructor(name, phone) { this.name = name; this.phone = phone; this.books = []; } borrow(book) { this.books.push(book); } return(book) { const index = this.books.indexOf(book); if (index !== -1) { this.books.splice(index, 1); } } } class Library { constructor() { this.books = []; this.borrowers = []; } addBook(book) { this.books.push(book); } removeBook(book) { const index = this.books.indexOf(book); if (index !== -1) { this.books.splice(index, 1); } } borrowBook(book, borrower) { if (book && !book.borrowed) { book.borrow(); borrower.borrow(book); this.borrowers.push(borrower); } } returnBook(book, borrower) { if (book && book.borrowed) { book.return(); borrower.return(book); const index = this.borrowers.indexOf(borrower); if (index !== -1) { this.borrowers.splice(index, 1); } } } } // 创建几本图书和几位借阅人 const book1 = new Book("JavaScript高级程序设计", "Nicholas C. Zakas", "人民邮电出版社", "9787115422358"); const book2 = new Book("CSS揭秘", "Lea Verou", "人民邮电出版社", "9787115378693"); const book3 = new Book("HTML5与CSS3基础教程", "南京审计大学出版社", "南京审计大学出版社", "9787304108356"); const borrower1 = new Borrower("张三", "13888888888"); const borrower2 = new Borrower("李四", "13999999999"); // 创建图书馆 const library = new Library(); library.addBook(book1); library.addBook(book2); library.addBook(book3); // 张三借书 library.borrowBook(book1, borrower1); console.log(`张三借了一本书《${book1.name}》。`); console.log(`借阅人数:${library.borrowers.length},在借书籍数:${library.books.filter(book => book.borrowed).length}`); // 李四借书 library.borrowBook(book2, borrower2); console.log(`李四借了一本书《${book2.name}》。`); console.log(`借阅人数:${library.borrowers.length},在借书籍数:${library.books.filter(book => book.borrowed).length}`); // 张三还书 library.returnBook(book1, borrower1); console.log(`张三还了一本书《${book1.name}》。`); console.log(`借阅人数:${library.borrowers.length},在借书籍数:${library.books.filter(book => book.borrowed).length}`); // 李四还书 library.returnBook(book2, borrower2); console.log(`李四还了一本书《${book2.name}》。`); console.log(`借阅人数:${library.borrowers.length},在借书籍数:${library.books.filter(book => book.borrowed).length}`);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值