图书馆系统
图书馆系统:需要分为管理员和普通用户的操作
需要以下包以及类
代码及注释如下
一、book包下
1.Book类
package book;
public class Book {
private String name;//书名
private String author;//作者
private int price;//价格
private String type;//书的类型
private boolean isBorrowed;//是否被借出
//构造方法
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
//被private,提供方法
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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
//重写toString
@Override
public String toString() {
return "BookList{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed == true)?"已借出":"未借出")+
//", isBorrowed=" + isBorrowed +
'}';
}
}
2.BookList类
package book;
public class BookList {
//封装
private Book[] books;
private int usedSize;//记录当前书架存放几本书
//不带参数的构造方法
public BookList(){
this.books = new Book[10];//分配内存 初始化
this.books[0] = new Book("三国演义","罗贯中",49,"小说");
this.books[1] = new Book("西游记","吴承恩",45,"小说");
this.books[2] = new Book("水浒传","施耐庵",50,"小说");
this.usedSize = 3;
}
//
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBook(int pos){
return books[pos];
}
public void setBook(Book book,int pos){
books[pos] = book;
}
}
二、operation包下
1.IOPeration接口
package operation;
import book.BookList;
//接口,不能有具体的实现
public interface IOPeration {
void work(BookList bookList);
}
2.AddOperation类
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOPeration {
//重写
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要新增图书的书名:");
String name = scanner.nextLine();
System.out.println("请输入您要新增图书的作者:");
String autor = scanner.nextLine();
System.out.println("请输入您要新增图书的价格:");
int price = scanner.nextInt();//在Int在Line前面时,nextLine会吃回车
System.out.println("请输入您要新增图书的类型:");
//String type = scanner.nextLine();
String type = scanner.next();//读不了空格
/*吃回车解决方案:
1.去掉Line:
int price = scanner.nextInt();
String type = scanner.next();//读不了空格
2.换位:
String type = scanner.nextLine();
int price = scanner.nextInt();
3.多读一次,把回车空格吃掉:
int price = scanner.nextInt();
scanner.nextLine();//多读一次
String type = scanner.nextLine();
*/
//构造一个书的对象
Book book = new Book(name, autor, price, type);
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
//找书
if (tmp.getName().equals(name)) {
System.out.println("本书存在,不能重复添加!");
return;
}
}
//没有重复的书,开始新增
bookList.setBook(book,currentSize);
bookList.setUsedSize((currentSize + 1));
}
}
3.BorrowPeration类
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowPeration implements IOPeration{
//重写
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("请输入你要借阅的图书的书名");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("没有你要借阅的书:"+name);
}
}
4.DelOperation类
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOPeration{
//重写
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要删除图书的书名:");
int currentSize = bookList.getUsedSize();
String name = scanner.nextLine();
//判断是否有删除的条件
int index = -1;//数组没有-1下标
int i = 0;
for ( ;i < currentSize; i++) {
Book tmp = bookList.getBook(i);
//找书
if (tmp.getName().equals(name)) {
index = i;
break;//已记录
}
}
if(i >= currentSize){
System.out.println("没有您要删除的图书!");
return;
}
//可删除
for (int j = index; j < currentSize-1; j++) {
//bookList[j] = bookList[j+1,但bookList不是个数组
//利用setbook,将后一个覆盖前一个
Book book = bookList.getBook(j+1);
bookList.setBook(book,j);
}
//此时两个书,指向同一个对象,需要防止对象的泄露
bookList.setBook(null,currentSize - 1);
//防止泄露:删除后置为空
bookList.setUsedSize(currentSize - 1);
System.out.println("删除成功!");
}
}
5.ExitOperation类
package operation;
import book.BookList;
public class ExitOperation implements IOPeration{
//重写
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
//退出后需要手动回收 bookList 资源
System.exit(0);
}
}
6.FindOperation类
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration{
//重写
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入你要查找的图书的书名");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
/*Book book = bookList[i];若不写一个public
此时的Book[]数组被,private修饰,无法调用。
所以不能用此方法*/
Book book = bookList.getBook(i);
//找书
if(book.getName().equals(name)){
System.out.println("本书存在,信息如下:");
System.out.println(book);
return;
}
}
//没找到
System.out.println("没找到本书,书名为:"+name);
}
}
7.ReturnOperation类
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOPeration{
//重写
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("请输入你要归还的图书的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("没有你要归还的书:"+name);
}
}
8.ShowOperation类
package operation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOPeration{
//重写
public void work(BookList bookList){
System.out.println("显示图书!");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
三、user包下
1.AdminUser类
package user;
import operation.*;
import java.util.Scanner;
//继承
public class AdminUser extends User{
//构造方法
public AdminUser(String name) {
super(name);
//数组初始化,对应下标引用方法
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
}
public int menu(){
System.out.println("*********管理员菜单*********");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("**************************");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;//返回给Main
}
}
2.NormalUser类
package user;
import operation.*;
import java.util.Scanner;
//继承
public class NormalUser extends User{//继承了User属性
public NormalUser(String name) {
super(name);
//数组初始化,对应下标引用方法
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowPeration(),
new ReturnOperation()
};
//不知道给数组赋值多大,请这样使用
//this.ioPerations = {};报错,只有在定义的时候才能这样赋值
}
public int menu(){
System.out.println("*********普通用户菜单*********");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("**************************");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;//返回给Main
}
}
3.User类
package user;
import book.BookList;
import operation.IOPeration;
//抽象类
public abstract class User {
protected String name;
//定义数组
protected IOPeration[] ioPerations;//未初始化
//数组未被分配空间时,数组的初始值为null,只有被分配空间后才能视为初始化
//构造方法
public User(String name) {
this.name = name;
}
//在User内定义的menu方法时,我们发现menu没有具体实现
//则没有具体的实现所有定义为 抽象方法,抽象方法由 抽象类 修饰
public abstract int menu();
//选择引用对象
public void doOperation(int choice, BookList bookList){
//非静态方法,调用时需要对象的引用
//this.ioPerations是会调用operation包内的类
this.ioPerations[choice].work(bookList);
}
}
四、Main
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
System.out.println("请输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入你的身份,1:管理员 2:普通用户->");
int choice = scanner.nextInt();
//向上转型
if(choice == 1){
return new AdminUser(name);
}else{
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
//此时user的指向未知(两种),向上转型
User user = login();
while(true){
int choice = user.menu();
/*通过父类引用menu,
但此时User中没有menu方法,
所以要定义一个menu方法,
*/
/* 发生动态绑定
根据菜单返回的choice来执行对应操作,
此时有多个menu方法,则发生了 多态,
(引用的对象不一样,调用同一个方法所展示的行为是不一样的)
*/
user.doOperation(choice,bookList);
}
}
}
————————————————
完…
敬请不吝赐教!