VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • 房屋租赁系统(文字版)

整体结构

 

工具类——Utility

复制代码
package oop.houserentexercise;

/**
 工具类的作用:
 处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。

 */

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'&& c != '6') {
                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=>N
            String 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;
    }
}
复制代码

 

domain包实体类——house

将house的各项属性封装到类中

复制代码
package oop.houserentexercise.domain;

public class House {

    private int id;//编号
    private String name;//房主姓名
    private String phone;//电话
    private String address;//地址
    private int rentByMonth;//月租
    private String state;//状态

    public House(int id, String name, String phone, String address, int rentByMonth, String state) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.rentByMonth = rentByMonth;
        this.state = state;
    }


    @Override
    public String toString() {
        return (getId() + "\t\t"
                + getName() + "\t"
                + getPhone() + "\t\t"
                + getAddress() + "\t"
                + getRentByMonth() + "\t\t"
                + getState());
    }



    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 getRentByMonth() {
        return rentByMonth;
    }

    public void setRentByMonth(int rentByMonth) {
        this.rentByMonth = rentByMonth;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}
复制代码

 

service类——HouseService

实现各项功能

复制代码
package oop.houserentexercise.service;

import oop.houserentexercise.domain.House;

public class HouseService {


    private int houseNums = 1; //记录当前有多少个房屋信息
//    private int idCounter = 1; //记录当前的id增长到哪个值

    public HouseService(int houseNums, int idCounter) {
        this.houseNums = houseNums;
//        this.idCounter = idCounter;
    }


    public void list(House[] houses) {
        for (int i = 0; i < houses.length; i++) {//大家想想,这里老韩有什么?雷,坑
            if (houses[i] == null) {//如果为null,就不用再显示了
                break;
            }
            System.out.println(houses[i]);
        }

    }

    public House[] add(House[] houses, House house) {

        if (houseNums == houses.length) {

            House[] num = new House[houses.length + 1];
            for (int i = 0; i < houses.length; i++) {
                num[i] = houses[i];
            }
            num[houses.length] = house;
            houses = num;
            System.out.println(houses[houseNums] + "添加成功!");
            houseNums++;
        } else {
            houses[houseNums] = house;
            houseNums++;
        }
        return houses;

    }

    public House[] del(House house[], int id) {
        House[] delhouse = new House[house.length-1];
        for (int i = id; i < house.length; i++) {
            if (house[i].getId() == id) {
                house[i] = null;
                break;
            }
        }
        for (int i = 0; i < id; i++) {
            delhouse[i]=house[i];
            }
        for (int i = id; i < house.length; i++) {
            delhouse[i-1]=house[i];
        }
            house=delhouse;
        return house;
        }




    public House findById(House[] houses, int id) {
        int index = -1;
        for (int i = 0; i < houses.length; i++) {
            if (id== (houses[i].getId())) {//能用equals就用
                index = i;
                break;
            }
        }
        return houses[index];
    }

    public int findById_int(House[] houses, int updateNum) {
        int index=0;
        for (int i = 0; i < houses.length; i++) {
            if (updateNum== (houses[i].getId())) {//能用equals就用
                index = i;
                break;
            }
        }
        return index;

    }
}
复制代码

 

界面类

调用service类的函数并在界面上实现

复制代码
package oop.houserentexercise.view;

import oop.houserentexercise.Utility;
import oop.houserentexercise.domain.House;
import oop.houserentexercise.service.HouseService;

public class HouseView {

    /*              项目界面
    12         1、主菜单---mainMenu
    13         2、新增房源---addHouse
    14         3、查找房源---findHouse
    15         4、删除房源---delHouse
    16         5、修改房源---updateHouse
    17         6、列出房源---listHouse
    18         7、退出程序---exit
    19         */
    boolean loop = true;
    String menuHead = "编号\t\t" + "房主姓名\t\t" + "联系电话\t\t" + "地址\t\t" + "月租费\t\t" + "状态(已出租/未出租)\t";
    House[] houses = new House[3];

