多平台统一管理软件接口,如何实现多平台统一管理软件接口
337
2022-10-30
java实现图书管理系统
本文通过实例为whiXyPg大家分享了java实现图书管理系统的具体代码,供大家参考,具体内容如下
一、背景介绍
通过一段时间java编程的学习,需要一个比较综合的实例来进行编程的练习,是一个对前段时间所学内容进行总合提升的一个契机。选择了图书管理系统,会用到的javaSE知识有:变量、包、继承、类、接口、循环结构等。是一个很综合的典例。
二、核心需求
1.用户可以登录到系统上 分为 管理员、普通用户两种角色,这两种不同的角色根据自己的身份可以实现不同的操作。
普通用户
a)查阅某个书籍的信息
b)借阅书籍
c) 归还书籍
d)退出程序
管理员
a)查阅某个书籍的信息
b)增加书籍
c) 删除书籍
d)查看书籍列表
e)退出程序
程序框架结构图
三、代码以及详解
1.User类
package booksystem;
import booksystem.operation.IOperation;
abstract public class User {
protected String name;//定义书名
protected IOperation[] operations;//定义一个接口数组
public abstract int menu();//是用户和管理员的父类,不进行实例化,所以定义为抽象方法
public void doOperation(int choice,BookList bookList){
IOperation operation=this.operations[choice-1];
operation.work(bookList);
}
}
User类是NormalUser类和Admin类的父类,由于不需要实例化,将menu()函数定义为了抽象函数。
2.NormalUser类
package booksystem;
import booksystem.operation.*;
import booksystem.operation.IOperation;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
this.name = name;
this.operations = new IOperation[]
{
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new ExitOperation(),
};
}
@Override
public int menu(){
System.out.println("~~~~~~~~~~~~~~~~~~");
System.out.println("Hello"+name+"Welcome to use booksyetem");
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;//返回一个整型数
}
}
NormalUser类针对与普通用户而编写,在代码中定义了一个接口数组,在其中添加了普通用户需要用到的查阅、借阅、归还、退出系统的四大功能,在menu()函数中也按照同样的顺序,menu()函数是重写父类的,所以为了提醒编译器,在函数头前加上了 @Override进行提示。
3.Admin类
package booksystem;
import booksystem.operation.*;
import java.util.Scanner;
public class Admin extends User {
public Admin(String name){
this.name=name;
this.operations=new IOperation[]{
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
new ExitOperation(),
};
}
@Override
public int menu(){
System.out.println("~~~~~~~~~~~~~~~~~~");
System.out.println("Hello"+name+"Welcome to use booksyetem");
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;
}
}
Admin类编写的思路和NormalUser类的思路相同,区别在于用户界面的不同,对应要使用的功能也不同,分别是查阅、新增、删除和退出系统。
4.Book类
package booksystem;
public class Book {
private String name;
private String author;
private double price;
private String type;
private boolean isBorrowed = false;
public Book(String name, String author, double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name=" + name + '\'' + ",author" + author + '\'' + ",price=" + price + ",type='"
+ '\'' + type + '\'' + ",isBorrow=" + isBorrowed + '}';
}
public String getName() {
return name;
}
public boolean isBorrowed(){
return isBorrowed;
}
public void setBorrowed(boolean borrowed){
isBorrowed=borrowed;
}
}
5.BookList类
package booksystem;
public class BookList {
private Book[] books=new Book[100];//定义一book数组
private int size=0;
public BookList(){
books[0]=new Book("计算机网络教程","郝文源",125,"专业书籍");
books[1]=new Book("盗墓笔记","南派三叔",150,"网络小说");
books[2]=new Book("三体","刘慈欣",178,"科幻小说");
size = 3;
}//给book数组中初始化一些书
public Book getBook(int index){
return books[index];
}
public void setBook(int index,Book book)
{
books[index]=book;
}
public int getSize(){
return size;
}
public void setSize(int size){
this.size=size;
}
}
BookList类中定义了一个book数组,并给数组中初始化了一些书,定义了常用的功能函数
6.Main类
package booksystem;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Object o=null;
BookList booklist= new BookList();
User user=login();//上转型,这里先调用了login()函数,返回一个Admin对象或NormalUser对象
while(true){
int choice=user.menu();
user.doOperation(choice,booklist);
}//在进行退出系统的功能时,会一直进行循环,menu()函数最终会返回一个整型数,对应选择操作中的一项
}
public static User login() {
System.out.println("请输入您的姓名");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println("请输入您的角色:1 管理员 0 普通用户");//根据不同的选择创建对应的对象
int who = scanner.nextInt();
if (who == 1) {
return new Admin(name);
}
return new NormalUser(name);
}
}
主函数中主要实现了login()函数,根据登录系统用户的选择,决定不同的身份,返回两种对象中的一种,在while循环中,只要不进行exit功能,循环便会一直执行。
7.IOperation
package booksystem.operation;
import booksystem.BookList;
public interface IOperation {
void work(BookList bookList);
}
9.AddOperation
package booksystem.operation;
import booksystem.Book;
import booksystem.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList){
Scanner scanner=new Scanner(System.in);
System.out.println("新增书籍");
System.out.println("请输入新书籍的名称");
String name=scanner.next();
String author=scanner.next();
System.out.println("请输入新书籍的价格");
double price=scanner.nextDouble();
System.out.println("请输入新书籍的类别");
String type=scanner.next();
Book newBook=new Book(name,author,price,type);
int curSize=bookList.getSize();
bookList.setBook(curSize,newBook);
bookList.setSize(curSize+1);
System.out.println("新增书籍成功");
}
}
10.BorrowOperation
package booksystem.operation;
import booksystem.Book;
import booksystem.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList)
{
Scanner scanner=new Scanner(System.in);
System.out.println("借阅书籍");
System.out.println("请输入要借阅的书籍的名称");
String name=scanner.next();
int i=0;
for(;i { if(bookList.getBook(i).getName().equals(name)){ break; } } if(i>=bookList.getSize()){ System.out.println("未找到指定的书籍,无法借阅!"); return; } Book currentBook=bookList.getBook(i); if(currentBook.isBorrowed()){ System.out.println("该书籍已经被借出!"); return; } bookList.getBook(i).setBorrowed(true); System.out.println("借书成功!"); } } 11.DelOperation package booksystem.operation; import booksystem.BookList; import java.util.Scanner; public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { Scanner scanner=new Scanner(System.in); System.out.println("删除书籍"); System.out.println("请输入要删除的书籍的名称"); String name=scanner.next(); int i=0; for(;i { if(bookList.getBook(i).getName().equals(name)){ break; } } if(i>=bookList.getSize()){ System.out.println("您输入的书籍在+"+name+"在系统中没有找到!删除失败!"); return; } if(i==bookList.getSize()-1) { bookList.setSize(bookList.getSize()-1); System.out.println("删除成功!"); return; } bookList.setBook(i,bookList.getBook(bookList.getSize()-1)); bookList.setSize(bookList.getSize()-1); System.out.println("删除成功!"); } } 13.ExitOperation package booksystem.operation; import booksystem.BookList; public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出程序"); http://System.exit(0); } } 14.FindOperation package booksystem.operation; import booksystem.BookList; public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出程序"); System.exit(0); } } 15.ReturnOperation package booksystem.operation; import booksystem.BookList; import booksystem.Book; import java.util.Scanner; public class ReturnOperation implements IOperation{ @Override public void work(BookList bookList){ Scanner scanner=new Scanner(System.in); System.out.println("归还书籍"); System.out.println("请输入要还的书籍的名称"); String name=scanner.next(); int i=0; for(;i { Book book=bookList.getBook(i); if(book.getName().equals(i)) { break; } } if(i>=bookList.getSize()) { System.out.println("书籍没有找到,无法归还"); return; } Book currentBook=bookList.getBook(i); if(!currentBook.isBorrowed()) { System.out.println("这本书没有借出,无法归还"); } currentBook.setBorrowed(false); System.out.println("归还书籍成功"); return; } } 四、编程截图及测试图 包和类放置图 运行截图
{
if(bookList.getBook(i).getName().equals(name)){
break;
}
}
if(i>=bookList.getSize()){
System.out.println("未找到指定的书籍,无法借阅!");
return;
}
Book currentBook=bookList.getBook(i);
if(currentBook.isBorrowed()){
System.out.println("该书籍已经被借出!");
return;
}
bookList.getBook(i).setBorrowed(true);
System.out.println("借书成功!");
}
}
11.DelOperation
package booksystem.operation;
import booksystem.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList)
{
Scanner scanner=new Scanner(System.in);
System.out.println("删除书籍");
System.out.println("请输入要删除的书籍的名称");
String name=scanner.next();
int i=0;
for(;i { if(bookList.getBook(i).getName().equals(name)){ break; } } if(i>=bookList.getSize()){ System.out.println("您输入的书籍在+"+name+"在系统中没有找到!删除失败!"); return; } if(i==bookList.getSize()-1) { bookList.setSize(bookList.getSize()-1); System.out.println("删除成功!"); return; } bookList.setBook(i,bookList.getBook(bookList.getSize()-1)); bookList.setSize(bookList.getSize()-1); System.out.println("删除成功!"); } } 13.ExitOperation package booksystem.operation; import booksystem.BookList; public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出程序"); http://System.exit(0); } } 14.FindOperation package booksystem.operation; import booksystem.BookList; public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出程序"); System.exit(0); } } 15.ReturnOperation package booksystem.operation; import booksystem.BookList; import booksystem.Book; import java.util.Scanner; public class ReturnOperation implements IOperation{ @Override public void work(BookList bookList){ Scanner scanner=new Scanner(System.in); System.out.println("归还书籍"); System.out.println("请输入要还的书籍的名称"); String name=scanner.next(); int i=0; for(;i { Book book=bookList.getBook(i); if(book.getName().equals(i)) { break; } } if(i>=bookList.getSize()) { System.out.println("书籍没有找到,无法归还"); return; } Book currentBook=bookList.getBook(i); if(!currentBook.isBorrowed()) { System.out.println("这本书没有借出,无法归还"); } currentBook.setBorrowed(false); System.out.println("归还书籍成功"); return; } } 四、编程截图及测试图 包和类放置图 运行截图
{
if(bookList.getBook(i).getName().equals(name)){
break;
}
}
if(i>=bookList.getSize()){
System.out.println("您输入的书籍在+"+name+"在系统中没有找到!删除失败!");
return;
}
if(i==bookList.getSize()-1)
{
bookList.setSize(bookList.getSize()-1);
System.out.println("删除成功!");
return;
}
bookList.setBook(i,bookList.getBook(bookList.getSize()-1));
bookList.setSize(bookList.getSize()-1);
System.out.println("删除成功!");
}
}
13.ExitOperation
package booksystem.operation;
import booksystem.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList)
{
System.out.println("退出程序");
http://System.exit(0);
}
}
14.FindOperation
package booksystem.operation;
import booksystem.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList)
{
System.out.println("退出程序");
System.exit(0);
}
}
15.ReturnOperation
package booksystem.operation;
import booksystem.BookList;
import booksystem.Book;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList){
Scanner scanner=new Scanner(System.in);
System.out.println("归还书籍");
System.out.println("请输入要还的书籍的名称");
String name=scanner.next();
int i=0;
for(;i { Book book=bookList.getBook(i); if(book.getName().equals(i)) { break; } } if(i>=bookList.getSize()) { System.out.println("书籍没有找到,无法归还"); return; } Book currentBook=bookList.getBook(i); if(!currentBook.isBorrowed()) { System.out.println("这本书没有借出,无法归还"); } currentBook.setBorrowed(false); System.out.println("归还书籍成功"); return; } } 四、编程截图及测试图 包和类放置图 运行截图
{
Book book=bookList.getBook(i);
if(book.getName().equals(i))
{
break;
}
}
if(i>=bookList.getSize())
{
System.out.println("书籍没有找到,无法归还");
return;
}
Book currentBook=bookList.getBook(i);
if(!currentBook.isBorrowed())
{
System.out.println("这本书没有借出,无法归还");
}
currentBook.setBorrowed(false);
System.out.println("归还书籍成功");
return;
}
}
四、编程截图及测试图
包和类放置图
运行截图
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~