⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookui.java

📁 本光盘包含了本书各章中出现的所有程序的源代码。 1. 如果是Java程序
💻 JAVA
字号:
package com.ui;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Iterator;

import com.dao.BookDAO;
import com.dao.IBookDAO;
import com.domain.Book;

public class BookUI {
	public String getInputString(String tip) throws Exception {
		System.out.print(tip);

		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(isr);

		String input = in.readLine();
		return input.trim();
	}

	public void start() throws Exception {
		System.out.println("<<<<>>>>");

		while (true) {
			String cmd = getInputString(">");

			if (cmd.equals("ls"))//输入ls执行doList()方法
				doList();
			else if (cmd.equals("add"))
				doAddBook();
			else if (cmd.equals("rm"))
				doRemoveBook();
			else if (cmd.equals("update"))
				doUpdateBook();
			else if (cmd.equals("clear"))
				doClear();
			else if (cmd.equals("exit"))
				break;
			else {
				System.out.println("( ls | add | rm | update | exit)");
			}
		}
	}
    private void doClear() {
		// TODO Auto-generated method stub
		
	}

	//显示数据库中所有的图书记录
	public void doList() throws Exception {
		
		IBookDAO bookDAO = new BookDAO();
		Collection ss = bookDAO.findAll();
		
		Iterator it = ss.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
	}

	public void doAddBook() throws Exception {
		
		IBookDAO bookDAO = new BookDAO();
		
		String bookId = getInputString("输入图书ID: ");//从控制台上得到输入图书的Id数据
		int id = Integer.parseInt(bookId);
		String bookname = getInputString("书名 : ");
		String author = getInputString("作者 : ");
		String price = getInputString("价格 : ");

		Book b = new Book();
		b.setId(id);
		b.setAuthor(author);
		//.....
		bookDAO.create(b);
		System.out.println("添加成功!");
	}

	public void doRemoveBook() throws Exception {
		IBookDAO bookDAO = new BookDAO();
		
		String bookId = getInputString("待删除图书号: ");
		int id = Integer.parseInt(bookId);
        Book book = new Book();
        book.setId(id);
		Book res = bookDAO.find(book);
		
		if (res == null) {
			System.out.println("没有此书!");
		} else {
			bookDAO.remove(book);
			System.out.println("完成删除!");
		}
	}

	public void doUpdateBook() throws Exception {
		IBookDAO bookDAO = new BookDAO();
		
		String sid = getInputString("待更新图书号: ");
		int id = Integer.parseInt(sid);
        Book book = new Book();
        book.setId(id);
		Book res = bookDAO.find(book);
		
		if (res == null) {
			System.out.println("没有此书!");
		} else {
			String name = getInputString("(:" + res.getName() + ") (): ");
			String author = getInputString("(:" + res.getAuthor() + ") (): ");
			String price = getInputString("(:" + res.getPrice()
					+ ") (): ");

			if (name != null && name.length() > 0)
				res.setName(name);
			if (author != null && author.length() > 0)
				res.setAuthor(author);
			if (price != null && price.length() > 0)
				res.setPrice(Double.parseDouble(price));

			bookDAO.update(res);
			System.out.println("更新完毕!");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -