码迷,mamicode.com
首页 > Web开发 > 详细

基于MVC的图书管理系统

时间:2016-06-21 12:52:42      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

<span style="font-size:18px;">用Java编写一个图书管理系统:</span>
<span style="font-size:18px;">需求:能列表化显示图书信息:并且能够完成图书出入库、显示全部、增加图书、客户能够购买图书;</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">一、编写图书类:图书属性有(书的ID、书名、作者、发布日期、书的价格、书的库存)即MVC中的模型</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">(</span><span style="font-size: 18px; color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;">model</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">)。</span>
<span style="font-size:18px;">package BookManager;

import java.io.Serializable;

/*图书管理系统
 * 第一层:图书类、模型(<span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px; font-size: 13px;">model</span>)
 */
public class Book implements Serializable{
	private int BookNo;
    private String BookName;
    private String BookWriter;
    private String BookPubDate;
    private double BookPrice;
    private int BookCount;
	public Book() {
		super();
	}
	public Book(int bookNo, String bookName, String bookWriter,
			String bookPubDate, double bookPrice, int bookCount) {
		super();
		this.BookNo = bookNo;
		this.BookName = bookName;
		this.BookWriter = bookWriter;
		this.BookPubDate = bookPubDate;
		this.BookPrice = bookPrice;
		this.BookCount = bookCount;
	}

    public int getBookNo() {
		return BookNo;
	}
	public void setBookNo(int bookNo) {
		BookNo = bookNo;
	}
	public String getBookName() {
		return BookName;
	}
	public void setBookName(String bookName) {
		BookName = bookName;
	}
	public String getBookWriter() {
		return BookWriter;
	}
	public void setBookWriter(String bookWriter) {
		BookWriter = bookWriter;
	}
	public String getBookPubDate() {
		return BookPubDate;
	}
	public void setBookPubDate(String bookPubDate) {
		BookPubDate = bookPubDate;
	}
	public double getBookPrice() {
		return BookPrice;
	}
	public void setBookPrice(double bookPrice) {
		BookPrice = bookPrice;
	}
	public int getBookCount() {
		return BookCount;
	}
	public void setBookCount(int bookCount) {
		BookCount = bookCount;
	}


}
</span>
<span style="font-size:18px;">二、对图书进行各种操作,MVC中的控制器<span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;">(controller).</span></span>
<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;"></span></span><pre name="code" class="java">
package BookManager;

import java.io.*;
import java.util.*;
/*
 * 第二层:对Book的操作
 */
public class BookZip {
	private List<Book> bookList = new ArrayList<Book>();

	public BookZip() {
		super();
		readFile();
		if (bookList.size() == 0) {    //对图书进行初始化,没有图书,则添加图书
		  Book book = new Book(10001, "斗破苍穹", "天蚕土豆", "2008-01-01", 120.5, 5555);
          insertBook(book);
          book = new Book(10002, "魔戒", "莫尔斯", "2006-01-01", 220.5, 5555);
          insertBook(book);
          book = new Book(10003, "哈利波特", "罗琳", "2009-08-01", 20.5, 2365);
          insertBook(book);
          book = new Book(10004, "时间机器", "威尔", "2002-01-05", 170.5, 5624);
          insertBook(book);
          book = new Book(10005, "斗罗大陆", "唐家三少", "2003-01-01", 80.5, 8762);
          insertBook(book);
		}
	}

	public void insertBook(Book book) {  //添加图书
		bookList.add(book);
		saveFile();
	}

	public void deleteBook(int id) {   //删除图书

		for (int i = 0; i < bookList.size(); i++) {
			Book book = bookList.get(i);
			if (book.getBookNo() == id) {
				bookList.remove(i);
				saveFile();
				return;
			}
		}
	}

	public void upDataBook(Book book) {   //更新图书
		for (int i = 0; i < bookList.size(); i++) {
			Book book1 = bookList.get(i);
			if (book1.getBookNo() == book.getBookNo()) {

				book1.setBookWriter(book.getBookWriter());
				book1.setBookName(book.getBookName());
				book1.setBookPrice(book.getBookPrice());
				book1.setBookCount(book.getBookCount());
				book1.setBookPubDate(book.getBookPubDate());
                
				saveFile();
				return;
			}
		}
	}

	public List<Book> selectBooks() {   //返回图书列表
		return bookList;
	}

	public Book selectBook(int id) {    <span style="line-height: 20.02px; font-family: arial;">//用书的ID找书名</span>

		Book book = new Book();
		for (int i = 0; i < bookList.size(); i++) {
			book = bookList.get(i);
			if (book.getBookNo() == id) {
				return book;
			}
		}
		return null;
	}

	public void readFile() {       //读取文件
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		String name = "G:/java/com.jerel.0619/src/Bookcopy.txt"; //读取文件路径

		try {
			fis = new FileInputStream(name);
			ois = new ObjectInputStream(fis);
			bookList = (List<Book>) ois.readObject();
		} catch (FileNotFoundException e) {
			e.printStackTrace();	
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ois != null)
					ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
	public void saveFile() {         //存放文件
		ObjectOutputStream oos = null;
		FileOutputStream fos = null;
		String name = "G:/java/com.jerel.0619/src/Bookcopy.txt";          //存放文件路径(与读取一致)

		try {
			fos = new FileOutputStream(name);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(bookList);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (oos != null)
					oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;">
</span></span>
三、管理员、用户登陆界面MVC中的View(视图)

<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;"></span></span><pre name="code" class="java">(一)、管理员
package BookManager;

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

/*
 * 第三层:管理员测试Book类
 */
public class BookTest {

	private BookZip bookzip = new BookZip();  //new 一个图书管理类对象;
	private static Scanner sc;
	private static int which;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		System.out.println("欢迎使用图书书店!");
		System.out.println("请输入用户名:");
		String userName = sc.next();
		System.out.println("请输入密码:");
		String userpwd = sc.next();

		BookTest bookTest = new BookTest();
		if (userName.equals("allen") && userpwd.equals("123")) {
			System.out.println("成功");
			bookTest.showMainView();
			while(which !=5 ){
			bookTest.adminOption();
			}
		} else {
			System.out.println("密码账号错误");
		}

	}

	public void showMainView() {
		System.out.println("===================================" +
			  "欢迎登陆库存管理系统===================================");
		System.out.println("书号\t\t书名\t\t作者\t\t发布日期\t\t价格\t\t库存");
		List<Book> list = bookzip.selectBooks();
		for (Book book : list) {
			System.out.print(book.getBookNo() + "\t\t");
			System.out.print(book.getBookName() + "\t\t");
			System.out.print(book.getBookWriter() + "\t\t");
			System.out.print(book.getBookPubDate() + "\t");
			System.out.print(book.getBookPrice() + "\t\t");
			System.out.print(book.getBookCount() + "\n");
		}
		System.out.println("请选择进行的操作:1.图书入库  2.图书出库  3.查询全部图书  4.新增图书5.退出");
	}
	public void adminOption(){
		
		int which = sc.nextInt();
		switch (which) {
		case 1://入库
			addCount();
			break;
		case 2://出库
			outCount();
			break;
		case 3://查询全部
			showMainView();
			break;
		case 4://新增图书
			addBook();
			break;

		default://退出a
			bookzip.saveFile();
			break;
		}
	}
	public void outCount(){
	    System.out.println("选择操作的图书书号");
	    int bookId = sc.nextInt();
	    Book book = bookzip.selectBook(bookId);
	    System.out.println("输入出库的数量");
	    int num = sc.nextInt();
	    book.setBookCount(book.getBookCount()-num);
	    bookzip.saveFile();
		showMainView();
		adminOption();
	}
	public void  addCount(){
		System.out.println("请选择要操作的图书书号");
		int  id = sc.nextInt();
		List<Book> list = bookzip.selectBooks();
		for (int i = 0; i < list.size(); i++) {
			Book book = list.get(i);
			if (book.getBookNo() == id){
				System.out.println("选择操作的数量");
				int a = sc.nextInt();
				book.setBookCount(book.getBookCount()+a);
                bookzip.saveFile();
        		showMainView();
        		adminOption();
			} 
		}System.out.println("输入错误!");
	}
	public void addBook(){
		System.out.println("请依次录入:");
		System.out.println("书的编号");
		int a = sc.nextInt();
		System.out.println("书的名字:");
		String b = sc.next();
		System.out.println("书的作者");
		String c = sc.next();
		System.out.println("书的发布日期");
		String d = sc.next();
		System.out.println("书的价格");
		double e = sc.nextDouble();
		System.out.println("书的库存");
		int f = sc.nextInt();
//		Book book = new Book(a,b,c,d,e,f);
		Book book = new Book();
		book.setBookNo(a);
		book.setBookName(b);
		book.setBookWriter(c);
		book.setBookPubDate(d);
		book.setBookPrice(e);
		book.setBookCount(f);
		bookzip.insertBook(book);
		showMainView();
		adminOption();
	}
}

(二)、用户

<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;">
</span></span>
<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;"></span></span><pre name="code" class="java">package BookManager;

//用户登录界面
import java.util.*;

public class BookUser {

	private BookZip bookzip = new BookZip();// new 一个对象;
	private static Scanner sc;
	private static int which;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		System.out.println("欢迎使用图书书店!");
		System.out.println("请输入用户名:");
		String userName = sc.next();
		System.out.println("请输入密码:");
		String userpwd = sc.next();

		BookUser bookUser = new BookUser();
		if (userName.equals("user") && userpwd.equals("12345")) {
			System.out.println("成功");
			bookUser.showMainView();
			while (which != 3) {
				bookUser.adminOption();
			}
		} else {
			System.out.println("密码账号错误");
		}

	}

	public void showMainView() {
		System.out.println("==================================="
				+ "欢迎登陆库存管理系统===================================");
		System.out.println("书号\t\t书名\t\t作者\t\t发布日期\t\t价格\t\t库存");
		List<Book> list = bookzip.selectBooks();
		for (Book book : list) {
			System.out.print(book.getBookNo() + "\t\t");
			System.out.print(book.getBookName() + "\t\t");
			System.out.print(book.getBookWriter() + "\t\t");
			System.out.print(book.getBookPubDate() + "\t");
			System.out.print(book.getBookPrice() + "\t\t");
			System.out.print(book.getBookCount() + "\n");
		}
		System.out.println("请选择进行的操作:1.查询全部图书  2.结账 3.退出");
	}

	public void adminOption() {

		int which = sc.nextInt();
		switch (which) {
		case 1:// 查询全部图书
			showMainView();
			break;
		case 2:// 结账
			BuyBooks();
			break;
		default: // 退出
			bookzip.saveFile();
			break;
		}
	}

	public void BuyBooks() {
		Book book = null;
		double total = 0.0, amt = 0.0;
		Map<Book, Object> map = new HashMap<Book, Object>();//
		while (true) {
			System.out.println("选择购买图书编号ID:");
			int bookId = sc.nextInt();
			book = bookzip.selectBook(bookId);
			System.out.println("选择购买数量:");
			int count = sc.nextInt();
			if (book.getBookCount() < count) {
				System.out.println("购买数量太大,请重新输入");
				continue;
			}
			map.put(book, count);//
			System.out.println(book.getBookName() + ":" + book.getBookPrice());
			total = book.getBookPrice() * count;
			System.out.println("小记:" + total);
			amt += total;
			// book.setBookCount(book.getBookCount() - count);
			//(95-101行代码与 68行和79行代码)和此代码相同
			System.out.println("是否继续?是/y 否/n");
			String a = sc.next();
			if (a.equals("n"))
				break;
		}
		System.out.println("总计:" + amt);
		System.out.println("支付:");
		double pay = sc.nextDouble();
		System.out.println("找零:" + (pay - amt));
		Set<Book> set = map.keySet();//95-101行代码与 68行和79行代码共同
		Iterator<Book> it = set.iterator();
		while (it.hasNext()) {
			Book book1 = (Book) it.next();
			int count = Integer.parseInt(map.get(book1).toString());
			book1.setBookCount(book1.getBookCount() - count);
		}
		bookzip.saveFile();
		showMainView();
		adminOption();
	}
}

四、部分截图:

<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;">
</span></span>
<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;">(一)、管理员(部分功能未展示)</span></span>
<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;"><img src="http://img.blog.csdn.net/20160621093947072?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</span></span>
<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;"><img src="http://img.blog.csdn.net/20160621093950916?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</span></span>
<span style="font-size:18px;"><span style="color: rgb(51, 51, 51); font-family: arial; line-height: 20.02px;">(二)、用户(结账)
<img src="http://img.blog.csdn.net/20160621093956588?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

</span></span>

技术分享

技术分享

基于MVC的图书管理系统

标签:

原文地址:http://blog.csdn.net/qq_34122768/article/details/51724700

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!