book包
Book类
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) ? "已借出" : "未借出")+
'}';
}
}
Booklist 类
package book;
public class Booklist {
private Book[] books = new Book[10];
public int usedSize;
public Booklist(){
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",13,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",11,"小说");
this.usedSize = 3;
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos,Book book) {
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book[] getBooks(){
return books;
}
}
ioperations包
IOPeration接口
package ioperations;
import book.Booklist;
public interface IOPeration {
void work(Booklist booklist);
}
AddOperation类
package ioperations;
import book.Book;
import book.Booklist;
import java.util.Scanner;
public class AddOperation implements IOPeration{
public void work(Booklist booklist){
System.out.println("新增图书");
//判断书架是否已经满了
int currentSize = booklist.getUsedSize();
if( currentSize == booklist.getBooks().length){
System.out.println("书架满了,不能放了.....");
return;
}
//构建图书
Scanner scanner = new Scanner(System.in);
System.out.println("输入你的书名: ");
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 newBook = new Book(name,author,price,type);
//判断是否已经存在
for (int i = 0; i < currentSize ; i++) {
Book book = booklist.getBook(i);
if(book.getName().equals(name)){
System.out.println("这本书已有不能插入");
return;
}
}
//新增图书
booklist.setBook(currentSize,newBook);
booklist.setUsedSize(currentSize+1);
System.out.println("新增图书成功!!!");
}
}
BorrowOperation类
package ioperations;
import book.Book;
import book.Booklist;
import java.util.Scanner;
public class BorrowOperation implements IOPeration{
public void work(Booklist booklist){
System.out.println("借阅图书");
Scanner scanner = new Scanner(System.in);
System.out.println("输入你要借阅的书");
String name = scanner.nextLine();
int currentSize = booklist.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = booklist.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("没有你要找的书");
}
}
ExitOperation
package ioperations;
import book.Booklist;
public class ExitOperation implements IOPeration{
public void work(Booklist booklist){
System.out.println("退出系统");
System.exit(0);
}
}
DelOperation类
package ioperations;
import book.Book;
import book.Booklist;
import java.util.Scanner;
public class FindOperation implements IOPeration{
public void work(Booklist booklist){
System.out.println("查找图书.....");
Scanner scanner = new Scanner(System.in);
System.out.println("输入你的书名");
String name = scanner.nextLine();
int currentSize = booklist.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = booklist.getBook(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书");
System.out.println(book);
return;
}
}
System.out.println("没有找到这本书");
}
}
FindOperation 类
package ioperations;
import book.Book;
import book.Booklist;
import java.util.Scanner;
public class DelOperation implements IOPeration{
public void work(Booklist booklist) {
System.out.println("删除图书");
Scanner scanner = new Scanner(System.in);
System.out.println("输入你要删除的书");
String name = scanner.nextLine();
int currentSize = booklist.getUsedSize();
int pos = -1;
//找到这本书
int i = 0;
for ( ; i < currentSize; i++) {
Book book = booklist.getBook(i);
if(book.getName().equals(name)){
pos = i;
break;
}
}
if(i == currentSize) {
System.out.println("没有你要删除的图书!");
return;
}
//删除
for (int j = pos; j < currentSize -1; j++) {
Book book = booklist.getBook(j+1);
booklist.setBook(j,book);
}
booklist.setBook(currentSize-1,null);
booklist.setUsedSize(currentSize-1);
}
}
ReturnOperation类
package ioperations;
import book.Book;
import book.Booklist;
import java.util.Scanner;
public class ReturnOperation implements IOPeration{
public void work(Booklist booklist){
System.out.println("归还图书");
Scanner scanner = new Scanner(System.in);
System.out.println("输入你要归还的书");
String name = scanner.nextLine();
int currentSize = booklist.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = booklist.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("没有你要归还的图书");
}
}
ShowOperation 类
package ioperations;
import book.Book;
import book.Booklist;
public class ShowOperation implements IOPeration{
public void work(Booklist booklist){
System.out.println("显示图书");
int currentSize = booklist.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = booklist.getBook(i);
System.out.println(book);
}
}
}
user包
User类
package user;
import book.Booklist;
import ioperations.IOPeration;
public abstract class User {
protected String name;
protected IOPeration[] ioperation;
public User(String name){
this.name = name;
}
public abstract int menu ();
public void doIoperation ( int choice, Booklist booklist){
this.ioperation[choice].work(booklist);
}
}
AdminUser类
package user;
import ioperations.*;
import java.util.Scanner;
import static java.util.Scanner.*;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.ioperation = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation(),
};
}
public int menu() {
System.out.println("************管理员菜单****************");
System.out.println("欢迎来"+this.name+"到图书管理系统");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出图书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作");
int choice = scanner.nextInt();
return choice;
}
}
NormalUser类
package user;
import ioperations.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.ioperation = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
public int menu() {
System.out.println("************管理员菜单****************");
System.out.println("欢迎来"+this.name+"到图书管理系统");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出图书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作");
int choice = scanner.nextInt();
return choice;
}
}
Main类
import book.Booklist;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User Login(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你姓名:");
String name = scanner.nextLine();
System.out.println("请输入你的身份,1:管理员 2:普通用户");
int choice = scanner.nextInt();
if(choice == 1){
return new AdminUser(name);
}if(choice == 2){
return new NormalUser(name);
}else{
return null;
}
}
public static void main(String[] args) {
Booklist booklist = new Booklist();
User user = Login();
while(true) user.doIoperation(user.menu(), booklist);
}
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 图书管理系统
发表评论 取消回复