JAVA程序设计(14)----- 图书馆管理系统 初步设计

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

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

然后直接导出到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 "Book [THE_DAY=" + THE_DAY + ", 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;
import java.util.Scanner;

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

	public BookManager() {
		// 添加图书作为测试数据
		list.add(new Book("123-3344-567-89", "Java编程思想", 1.5));
		list.add(new Book("123-4455-987-65", "鹿鼎记", 0.5));
		list.add(new Book("321-3344-777-89", "育儿百科", 1.2));
		list.add(new Book("222-3344-555-89", "寻秦记", 0.7));
		list.add(new Book("333-3344-666-89", "斗罗大陆", 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) {
			b.getIsbn().equals(isbn);
			return b;
		}
		return null;
	}

	@SuppressWarnings("null")
	public List<Book> findTop10() {
		if (list.size() < 2) {
			return list;
		} else {
			Book[] tempBook = new Book[list.size()];
			for (int i = 0; i < list.size(); i++) {
				tempBook[i] = list.get(i);
			}
			Arrays.sort(tempBook);
			List<Book> temp = new ArrayList<Book>();
			for (int i = 0; i < tempBook.length && i < 10; i++) {
				temp.add(tempBook[i]);
			}
			return temp;
		}
	}

	/**
	 * 归还
	 * 
	 * @param b
	 * @return 租金
	 */
	public double getBackFromUser(Book b) {
		return b.returnBack();
	}

	/**
	 * 借出
	 * 
	 * @param b
	 * @return 是否借书成功
	 */
	public boolean lendedToUser(Book b) {
		return b.lendOut();
	}

	/**
	 * 修改内容
	 * 
	 * @param book
	 */
	public void upDate(Book b) {
		boolean goon = false;
		do {
			System.out.print("请问您需要修改的项目是什么?1书名 2日租价 3租赁次数");
			int i = sc.nextInt();
			if (i == 1) {
				System.out.print("请输入《" + b.getName() + "》要更改成的新名字:");
				String name = sc.nextLine();
				b.setName(name);
			} else if (i == 2) {
				System.out.print("请输入要更改成的新的价格:");
				double price = sc.nextDouble();
				b.setPrice(price);
			} else if (i == 3) {
				System.out.print("请输入要更改成的新的被租借次数(不要太过分哦~亲~):");
				int counter = sc.nextInt();
				b.setCounter(counter);
			} else {
				System.out.println("输入不正确");
			}
			System.out.println("请问还需要继续修改其他项目吗?y/n");
			String answer = sc.nextLine();
			if (answer.equals("y")) {
				goon = true;
			} else if (answer.equals("n")) {
				goon = false;
			}
		} while (goon);
		sc.close();
	}

	public static void main(String[] args) {
		BookManager temp = new BookManager();
		for (Book x : temp.findTop10()) {
			System.out.println(x);
		}
	}
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。