在学习了Java语法的类、接口、继承、多态、接口等基础语法后的一简单的项目练习。
具体功能为:
1、简单的根据姓名的登录
2、管理员端:其功能为,查阅书籍、增加书籍、删除书籍、打印书籍、按价格整理书籍、退出。
3、普通用户端:查阅书籍、借阅书籍、归还书籍、退出。
实现效果:
先输入用户姓名,在根据选择决定身份为管理员或者是用户。如果是管理员,就显示管理员和它的菜单,如果是用户就显示用户的菜单。
整个系统分为4部分,这里分别用三个包和一个Main类实现。分别是Book包,Operation包,User包。
包含book、bookList。两个java文件
book用来描述每本书的特征。
bookList则表示整个书架。
package Book;
/**
* @author 珍菇辣条
*/
public class book {
private String name;
private String author;
private double price;
private String type;
private boolean isBorrowed;
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 double 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;
}
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 + '\'' +
((isBorrowed==true)? "已借出 " :" 未借出") +
'}';
}
public double compare(book o1,book o2){
return o1.getPrice()-o2.getPrice();
}
}
package Book;
/**
* @author 珍菇辣条
*/
public class bookList {
private book[]books=new book[10];
private int usedSize;
public bookList(){
books[0]=new book("百年孤独", "加西亚·马尔克斯", 39.60,"小说");
books[1]=new book("无声告白", "伍绮诗", 35.00, "小说");
books[2]=new book("沉思录", "马可·奥勒留", 32.00,"哲学");
books[3]=new book("数据结构与算法", "汪沁", 45.00, "教科书");
this.usedSize=4;
}
/**
* 合法和放没放满目前没有考虑
* 默认放到顺序表的最后
* @param pos
* @param book
*/
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 void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public book[] getBooks(){
return this.books;
}
}
将操作写成接口。后面让每个方法继承这个接口
Operation包包含了两种身份的所有方法。先在包里将所有的类方法全部实现。在具体整合到User包里的管理员和普通用户里。用一个数组来囊括这些方法。
增加书籍会提示管理员一步步输入需要增加书籍的信息。如果书架里已经用了这本书,那就不会再添加到书架上。
package Opreation;
import Book.bookList;
import Book.book;
import java.util.Scanner;
public class AddOperation implements IOperaion{
@Override
public void work(bookList bookList){
System.out.println("新增书籍");
Scanner scanner=new Scanner(System.in);
System.out.println("输入新增图书的名字");
String name=scanner.nextLine();
System.out.println("输入图书的作者");
String Authorname=scanner.nextLine();
System.out.println("输入图书的类型");
String sype=scanner.nextLine();
System.out.println("输入图书的价格");
int price=scanner.nextInt();
int currentSize=bookList.getUsedSize();
for (int i = 0; i <bookList.getUsedSize() ; i++) {
book book=bookList.getBook(i);
if(name.equals(book.getName())){
System.out.println("书架已经有这本书了,不要再添加了哦: ");
return;
}
}
String type = null;
String author=null;
book book=new book(name, author, price, type);
bookList.setBooks(currentSize, book);
bookList.setUsedSize(currentSize+1);
System.out.println("新增成功");
}
}
在删除之前先检查书架上是否包含要删除的书,如果没有就不能删除。
package Opreation;
import Book.bookList;
import Book.book;
import java.util.Scanner;
public class DelOperation implements IOperaion {
@Override
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 index=-1;
for (int i = 0; i <currentSize ; i++) {
book book=bookList.getBook(i);
if(name.equals(book.getName())){
//找到了
index=i;
}
}
if(index==-1){
//没找到
System.out.println("没有你要删除的书 ~.~");
return;
}else {
for (int i = index; i <currentSize-1 ; i++) {
book book1=bookList.getBook(i+1);
bookList.setBooks(i, book1);
}
//防止内存泄漏
bookList.setBooks(currentSize-1, null);
//控制usedSize
bookList.setUsedSize(currentSize-1);
}
}
}
这个很好实现,只需要遍历书籍找到这本书。把书籍的状态改成true就可以啦。
package Opreation;
import Book.bookList;
import Book.book;
import java.util.Scanner;
public class BorrowOperation implements IOperaion{
@Override
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(name.equals(book.getName())&& !book.isBorrowed()){
//找到了,就借出去呗。把它的状态改成true。
book.setBorrowed(true);
System.out.println("给你借出去啦");
return;
}
}
System.out.println("没有这本书呀或者已经被借出 ");
}
}
和借阅书籍相同,将修改后的已借出,修改成未借出即可。
package Opreation;
import Book.book;
import Book.bookList;
import java.util.Scanner;
public class ReturnOperation implements IOperaion{
@Override
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(name.equals(book.getName())){
//找到了,就归还呗。把它的状态改成false。
book.setBorrowed(false);
System.out.println("已归还!");
return;
}
}
System.out.println("没有这本书呀,还不了");
}
}
遍历书籍,并打印每一本书的信息即可。
package Opreation;
import Book.book;
import Book.bookList;
public class DisPlayOperation implements IOperaion{
@Override
public void work(bookList bookList){
int currentSize=bookList.getUsedSize();
for (int i = 0; i <currentSize ; i++) {
book book=bookList.getBook(i);
System.out.println(book);
}
}
}
通过书籍的名字遍历书架,查找书籍,如果名字没有对的上的,认为没有这本书。
package Opreation;
import Book.bookList;
import Book.book;
import jdk.swing.interop.SwingInterOpUtils;
import java.awt.print.Book;
import java.util.Scanner;
public class FindOperation implements IOperaion{
@Override
public void work(bookList bookList){
System.out.println("这是给好兄弟查找书籍的: ");
Scanner scanner=new Scanner(System.in);
System.out.println("好兄弟找查找那本书? ");
String name= scanner.nextLine();
for (int i = 0; i <bookList.getUsedSize(); i++) {
book book=bookList.getBook(i);
if(name.equals(book.getName())){
System.out.println("找到这本书了,信息如下: ");
System.out.println(book);
return;
}
}
System.out.println("确实是没有这本书哦!");
}
}
将书架的书按照价格降序排序。需要先给book类实现比较方法。排序用的是直接插入排序。
package Opreation;
import Book.bookList;
import Book.book;
import java.util.Scanner;
public class SortOperation implements IOperaion {
@Override
public void work(bookList bookList){
System.out.println("整理图书!");
int currentSize=bookList.getUsedSize();
book book1=bookList.getBook(0);
book books[]=bookList.getBooks();
for (int i = 1; i <currentSize ; i++) {
book tmp=books[i];
int j=i-1;
for (; j >=0 ; j--) {
if(compare(books[j],tmp)<0){
books[j+1]=books[j];
}
else {
break;
}
}
books[j+1]=tmp;
}
System.out.println("已按价格降序整理完毕");
}
public double compare(book o1,book o2){
return o1.getPrice()-o2.getPrice();
}
即为退出程序。
package Opreation;
import Book.bookList;
public class ExitOperation implements IOperaion{
@Override
public void work(bookList bookList){
System.out.println("退出系统咯");
System.exit(1);
}
}
包含管理员和普通用户,和User类。每种身份包含不同的方法、菜单。管理员个普通用户继承自User。
package USER;
import Opreation.IOperaion;
import Book.bookList;
public abstract class User {
protected String name;
protected IOperaion[] iOperaions;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doOperatopm(int choice,bookList bookList){
this.iOperaions[choice].work(bookList);
}
}
package USER;
import Opreation.*;
import java.util.Scanner;
public class AdminUser extends User {
public AdminUser(String name){
super(name);
this.iOperaions=new IOperaion[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisPlayOperation(),
new SortOperation()
};
}
@Override
public int menu() {
Scanner scanner=new Scanner(System.in);
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("5、整理书籍(按价格)");
System.out.println("0、退出系统");
System.out.println("~~~~~~~~~~~~~");
int choice=scanner.nextInt();
return choice;
}
}
package USER;
import Opreation.*;
import java.util.Scanner;
public class NormarUser extends User{
public NormarUser(String name) {
super(name);
//就把普通用户的操作,封装/存储到接口数组重
this.iOperaions=new IOperaion[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
@Override
public int menu() {
Scanner scanner=new Scanner(System.in);
System.out.println("~~~~~~~~~~~~~");
System.out.println("欢迎"+this.name +",这里是用户菜单");
System.out.println("1、查找图书");
System.out.println("2、借阅图书");
System.out.println("3、归还图书");
System.out.println("0、退出系统");
System.out.println("~~~~~~~~~~~~~");
int choice=scanner.nextInt();
return choice;
}
}
通过一个Main类来调动所有的类。
import USER.AdminUser;
import USER.NormarUser;
import USER.User;
import Book.bookList;
import java.util.Scanner;
/**
* @author 珍菇辣条
*/
public class Main {
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名 : ");
String name = scanner.nextLine();
System.out.println(name+"你的身份是?: 1->管理员 0->:用户");
int choice = scanner.nextInt();
if (choice == 1) {
return new AdminUser(name);
} else {
return new NormarUser(name);
}
}
public static void main(String[] args) {
bookList bookList = new bookList();
User user = login();
/**
* 打印菜单,并让用户选择身份,要操作的事情
*/
while (true) {
int choice = user.menu();
//根据菜单的选项,来调用合适的方法
user.doOperatopm(choice, bookList);
}
}
}
以上就是图书管理系统的所有代码。祝大家学习顺利。工作顺利。