JAVA程序设计(14.1)----- 图书馆管理系统 初步设计 内部功能篇

时间:2021-07-16 17:54:48

首先绘制思维导图,右下的user暂时不管先……

先做Book,抽象化属性,核心功能。然后是绘制BookManager的主要功能

JAVA程序设计(14.1)----- 图书馆管理系统 初步设计 内部功能篇

然后直接导出到Eclipse进行修改调整,完成功能的内部结构如下:

package com.lovo.BookManager;

import java.sql.Date;
import java.text.SimpleDateFormat;

/**
* @author Abe
* @version 1.0
* @created 05-十一月-2011 15:58:35
*/
public class Book implements Comparable<Book>{
final long THE_DAY = 1000 * 60 * 60 * 24;
private String name;
private String isbn;
private double price;
private boolean lended;
private int counter;
private long lendDate;

/**
* 构造器
*/
public Book(String isbn, String name , double price){
this.isbn = isbn ;
this.name = name ;
this.price = price;
this.counter = (int)(Math.random()* 50);
}

/**
* 借出
* @return
*/
public boolean lendOut() {
if (!lended) {
lended = true;
counter++;
lendDate = System.currentTimeMillis();
return true;
}
return false;
}

/**
* 归还
* @return
*/
public double returnBack() {
if (lended) {
lended = false;
long currentTime = System.currentTimeMillis();
int days = (int) Math.ceil((currentTime - lendDate) / THE_DAY);
lendDate = 0;
return days * price;
}
return 0;
}

public String getName() {
return name;
}

public String getIsbn() {
return isbn;
}

public double getPrice() {
return price;
}

public boolean isLended() {
return lended;
}

public int getCounter() {
return counter;
}

/**
* 出借日期
* @return
*/
public String getLendDate() {
if (lended) {
Date date = new Date(lendDate);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
return sdf.format(date);
} else {
return "---";
}
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public void setCounter(int counter) {
this.counter = counter;
}

/**
* 重写比较的方法
*/
@Override
public int compareTo(Book other) {
return (other.counter - this.counter);
}

@Override
public String toString() {
return " name=" + name + ", isbn=" + isbn
+ ", price=" + price + ", lended=" + lended + ", counter="
+ counter + ", lendDate=" + lendDate + "]";
}
}

然后是 Manager

package com.lovo.BookManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author Abe
* @version 1.0
* @created 05-十一月-2011 15:58:35
*/
public class BookManager {
private List<Book> list = new ArrayList<Book>();

public BookManager() {
// 添加图书作为测试数据
list.add(new Book("111", "编程思想", 1.5));
list.add(new Book("222", "鹿鼎记", 0.5));
list.add(new Book("333", "育儿百科", 1.2));
list.add(new Book("444", "寻秦记", 0.7));
list.add(new Book("555", "斗罗大陆", 1.1));
}

/**
* 方法:新增书籍
* @param Book
*/
public boolean add(Book book) {
if (findByISBN(book.getIsbn()) == null) {
list.add(book);
return true;
}
return false;
}

/**
* 移除书籍
* @param isbn
*/
public boolean deleteByISBN(String isbn) {
if (findByISBN(isbn) == null) {
return false;
} else {
list.remove(findByISBN(isbn));
return true;
}
}

/**
* 方法:返回全部容器
* @return
*/
public List<Book> findAll() {
return list;
}

/**
* 按isbn查找目标书籍
* @param isbn
*/
public Book findByISBN(String isbn) {
for (Book b : list) {
if (b.getIsbn().equals(isbn)) {
return b;
}
}return null;
}

public Book[] findTop10() {
if (list != null) {
Book[] tempBook = new Book[list.size()];
tempBook = list.toArray(tempBook);
Arrays.sort(tempBook); //匿名内部类借口 A

Book[] top10 = new Book[list.size() < 10 ? list.size() : 10];
System.arraycopy(tempBook, 0, top10, 0, top10.length);
return top10;
} else {
return null;
}
}

/**
* 归还
* @param b
* @return 租金
*/
public double getBackFromUser(String isbn) {
if (findByISBN(isbn) != null) {
return findByISBN(isbn).returnBack();
}
return 0;
}

/**
* 借出
* @param b
* @return 是否借书成功
*/
public boolean lendedToUser(String isbn) {
if (findByISBN(isbn) != null) {
return findByISBN(isbn).lendOut();
}
return false;
}
}

3.最后是控制台的功能验证

package com.lovo.BookManager;

import java.util.List;
import java.util.Scanner;

/**
* @author Abe
* @version 1.0
* @created 05-十一月-2014 15:37:33
*/
public class SystemUI {

public static void main(String[] args) {
BookManager manager = new BookManager();
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);

do {
int choice = 0;
do {
System.out.println("\n---------------\n");
System.out.println("1. 新增图书");
System.out.println("2. 删除图书");
System.out.println("3. 修改图书");
System.out.println("4. 查看所有图书");
System.out.println("5. 查找图书");
System.out.println("6. 借书");
System.out.println("7. 还书");
System.out.println("8. 查看排行榜");
System.out.println("9. 退出系统");
System.out.println("\n---------------\n");
System.out.print("请选择: ");
choice = sc2.nextInt();
} while(choice < 1 || choice > 9);

switch(choice) {
case 1:
System.out.print("请输入书的编号: ");
String isbn1 = sc1.nextLine();
System.out.print("请输入书的名称: ");
String name = sc1.nextLine();
System.out.print("请输入书的日租金: ");
double price = sc2.nextDouble();

Book newBook = new Book(isbn1, name, price);
System.out.println(manager.add(newBook)? "添加成功!" : "添加失败!");

break;
case 2:
System.out.print("请输入书的编号: ");
String isbn2 = sc1.nextLine();

System.out.println(manager.deleteByISBN(isbn2)? "删除成功!" : "删除失败!");

break;
case 3:
break;
case 4:
List<Book> bookList = manager.findAll();
System.out.println("名称\t\t日租金\t状态\t借出日期\t借出次数");
for(Book b : bookList) {
System.out.println(b.getName() + "\t\t" + b.getPrice() + "\t" +
(b.isLended()? "已借出" : "未借出") + "\t" +
b.getLendDate() + "\t" + b.getCounter());
}
break;
case 5:
System.out.print("请输入书的编号: ");
String isbn3 = sc1.nextLine();

Book temp = manager.findByISBN(isbn3);
if(temp != null) {
System.out.println("书名: " + temp.getName());
System.out.println("日租金: " + temp.getPrice());
System.out.println("借出状态: " + (temp.isLended()? "已借出" : "未借出"));
System.out.println("借出日期: " + temp.getLendDate());
System.out.println("借出次数: " + temp.getCounter());
}
else {
System.out.println("没有此书!!!");
}
break;
case 6:
System.out.print("请输入书的编号: ");
String isbn4 = sc1.nextLine();

System.out.println(manager.lendedToUser(isbn4)? "借出成功!" : "借出失败!");
break;
case 7:
System.out.print("请输入书的编号: ");
String isbn5 = sc1.nextLine();

System.out.println("请交纳租金: ¥" + manager.getBackFromUser(isbn5));
break;
case 8:
Book[] list = manager.findTop10();
System.out.println("名称\t\t日租金\t状态\t借出日期\t借出次数");
for(Book b : list) {
System.out.println(b.getName() + "\t\t" + b.getPrice() + "\t" +
(b.isLended()? "已借出" : "未借出") + "\t" +
b.getLendDate() + "\t" + b.getCounter());
}
break;
case 9:
System.exit(0);
break;
}
} while(true);
}
}

除了修改,都能正常运行