Java是一个面向对象的语言,在编写代码的之前,我们要先确定有哪些对象
图书馆,首先有很多书,还有书架来放置这些书。然后是对书进行操作的人,比如普通用户和管理员。最后是对关于书的各种操作,例如:对于普通用户,借书、还书等等。对于管理员,添加书籍、查找书籍等等。
大致结构如图
package book;
/**
* 书籍
*/
public class Book {
private String name;//书名
private String auther;//作者
private int Price;//价格
private String type;//书籍类型
private boolean isBorrowed;//是否被借出
public Book(String name, String auther, int price, String type) {
this.name = name;
this.auther = auther;
Price = price;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuther() {
return auther;
}
public void setAuther(String auther) {
this.auther = auther;
}
public int getPrice() {
return Price;
}
public void setPrice(int price) {
Price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", auther='" + auther + '\'' +
", Price=" + Price +
", type='" + type + '\'' +
((isBorrowed == true)?", 已经借出":", 未被借出") +
// ", num=" + num +
'}';
}
}
package book;
/**
* 书架
*/
public class BookList {
private Book[] books; //书架
private int usedSize;//记录当前书架上 实际存放的数量,默认为0
public BookList() {
this.books = new Book[10];//初始化书架的大小
//书架上原本有的书籍
books[0] = new Book("三国演义","罗贯中",10,"小说");
books[1] = new Book("西游记","吴承恩",10,"小说");
books[2] = new Book("水浒传","曹雪芹",10,"小说");
this.usedSize = 3;//现有书籍数量为3
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
//传入下标,返回该下标的书籍信息
public Book getBook(int pos){
return this.books[pos];
}
//设置该下标为某本书
public void setBooks(int pos,Book book){
this.books[pos]=book;
}
public int getBooksNum(){
return books.length; //获取书架容量
}
}
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
//判断容量是否满了
int currentSize = bookList.getUsedSize();
if (currentSize == bookList.getBooksNum()){
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 book = new Book(name,author,price,type);//实例化书籍对象
//遍历数组,是否有这本书,提示有或无,并且增加数量
for (int i = 0; i < currentSize; i++) {
if (name.equals(bookList.getBook(i).getName())){
System.out.println("已有这本书,不进行存放了!");
return;
}
}
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
}
}
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@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.setBorrowed(true);
System.out.println("借阅成功!");
System.out.println(book);
return;
}
}
System.out.println("该书不存在!!");
}
}
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
//System.out.println("删除图书!");
//判断容量是否为0了,能不能继续删下去
//输入书名,看看有没有这本书,有才能删
System.out.println("请输入要删除的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
//找这本书,记录下标pos
int pos = -1;
int currentSize = bookList.getUsedSize();
int i = 0;
for ( ; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (name.equals(book.getName())){
pos = i;
break;
}
}
if(currentSize == i){
System.out.println("没有你要删除的书!!");
return;
}
//开始删除
int j = pos;
for (; j < currentSize-1; j++) {
Book book = bookList.getBook(j+1);
bookList.setBooks(j,book);
}
bookList.setBooks(j,null);
bookList.setUsedSize(currentSize-1);
}
}
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.exit(0);
System.out.println("退出图书系统!");
}
}
package operation;
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 bookname = scanner.nextLine();
//遍历数组
int currentSize = bookList.getUsedSize();//当前已有书籍数量
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);//获取每一本书
if (bookname.equals(book.getName())){
System.out.println("找到了这本书,信息如下:");
System.out.println(book);
return;
}
}
System.out.println("没有找到这本书!!");
}
}
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@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.setBorrowed(false);
System.out.println("归还成功!");
System.out.println(book);
return;
}
}
System.out.println("该书不存在!!");
}
}
package operation;
import book.BookList;
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
//System.out.println("打印图书!");
int currentSize = bookList.getUsedSize();//当前已有书籍的数量
for (int i = 0; i < currentSize; i++) {
System.out.println(bookList.getBook(i));
}
}
}
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
}
public int menu(){
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("******************************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入操作");
int choice = scanner.nextInt();
return choice;
}
}
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new ShowOperation()
};
}
public int menu(){
System.out.println("************普通用户**********");
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);
System.out.println("请输入操作");
int choice = scanner.nextInt();
return choice;
}
}
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
protected IOperation[] iOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
}
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 name = scanner.nextLine(); //输入姓名
System.out.println("请输入您的身份:1.管理员 2.普通用户");
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 user = login();//
while (true){
//choice 指向的对象 菜单返回的(操作数字)
int choice = user.menu();
System.out.println("操作数:"+choice);
//根据choice 的选择 来调用对应的方法
user.doOperation(choice,bookList);
}
}
}