Day33:综合项目之图书管理系统

主要是将MCV模式、代理模式、工厂模式、反射、文件的I/O综合起来实现的简单的图书管理项目,主要功能就是增删改查图书,没有什么难度,后期会进行更多扩展。具体代码实现如下:

包结构如下:

在这里插入图片描述

Book.java

package com.biubiubiu.bookmgr.pojo;

import java.util.Date;

import com.biubiubiu.bookmgr.util.Utils;

/**
 * 图书类实体类bean
 * @author dell
 *
 */


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book implements Comparable<Book>{
	private String bookno;
	private String bookname;
	private Date booktime;
	
	@Override
	public int compareTo(Book o) {
		String one=Utils.dateToStr(this.getBooktime());
		String two=Utils.dateToStr(o.getBooktime());
		return one.compareTo(two);
	}
	
}

BookDAO.java

package com.biubiubiu.bookmgr.dao;

import java.util.List;

import com.biubiubiu.bookmgr.pojo.Book;

public interface BookDAO {

	// 查询图书
	List<Book> select();

	// 添加图书
	void add(Book book);

	// 修改图书
	Book update(int index, Book book);

	// 删除图书
	boolean delete(int index);

	// 根据图书编号返回下标
	int getIndex(String bookId);

}

BookDAOImpl.java

package com.biubiubiu.bookmgr.dao.daoimpl;

/**
 * 处理数据,具体实现
 */
import java.util.ArrayList;
import java.util.List;

import com.biubiubiu.bookmgr.dao.BookDAO;
import com.biubiubiu.bookmgr.pojo.Book;
import com.biubiubiu.bookmgr.util.Utils;

public class BookDAOImpl implements BookDAO {
	public static List<Book> books = new ArrayList<Book>();
	
	static {
		String bookstrs = Utils.readFile();
		String[] booksn = bookstrs.split("\n");
		for (String str : booksn) {
			String values[] = str.split(",");
			Book book = new Book(values[0], values[1], Utils.strToDate(values[2]));
			books.add(book);
		}
	}
	
	// 查询图书
	/* (non-Javadoc)
	 * @see com.biubiubiu.bookmgr.dao.BookDAO#select()
	 */
	@Override
	public List<Book> select() {
		return books;
	}

	// 添加图书
	/* (non-Javadoc)
	 * @see com.biubiubiu.bookmgr.dao.BookDAO#add(com.biubiubiu.bookmgr.pojo.Book)
	 */
	@Override
	public void add(Book book) {
		books.add(book);
		String id = book.getBookno();
		String name = book.getBookname();
		String date = Utils.dateToStr(book.getBooktime());
		String bookstr = "\n" + id + "," + name + "," + date;
		Utils.writeFile(bookstr, true);
	}

	// 修改图书
	/* (non-Javadoc)
	 * @see com.biubiubiu.bookmgr.dao.BookDAO#update(int, com.biubiubiu.bookmgr.pojo.Book)
	 */
	@Override
	public Book update(int index, Book book) {
		Book bookChange = books.set(index, book);
		return bookChange;
	}

	// 删除图书
	/* (non-Javadoc)
	 * @see com.biubiubiu.bookmgr.dao.BookDAO#delete(int)
	 */
	@Override
	public boolean delete(int index) {
		return books.remove(index) != null ? true : false;
	}

	// 根据图书编号返回下标
	/* (non-Javadoc)
	 * @see com.biubiubiu.bookmgr.dao.BookDAO#getIndex(java.lang.String)
	 */
	@Override
	public int getIndex(String bookId) {
		int index = -1;
		for (int i = 0; i < books.size(); i++) {
			if (books.get(i).getBookno().equalsIgnoreCase(bookId)) {
				index = i;
			}
		}
		return index;
	}

}

BookService.java

package com.biubiubiu.bookmgr.service.serviceimpl;
/**
 * 业务层
 * @author 11142
 *
 */

import java.util.Collections;
import java.util.List;

import com.biubiubiu.bookmgr.dao.daoimpl.BookDAOImpl;
import com.biubiubiu.bookmgr.pojo.Book;
import com.biubiubiu.bookmgr.service.BookService;
import com.biubiubiu.bookmgr.util.ObjectFactory;
import com.biubiubiu.bookmgr.util.Utils;

public class BookServiceImpl implements BookService {
	BookDAOImpl bookDAOImpl;
	
	public BookServiceImpl() {
		bookDAOImpl = ObjectFactory.getInstance(Utils.B_D);
	}
	
