带你编写图书管理系统(java)
很多同学在学校学习完Java后,就要开始做一个管理系统来通过课设考核,今天我就来交大家来实现一个图书管理系统。
一, 涉及的知识点
1.类和对象;(类是对象的实例化引用)
2.接口;
3.封装和继承;
4.顺序链表存储数据;
二,分析图书管理系统
图书管理系统就是对图书的增删查改,那么由谁来控制呢?我们一般的系统都是分为管理员和普通用户,因此我们是不是可以分析出该系统就是用户通过一系列的操作来控制图书。 那么该系统是不是只需要实现用户,操作活动,图书三个结构来实现呢?
三,定义图书,操作,用户类
1.图书类book
1. 我们先在idea中创建一个文件夹命名为图书管理系统,打开文件夹,点击src 新建一个Package ,并命名为book
2. 点击book包,新建一个javaclass文件,命名为book;在点击book包新建一个javaclass文件,命名为BookList
3.点击book Java文件,图书有书名name,作者acthor,价格price,类型type,是否借阅tatues5个属性;因此定义这些属性,然后构造方法,get,set,其属性,然后添加一个 tostring方法;源代码如下
book
package book;
public class Book {
private String name;
private String acthor;
private int price;
private String type;
private boolean statues;
public Book(String name, String acthor, int price, String type) {
this.name = name;
this.acthor = acthor;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public String getActhor() {
return acthor;
}
public int getPrice() {
return price;
}
public String getType() {
return type;
}
public boolean isStatues() {
return statues;
}
public void setName(String name) {
this.name = name;
}
public void setActhor(String acthor) {
this.acthor = acthor;
}
public void setPrice(int price) {
this.price = price;
}
public void setType(String type) {
this.type = type;
}
public void setStatues(boolean statues) {
this.statues = statues;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", acthor='" + acthor + '\'' +
", price=" + price +
", type='" + type + '\'' +
", statues=" + ((statues==true)?" 借出 ":" 未借出 ")+
'}';
}
}
2.图书链表BookList类;
1.在book包下的BookList类下定义一个单链表,首先我们知道单链表的底层是数组,所以我们定义一个数组Book[ ] book和数组的使用长度int usedsize;然后构造方法,get ,set其属性。
2.我们开始首先在单链表中先存储四本图书,中国的四大名著,因为我们已经使用了数组的4个长度,所以此时的this.usedsize=4 ;
public BookList() {
this.books = new Book[100];
books[0]=new Book(“三国演义”,“罗贯中”,120,“小说”);
books[1]=new Book(“西游记”,“吴承恩”,110,“小说”);
books[2]=new Book(“红楼梦”,“曹雪芹”,150,“小说”);
books[3]=new Book(“水浒传”,“斯奈庵”,152,“小说”);
this.usedsize=4;
}
源代码如下;
BookList
package book;
public class BookList {
private int usedsize;
private Book[] books;
public BookList() {
this.books = new Book[100];
books[0]=new Book("三国演义","罗贯中",120,"小说");
books[1]=new Book("西游记","吴承恩",110,"小说");
books[2]=new Book("红楼梦","曹雪芹",150,"小说");
books[3]=new Book("水浒传","斯奈庵",152,"小说");
this.usedsize=4;
}
//下面可以写一些功能;
public void setBooks(int pos,Book book){
this.books[pos]=book;
}
public Book getBook(int pos){
return this.books[pos];
}
public int getUsedsize() {
return usedsize;
}
public Book[] getBooks() {
return books;
}
public void setUsedsize(int usedsize) {
this.usedsize = usedsize;
}
}
3.操作类
1.点击src,创建一个新的包Operation.然后在这个包下新建7个类,分别是Addbook(添加图书),DelBook(删除图书),DisplayBook(显示所有图书),BorrowBook(借书),ReturnBook(归还图书),FoundBook(查找图书),ExitBook(退出系统),最后添加一个接口IBook用于调用功能。
源代码如下:
Addbook(添加图书)
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddBook implements IBook{
@Override
public void work(BookList bookList) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入图书名字:");
String name = sc.nextLine();
System.out.print("请输入图书作者:");
String acthor = sc.nextLine();
System.out.print("请输入图书价格:");
int price = sc.nextInt();
System.out.print("请输入图书类别:");
String type = sc.next();
Book book = new Book(name,acthor,price,type);
int len = bookList.getUsedsize();
bookList.setBooks(len,book);
bookList.setUsedsize(len+1);
System.out.println("添加成功!");
}
}
DelBook(删除图书)
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelBook implements IBook {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入需要删除的图书书名:");
String name = scanner.nextLine();
int i = 0;
for (; i < bookList.getUsedsize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
break;
}
}
if (i >= bookList.getUsedsize()) {
System.out.println("没有要删除的书!");
return;
}
int pos = i;
for (int j = pos; j < bookList.getUsedsize() - 1; j++) {
Book book = bookList.getBook(j + 1);
bookList.setBooks(j, book);
}
bookList.setUsedsize(bookList.getUsedsize() - 1);
System.out.println("删除成功!");
}
}
DisplayBook(显示所有图书)
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DisplayBook implements IBook {
@Override
public void work(BookList bookList) {
for (int i = 0; i < bookList.getUsedsize(); i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
System.out.println("以上是全部图书");
}
}
BorrowBook(借书)
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowBook implements IBook {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入需要查找图书的书名:");
String name = scanner.nextLine();
for (int i =0;i<bookList.getUsedsize();i++){
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
if(book.isStatues()==true){
System.out.println("该图书已经借出,无法借阅!");
return;
}else{
book.setStatues(true);
return;
}
}
}
System.out.println("没有这本书,无法借阅");
}
}
ReturnBook(归还图书)
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnBook implements IBook {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入需要归还的图书的书名:");
String name = scanner.nextLine();
for (int i =0;i<bookList.getUsedsize();i++){
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
if(book.isStatues()==false){
System.out.println("该图书没有被借出,请重新输入!");
return;
}else{
book.setStatues(false);
System.out.println("归还成功!");
return;
}
}
}
System.out.println("没有这本书,无法归还");
}
}
FoundBook(查找图书)
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FoundBook implements IBook{
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入需要查找图书的书名:");
String name = scanner.nextLine();
for (int i =0;i<bookList.getUsedsize();i++){
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("查找成功,有这本书!");
System.out.println(book);
return;
}
}
System.out.println("没有这本书");
}
}
ExitBook(退出系统)
package Operation;
import book.BookList;
public class ExitBook implements IBook{
@Override
public void work(BookList bookList) {
System.exit(-1);
}
}
IBook接口
package Operation;
import book.BookList;
public interface IBook {
void work(BookList bookList);
}
4.用户类的实现
1.用户分为管理员和普通用户,但是他们都是用户,因此点击src创建一个包命名为user,在这个包中创建三个类User(用户),AdminUser(管理员),NormalUser(普通用户)。
2.在类User中定义其属性,姓名和一个存储操作的一个数组IBook[ ],然后构造方法。在AdminUser(管理员)NormalUser(普通用户)类中我们不需要重新定义,只需要继承父类User中的构造方法和属性;然后分别在AdminUser(管理员),NormalUser(普通用户)创建一个数组operation,在里面加入你对应的一些操作,通过键盘输入的数字,访问数组下标,选择方法并调用函数。
(管理员)菜单
System.out.println("==========================");
System.out.println("欢迎"+name+"使用图书管理系统");
System.out.println(" 1.查找图书 ");
System.out.println(" 2.新增图书 ");
System.out.println(" 3.删除图书 ");
System.out.println(" 4.显示所有图书 ");
System.out.println(" 5.退出系统 ");
System.out.println("===========================");
System.out.println("请输入你的选择:");
(普通用户) 菜单
System.out.println("==========================");
System.out.println("欢迎"+name+"使用图书管理系统");
System.out.println(" 1.查找图书 ");
System.out.println(" 2.新增图书 ");
System.out.println(" 3.删除图书 ");
System.out.println(" 4.显示所有图书 ");
System.out.println(" 5.退出系统 ");
System.out.println("===========================");
System.out.println("请输入你的选择:");
源代码:
User
package user;
import Operation.IBook;
import book.Book;
import book.BookList;
abstract public class User {
protected String name;
protected IBook[] operation;
public User(String name) {
this.name = name;
}
abstract public int menu();
public void doOperation(int choice, BookList bookList){
operation[choice-1].work(bookList);
}
}
AdminUser
package user;
import Operation.*;
import java.util.Scanner;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.operation = new IBook[]{
new FoundBook(),
new AddBook(),
new DelBook(),
new DisplayBook(),
new ExitBook()
};
}
@Override
public int menu() {
System.out.println("==========================");
System.out.println("欢迎"+name+"使用图书管理系统");
System.out.println(" 1.查找图书 ");
System.out.println(" 2.新增图书 ");
System.out.println(" 3.删除图书 ");
System.out.println(" 4.显示所有图书 ");
System.out.println(" 5.退出系统 ");
System.out.println("===========================");
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.operation = new IBook[]{
new FoundBook(),
new BorrowBook(),
new ReturnBook(),
new ExitBook()
};
}
@Override
public int menu() {
System.out.println("=================+++++++++");
System.out.println("欢迎"+name+"使用图书管理系统");
System.out.println(" 1.查找图书 ");
System.out.println(" 2.借阅图书 ");
System.out.println(" 3.归还图书 ");
System.out.println(" 4.退出系统 ");
System.out.println("=================++++++++++");
System.out.println("请输入你的选择:");
Scanner scanner=new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
5.Main
我们通过分析和编程,终于把所有的功能实现了,现在我们要把我们的程序运行起来,就要通过Main函数进入;
源代码:
import book.BookList;
import user.User;
import user.AdminUser;
import user.NormalUser;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while (true){
int choice = user.menu();
user.doOperation(choice,bookList);
}
}
public static User login(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name =sc.nextLine();
System.out.println("请输入身份 1--》管理员登录 2--》普通用户登录");
int choice = sc.nextInt();
if (choice == 1){
return new AdminUser(name);
}else{
return new NormalUser(name); //通过判断验证创建一个登录菜单;向上转型;
}
}
}
运行截图:
管理员登录:
普通用户登录:
全部源代码大家通过Githu链接下载学习:https://github.com/41809310102/study/tree/master/javashanji/src