利用知识点:类,抽象类,封装,继承,多态,接口等进行的一个简单的代码练习。
框架
Book包
建立两个包一个包是我们的book包用来
Book
我们要想想图书馆的一本书我们需要干什么,我们需要设置一个书名,作者名,类型(都可以用String这个类型),价格(用int),是否借出(用布尔类型)。然后可以构造book类的构造方法。
然后一顿操作完成之后我们要重点注意一下下面的操作只需要两步我们直接获得70行的代码。由于由于成员变量都是private修饰的,所以要提供公开的输入输出接口,我们直接构造 setter方法,然后再直接构造tostring方法。
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 getBorrowed() {
return isBorrowed;
}
public void setBorrowed(Boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
}
然后我们的book就算完成了。
Booklist
在Book包里,再创建一个 BookList 用顺序表实现书架。 book包下面再创建一个booklist类
package Book;
public class BookLIST {
private book[] books = new book[10];
private int usedSize;
public BookLIST(){
books[0] = new book("三国演义","罗贯中",40,"小说");
books[1] = new book("西游记","吴承恩",45,"小说");
books[2] = new book("水浒传","施耐庵",47,"小说");
books[3] = new book("红楼梦","曹雪芹",50,"小说");
this.usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public book getPos(int pos){
if (pos<0&&pos>usedSize){
System.out.println("没有书籍的编号符合你的需求。");
return null;
}
return this.books[pos];
}
}
图书管理系统总要实现该有的功能,所以我们直接让功能面向对象。 我们用一个包储存我们的所有方法。这么做的目的,就是为了方便导入 普通用户,和 管理员的功能,我可以通过这个接口引用全部功能。 因为 管理员 和 普通用户所使用的功能是有所不同的,有了这个接口,我们可以自主选择,那些是 管理员的功能,那些是普通用户的功能。 由于每个类都需要一个work(BookList bookList)所有我们搞一个io接口 这下让这个包里面的每个类都继承就行了。
user包
user 用户的意思,无论是管理员,还是普通用户,都是用户。 那么就意味着两者具有共性,我们将其提取出来。 建立两个类一个是管理人员另外一个是普通用户。
这个是User这是下面两个用户类的父类
package user;
import Book.BookLIST;
import operation.IOoperation;
public abstract class User {
protected String name;
protected IOoperation[] iOoperations;
public User(String name){
this.name=name;
}
public abstract int menu() ;
public void doWork(int choice, BookLIST bookLIST){
iOoperations[choice].work(bookLIST);
}
}
这个是NormalUser
import operation.*;
import java.util.Random;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.iOoperations=new IOoperation[]{
new ExitOperation(),
new AddOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
public int menu(){
System.out.println("===============================");
System.out.println("hello"+this.name+"欢迎来到这里");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("===============================");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
下面这个是AdminUser
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name){
super(name);
this.iOoperations=new IOoperation[]{
new ExitOperation(),
new AddOperation(),
new FindOperation(),
new DelOperation(),
new DisplayOperation(),
};
}
public int menu(){
System.out.println("===============================");
System.out.println("hello"+this.name+"欢迎来到这里");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("===============================");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
我们建立一个Main类用来实现我们这个系统
import Book.BookLIST;
import operation.IOoperation;
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 name=scanner.nextLine();
System.out.println("请输入你的身份:1.管理员 0.普通");
int choice=scanner.nextInt();
if(choice==1){
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookLIST bookLIST=new BookLIST();
User user=login();
while (true){
int choice=user.menu();
user.doWork(choice,bookLIST);
}
}}
系统功能
有了这些之后我们就要逐个完善我们的系统功能就好了。
展示功能
package operation;
import Book.BookLIST;
import Book.book;
public class DisplayOperation implements IOoperation{
public void work(BookLIST bookLIST){
System.out.println("打印图书");
for (int i = 0; i < bookLIST.getUsedSize(); i++) {
book book = bookLIST.getPos(i);
System.out.println(book);
}
}
}
添加功能
import java.util.Scanner;
public class AddOperation implements IOperation{
public void work(BookList bookList){
System.out.println("新增图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书名称:");
String name = sc.nextLine();
System.out.println("请输入图书的作者:");
String author = sc.nextLine();
System.out.println("请输入图书的类型:");
String type = sc.nextLine();
System.out.println("请输入图书的价格:");
int price = sc.nextInt();
Book book = new Book(name,author,price,type);
int size = bookList.getUsedSize();
bookList.setBook(size,book);
bookList.setUsedSize(size+1);
System.out.println("添加成功");
}
}
查找图书功能
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书");
Scanner sc = new Scanner(System.in);
System.out.println("输入你要找的书籍名称:");
String name = sc.nextLine();
BookList bookList1 = new BookList();
for (int i = 0; i < bookList1.getUsedSize(); i++) {
Book book = bookList1.getPos(i);
if (name.equals(book.getName())){
System.out.println("找到了,信息如下:");
System.out.println(book);
return;
}
}
System.out.println("没有你所搜索的书籍");
}
}
删除功能
import java.util.Scanner;
public class DelOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
System.out.println("输入你要删除书籍的名字:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int index = 0;
int i = 0;
for (; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if (name.equals(book.getName())){
index = i;
break;
}
}
if (i< bookList.getUsedSize()){
for (int j = 0; j < bookList.getUsedSize()-1; j++) {
Book book = bookList.getPos(j+1);
bookList.setBook(j,book);
}
bookList.setBook(bookList.getUsedSize(), null);
bookList.setUsedSize(bookList.getUsedSize()-1);
System.out.println("删除成功");
return;
}
System.out.println("没有你要删除的书");
}
}
借阅功能
import java.util.Scanner;
public class BorrowOperation implements IOperation{
public void work(BookList bookList){
System.out.println("借阅图书");
System.out.println("请输入你想借阅的书籍名称:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if (name.equals(book.getName())){
if(book.getBorrowed() == true){
System.out.println("你要借阅的书籍,已被借走。无法借阅。");
System.out.println("信息如下:");
System.out.println(book);
return;
}
book.setBorrowed(true);
System.out.println("借阅成功!");
System.out.println(book);
return;
}
}
System.out.println("没有你要借阅的书籍");
}
}
归还图书
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
System.out.println("请输入你想借阅的书籍名称:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if (name.equals(book.getName())){
if(book.getBorrowed() == false){
System.out.println("你要归还的书籍,已归还。无法重复此操作。");
System.out.println("信息如下:");
System.out.println(book);
return;
}
book.setBorrowed(false);
System.out.println("归还成功!");
System.out.println(book);
return;
}
}
System.out.println("你要归还的书籍,不属于本图书馆。");
}
}
|