程序入口
public class HouseRent {
public static void main ( String [ ] args) {
new HouseView ( ) . Menu ( ) ;
}
}
用户界面
public class HouseView {
boolean loop = true ;
HouseService houseService = new HouseService ( ) ;
public void Menu ( ) {
do {
System . out. println ( "----------房屋出租系统----------" ) ;
System . out. println ( "\t\t1 新增房屋" ) ;
System . out. println ( "\t\t2 查找房屋" ) ;
System . out. println ( "\t\t3 删除房屋" ) ;
System . out. println ( "\t\t4 修改房屋" ) ;
System . out. println ( "\t\t5 房屋列表" ) ;
System . out. println ( "\t\t6 退出系统" ) ;
System . out. print ( "请选择(1-6):" ) ;
char choice = Utility . readChar ( ) ;
switch ( choice) {
case '1' :
addHouse ( ) ;
break ;
case '2' :
findHouse ( ) ;
break ;
case '3' :
delHouse ( ) ;
break ;
case '4' :
updateHouse ( ) ;
break ;
case '5' :
listHouse ( ) ;
break ;
case '6' :
Exit ( ) ;
break ;
default :
System . out. println ( "输入有误,请重新选择!" ) ;
}
} while ( loop) ;
System . out. println ( "已成功退出房屋出租系统~~" ) ;
}
public void listHouse ( ) {
System . out. println ( "----------房屋列表----------" ) ;
System . out. println ( "编号\t\t" + "房主\t\t" + "电话\t\t" + "地址\t\t" + "月租\t\t" + "状态(已出租/未出租)" ) ;
House [ ] house = houseService. List( ) ;
for ( int i = 0 ; i < house. length; i++ ) {
if ( house[ i] == null ) {
break ;
}
System . out. println ( house[ i] ) ;
}
System . out. println ( "房屋列表显示完毕~~" ) ;
}
public void addHouse ( ) {
System . out. println ( "----------新增房屋----------" ) ;
System . out. print ( "姓名:" ) ;
String name = Utility . readString ( 4 ) ;
System . out. print ( "电话:" ) ;
String telephone = Utility . readString ( 11 ) ;
System . out. print ( "地址:" ) ;
String address = Utility . readString ( 20 ) ;
System . out. print ( "月租:" ) ;
int rent = Utility . readInt ( 4 ) ;
System . out. print ( "状态(已出租/未出租):" ) ;
String state = Utility . readString ( 3 ) ;
House newHouse = new House ( 0 , name, telephone, address, rent, state) ;
if ( houseService. Add( newHouse) ) {
System . out. println ( "添加房屋成功~~" ) ;
} else {
System . out. println ( "添加房屋失败~~" ) ;
}
}
public void findHouse ( ) {
System . out. println ( "----------查找房屋----------" ) ;
System . out. print ( "请输入要查找的房屋id:" ) ;
int findId = Utility . readInt ( 4 ) ;
House house = houseService. findById ( findId) ;
if ( house != null ) {
System . out. println ( house) ;
} else {
System . out. println ( "该房屋编号不存在~~" ) ;
}
}
public void delHouse ( ) {
System . out. println ( "----------删除房屋----------" ) ;
System . out. print ( "请输入待删除房屋的编号(-1退出):" ) ;
int delId = Utility . readInt ( 4 ) ;
if ( delId == - 1 ) {
System . out. println ( "放弃删除房屋信息~~" ) ;
return ;
}
System . out. println ( "你确定要删除吗?" ) ;
char choice = Utility . readConfirmSelection ( ) ;
if ( choice == 'Y' ) {
if ( houseService. Delete( delId) ) {
System . out. println ( "删除房屋信息成功~~" ) ;
} else {
System . out. println ( "房屋编号不存在,无法删除~~" ) ;
}
} else {
System . out. println ( "放弃删除房屋信息~~" ) ;
}
}
public void updateHouse ( ) {
System . out. println ( "----------修改房屋----------" ) ;
System . out. print ( "请输入待修改房屋的编号(-1退出):" ) ;
int updateId = Utility . readInt ( 4 ) ;
if ( updateId == - 1 ) {
System . out. println ( "放弃修改房屋信息~~" ) ;
return ;
}
House house = houseService. findById ( updateId) ;
if ( house == null ) {
System . out. println ( "房屋编号不存在,无法修改~~" ) ;
return ;
} else {
System . out. print ( "姓名(" + house. getName ( ) + "):" ) ;
String name = Utility . readString ( 4 , "" ) ;
if ( ! "" . equals ( name) ) {
house. setName ( name) ;
}
System . out. print ( "电话(" + house. getTelephone ( ) + "):" ) ;
String telephone = Utility . readString ( 11 , "" ) ;
if ( ! "" . equals ( telephone) ) {
house. setTelephone ( telephone) ;
}
System . out. print ( "地址(" + house. getAddress ( ) + "):" ) ;
String address = Utility . readString ( 20 , "" ) ;
if ( ! "" . equals ( address) ) {
house. setAddress ( address) ;
}
System . out. print ( "月租(" + house. getRent ( ) + "):" ) ;
int rent = Utility . readInt ( - 1 ) ;
if ( rent != - 1 ) {
house. setRent ( rent) ;
}
System . out. print ( "状态(" + house. getState ( ) + "):" ) ;
String state = Utility . readString ( 3 , "" ) ;
if ( ! "" . equals ( state) ) {
house. setState ( state) ;
}
System . out. println ( "房屋信息修改成功~~" ) ;
}
}
public void Exit ( ) {
char isExit = Utility . readConfirmSelection ( ) ;
if ( isExit == 'Y' ) {
loop = false ;
}
}
}
业务逻辑类
public class HouseService {
House [ ] house = new House [ 1000 ] ;
int houseNum = 0 ;
int idCount = 0 ;
public House [ ] List ( ) {
return house;
}
public boolean Add ( House newHouse) {
if ( houseNum == house. length) {
System . out. println ( "房屋已满,不能再添加~~" ) ;
return false ;
}
house[ houseNum++ ] = newHouse;
newHouse. setId ( ++ idCount) ;
return true ;
}
public House findById ( int findId) {
for ( int i = 0 ; i < houseNum; i++ ) {
if ( findId == house[ i] . getId ( ) ) {
return house[ i] ;
}
}
return null ;
}
public boolean Delete ( int delId) {
int index = - 1 ;
for ( int i = 0 ; i < houseNum; i++ ) {
if ( delId == house[ i] . getId ( ) ) {
index = i;
break ;
}
}
if ( index == - 1 ) {
return false ;
}
for ( int i = index; i < houseNum - 1 ; i++ ) {
house[ i] = house[ i + 1 ] ;
}
house[ -- houseNum] = null ;
return true ;
}
}
房屋类
public class House {
private int id;
private String name;
private String telephone;
private String address;
private int rent;
private String state;
public House ( int id, String name, String telephone, String address, int rent, String state) {
this . id = id;
this . name = name;
this . telephone = telephone;
this . address = address;
this . rent = rent;
this . state = state;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getTelephone ( ) {
return telephone;
}
public void setTelephone ( String telephone) {
this . telephone = telephone;
}
public String getAddress ( ) {
return address;
}
public void setAddress ( String address) {
this . address = address;
}
public double getRent ( ) {
return rent;
}
public void setRent ( int rent) {
this . rent = rent;
}
public String getState ( ) {
return state;
}
public void setState ( String state) {
this . state = state;
}
@Override
public String toString ( ) {
return id + "\t\t" + name + "\t\t" + telephone + "\t\t"
+ address + "\t\t" + rent + "\t\t" + state;
}
}