首页 > 代码库 > java模拟而一个电话本操作

java模拟而一个电话本操作

哈哈,大家平时都在使用电话本,下面使用java来模拟而一个简单的电话本吧...

首先给出联系人的抽象类

package net.itaem.po;

/**
 * 
 * 电话人的信息
 * */
public class User {

	private String name;
	private String phoneNumber;
	private String companyName;
	private String email;
	private String address;
	private String birthday;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public String getCompanyName() {
		return companyName;
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", phoneNumber=" + phoneNumber
				+ ", companyName=" + companyName + ", email=" + email
				+ ", address=" + address + ", birthday=" + birthday + "]";
	}
	public User(String name, String phoneNumber, String companyName,
			String email, String address, String birthday) {
		this.name = name;
		this.phoneNumber = phoneNumber;
		this.companyName = companyName;
		this.email = email;
		this.address = address;
		this.birthday = birthday;
	}
	
	public User(String name, String phoneNumber){
		this.name = name;
		this.phoneNumber = phoneNumber;
	}
	
}

给出操作数据库的UserDao,这里只是简单的模拟操作...所以使用打印来提示操作结果


下面给出工具类,用来获得MVC中的VIEW的请求内容

package net.itaem.tool;

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

public class GetInput {
    private static BufferedReader bufReader;
    
    static{
    	bufReader = new BufferedReader(new InputStreamReader(System.in));
    }
	
	public static String getInputString(String info) throws IOException{
		System.out.println(info);
		String result = null;
		result = bufReader.readLine();   //读取内存输入的一行数据
		return result;
	}
	
	public static void main(String[] args) throws IOException{
	}
}

下面给出控制类,相当于MVC中的C,Controller,这个类负责和DAO,VIEW,TOOL等其它类交互...

package net.itaem.operate;

import java.io.IOException;

import net.itaem.dao.UserDao;
import net.itaem.po.User;
import net.itaem.tool.GetInput;

/**
 * 操作类
 * */
public class Operate {
	
	public Operate(){
		start();
	}
	
	public void start(){
		try {
			while(true){   //一直运行
				run();
			}
		} catch (IOException e) {
			e.printStackTrace();

		}		
	}

	private void run() throws IOException{
		System.out.println("======================电话本===================");

		System.out.println("1查看所有联系人");
		System.out.println("2增加联系人");
		System.out.println("3删除联系人");
		System.out.println("4搜索联系人");
		System.out.println("5退出");

		String choose = GetInput.getInputString("输入你的选择");
		char choice = choose.toCharArray()[0];
		switch(choice){
		case ‘1‘:
			System.out.println(UserDao.getInstance().findAll());  //输出所有电话本用户
			break;
		case ‘2‘:
			UserDao.getInstance().add(new User(GetInput.getInputString("输入名字"), 
					GetInput.getInputString("输入电话号码")));
			break;
		case ‘3‘:
			UserDao.getInstance().delete(new User(GetInput.getInputString("输入名字"), 
					GetInput.getInputString("输入电话号码")));
			break;
		case ‘4‘:
			System.out.println(UserDao.getInstance().findUsersByKeyword(GetInput.getInputString("输入要查询的关键字")));
			break;
		case ‘5‘:
			exit();
			break;
		default:
			System.out.println("sorry your input was wrong, please input 1、2、3、4");	
		}
	}

	private void exit(){
		System.exit(0);  //退出
	}

	public static void main(String[] args) {
		new Operate().start();
	}
}

具体的程序运行结果大家可以自己测试下,或者读读代码看看也应该可以明白...

下面是类图:




总结:这个模拟并没有什么太大的难度,但是重点就是使用面向对象的思想去设计,然后各个类之间达到解耦。