房屋出租系统【韩顺平 Java 基础案例复现】 | 字数总计: 3.4k | 阅读时长: 15分钟 | 阅读量: 
本项目是根据B站韩顺平【零基础快速 Java】课程进行的。项目的主要目的是学习巩固 Java 基础知识。
房屋出租系统【韩顺平 Java 基础案例复现】 前言 本项目是根据B站韩顺平【零基础快速 Java】课程进行的。项目的主要目的是学习巩固 Java 基础知识。
需求 实现房屋出租系统,基本功能包括对房屋信息的增删改查(存储用数组实现)。
项目设计-程序框架图  
项目代码实现 文件夹结构  
工具类准备 这里的 Utility 类是韩顺平Utility工具类(java房屋出租项目),创建工具类文件复制到自己的项目里即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 package  com.codedog.houserent.utils;    import  java.util.*;public  class  Utility  {	     private  static  Scanner  scanner  =  new  Scanner (System.in);           	public  static  char  readMenuSelection ()  {         char  c;         for  (; ; ) {             String  str  =  readKeyBoard(1 , false );             c = str.charAt(0 );             if  (c != '1'  && c != '2'  &&                  c != '3'  && c != '4'  && c != '5' ) {                 System.out.print("选择错误,请重新输入:" );             } else  break ;         }         return  c;     } 	     public  static  char  readChar ()  {         String  str  =  readKeyBoard(1 , false );         return  str.charAt(0 );     }               public  static  char  readChar (char  defaultValue)  {         String  str  =  readKeyBoard(1 , true );         return  (str.length() == 0 ) ? defaultValue : str.charAt(0 );     } 	          public  static  int  readInt ()  {         int  n;         for  (; ; ) {             String  str  =  readKeyBoard(10 , false );             try  {                 n = Integer.parseInt(str);                 break ;             } catch  (NumberFormatException e) {                 System.out.print("数字输入错误,请重新输入:" );             }         }         return  n;     }          public  static  int  readInt (int  defaultValue)  {         int  n;         for  (; ; ) {             String  str  =  readKeyBoard(10 , true );             if  (str.equals("" )) {                 return  defaultValue;             } 			 			             try  {                 n = Integer.parseInt(str);                 break ;             } catch  (NumberFormatException e) {                 System.out.print("数字输入错误,请重新输入:" );             }         }         return  n;     }          public  static  String readString (int  limit)  {         return  readKeyBoard(limit, false );     }      	     public  static  String readString (int  limit, String defaultValue)  {         String  str  =  readKeyBoard(limit, true );         return  str.equals("" )? defaultValue : str;     } 	     public  static  char  readConfirmSelection ()  {         System.out.println("请输入你的选择(Y/N): 请小心选择" );         char  c;         for  (; ; ) {         	         	             String  str  =  readKeyBoard(1 , false ).toUpperCase();             c = str.charAt(0 );             if  (c == 'Y'  || c == 'N' ) {                 break ;             } else  {                 System.out.print("选择错误,请重新输入:" );             }         }         return  c;     }          private  static  String readKeyBoard (int  limit, boolean  blankReturn)  {          		 		String  line  =  "" ; 		         while  (scanner.hasNextLine()) {             line = scanner.nextLine();             			 			if  (line.length() == 0 ) {                 if  (blankReturn) return  line;                 else  continue ;              } 			 			             if  (line.length() < 1  || line.length() > limit) {                 System.out.print("输入长度(不能大于"  + limit + ")错误,请重新输入:" );                 continue ;             }             break ;         }         return  line;     } } 
 
