前言
这篇博客是在学习了一部分Java基础语法之后的练习项目,通过这个小项目的练习,对Java中的类和对象,抽象类和接口,面向对象的继承、多态和封装、组合等进行熟悉理解;抽象出不同的对象,将对象进行合理的设计,完成对象之间的交互,面向对象进行编程。
1. 项目需求
图书管理系统,面向管理员和普通用户使用,对管理员的开放的功能有:添加图书,删除图书,查找图书,显示图书和退出程序等;对普通用户的开放的功能有:查找图书,借阅图书,归还图书和退出程序。
2. 实现思路
先抽象提取出不同的对象,首先想到的对象是用户和书,
用户可分为管理员和普通用户,将管理员和普通用户共有的属性设置为一个父类User,管理员和普通用户继承User,可以先将父类设置为普通类,在写码过程如果合适可以修改为抽象类;
系统对书进行管理,书本身就可以抽象为一个对象;对多本书进行操作管理,书架也可以抽象为一个对象;所以书和书架用两个类来实现,书架可以设计为一个数组;
这里系统对书进行管理,其实就是对书架对应数组进行操作,不同的操作可以通过一个标准去实现,也就是可以定义一个接口,每一个操作类去实现这个接口,完成向上转型,通过接口实现多态;
然后写代码的过程中思考完善各个对象的成员,最终实例化对象,通过不同对象之间的交互完成管理系统。
3. 代码实现
包的设计
book包
Book类
package book; public class Book { private String name;//书名 private String author;//书的作者 private int price;//书的价格 private String type;//书的类型 private boolean borrowed;//是否被借出,默认false 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 getBorrowed() { return borrowed; } public void setBorrowed(Boolean borrowed) { this.borrowed = borrowed; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ", borrowed=" + ((borrowed==true) ? "已借出" : "未借出") + '}'; } }
BookList类
package book; public class BookList { private Book[] books = new Book[100]; private int usedsize; //书架当中默认有三本书 public BookList() { this.books[0] = new Book("三国演义","罗贯中",66,"小说"); this.books[1] = new Book("西游记","吴承恩",68,"小说"); this.books[2] = new Book("红楼梦","曹雪芹",64,"小说"); this.usedsize = 3; } public int getUsedsize() { return usedsize; } public void setUsedsize(int usedsize) { this.usedsize = usedsize; } public Book getBooks(int pos){ return this.books[pos]; } public void setBooks(Book book, int pos) { this.books[pos] = book; } }
operations包
IOperation接口
package operations; import book.BookList; public interface IOperation { void work(BookList bookList); }
AddOperation类
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class AddOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("新增图书"); //输入图书信息 System.out.println("输入新增图书名称:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.println("输入新增图书作者:"); String author = scanner.nextLine(); System.out.println("输入新增图书价格:"); int price = scanner.nextInt(); scanner.nextLine(); System.out.println("输入新增图书类型:"); String type = scanner.nextLine(); Book book = new Book(name, author, price, type); //获取存书位置 int currentSize = bookList.getUsedsize(); //把书放到书架(数组) bookList.setBooks(book, currentSize); //书的总数加1 bookList.setUsedsize(currentSize+1); System.out.println("添加成功!"); } }
BorrowOperation类
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class BorrowOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("借阅图书"); System.out.println("输入要借阅图书的名称:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int currentSize = bookList.getUsedsize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBooks(i); if (name.equals(book.getName())){ if(book.getBorrowed()){ System.out.println("该书已经被借出!"); return; }else { book.setBorrowed(true); System.out.println("借阅成功!"); return; } } } System.out.println("没有找到你要借阅的书"); } }
DelOperation类
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("删除图书"); System.out.println("输入要删除图书的名称:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int currentSize = bookList.getUsedsize(); //找到并记录要删除书的下标 int index = -1; for (int i = 0; i < currentSize; i++) { Book book = bookList.getBooks(i); if (name.equals(book.getName())){ index = i; break; } } if(-1 == index) { System.out.println("没有找到要删除的这本书!"); }else { for (int i = index; i < currentSize-1; i++) { Book book = bookList.getBooks(i+1); bookList.setBooks(book, i); } //每次删除,都要将原来最后一本书位置的引用置空 bookList.setBooks(null,currentSize-1); bookList.setUsedsize(currentSize-1); System.out.println("删除成功"); } } }
DisplayOperation类
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class DisplayOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("显示图书"); int currentSize = bookList.getUsedsize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBooks(i); System.out.println(book); } } }
ExitOperation类
package operations; import book.BookList; public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出系统"); System.exit(0); } }
FindOperation类
import book.Book; import book.BookList; import java.util.Scanner; public class FindOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("查找图书"); System.out.println("输入要查找图书的名称:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int currentSize = bookList.getUsedsize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBooks(i); if (name.equals(book.getName())){ System.out.println("找到了,该书信息如下:"); System.out.println(book); return; } } System.out.println("没有找到这本书!"); } }
ReturnOperation类
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class ReturnOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("归还图书"); System.out.println("输入要归还图书的名称:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int currentSize = bookList.getUsedsize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBooks(i); if (name.equals(book.getName())){ if(book.getBorrowed()){ book.setBorrowed(false); System.out.println("归还成功!"); return; }else { System.out.println("该书未借出!"); return; } } } System.out.println("没有找到你要归还的书"); } }
user包
User类
public abstract class User { protected String name; protected IOperation[] iOperation; public User(String name) { this.name = name; } public abstract int menu(); public void doOperation(int choice, BookList bookList){ iOperation[choice].work(bookList); } }
AdminUser类
package user; import operations.*; import 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 DisplayOperation() }; } @Override public int menu() { System.out.println("---------------------------------"); System.out.println("Hello 管理员:>"+""+name+" 欢迎来到图书管理系统!"); System.out.println(" 1.查找图书 2.新增图书"); System.out.println(" 3.删除图书 4.显示图书"); System.out.println(" 0.退出系统"); System.out.println("---------------------------------"); System.out.println("请输入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
NormalUser类
package user; import operations.*; 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() }; } @Override public int menu() { System.out.println("---------------------------------"); System.out.println("Hello 用户:>"+name+" 欢迎来到图书管理系统!"); System.out.println(" 1.查找图书 2.借阅图书"); System.out.println(" 3.归还图书 0.退出系统"); System.out.println("---------------------------------"); System.out.println("请输入你的操作:"); Scanner scanner = new Scanner(System.in); 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(){ System.out.println("请输入你的姓名:"); Scanner scanner = new Scanner(System.in); String userName = scanner.nextLine(); System.out.println("请确认你的身份:> |1->管理员|0->普通用户|"); int choice = scanner.nextInt(); if(1==choice){ return new AdminUser(userName); }else{ return new NormalUser(userName); } } public static void main(String[] args) { //准备数据 BookList bookList = new BookList(); //登录 User user = login(); while (true) { int choice = user.menu(); user.doOperation(choice, bookList); } } }
4. 实现效果
管理员
普通用户
以上就是JavaSE实现图书管理系统的示例代码的详细内容,更多关于JavaSE图书管理系统的资料请关注脚本之家其它相关文章!