今天我们来练习一个小项目:图书管理系统 我们先来看一下这个项目的运行效果: 使用者分为管理员和普通用户。
我们先来看一下普通用户的操作:
我们再来看一下管理员的操作:
接下来我们一起来看一下这个程序的代码:
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;
}
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;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed() == true)? "该书已经借出":"该书未借出")+
'}';
}
}
package book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;//储存当前书的个数
public BookList(){
books[0] = new Book("三体地球往事","刘慈欣",33,"小说");
books[1] = new Book("三体黑暗森林","刘慈欣",34,"小说");
books[2] = new Book("三体死神永生","刘慈欣",35,"小说");
this.usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getPos(int pos){
return books[pos];
}
public void setBooks(Book book,int pos){
books[pos] = book;
}
}
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("新增图书!");
System.out.println("请输入你要新增的图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入你要新增图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入你要新增图书的价格:");
int price = scanner.nextInt();
scanner.nextLine();//读入回车
System.out.println("请输入你要新增图书的类型:");
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
//1.获取当前可以存放的书籍
int currentSize = bookList.getUsedSize();
//2.把书放在指定位置
bookList.setBooks(book,currentSize);
//书的有效个数
bookList.setUsedSize(currentSize+1);
}
}
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOPeration{
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.getPos(i);
if (name.equals(book.getName())) {
if (book.isBorrowed()) {
System.out.println("该书已经被借出!");
} else {
book.setBorrowed(true);
System.out.println("借阅图书成功!");
}
return;
}
}
System.out.println("没有你要借阅的书!");
}
}
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("删除图书!");
System.out.println("请输入要删除的图书:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
//1.遍历数组当中 是否有你要删除的书,有记录下标
int index = -1;
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getPos(i);
if(name.equals(book.getName())){
index = i;
break;
}
}
if(index == -1){
System.out.println("没有你要删除的书!");
return;
}
for (int i = index; i < currentSize-1; i++) {
Book book = bookList.getPos(i+1);
bookList.setBooks(book,i);
}
bookList.setBooks(null,currentSize-1);
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功!");
}
}
package operations;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("显示所有图书!");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getPos(i);
System.out.println(book);
}
}
}
package operations;
import book.BookList;
public class ExitOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
package operations;
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.getPos(i);
if(name.equals(book.getName())){
System.out.println("找到了这本书!");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
}
package operations;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);
}
package operations;
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.getPos(i);
if(name.equals(book.getName())){
book.setBorrowed(false);
System.out.println("归还图书成功!");
return;
}
}
System.out.println("没有你要归还的图书!");
}
}
package user;
import operations.*;
import java.util.Scanner;
public class AdminerUser extends User{
public AdminerUser(String name){
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
public int menu(){
System.out.println("**************************************");
System.out.println("hello "+name+ "欢迎来到图书管理系统!");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("**************************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choic = scanner.nextInt();
return choic;
}
}
package user;
import operations.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.ioPerations =new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
public int menu(){
System.out.println("**************************************");
System.out.println("hello "+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 choic = scanner.nextInt();
return choic;
}
}
package user;
import book.BookList;
import operations.IOPeration;
public abstract class User {
protected String name;
protected IOPeration[] ioPerations;
public User(String name){
this.name = name;
}
public abstract int menu();
public void doOperation(int choic, BookList bookList){
ioPerations[choic].work(bookList);
}
}
import book.BookList;
import user.AdminerUser;
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 userName = scanner.nextLine();
System.out.println("请输入你的身份:1-》管理员 0-》普通用户");
int choic = scanner.nextInt();
//根据用户输入的数字判断身份 发生向上转型
if(choic == 1){
return new AdminerUser(userName);
}else{
return new NormalUser(userName);
}
}
public static void main(String[] args) {
//0.准备数据
BookList bookList = new BookList();
//1.登录
User user = login();
while (true){
int choic = user.menu();
user.doOperation(choic,bookList);
}
}
}