Java之图书管理系统
思路:
1.最终程序在Main上运行
2.书
3.操作
4.用户
book
Book
package book;
public class Book {
private String name;//书名
private String author;//作者
private String price;//价格
private String type;//类型
private boolean isBorrowed;//是否被借出 默认是false
public Book(String name, String author, String price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
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 String getPrice() {
return price;
}
public void setPrice(String 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;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price='" + price + '\'' +
", type='" + type + '\'' +
/* ", isBorrowed=" + isBorrowed */((isBorrowed == true)?"已经被借出":"未被借出")+
'}';
}
}
BookList
package book;
/*书架
*
*
* */
public class BookList {
private Book[] books = new Book[10];
private int usedSize;//计数器 来记录 当前实际放的书的数目
public BookList(){
//构造方法 来初始化成员 当实例化BookList这个对象的时候,就能有三本书
this.books[0] = new Book("三国演义","罗贯中","10","小说");
this.books[1] = new Book("西游记","吴承恩","23","小说");
this.books[2] = new Book("红楼梦","曹雪芹","8","小说");
this.usedSize = 3;
}
public Book getBook(int pos){
return books[pos];//返回pos位置的书
}
public void setBooks(int pos,Book book){
books[pos] = book;//在pos位置放一本书
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
operation
IOPeration
package operation;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);//标准制定
}
AddOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOPeration{
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 author = scanner.nextLine();
System.out.println("请输入价格:");
String price = scanner.nextLine();
scanner.nextLine();//多读一次(整型在上面,字符串在下面,会把回车都进去)
System.out.println("请输入类型:");
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();//!!!!!
//如果有这本书 就不能添加了
for(int i = 0;i < currentSize;i++){
Book book1 = bookList.getBook(i);
if(book1.getName().equals(name)){
System.out.println("书架存在这种书,不能进行添加!");
return;
}
}
//默认放到了数组的最后的位置
bookList.setBooks(currentSize,book);
//让usedSize++
bookList.setUsedSize(currentSize+1);
}
}
BorrowedOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowedOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();//!!!!!
for(int i = 0;i < currentSize;i++){
Book book1 = bookList.getBook(i);
if(book1.getName().equals(name)){
if(book1.isBorrowed()==false){
book1.setBorrowed(true);
System.out.println("借阅成功");
}else{
System.out.println("这本书已经被借走了");
}
return;
}
}
System.out.println("没有你要借阅的图书!");
}
}
DelOperation
package operation;
import java.util.Scanner;
import book.BookList;
import book.Book;
public class DelOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
//1.找到你要删除的书
int currentSize = bookList.getUsedSize();
if(currentSize == 0){
System.out.println("书架为空,不能删除!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
int index = -1;
for(int i = 0;i<currentSize;i++){
Book book1 = bookList.getBook(i);
if(book1.getName().equals(name)){
index = i;
break;
}
}
//2.index != -1 有这本书 开始删除
if(index == -1){
System.out.println("没有你要删除的书!");
return;
}
for(int i = index;i<currentSize -1;i++){
Book book1 = bookList.getBook(i+1);
bookList.setBooks(i,book1);
}
//当书被删掉之后,需要维持usedSize
bookList.setUsedSize(currentSize-1);
}
}
ExitOperation
package operation;
import book.BookList;
public class ExitOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
int currentSize = 0;
for(int i = 0; i < currentSize; i++){
bookList.setBooks(i,null);
}
bookList.setUsedSize(0);
System.exit(0);
}
}
FindOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration {
public void work(BookList bookList){
System.out.println("查找图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要查找的图书:");
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)){
System.out.println("找到了你查找的书:");
System.out.println(book);
return;
}
}
System.out.println("不好意思,你好像没有这笔书!");
}
}
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("归还图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();//!!!!!
for(int i = 0;i < currentSize;i++){
Book book1 = bookList.getBook(i);
if(book1.getName().equals(name)){
book1.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("没有你要归还的图书!");
}
}
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
User
package user;
import book.BookList;
import operation.IOPeration;
public abstract class User {
protected String name;
public IOPeration[] ioPerations;//只是定义并没有初始化大小
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList ){
ioPerations[choice].work(bookList);
}
}
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("hello"+this.name+"欢迎来到管理员菜单!");
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;
}
}
NormalUser
package user;
import operation.*;
import java.util.Scanner;
/*普通用户*/
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowedOperation(),
new ReturnOperation(),
};
}
public int menu(){
System.out.println("*********************");
System.out.println("hello "+this.name+"欢迎来到普通用户菜单!");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("**********************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
Main
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main{
//可以利用返回值的向上转型 达到返回的一致性
public static User login(){
//登陆系统
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = scanner.nextLine();
System.out.println("请输入你的身份:1-》管理员 0-》普通用户");
int ret = scanner.nextInt();
if(ret == 1){
/*AdminUser adminUser = new AdminUser(name);
return adminUser;*/
return new AdminUser(name);
}else{
/*NormalUser normalUaer = new NormalUser(name);
return normalUaer;*/
return new NormalUser(name);
}
}
public static void main(String[] args){
BookList bookList = new BookList();//实例化书架这个对象,默认有三本书
User user = login();//向上转型 有可能是管理员/普通用户
while(true){
int choice = user.menu();
//user是哪个对象?choice是几--》能够确定:我能够操作哪个对象的哪个方法
//通过这两个变量 可以确定了 但是怎么联系起来?
/*
*1.先让双方存好对应自己的操作
*2.就是调用对应的操作!
**/
user.doOperation(choice, bookList);//获得对应用户的对用操作
}
}
}