练习知识点:
java类,面向对象,抽象类,接口,封装,继承,多态,顺序表
练习通过封装抽象类user以及操作接口来实现多态,目的是将图书管理人员和普通用户区分开,以及将不同权限的人操作区分开.
1.书籍,书架包
书籍类
package book;
public class Book {
private String name;
private String author;
private double price;
private String type;
private boolean isBorrow;
public Book(String name, String author, double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
this.isBorrow = false;
}
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(double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isBorrow() {
return isBorrow;
}
public void setBorrow(boolean borrow) {
isBorrow = borrow;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
(this.isBorrow ?", 已借出":", 未借出")+
'}';
}
}
书架类
package book;
import java.util.Arrays;
public class BookList {
private Book[] books;
private int usenum;//书本个数
private int capacity;
public Book[] getBooks() {
return books;
}
public BookList(){
this.books=new Book[]{new Book("小王子","安托万·德·圣-埃克苏佩",34.5,"童话")};
usenum=1;
capacity=1;
}
public int getUsenum() {
return usenum;
}
public int findBook(String name){
for(int i=0;i<usenum;i++){
if(books[i].getName().equals(name)){
return i;
}
}
return -1;
}
public void setUsenum(int usenum) {
this.usenum = usenum;
}
public Book GetPos(int pos){
if(pos>usenum){
return null;
}
return this.books[pos];
}
public void addBook(Book book){
if(usenum==capacity){
this.books=Arrays.copyOf(this.books,2*capacity);
capacity*=2;
System.out.println("扩容成功");
}
this.books[usenum]=book;
usenum++;
}
}
2.操作包
操作接口
package operation;
import book.BookList;
public interface Operation {
void work(BookList bookList);
}
增加
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements Operation{
public void work(BookList booklist){
String name;String author;double price;String type;
Scanner io=new Scanner(System.in);
System.out.println("书名:");
name=io.nextLine();
System.out.println("作者: ");
author=io.nextLine();
System.out.println("书籍类型: ");
type=io.nextLine();
System.out.println("价格: ");
price=io.nextDouble();
Book book=new Book(name,author,price,type);
booklist.addBook(book);
System.out.println("添加图书成功!");
//System.out.println(booklist.getUsenum());
}
}
删除
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements Operation{
public void work(BookList booklist){
Scanner io=new Scanner(System.in);
System.out.println("输入要删除的书名:");
String name=io.nextLine();
int pos=booklist.findBook(name);
Book[] books=booklist.getBooks();
if(pos!=-1){
int size=booklist.getUsenum();
for (int i = pos; i < size-1; i++) {
books[i] = books[i + 1];
}
booklist.setUsenum(size-1);
System.out.println("删除成功");
}
else{
System.out.println("没有找到"+name+"这本书");
}
}
}
显示
package operation;
import book.BookList;
public class DisplayOperation implements Operation{
public void work(BookList booklist){
int size=booklist.getUsenum();
if(size==0){
System.out.println("书架为空");
return;
}
for(int i=0;i<size;i++){
System.out.println(booklist.GetPos(i));
}
}
}
查找
package operation;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements Operation{
public void work(BookList booklist){
Scanner io=new Scanner(System.in);
int size=booklist.getUsenum();
if(size==0){
System.out.println("书架为空!");
return;
}
System.out.println("输入要查找的书名: ");
String name=io.nextLine();
int pos=booklist.findBook(name);
if(pos!=-1){
System.out.println(booklist.GetPos(pos));
}
else{
System.out.println("没找到这本书!!");
}
}
}
借阅
package operation;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements Operation{
public void work(BookList booklist){
Scanner io=new Scanner(System.in);
int size=booklist.getUsenum();
if(size==0){
System.out.println("书架为空!");
return;
}
System.out.println("输入要查找的书名: ");
String name=io.nextLine();
int pos=booklist.findBook(name);
if(pos!=-1){
System.out.println(booklist.GetPos(pos));
}
else{
System.out.println("没找到这本书!!");
}
}
}
归还
package operation;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements Operation{
public void work(BookList booklist){
System.out.println("输入要归还的书籍");
String name=new Scanner(System.in).nextLine();
int pos=booklist.findBook(name);
if(pos!=-1){
booklist.GetPos(pos).setBorrow(false);
System.out.println("归还成功");
}
else{
System.out.println("书库无"+name+"这本书的存储记录");
}
}
}
退出
package operation;
import book.BookList;
public class ExitOperation implements Operation{
public void work(BookList booklist){
System.out.println("退出系统");
System.exit(0);
}
}
3.用户管理包
用户抽象类
package user;
import book.BookList;
import operation.Operation;
import java.util.Scanner;
public abstract class User {//抽象类
protected String name;
protected Scanner choice;
protected Operation[] operation;
public User(String name){
this.name=name;
choice=new Scanner(System.in);
}
public void SelectWork(int op, BookList booklist){
this.operation[op].work(booklist);
}
public abstract int menu();
}
超级用户
package user;
import operation.*;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.operation=new Operation[]{new ExitOperation(),new FindOperation(),new AddOperation()
,new DelOperation(),new DisplayOperation()
};
}
public int menu(){
System.out.println("Hello Admin "+this.name+": ");
System.out.println("=====管理员菜单=====");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("=================");
return choice.nextInt();
}
}
普通用户
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.operation=new Operation[]{new ExitOperation(),new FindOperation(),new BorrowOperation()
,new ReturnOperation(),new DisplayOperation()
};
}
public int menu(){
System.out.println("Hello User "+this.name+": ");
System.out.println("=====操作菜单=====");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("================");
return choice.nextInt();
}
}
4.主函数,程序入口
//程序入口
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 io=new Scanner(System.in);
String name=io.nextLine();
System.out.println("输入用户权限 1:管理员 0:普通用户");
int choice=io.nextInt();
if(choice==0||choice==1) {
if (choice == 1) {
return new AdminUser(name);
}
else{
return new NormalUser(name);
}
}
else{
System.out.println("用户权限错误,退出程序");
return null;
}
}
public static void main(String[] args) {
BookList bookList=new BookList();
User user=login();//向上转型
while(user!=null) {
int choice = user.menu();//动态绑定
user.SelectWork(choice, bookList);
}
}
}