码迷,mamicode.com
首页 > 编程语言 > 详细

java模拟而一个电话本操作

时间:2017-07-20 18:49:41      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:choice   ace   ice   操作   str   efault   ase   except   string   

哈哈。大家平时都在使用电话本。以下使用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("输入要查询的keyword")));
			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();
	}
}

详细的程序执行结果大家能够自己測试下,或者读读代码看看也应该能够明确...

以下是类图:




技术分享

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

java模拟而一个电话本操作

标签:choice   ace   ice   操作   str   efault   ase   except   string   

原文地址:http://www.cnblogs.com/slgkaifa/p/7213238.html

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