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

📄 employeeconsoleview.java

📁 对XML和DB的CURD的基本操作.使用的两个比较常用的组合设计模式:MVC模式+DAO模式。
💻 JAVA
字号:
package com.mycompany.myapp.view.console;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import com.mycompany.myapp.control.ControllerInterface;
import com.mycompany.myapp.model.Employee;

/**
 *	the view of mvc 
 */
public class EmployeeConsoleView {
	private ControllerInterface<Employee,Long> controller;
	
	public EmployeeConsoleView(ControllerInterface<Employee,Long> employeeController){
		this.controller = employeeController;
	}

	public void createView(){
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
		PrintStream w = System.out;
		while (true) {
			try {
				w.print("employeeCRUD> ");
				String line = r.readLine();
				if (line == null)
					System.exit(0); // used for keyboard interrupt
				String[] ss = line.split("\\s+", 2);
				if (ss.length > 0) {
					String cmd = ss[0];
					String cmdLine;
					if (ss.length > 1)
						cmdLine = ss[1];
					else
						cmdLine = "";

					if (cmd.equalsIgnoreCase("LIST")||cmd.equalsIgnoreCase("LS")||cmd.equalsIgnoreCase("ALL")) {
						w.print("LIST:"+"\n");
						controller.getAll();
					} else if (cmd.equalsIgnoreCase("INSERT")||cmd.equalsIgnoreCase("ADD")) {
						w.print("INSERT:"+"\n");
						w.print("first name:");
						String firstName=r.readLine();
						w.print("last name:");
						String lastName=r.readLine();
						w.print("phone number:");
						String phoneNumber=r.readLine();
						w.print("address:");
						String address=r.readLine();
						Employee employee = new Employee();
						employee.setId(System.currentTimeMillis());
						employee.setFirstName(firstName);
						employee.setLastName(lastName);
						employee.setPhoneNumber(phoneNumber);
						employee.setAddress(address);
						controller.insert(employee);
						w.print("INSERT DONE!\n");
					} else if (cmd.equalsIgnoreCase("REMOVE")||cmd.equalsIgnoreCase("DELETE")) {
						w.print("REMOVE:"+"\n");
						w.print("employee id:");
						String id=r.readLine();
						while(!id.toUpperCase().equals(id)){
							w.println("Not number,try again pls!\n");
							id=r.readLine();
						}
						Employee employee = controller.get(Long.valueOf(id));
						if(employee!=null){
							controller.remove(Long.valueOf(id));
						}
						else{
							w.print("No employee with id="+id+" exist!\n");
						}
						w.print("REMOVE DONE!\n");
					} else if (cmd.equalsIgnoreCase("FIND")||cmd.equalsIgnoreCase("VIEW")||cmd.equalsIgnoreCase("SEARCH")) {
						w.print("FIND:"+"\n");
						w.print("employee id:");
						String id=r.readLine();
						while(!id.toUpperCase().equals(id)){
							w.println("Not number,try again pls!\n");
							id=r.readLine();
						}
						Employee employee = controller.get(Long.valueOf(id));
						if(employee!=null){
							w.print("employee id:"+employee.getId()+"\n");
							w.print("first name:"+employee.getFirstName()+"\n");
							w.print("last name:"+employee.getLastName()+"\n");
							w.print("phone number:"+employee.getPhoneNumber()+"\n");
							w.print("address:"+employee.getAddress()+"\n");	
						}
						else{
							w.print("No employee with id="+id+" exist!\n");
						}
						w.print("FIND DONE!\n");
					} else if (cmd.equalsIgnoreCase("UPDATE")||cmd.equalsIgnoreCase("MODIFY")) {
						w.print("UPDATE:"+"\n");
						w.print("employee id:");
						String id=r.readLine();
						while(!id.toUpperCase().equals(id)){
							w.println("Not number,try again pls!\n");
							id=r.readLine();
						}
						w.print("first name:");
						String firstName=r.readLine();
						w.print("last name:");
						String lastName=r.readLine();
						w.print("phone number:");
						String phoneNumber=r.readLine();
						w.print("address:");
						String address=r.readLine();
						Employee employee = new Employee();
						employee.setId(Long.valueOf(id));
						employee.setFirstName(firstName);
						employee.setLastName(lastName);
						employee.setPhoneNumber(phoneNumber);
						employee.setAddress(address);
						controller.update(employee);
						w.print("UPDATE DONE!\n");
					} else if (cmd.equalsIgnoreCase("EXIT")
							|| cmd.equalsIgnoreCase("QUIT")
							|| cmd.equalsIgnoreCase("BYE")) {
						System.exit(0);
					} else if (cmd.equalsIgnoreCase("HELP")
							|| cmd.equalsIgnoreCase("?")) {
						w.println("available commands:");
						w.println("----------------------------");
						w.println("help:");
						w.println("  Here am i,wellcome!");
						w.println("list/all:");
						w.println("  List all employees.");
						w.println("insert/add:");
						w.println("  Add a new employee record.");
						w.println("remove/delete:");
						w.println("  Remove a employee record.");
						w.println("find/view:");
						w.println("  Show the detail of a employee.");
						w.println("update/modify:");
						w.println("  Update the detail of a employee.");
					} else {
						w.print("unrecognized command!\n");
					}
				} else {
					w.print("unrecognized command!\n");
				}
			} catch (IOException e) {
				w.print("I have traced a IO exception, program exits.\n");
				System.exit(1);
			}
		}
	}
}

⌨️ 快捷键说明

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