    HouseService houseService = new HouseService(1, 1);

    public void mainMenu() {
        houses[0] = new House(1, "jack", "13703259980", "四社区C-417", 1500, "未出租");


        do {
            System.out.println("-------------------房屋出租系统--------------------");
            System.out.println("                   1、新增房源");
            System.out.println("                   2、查找房源");
            System.out.println("                   3、删除房源");
            System.out.println("                   4、修改房源");
            System.out.println("                   5、列出房源");
            System.out.println("                   6、退出程序");
            System.out.println("------------------------------------------------");
            System.out.print("请输入你的选择:");
            switch (Utility.readMenuSelection()) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    exit();
                    break;
            }


        } while (loop);
        System.out.println("您已成功退出系统!");


    }

    public void exit() {
        char choice = ' ';
        while (true) {
            System.out.println("是否确认退出?(y/n)");
            choice = Utility.readChar();
            if (choice == 'n' || choice == 'y')
                break;
        }
        if (choice == 'y') {
            loop = false;
            return;
        }
        return;
    }

    public void listHouse() {

        System.out.println("=============房屋列表============");
        System.out.println(menuHead);
        houseService.list(houses);//得到所有房屋信息

        System.out.println("=============房屋列表显示完毕============");

    }

    public void addHouse() {
        System.out.println("===================添加房屋====================");
        System.out.print("编号:");
        int id = Utility.readInt();
        System.out.print("房主姓名:");
        String name = Utility.readString(4);
        System.out.print("电话:");
        String phone = Utility.readString(11);
        System.out.print("地址:");
        String address = Utility.readString(20);
        System.out.print("月租:");
        int rentByMonth = Utility.readInt();
        System.out.print("状态:");
        String state = Utility.readString(3, "未表明");
        House houseNew = new House(id, name, phone, address, rentByMonth, state);
        houses = houseService.add(houses, houseNew);//记得要把添加后的新数组重新赋值回去

    }

    public void delHouse() {


        System.out.print("请输入你要删除的房源的id号:");
        int id = Utility.readInt(10);
        houses = houseService.del(houses, id);


    }

    public void findHouse() {
        System.out.println("请输入你要查找的房源的id号");
        House houseFound = houseService.findById(houses, Utility.readInt());
        System.out.println("房源信息如下:\n" + menuHead + "\n" + houseFound);
    }

    public void updateHouse() {

        System.out.println("===================修改房屋====================");
        System.out.print("请输入你要修改的id编号");
        int updateNum = Utility.readInt(10);
        int updateIndex = houseService.findById_int(houses, updateNum);
        System.out.println("房源信息如下:+\n" + menuHead + "\n" + houses[updateIndex]);

        System.out.println("编号(" + houses[updateIndex].getId() + "):");
        int id = Utility.readInt();
        System.out.print("房主姓名(" + houses[updateIndex].getName() + "):");
        String name = Utility.readString(4);
        System.out.print("电话(" + houses[updateIndex].getPhone() + "):");
        String phone = Utility.readString(11);
        System.out.print("地址(" + houses[updateIndex].getAddress() + "):");
        String address = Utility.readString(20);
        System.out.print("月租(" + houses[updateIndex].getRentByMonth() + "):");
        int rentByMonth = Utility.readInt();
        System.out.print("状态(" + houses[updateIndex].getState() + "):");
        String state = Utility.readString(3, "未表明");
        houses[updateIndex] = new House(id, name, phone, address, rentByMonth, state);

    }


}
复制代码

 

运行类

程序的运行入口

复制代码
package oop.houserentexercise;

import oop.houserentexercise.view.HouseView;

public class HouseRentApp {
    public static void main(String[] args) {
        HouseView houseView = new HouseView();
        houseView.mainMenu();

    }
}
复制代码



原文:https://www.cnblogs.com/recorderM/p/15733935.html


相关教程