实现代码:
Model:
package org.jason.model;
//用户类,生成set/get/ hashcode()/equals/构造函数等
publicclass User {
privateintid;
privateintage;
private String name;
private String sex;
privatedoublesalary;
public User(int id, int age, String name, String sex, double salary) {
super();
this.id = id;
this.age = age;
this.name = name;
this.sex = sex;
this.salary = salary;
}
public User() {
super();
}
publicint getId() {
returnid;
}
publicvoid setId(int id) {
this.id = id;
}
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age = age;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public String getSex() {
returnsex;
}
publicvoid setSex(String sex) {
this.sex = sex;
}
publicdouble getSalary() {
returnsalary;
}
publicvoid setSalary(double salary) {
this.salary = salary;
}
@Override
publicint hashCode() {
finalint prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(salary);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
publicboolean equals(Object obj) {
if (this == obj)
returntrue;
if (obj == null)
returnfalse;
if (getClass() != obj.getClass())
returnfalse;
User other = (User) obj;
if (age != other.age)
returnfalse;
if (id != other.id)
returnfalse;
if (name == null) {
if (other.name != null)
returnfalse;
} elseif (!name.equals(other.name))
returnfalse;
if (Double.doubleToLongBits(salary) != Double
.doubleToLongBits(other.salary))
returnfalse;
if (sex == null) {
if (other.sex != null)
returnfalse;
} elseif (!sex.equals(other.sex))
returnfalse;
returntrue;
}
@Override
public String toString() {
return"User [id=" + id + ", age=" + age + ", name=" + name
+ ", sex=" + sex + ", salary=" + salary + "]";
}
}
Ui 包
package org.jason.ui;
import java.util.Properties;
import org.jason.io.IoUtils;
import org.jason.model.*;
public class UserUi{
private static Properties properties=null;
static{
//静态代码块,调用ioutils.loadproperties方法
properties=IoUtils.loadProperties("resources", "user.properties");
}
public void loadUI(){
System.out.println(getHelpInfo("welcome"));
}
public String getHelpInfo(String key){
//调用properties.getProperty方法,返回字符串
String helpinfo=properties.getProperty(key);
return helpinfo;
}
publicvoid displayUserInfo(User user){
System.out.println("id:"+user.getId()+"\tname"+user.getName()+"\tage:"+user.getAge()+"\tsex"+user.getSex()+"\tsalary"+user.getSalary());
}
}
Io包:
package org.jason.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
public class IoUtils {
/**
* @param args
*/
//dir文件路径 fileName文件名
public static Properties loadProperties(String dir,String fileName){
// TODO Auto-generated method stub
Properties properties=new Properties();
//调用System的getProperty方法,得到项目路径
String projectPath=System.getProperty("user.dir");
//File.separator分割符
String propertiespath=projectPath+File.separator+dir+File.separator+fileName;
try {
InputStream in=new FileInputStream(propertiespath);
//调用load方法,将properties装载
properties.load(in);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return properties;
}
}
package org.jason.io;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//用于接收用户从键盘当中输入的数据
public class UserInput {
private BufferedReader bufferedReader;
public UserInput(){
//System.in是标准的接收用户输入流
//生成bufferedReader对象,字节流转换为字符流,而后处理流
bufferedReader=new BufferedReader(new InputStreamReader(System.in));
}
public String getInputLine(){
String inputLine=null;
try{
//用户输入数据,按下回车键,该行代码读取用户输入,如果不输入则处于阻塞状态
inputLine=bufferedReader.readLine();
}
catch(Exception e){
e.printStackTrace();
}
return inputLine;
}
}
Logic包
package org.jason.logic;
import org.jason.io.UserInput;
publicclass MainLooper {
privatebooleanflag=true;
public UserInput userInput;
public MainLooper(){
userInput=new UserInput();
}
publicvoid loop(){
while(flag){
String input=userInput.getInputLine();
System.out.println(input);
}
}
}
Main.java
package org.jason.model;
import org.jason.logic.MainLooper;
import org.jason.ui.UserUi;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UserUi userUi=new UserUi();
userUi.loadUI();
MainLooper mainLooper=new MainLooper();
mainLooper.loop();
}
}