零基础学JAVA--Day21(房屋出租系统+韩顺平Utility类原码)
前言
一个简单的房屋出租系统,实现的功能主要有以下几个方面。

HouseRentApp[程序入口]
功能
1、创建HouseView对象
2、调用该对象显示主菜单
代码
package com.Houserent;import com.Houserent.View.HouseView;public class HouseRentApp {public static void main(String[] args) {new HouseView().mainMenu();//进入主菜单System.out.println("退出成功");}
}
HouseView类[界面层]
功能
1、显示页面
2、接收用户的输入
3、调用HouseService完成对房屋信息的各种操作(增删改查的调用)
代码
package com.Houserent.View;import com.Houserent.domain.House;
import com.Houserent.service.HouseService;
import com.Houserent.utils.Utility;import java.util.Scanner;public class HouseView {private boolean isloop = true;//控制显示菜单private char key =' ';//接收用户输入'private HouseService houseService = new HouseService(10);//public void mainMenu(){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.println("=====================================");System.out.println("请选择功能(1-6)");key = Utility.readChar();switch (key){case '1':addHouse();break;case '2':findHouse();break;case '3':deleteHouse();break;case '4':updateHouse();break;case '5':Houselist();//调用HouseView的Houselist方法break;case '6':System.out.println("退出");char c = Utility.readConfirmSelection();if(c == 'Y'){isloop = false;}else {isloop = true;}break;}}while(isloop);}public void Houselist(){System.out.println("============房屋列表============");System.out.println("编号\t名称\t电话\t地址\t租金\t状态\t");//调用HouseService的list方法需要先有一个HouseService的对象House[] houses = houseService.houselist();for (int i = 0; i < houses.length; i++) {//遍历数组,打印houses数组中的信息if (houses[i] != null){//如果不为空,则打印System.out.println(houses[i]);}}System.out.println("============显示完毕============");}public void addHouse(){System.out.println("============添加房源============");System.out.println("请输入名称: ");String name = Utility.readString(5);//读取用户输入的字符串,长度<=10System.out.println("请输入电话: ");String phone = Utility.readString(12);//读取用户输入的字符串,长度<=12)System.out.println("输入房源地址: ");String address = Utility.readString(20);System.out.println("请输入月租金:");int rent = Utility.readInt();System.out.println("请输入状态(已租出/未租出)");String state = Utility.readString(3);//创建一个新的House对象,id是系统分配的,用户不能直接输入(自增长的方式)House newhouse = new House(0, name, phone, address, rent, state);if(houseService.addHouse(newhouse)){//HouseService的addHouse方法回true,则添加成功System.out.println("添加房屋成功");}}//删除房屋信息public void deleteHouse(){System.out.println("============删除房源============");System.out.println("请输入要删除的房屋编号:(-1退出)");int delId = Utility.readInt();if (delId == -1){return;}char isdel = Utility.readConfirmSelection();//该方法是输入Y/N,否则死循环if(isdel == 'Y'){if(houseService.del(delId)){System.out.println("删除成功");}else{System.out.println("删除失败");}}else {System.out.println("用户取消删除");}}//查找房屋信息public void findHouse(){System.out.println("请输入查找的房屋编号");int findId = Utility.readInt();if(findId < 0){System.out.println("输入错误,请重新输入");findHouse();}else if(houseService.find(findId)){System.out.println("===============查找成功==============");}else{System.out.println("===============查找失败==============");}}//修改房屋信息public void updateHouse(){System.out.println("请输入要修改的房屋编号");int updateId = Utility.readInt();if(updateId < 0){System.out.println("输入错误,请重新输入");updateHouse();}else if(houseService.update(updateId)){System.out.println("===============修改成功==============");}else{System.out.println("===============修改失败==============");}}
}
HouseService[业务层]
功能
1、相应HouseView的调用
2、完成对房屋信息的crud(增删改查)
代码
package com.Houserent.service;import com.Houserent.domain.House;
import com.Houserent.utils.Utility;public class HouseService {private House[] houses;//定义一个数组存放HOUSE对象private int housenum = 0;//定义一个变量,记录数组中HOUSE对象的个数private int idCount = 0;//定义一个变量,记录生成的idpublic HouseService(int size){//定义一个house数组对象houses = new House[size];}public House[] houselist(){return houses;}//添加房源public boolean addHouse(House house){if (housenum == houses.length){System.out.println("数组已满,不能添加");return false;}else{houses[housenum++] = house;//把传入的house对象,传入数组house.setId(++idCount);return true;}}//del方法,删除用户房屋信息public boolean del(int delId){//先找到要删除的房屋信息对应的下标int index = -1;//用于表示需要删除的数组下标for (int i = 0; i < housenum; i++) {if(delId == houses[i].getId()){index = i;//}}if(index == -1){System.out.println("未找到房屋编号");return false;}//找到则删除,直接把该元素后面的元素依次往前移动,达到覆盖的效果,最后一位置空for (int i = index; i < housenum - 1; i++) {houses[index] = houses[index+1];//把后一位的信息转移到前一位}houses[--housenum] = null;//把房屋信息的最后一个先设为nullreturn true;}//find方法,根据id查找房屋信息public boolean find(int findId){for (int i = 0; i < housenum; i++) {//遍历查找if (findId == houses[i].getId()){System.out.println("=============查找结果=============");System.out.println(houses[i]);return true;}}return false;}public boolean update(int updateId){for (int i = 0; i < housenum; i++) {if(updateId == houses[i].getId()){//重新设置一下就行System.out.println("请输入新的房主姓名");houses[i].setName(Utility.readString(5));System.out.println("请输入新的房主电话");houses[i].setPhone(Utility.readString(11));System.out.println("请输入新的房主地址");houses[i].setAddress(Utility.readString(20));System.out.println("请输入新的月租");houses[i].setRent(Utility.readInt());System.out.println("请输入新的状态(未出租/已出租)");houses[i].setState(Utility.readString(3));System.out.println(houses[i]);return true;}}return false;}
}
House[model层/domain层]
功能
创建House对象,代表一个房屋信息
代码
package com.Houserent.domain;public class House {//定义一个类,包含编号、房主名字、电话、地址、月租和状态private int id;private String name;private String phone;private String address;private int rent;private String 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;}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;}@Overridepublic String toString() {return id +"\t" + name +"\t"+ phone +"\t" + address +"\t" + rent +"\t" + state +"\t";}
}
韩顺平老师Utility类工具类原码
package com.Houserent.utils;
/**工具类的作用:处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。*/import java.util.*;
/***/
public class Utility {//静态属性。。。private static Scanner scanner = new Scanner(System.in);/*** 功能:读取键盘输入的一个菜单选项,值:1——5的范围* @return 1——5*/public static char readMenuSelection() {char c;for (; ; ) {String str = readKeyBoard(1, false);//包含一个字符的字符串c = str.charAt(0);//将字符串转换成字符char类型if (c != '1' && c != '2' &&c != '3' && c != '4' && c != '5') {System.out.print("选择错误,请重新输入:");} else break;}return c;}/*** 功能:读取键盘输入的一个字符* @return 一个字符*/public static char readChar() {String str = readKeyBoard(1, false);//就是一个字符return str.charAt(0);}/*** 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符* @param defaultValue 指定的默认值* @return 默认值或输入的字符*/public static char readChar(char defaultValue) {String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符return (str.length() == 0) ? defaultValue : str.charAt(0);}/*** 功能:读取键盘输入的整型,长度小于2位* @return 整数*/public static int readInt() {int n;for (; ; ) {String str = readKeyBoard(10, false);//一个整数,长度<=10位try {n = Integer.parseInt(str);//将字符串转换成整数break;} catch (NumberFormatException e) {System.out.print("数字输入错误,请重新输入:");}}return n;}/*** 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数* @param defaultValue 指定的默认值* @return 整数或默认值*/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;}/*** 功能:读取键盘输入的指定长度的字符串* @param limit 限制的长度* @return 指定长度的字符串*/public static String readString(int limit) {return readKeyBoard(limit, false);}/*** 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串* @param limit 限制的长度* @param defaultValue 指定的默认值* @return 指定长度的字符串*/public static String readString(int limit, String defaultValue) {String str = readKeyBoard(limit, true);return str.equals("")? defaultValue : str;}/*** 功能:读取键盘输入的确认选项,Y或N* 将小的功能,封装到一个方法中.* @return Y或N*/public static char readConfirmSelection() {System.out.println("请输入你的选择(Y/N): 请小心选择");char c;for (; ; ) {//无限循环//在这里,将接受到字符,转成了大写字母//y => Y n=>NString str = readKeyBoard(1, false).toUpperCase();c = str.charAt(0);if (c == 'Y' || c == 'N') {break;} else {System.out.print("选择错误,请重新输入:");}}return c;}/*** 功能: 读取一个字符串* @param limit 读取的长度* @param blankReturn 如果为true ,表示 可以读空字符串。* 如果为false表示 不能读空字符串。** 如果输入为空,或者输入大于limit的长度,就会提示重新输入。* @return*/private static String readKeyBoard(int limit, boolean blankReturn) {//定义了字符串String line = "";//scanner.hasNextLine() 判断有没有下一行while (scanner.hasNextLine()) {line = scanner.nextLine();//读取这一行//如果line.length=0, 即用户没有输入任何内容,直接回车if (line.length() == 0) {if (blankReturn) return line;//如果blankReturn=true,可以返回空串else continue; //如果blankReturn=false,不接受空串,必须输入内容}//如果用户输入的内容大于了 limit,就提示重写输入//如果用户如的内容 >0 <= limit ,我就接受if (line.length() < 1 || line.length() > limit) {System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");continue;}break;}return line;}
}——————————————————————
Day21 End
今天学着写了一个很简单的面向对象编程的程序系统,主要还是想熟练一下基本语法和写这类系统的结构,结构搭建好了,写下来还是比较顺利的。
离回家还有69天~
