java小项目——图书管理系统
当我们学完了javaSE之后,总感觉有些空虚——学了这些东西能做些什么呢?
今天给大家介绍一个项目——图书管理系统小项目,一个包含了封装、继承、多态、异常的项目,将自己所学到的知识有运用出来!
项目大概组成:
我们将实现一个:管理员可以增加、删除、查找书籍,用户可以查看、借阅、归还书籍
我们将对每一个大的分类开辟一个包,方便对其进行管理。
书架包
对于书架这个包,我们将创建两个类:书、书架。
书
书是用于描述书这个抽象类,将包含书的名字,作者,价格,类型,是否被借出;
而对于书我们只实现一些基本都get和set操作,并重写toString方法。
package book;
import java.util.Objects;
public class Book {
private String name;
private String athour;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String athour, int price, String type) {
this.name = name;
this.athour = athour;
this.price = price;
this.type = type;
this.isBorrowed = isBorrowed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAthour() {
return athour;
}
public void setAthour(String athour) {
this.athour = athour;
}
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 setBorrown(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", athour='" + athour + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed == true) ? " 已经借出" : " 未被借出") +
'}';
}
}
书架类
书架类的成员变量也比较简单:书数组和记录书数组内有效书的大小。
书架类的方法我们只是有简单的set和get方法
package book;
public class BookList {
private Book[] books;
private int usedSize;
public static final int maxSize=10;
public BookList() {
this.books = new Book[maxSize];
this.usedSize = 3;
this.books[0]=new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",9,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
}
public Book getBooks(int pos) {
return books[pos];
}
public void setBooks(Book books,int pos) {
this.books[pos] = books;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
操作包
对于操作包内的类,我们讲使用接口来实现——为什么使用接口?
因为对于不同的操作,只需要使用同一个接口,我们就行只用一个接口来调用所有操作,这大大减少了我们的代码量和增加阅读便利。
操作接口
package operation;
import book.BookList;
public interface IOopertation {
void work(BookList books);
}
增加书籍类
由于我们要对书籍类的对象进行创建,并将书籍放入书架的操作,所以我们需要导入书籍类和书架类
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOpertation implements IOopertation{
@Override
public void work(BookList books) {
System.out.println("输入书名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("请输入类型:");
String type = scanner.nextLine();
System.out.println("请输入价格:");
int price = scanner.nextInt();
Book tempbook=new Book(name,author,price,type);
//创建书籍对象
int currentsize= books.getUsedSize();
//查看是否有重复的
for (int i = 0; i < currentsize; i++) {
Book book1 = books.getBooks(i);
if(book1.getName().equals(name)) {
System.out.println("有这本书,不进行存放了!");
return;
}
}
//书架容量是否满了
if(books.getUsedSize()==BookList.maxSize){
System.out.println("书架满了");
}else{
//未满
books.setBooks(tempbook,currentsize);
//容量加一
books.setUsedSize(currentsize+1);
}
}
}
删除书籍类
引入书架包中的书籍类和书架类
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Deleteopertation implements IOopertation{
@Override
public void work(BookList book) {
System.out.println("删除图书!");
System.out.println("请输入要删除的图书");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentsize= book.getUsedSize();
int pos=-1;
for (int i = 0; i < currentsize; i++) {
Book tempbook=book.getBooks(i);
if(tempbook.getName().equals(name)){
pos=i;
break;
}
if(i == currentsize) {
System.out.println("没有你要删除的图书!");
return;
}
}
int i=0;
//将空出来的位置填充
for (i = pos; i < currentsize-1; i++) {
Book temp=book.getBooks(i+1);
book.setBooks(temp,i);
}
book.setBooks(null,i);
book.setUsedSize(currentsize-1);
}
}
借阅书架类
引入书架包中的书籍类和书架类
在此操作中,我们查阅到你想要的书,并将书的是否借阅的值改为true
package operation;
import book.BookList;
import book.Book;
import java.util.Scanner;
public class Borrowoperation implements IOopertation{
@Override
public void work(BookList books) {
System.out.println("请输入要借阅的书籍:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentsize= books.getUsedSize();
for (int i = 0; i < currentsize; i++) {
Book tempbook=books.getBooks(i);
if(tempbook.getName().equals(name)){
if(tempbook.isBorrowed()==false){
System.out.println("借阅成功!");
tempbook.setBorrown(true);
return;
}
else {
System.out.println("已经被借阅!");
}
}
}
System.out.println("没有此书!");
}
}
退出类
package operation;
import book.BookList;
public class Exitopertation implements IOopertation{
@Override
public void work(BookList book) {
System.out.println("退出系统");
System.exit(0);
}
}
查找书籍类
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Findopertation implements IOopertation{
@Override
public void work(BookList book) {
System.out.println("请输入你所查询的书籍:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentsize= book.getUsedSize();
for (int i = 0; i < currentsize; i++) {
Book tempbook=book.getBooks(i);
if(tempbook.getName().equals(name)){
System.out.println("找到你所查询的书籍了:");
System.out.println(tempbook);
}
}
System.out.println("书库中没有此书籍!");
}
}
归还书籍类
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOopertation{
@Override
public void work(BookList book) {
System.out.println("归还图书!");
Scanner scanner=new Scanner(System.in);
System.out.println("输入要归还的书籍:");
String name=scanner.nextLine();
for (int i = 0; i < book.getUsedSize(); i++) {
Book tempbook=book.getBooks(i);
if(tempbook.getName().equals(name)){
tempbook.setBorrown(false);
System.out.println("归还成功");
System.out.println(tempbook);
}
}
System.out.println("你归还的图书 不存在!! ");
}
}
打印全部书籍类
package operation;
import book.Book;
import book.BookList;
import com.sun.javaws.IconUtil;
public class ShowOperation implements IOopertation{
@Override
public void work(BookList book) {
System.out.println("打印图书:");
int currentsize= book.getUsedSize();
for (int i = 0; i < currentsize; i++) {
Book tempbook=book.getBooks(i);
System.out.println(tempbook);
}
}
}
用户包
对于用户包,我们在包下会创建两个类:普通用户,管理员,但是我们在实现时,发现,两个类有很多相似的地方,于是就用一个名为的user的父类,来包含他们之间的共同之处。
user父类
我们作为使用人员,可以进行不同的操作,所以我们使用了一个接口数组,来实现我们不同的操作
对于用户来说,都有点一个菜单栏
package user;
import operation.IOopertation;
import book.BookList;
public abstract class user {
protected String name;
protected IOopertation[] ioopertation;
public user(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
ioopertation[choice].work(bookList);
}
}
adminUser
对于管理人员来说,可以使用的操作为:增删查改,和打印全部书籍
package user;
import operation.*;
import java.util.Scanner;
public class adminUser extends user {
public adminUser(String name) {
super(name);
this.ioopertation= new IOopertation[]{
new Exitopertation(),
new Findopertation(),
new AddOpertation(),
new Deleteopertation(),
new ShowOperation(),
};
}
@Override
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);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
normalUser
package user;
import operation.*;
import java.util.Scanner;
public class adminUser extends user {
public adminUser(String name) {
super(name);
this.ioopertation= new IOopertation[]{
new Exitopertation(),
new Findopertation(),
new AddOpertation(),
new Deleteopertation(),
new ShowOperation(),
};
}
@Override
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);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
管理员登陆异常
密码异常
package user;
public class PasswardException extends RuntimeException{
public PasswardException(String message) {
super(message);
}
}
用户名异常
package user;
public class UsernameException extends RuntimeException{
public UsernameException(String message) {
super(message);
}
}
登陆
import book.BookList;
import user.*;
import java.util.Scanner;
public class Login {
private static final String userName="admin";
private static final String passward="123456";
//返回不同的用户类
public static user login(){
System.out.println("请输入你的身份,1:管理员 2:普通用户-》");
Scanner in=new Scanner(System.in);
int choice=in.nextInt();
if(choice==1){
System.out.println("请输入管理员姓名:");
in = new Scanner(System.in);
String name=in.nextLine();
//抛出异常
if(!name.equals(Login.userName)) {
throw new UsernameException("管理员名字输入错误");
}
System.out.println("请输入管理员密码");
String passwords=in.nextLine();
//抛出异常
if(!passwords.equals(Login.passward)){
throw new PasswardException("管理员密码错误");
}
return new adminUser(name);
}
else{
System.out.println("请输入你的姓名:");
String name=in.nextLine();
return new normalUser(name);
}
}
public static void main(String[] args) {
//创建书架
BookList bookList = new BookList();
//可能有异常
try {
//对用户类型进行选择,用父类对象接受子类对象,向上转型
user huser=login();
//不停的执行操作,知道选择退出
while(true){
int choice= huser.menu();
huser.doOperation(choice,bookList);
}
}catch (UsernameException x){
x.printStackTrace();
}catch (PasswardException x){
x.printStackTrace();
}
}
}
总结
在这个小项目中,我们运用了包对类进行管理,运用接口来实现调用的简化——调用接口就可以调用使用了接口的类,运用了继承来实现了多态,减少了代码量,提高了代码可读性。