具体功能类 House类 House实体类的属性有编号,房主, 电话,地址,月租,状态。House的对象表示一个房屋信息。其中构造器、Get和Set方法、toSring方法快捷键Alt+Insert生成即可,toSring略微改成自己想要的格式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 package  com.coderq.houserent.domain;public  class  House  {         private  int  id;     private  String name;     private  String phone;     private  String address;     private  int  rent;     private   String state;     public  House (int  id, String name, String phone, String address, int  rent, String state)  {         this .id = id;         this .name = name;         this .phone = phone;         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 getPhone ()  {         return  phone;     }     public  void  setPhone (String phone)  {         this .phone = phone;     }     public  String getAddress ()  {         return  address;     }     public  void  setAddress (String address)  {         this .address = address;     }     public  int  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"  + phone +                 "\t\t"  + address +                 "\t\t"  + rent +                 "\t\t"  + state ;     } } 
 
HouseView类 HouseView类用于显示界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 package  com.coderq.houserent.view;import  com.coderq.houserent.domain.House;import  com.coderq.houserent.service.HouseService;import  com.coderq.houserent.utils.Utility;public  class  HouseView  {    private  boolean  loop  =  true ;       private  char  key  =  ' ' ;       private  HouseService  houseService  =  new  HouseService (10 );           public  void  updateHouse ()  {         System.out.println("=================修改房屋==================" );         System.out.print("请输入待修改房屋id(-1退出查找)" );         int  updateId  =  Utility.readInt();         if  (updateId == -1 ) {             System.out.println("=================已退出修改房屋==================" );             return ;         }                                    House  house  =  houseService.findById(updateId);         if  (house == null ) {             System.out.println("==============修改的房屋信息不存在===============" );             return ;         }         System.out.println("姓名("  + house.getName() + "):" );         String  name  =  Utility.readString(8 , "" );         if  ( !"" .equals(name) ) {              house.setName(name);         }         System.out.println("电话("  + house.getPhone() + "):" );         String  phone  =  Utility.readString(12 ,"" );         if  ( !"" .equals(phone) ) {              house.setPhone(phone);         }         System.out.println("地址("  + house.getAddress() + "):" );         String  address  =  Utility.readString(16 ,"" );         if  ( !"" .equals(address) ) {              house.setAddress(address);         }         System.out.println("月租("  + house.getRent() + "):" );         int  rent  =  Utility.readInt(-1 );         if  ( rent != -1  ) {              house.setRent(rent);         }         System.out.println("状态("  + house.getState() + "):" );         String  state  =  Utility.readString(3 ,"" );         if  ( !"" .equals(state) ) {              house.setState(state);         }         System.out.println("=================修改房屋成功==================" );     }               public  void  findHouse ()  {         System.out.println("=================查找房屋==================" );         System.out.print("请输入待查找房屋id(-1退出查找)" );         int  findId  =  Utility.readInt();         if  (findId == -1 ) {             System.out.println("=================已退出删除房屋==================" );             return ;         }                  if  (houseService.findById(findId) != null ) {             House  house  =  houseService.findById(findId);             System.out.println(house);             System.out.println("=================查找房屋成功==================" );         } else  {             System.out.println("=================查找房屋失败==================" );         }     }          public  void  exit ()  {         char  c  =  Utility.readConfirmSelection();         if  (c == 'Y' ) {             loop = false ;         }     }               public  void  delHouse ()  {         System.out.println("=================删除房屋==================" );         System.out.print("请输入待删除房屋id(-1退出删除)" );         int  delId  =  Utility.readInt();         if  (delId == -1 ) {             System.out.println("=================已退出删除房屋==================" );             return ;         }                  char  choice  =  Utility.readConfirmSelection();         if  (choice == 'Y' ) {                          if  (houseService.del(delId)) {                 System.out.println("=================删除房屋成功==================" );             } else  {                 System.out.println("=================删除房屋失败==================" );             }         } else  {             System.out.println("=================退出删除房屋==================" );         }     }               public  void  addHouse ()  {         System.out.println("=================添加房屋==================" );         System.out.println("姓名:" );         String  name  =  Utility.readString(8 );         System.out.println("电话:" );         String  phone  =  Utility.readString(12 );         System.out.println("地址:" );         String  address  =  Utility.readString(16 );         System.out.println("月租:" );         int  rent  =  Utility.readInt();         System.out.println("状态:" );         String  state  =  Utility.readString(3 );                  House  newHouse  =  new  House (0 , name, phone, address, rent, state);         if  (houseService.add(newHouse)) {             System.out.println("=================添加房屋成功==================" );         } else  {             System.out.println("=================添加房屋失败==================" );         }     }          public  void  listHouses ()  {         System.out.println("=================房屋列表==================" );         System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)" );         House[] houses = houseService.list();          for  (int  i  =  0 ; i < houses.length; i++) {             if  (houses[i] == null ) {                 break ;             }             System.out.println(houses[i]);         }         System.out.println("=============房屋列表显示完毕==============\n" );     }          public  void  mainMenu ()  {         do  {             System.out.println("=============房屋租赁系统菜单==============" );             System.out.println("\t\t\t1 新 增 房 源" );             System.out.println("\t\t\t2 查 找 房 源" );             System.out.println("\t\t\t3 删 除 房 源 信 息" );             System.out.println("\t\t\t4 修 改 房 源 信 息" );             System.out.println("\t\t\t5 房 屋 列 表" );             System.out.println("\t\t\t6 退       出" );             System.out.println("请输入你的选择(1-6)" );             key = Utility.readChar();             switch  (key) {                 case  '1' :                                          addHouse();                     break ;                 case  '2' :                                          findHouse();                     break ;                 case  '3' :                                          delHouse();                     break ;                 case  '4' :                                          updateHouse();                     break ;                 case  '5' :                                          listHouses();                     break ;                 case  '6' :                                          exit();                     break ;             }         } while  (loop);     } } 
 
HouseService类 HouseService类用于给HouseView类传输相应的数据,如HouseView中有listHouse()方法,而HouseService中的list()方法用于给listHouse()方法传送房屋列表的信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 package  com.coderq.houserent.service;import  com.coderq.houserent.domain.House;public  class  HouseService  {    private  House[] houses;      private  int  houseNums  =  1 ;      private  int  idCounter  =  1 ;           public  HouseService (int  size)  {                  houses = new  House [size];                   houses[0 ] = new  House (1 ,"coderq" ,"188" ,"安徽" ,500 ,"未出租" );     }               public  House findById (int  findId) {                  for  (int  i  =  0 ; i < houseNums; i++) {             if  (findId == houses[i].getId() ){                  return  houses[i];             }         }         return  null ;     }          public  boolean  del (int  delId) {                  int  index  =  -1 ;         for  (int  i  =  0 ; i < houseNums; i++) {             if  (delId == houses[i].getId() ){                  index = i;             }         }         if  (index == -1 ){                          return  false ;         }                  for  (int  i  =  index; i < houseNums - 1  ; i++) {             houses[i] = houses[i+1 ];         }         houses[--houseNums] = null ;          return  true ;     }          public  boolean  add (House newHouse) {                  if (houseNums == houses.length){             System.out.println("数组已满,无法添加" );             return  false ;         }                  houses[houseNums++] = newHouse;                  newHouse.setId(++idCounter);         return  true ;     }          public  House[] list(){         return  houses;     } } 
 
HouseRentApp类 主程序启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package  com.coderq.houserent;import  com.coderq.houserent.view.HouseView;public  class  HouseRentApp  {    public  static  void  main (String[] args)  {                  new  HouseView ().mainMenu();         System.out.println("=============你退出了系统==============" );     } } 
 
结束语 以上就是房屋出租系统的全部内容, 本项目也算是Java基础的一个小案例, 增删两个功能看着老韩的视频逐步写下来的,查改是自己尝试了去实现一下,然后发现自己实现确实思路上还有点乱,磕磕绊绊也算是最终实现了。本项目也算简单有了一点增删改查的小锻炼,希望接下来的岁月里继续脚踏实地,努力努力再努力。