	//查询图书数据业务(排序)
	/* (non-Javadoc)
	 * @see com.biubiubiu.bookmgr.service.BookService#selectBooks()
	 */
	@Override
	public List<Book> selectBooks(){
		List<Book> booklist=bookDAOImpl.select();
		Collections.sort(booklist);
		return booklist;
	}
	
	//添加图书数据操作
	/* (non-Javadoc)
	 * @see com.biubiubiu.bookmgr.service.BookService#addBook(com.biubiubiu.bookmgr.pojo.Book)
	 */
	@Override
	public String addBook(Book book) {
		String message = "";
		int index = bookDAOImpl.getIndex(book.getBookno());
		if(index == -1) {
			bookDAOImpl.add(book);
			message = "添加成功";
		}else {
			message="添加失败!图书编号已存在!";
		}
		return message;
	}
	
	//更新图书数据操作
	/* (non-Javadoc)
	 * @see com.biubiubiu.bookmgr.service.BookService#updateBook(int, java.lang.String, java.lang.String)
	 */
	@Override
	public String updateBook(int index, String type, String value) {
		String message = "修改成功~";
		Book book  = bookDAOImpl.select().get(index);
		if(type.equalsIgnoreCase("a")) {
			book.setBookno(value);
		}else if (type.equalsIgnoreCase("b")) {
			book.setBookname(value);
		}else if (type.equalsIgnoreCase("c")) {
			book.setBooktime(Utils.strToDate(value));
		}else {
			message = "你的输入有误~";
		}
		Utils.updateFile();
		return message;
	}
	
	//删除图书数据操作
	/* (non-Javadoc)
	 * @see com.biubiubiu.bookmgr.service.BookService#deleteBook(int)
	 */
	@Override
	public String deleteBook(int index) {
		String message = "删除成功";
		Book book  = bookDAOImpl.select().get(index);
		bookDAOImpl.books.remove(book);
		Utils.updateFile();
		return message;
	} 
	
}

BookAction.java

package com.biubiubiu.bookmgr.action;

import java.util.List;
/**
 * 界面层
 */
import java.util.Scanner;

import org.junit.Test;

import com.biubiubiu.bookmgr.pojo.Book;
import com.biubiubiu.bookmgr.service.BookService;
import com.biubiubiu.bookmgr.util.Utils;
import com.biubiubiu.bookmgr.util.ObjectFactory;

public class BookAction {
	BookService bookServiceImpl ;
	
	public BookAction() {
		bookServiceImpl = ObjectFactory.getInstance(Utils.B_S);
	}

	@Test
	public void menu() {
		System.out.println("1.显示所有图书");
		System.out.println("2.添加图书");
		System.out.println("3.修改图书");
		System.out.println("4.删除图书");
		System.out.println("0.退出系统");
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入:>>");
		try {
			int n = sc.nextInt();
			switch (n) {
			case 1:
				selectBook();
				break;
			case 2:
				addBook();
				break;
			case 3:
				updateBook();
				break;
			case 4:
				delectBook();
				break;
			case 0:
				if (exit()) {
					return;
				}
				break;
			default:
				System.out.println("输入的序号有误,请重新输入!");
			}

		} catch (Exception e) {
			System.out.println("请输入合法序号!");
		}
		menu();
	}

	/**
	 * 查询图书
	 */
	public void selectBook() {
		List<Book> booksList = bookServiceImpl.selectBooks();
		if(booksList.isEmpty()) {
			System.out.println("暂时没有图书信息~");
		}else {
			for (int i=0; i<booksList.size(); i++) {
				System.out.println((i+1) + "、  " + booksList.get(i).getBookno() + "," + booksList.get(i).getBookname() + "," + Utils.dateToStr(booksList.get(i).getBooktime()));
			}
			System.out.println();
		}
	}

	/**
	 * 添加图书
	 */
	public void addBook() {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入图书编号:");
		String bId = sc.nextLine();
		System.out.println("请输入图书名称:");
		String bName = sc.nextLine();
		System.out.println("请输入图书创建日期(yyyy-MM-dd HH:mm:ss):");
		String bDate = sc.nextLine();
		Book book = new Book(bId, bName, Utils.strToDate(bDate));
		String message= bookServiceImpl.addBook(book);
        System.out.println(message);
	}

