大家好,又见面了,我是你们的朋友全栈君。
图书馆借阅系统
一、图书馆借阅系统是对书籍信息操作的信息管理系统。书籍信息包括书名、作者、编号、是否借出。本系统针对两类用户(图书管理员和学生)使用,对于图书馆管理员,可以进行查看书籍列表、查找书籍、新增书籍信息、删除书籍、退出登录操作;对于学生,可以进行查看书籍列表、查找书籍、借阅书籍、归还书籍、退出登录操作。
二、代码部分
1.book包的建立
Book类的建立对于图书馆借阅系统,把书看作一个对象,根据该book所具有的属性,定义一系列书籍的私有属性,以及对这些属性进行操作的访问器(get())和设置器(set())。
package book;
public class Book {
private String name;// 书名
private String author;// 书的作者
private int num;// 书的编号
private boolean flag;// 判断书是否被借出去了
public Book(String name, String author, int num) {
this.name = name;
this.author = author;
this.num = num;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getNum() {
return num;
}
public void setFlag(boolean borrowed) {
flag = borrowed;
}
public boolean getFlag() {
return flag;
}
public String toString() {
return "name:" + name + " author:" + author + " num:" + num +" "+ (flag ? "已经借出" : "未借出");
}
}
Booklist类的建立
Booklist类中的数组用来存放书籍信息,以及针对Booklist类完成增删改等相关操作。
package book;
public class Booklist {
public Book books[];
private int number;
public Booklist() {
this.books = new Book[20];
this.books[0] = new Book("Java语言基础教程", "李东明、张丽娟", 1);
this.books[1] = new Book("C程序设计", "谭浩强", 2); this.books[2] = new Book("高等数学I", "同济", 3); this.books[3] = new Book("高等数学II", "同济", 4); this.books[4] = new Book("数据结构", "严蔚敏", 5); this.number = 5;
}
public void setBook(int b, Book book) {
books[b] = book;
}
public Book getBook(int b) {
return books[b]; } public void setNumber(int n) {
number = n; }
public int getNumber( ) {
return number; }
}
2.operation包的建立
Operation类的建立①这是所有的操作(查找,增加,删除,借阅,归还,退出)共同的接口;②需要一个oper()方法,这个方法针对Booklist类进行操作。
package operation;
import book.Booklist;//接口
public interface Operation {
void oper(Booklist booklist);
}
查找书籍信息(根据书名或者编号)
选择相应的查找功能,根据你输入的name或者number进行查找。
package operation;
import java.util.Scanner;
import book.Booklist;//查找书籍
public class Find implements Operation {
public void oper(Booklist booklist) {
int i;
Scanner reader = new Scanner(System.in); System.out.println("请输入您需要寻找的书名:"); String name = reader.next();
for (i = 0; i < booklist.getNumber(); i++) {
//System.out.println(booklist.getBook(i).getName().equals(name));
if (booklist.getBook(i).getName().equals(name)) {
System.out.println("找到您所需要的书:"+booklist.books[i]);
return;
}
}
if (i >= booklist.getNumber()) {
System.out.println("抱歉!未找到您需要的书!");
}
}
}
package operation;
import java.util.Scanner;
import book.Booklist;根据编号查找书籍
public class Find1 implements Operation{
public void oper(Booklist booklist) {
int i;
Scanner reader = new Scanner(System.in);
System.out.println("请输入您需要寻找的书的编号:");
int number = reader.nextInt();
for (i = 0; i < booklist.getNumber(); i++) {
//System.out.println(booklist.getBook(i).getNum()==number);
if (booklist.getBook(i).getNum()==number){
System.out.println("找到您所需要的书:"+booklist.books[i]);
return;
}
}
if (i >= booklist.getNumber()) {
System.out.println("抱歉!未找到您需要的书!"); }
}
}
增加书籍
在主函数界面输入新的书的信息,将这些信息赋值于Book类相应的信息,并将书插入Booklist类的books数组中。
package operation;import java.util.Scanner;import book.Book;import book.Booklist;//增加书籍
public class Add implements Operation {
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in); System.out.println("请输入需要添加的书籍:");
String name = reader.nextLine(); System.out.println("请输入书籍的作者:");
String author = reader.nextLine(); System.out.println("请输入书籍的编号:");
int num = reader.nextInt();
Book book = new Book(name, author, num);
int a = booklist.getNumber();
booklist.setBook(a, book);
booklist.setNumber(a + 1);
System.out.println("成功增加"+name+"书籍!");
}
}
删除书籍(根据书名或者编号)
package operation;import java.util.Scanner;import book.Book;import book.Booklist;//删除书籍
public class Delete implements Operation {
public void oper(Booklist booklist) {
System.out.println("请输入您需要删除的书籍:"); Scanner reader = new Scanner(System.in);
String name = reader.nextLine();
int de = booklist.getNumber();
int b = 0;
for (int i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getName().equals(name)) {
b = i;
break;
}
}
for (int j = b; j < de - 1; j++) {
booklist.setBook(j, booklist.getBook(j + 1));
}
booklist.setNumber(de - 1); System.out.println("成功删除书籍!");
}
}
package operation;import java.util.Scanner;import book.Book;import book.Booklist;//根据编号删除书籍
public class Delete1 implements Operation{
public void oper(Booklist booklist) {
System.out.println("请输入您需要删除的书籍:"); Scanner reader = new Scanner(System.in);
int number = reader.nextInt();
int de = booklist.getNumber();
int i;
for (i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getNum()==number) {
break;
}
}
if(i>booklist.getNumber()) {
return ;
}
Book book1=booklist.getBook(de-1);
booklist.setBook(i, book1);
booklist.setNumber(de-1);
System.out.println("成功删除书籍!");
}
}
借阅书籍(根据书名或者编号)
package operation;import java.util.Scanner;import book.Book;import book.Booklist;//借阅书籍
public class Borrow implements Operation {
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in); System.out.println("请输入您需要借阅的书籍:"); String name = reader.next();
int i;
for (i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (booklist.getBook(i).getName().equals(name)) {
if (book.getFlag()) {
System.out.println("抱歉!此书籍"+book.getName()+"已经被借阅!");
}
else {
book.setFlag(true); System.out.println("成功借阅书籍"+book.getName()+"!");
break;
}
}
} }}
package operation;import java.util.Scanner;import book.Book;import book.Booklist;
public class Borrow1 implements Operation{
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in); System.out.println("请输入您需要借阅的书籍的编号:"); int number = reader.nextInt();
int i;
for (i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getNum()==number) {
if (book.getFlag()) {
System.out.println("抱歉!此书籍"+book.getName()+"已经被借阅!");
}
else {
book.setFlag(true);
System.out.println("成功借阅书籍"+book.getName()+"!");
break;
} } } }}
归还书籍(根据书名或者编号)
package operation;import java.util.Scanner;import book.Book;import book.Booklist;//归还书籍
public class Return implements Operation {
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in);
System.out.println("请输入您需要归还的书籍:");
String name = reader.nextLine();
for (int i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getName().equals(name)) {
System.out.println("成功归还书籍!");
}
book.setFlag(false); } }}
package operation;import java.util.Scanner;import book.Book;import book.Booklist;
public class Return1 implements Operation{
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in);
System.out.println("请输入您需要归还的书籍:");
int number = reader.nextInt();
for (int i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getNum()==number) {
System.out.println("成功归还书籍!");
}
book.setFlag(false); } }}
退出系统
package operation;import book.Booklist;
public class Exit implements Operation {
public void oper(Booklist booklist) {
System.out.println("谢谢使用图书馆系统!"); System.exit(0);//正常运行程序并退出程序
}}
3.user包的建立
本系统针对两类用户使用(管理员和学生)
用户(抽象类)
package users;import book.Booklist;import operation.Operation;
public abstract class User {
public String name;
public Operation operations[];
public User(String name) {
this.name = name;
}
public abstract int meanu();
public void opera(int choice, Booklist booklist) {
operations[choice].oper(booklist);
}}
管理员
package users;import java.util.Scanner;import operation.Add;import operation.Delete;import operation.Delete1;import operation.Display;import operation.Exit;import operation.Find;import operation.Find1;import operation.Operation;//管理员
public class Administrator extends User {
public int meanu() {
Scanner reader = new Scanner(System.in);
System.out.println("\t-----------------------------------");
System.out.println("\t\t********0.书籍列表********");
System.out.println("\t********1.查找书籍(根据书名)********");
System.out.println("\t********2.查找书籍(根据编号)********");
System.out.println("\t\t********3.增加书籍********");
System.out.println("\t********4.删除书籍(根据书名)********");
System.out.println("\t********5.删除书籍(根据编号)********");
System.out.println("\t\t********6.退出登录********");
System.out.println("\t-----------------------------------");
int choice = reader.nextInt();
return choice;
}
public Administrator(String name) {
super(name);
this.operations = new Operation[] {
new Display(), new Find(),new Find1(), new Add(), new Delete(),new Delete1(), new Exit() };
}}
学生
package users;import java.util.Scanner;import operation.Borrow;import operation.Borrow1;import operation.Display;import operation.Exit;import operation.Find;import operation.Find1;import operation.Operation;import operation.Return;import operation.Return1;//学生
public class Student extends User {
public int meanu() {
Scanner reader = new Scanner(System.in);
System.out.println("\t-----------------------------------");
System.out.println("\t\t********0.书籍列表********");
System.out.println("\t********1.查找书籍(根据书名)********");
System.out.println("\t********2.查找书籍(根据编号)********");
System.out.println("\t********3.借阅书籍(根据书名)********");
System.out.println("\t********4.借阅书籍(根据编号)********");
System.out.println("\t********5.归还书籍(根据书名)********");
System.out.println("\t********6.归还书籍(根据编号)********");
System.out.println("\t\t********7.退出登录********");
System.out.println("\t-----------------------------------");
int choice = reader.nextInt();
return choice; }
public Student(String name) {
super(name);
this.operations = new Operation[] {
new Display(), new Find(),new Find1(), new Borrow(),new Borrow1(), new Return(),new Return1(), new Exit() };
}}
4.Main类的建立
在Main类中有一个系统入口的主函数界面,在这个界面里,通过输入的功能号选择用户想要实现的功能,这个主函数的相应功能的实现通过与operation包内的类的方法进行交互。
package main;import java.util.Scanner;import book.Booklist;import users.Administrator;import users.Student;import users.User;//登录界面
class Enter {
private static String password = "1234567";//设置密码为123456
private static String password1 = "123456";//设置密码为123456
public static User login() {
Scanner reader = new Scanner(System.in);
System.out.println("********登录********");
System.out.println("请输入用户名:");
String name = reader.nextLine(); //管理员登录
if ("administrator".equals(name)) {
System.out.println("请输入密码:");
if (reader.next().equals(password)) {
System.out.println("欢迎使用图书馆借阅系统!");
User Administrator = new Administrator(name);
return Administrator;
}
else {
System.out.println("密码错误!请重新输入密码!"); } } //学生登录
if ("student".equals(name)) {
System.out.println("请输入密码:");
if (reader.next().equals(password1)) {
System.out.println("欢迎使用图书馆借阅系统!");
User Student = new Student(name);
return Student;
}
else {
System.out.println("密码错误!请重新输入密码!"); }
}
return null; }}
public class Main {
public static void main(String args[]) {
Booklist booklist = new Booklist();
User user = Enter.login();
while (true) {
int choice = user.meanu();
user.opera(choice, booklist);
}
}}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/128815.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...