	/**
	 * 更新图书
	 */
	public void updateBook() {
		selectBook();
		Scanner sc = new Scanner(System.in);
		System.out.println("请选择你要更新的图书序号:");
		int id = sc.nextInt();
		if(id<=0 || id>bookServiceImpl.selectBooks().size()) {
			System.out.println("你要更新的图书不存在~");
		}else {
			int index = id - 1;
			System.out.println("请选择要修改的类型:a.图书编号 b.图书名称  c.图书日期");
			String type = sc.next();
			System.out.println("请输入修改后的数据:");
			Scanner scd = new Scanner(System.in);
			String value = scd.nextLine();
			String message = bookServiceImpl.updateBook(index, type, value);
			System.out.println(message);
		}
	}
	/**
	 * 删除图书
	 */
	public void delectBook() {
		selectBook();
		Scanner sc = new Scanner(System.in);
		System.out.println("请选择你要删除的图书序号:");
		int id = sc.nextInt();
		if(id<=0 || id>bookServiceImpl.selectBooks().size()) {
			System.out.println("你要删除的图书不存在~");
		}else {
			int index = id - 1;
			String message = bookServiceImpl.deleteBook(index);
			System.out.println(message);
		}
	}
	/**
	 * 退出
	 * @return
	 */
	
	public boolean exit() {
		System.out.println("确定退出吗?(y/任意键返回)");
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		if (s.equalsIgnoreCase("y")) {
			System.out.println("程序退出成功!");
			return true;
		} else {
			System.out.println("取消了退出,程序继续运行!");
			return false;
		}
	}
}

ObjectFactory.java

package com.biubiubiu.bookmgr.util;

import java.util.HashMap;
import java.util.Map;

public class ObjectFactory {
	
	private static Map<String, Object> mapobject = new HashMap<String, Object>();
	
	public static <E> E getInstance(String key) {
		E obj = null;
		try {
			if(mapobject.containsKey(key)) {
				obj = (E) mapobject.get(key);
			}else {
				obj = (E)Class.forName(Utils.getValue(key)).newInstance();
				mapobject.put(key, obj);
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return obj;
	}
}

Utils.java

package com.biubiubiu.bookmgr.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
 * 工具类
 */
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.biubiubiu.bookmgr.dao.daoimpl.BookDAOImpl;

public class Utils {
	
	public final static String B_D = "bookDAOImpl";
	public final static String B_S = "bookServiceImpl";
	static Map<String, String> map = new HashMap<String, String>();
	static {
		map.put("bookDAOImpl", "com.biubiubiu.bookmgr.dao.daoimpl.BookDAOImpl");
		map.put("bookServiceImpl", "com.biubiubiu.bookmgr.service.serviceimpl.BookServiceImpl");
	}
	public static String getValue(String key) {
		return map.get(key);
	} 
	
	/**
	 * 把字符串转为日期
	 * @param str
	 * @return
	 */
	public static Date strToDate(String str) {
		Date date = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = df.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}



	/**
	 * 日期转为字符串
	 * @param date
	 * @return
	 */
	public static String dateToStr(Date date) {
		String str = "";
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		str = ft.format(date);
		return str;
	}
	
	/**
	 * 写文件
	 * @param str
	 * @param isAppend 追加还是覆盖
	 */
	public static void writeFile(String str, boolean isAppend) {
		File file = new File("E:\\JAVA\\JAVA_WorkSpace\\KGC_Senior\\Senior1025\\src\\book.txt");
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file, isAppend);
			byte[] bytes = str.getBytes();
			fos.write(bytes);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 读文件
	 * @return
	 */
	public static String readFile() {
		StringBuffer str = new StringBuffer();
		InputStream is = BookDAOImpl.class.getClassLoader().getResourceAsStream("book.txt");
		InputStreamReader isr = new InputStreamReader(is);
		int i = -2;
		try {
			while ((i = isr.read()) != -1) {
				str.append((char) i);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				isr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return str.toString();
	}
	
	/**
	 * 修改删除操作
	 */
	public static void updateFile() {
		for(int i=0; i<BookDAOImpl.books.size(); i++) {
			writeFile("", false);
		}
		for(int i=0; i<BookDAOImpl.books.size(); i++) {
			String id = BookDAOImpl.books.get(i).getBookno();
			String name = BookDAOImpl.books.get(i).getBookname();
			String date = Utils.dateToStr(BookDAOImpl.books.get(i).getBooktime());
			String bookstr = "";
			if(i == BookDAOImpl.books.size()-1) {
				bookstr = id + "," + name + "," + date;
			}else {
				bookstr = id + "," + name + "," + date + "\n";
			}
			writeFile(bookstr, true);
		}
	} 
	
}

book.txt

book001,斗破苍穹,2018-03-03 09:58:30
book003,斗罗大陆,2019-02-02 09:58:30
book002,武动乾坤,2019-04-04 09:58:30
book004,大主宰,2019-05-05 09:58:30

发布了108 篇原创文章 · 获赞 39 · 访问量 9367

猜你喜欢

转载自blog.csdn.net/qq_40246175/article/details/102791432