如何用Java实现房屋出租系统
更新:HHH   时间:2023-1-7


本篇内容介绍了“如何用Java实现房屋出租系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

目录
  • houseView

  • house_Sevice

  • House

  • App

  • 总结

houseView

package house.view;
import house.model.House;
import house.sevice.house_Sevice;
import house.tool.Utility;
import java.util.Scanner;
/**
 * 1 显示界面
 * 2 接收用户的输入
 * 3 调用houseService完成对房屋信息的各种输人
 */
public class houseView {
    private boolean loop=true;
    private char key=' ';
    //编写addhouse(),接收输入,创建addhouse对象,调用add方法
    public void addhouse(){
        System.out.println("---------添加房屋-------");
        System.out.print("姓名:");
        String name=Utility.readString(6);
        System.out.print("电话:");
        int phone=Utility.readInt(12);
        System.out.print("地址:");
        String address=Utility.readString(14);
        System.out.print("月租:");
        int rent=Utility.readInt();
        System.out.print("状态:");
        String state=Utility.readString(3);
        //注意id是系统分配的
        House newhouse=new House(0,name,phone,address,rent,state);
        if(house_sevice.add(newhouse)){
            System.out.println("添加房屋成功");
        }else {
            System.out.println("添加房屋失败");
        }
    }
    //查找房屋
    public void find(){
        System.out.println("---------查找房屋-------");
        System.out.print("请输入要查找的房屋编号:");
        int f=Utility.readInt();
        if(house_sevice.Find(f)==null){
            System.out.println("没有找到该房屋的信息");
        }else {
            System.out.println("编号  房主  \t电话  \t地址  \t月租  \t状态(出租/未出租)\n"+house_sevice.Find(f));
        }
    }
    //编写delhosue方法,接收输入的id,调用service的del方法
    public void delhouse(){
        System.out.println("---------删除房屋-------");
        System.out.print("请输入要删除的房屋编号(-1退出):");
        int delid=Utility.readInt();
        if(delid==-1){
            System.out.println("---------退出删除-------");
            return;
        }
        char c=Utility.readConfirmSelection();//调用该方法,一直循环直到用户输入y或n
        if(c=='Y'){
            if(house_sevice.del(delid)){
                System.out.println("---------删除成功-------");
            }else {
                System.out.println("---------删除失败-------");
            }
        }else {
            System.out.println("---------退出删除-------");
            return;
        }
    }
    //修改房屋信息
    public void update(){
        System.out.println("---------修改房屋信息-------");
        System.out.print("请输入要修改的房屋编号(-1退出):");
        int up=Utility.readInt();
        if(up==-1){
            System.out.println("---------退出修改-------");
            return;
        }
        house_sevice.updata(up);
        System.out.println("---------修改成功-------");
    }
    //显示房屋列表
    private house_Sevice house_sevice=new house_Sevice(10);//设置数组大小为10
    public  void listHouse(){
        System.out.println("---------房屋出租列表-------");
        System.out.println("编号  房主  \t电话  \t地址  \t月租  \t状态(出租/未出租)");
        House[] houses=house_sevice.list();//得到所有房屋的信息
        for (int i = 0; i < houses.length; i++) {
            if(houses[i]==null){
                break;
            }
            System.out.println(houses[i]);
        }
    }
    //主菜单
    public void main_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("请输入你的选择:");
            key= Utility.readChar();
            switch (key){
                case '1':
                    addhouse();
                    break;
                case '2':
                    find();
                    break;
                case '3':
                    delhouse();
                    break;
                case '4':
                    update();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    char n=Utility.readConfirmSelection();
                    if(n=='Y'){
                        loop=false;
                    }
                    break;
                default:
                    System.out.println("输入错误!");
            }
        }while (loop);
    }
}

house_Sevice

package house.sevice;
import house.model.House;
import house.tool.Utility;
/**
 * house_Sevice.java<=> 类 [业务层]
 * //定义house[],保存house对象
 * 1 响应houseView的调用
 * 2 完成对房屋信息的各种操作(增删改查)
 */
public class house_Sevice {
    private House[] houses;//保存house对象
    private int nums=3;//记录数组内的房屋个数
    private int count=3;//id
    //初始化房屋列表
    public house_Sevice(int size){//传入数组大小
        houses=new House[size];
        houses[0]=new House(1,"Morty",1020,"纽约",111,"未出租");
        houses[1]=new House(2,"莱月昴",1021,"东京",222,"未出租");
        houses[2]=new House(3,"李星云",1022,"洛阳",333,"未出租");
    }
    public House[] list(){
        return houses;//因为重写了tostring
    }
    //添加房屋信息
    public boolean add(House newhouse){
        if(nums==houses.length){
            return false;
        }else {
            houses[nums++]=newhouse;
            newhouse.setId(++count);//id自增长机制,更新newhouse的id
            return true;
        }
    }
    //删除房屋
    public boolean del(int Id){
        //找到要删除房屋信息元素对应的下标
        int index=-1;
        for (int i = 0; i < nums; i++) {
            if(Id==houses[i].getId()){
                index=i;
            }
        }
        if(index==-1){
            return false;
        }
        for (int i = index; i < houses.length-1; i++) {
            houses[i]=houses[i+1];//将该位置之后的元素前移覆盖
        }
        houses[--nums]=null;//将数组长度减一并将最后一个元素置空
        return true;
    }
    //查找房屋
    public House Find(int id){
        //找到要查找房屋信息元素对应的下标
        for (int i = 0; i < nums; i++) {
            if(id==houses[i].getId()){
                return houses[i];
            }
        }
        return null;
    }
    //修改房屋信息
    public void updata(int up){
        House house=Find(up);
        if(house==null){
            System.out.println("该房屋不存在");
        }else {
            System.out.print("姓名:("+house.getName()+"):");
            String name= Utility.readString(8,"");//用户如果直接回车代表不修改,默认值为""
            if(!name.equals("")){
                house.setName(name);//将用户输入的name覆盖原来的name
            }
            System.out.print("手机号:("+house.getPhone()+"):");
            int phone=Utility.readInt(0);//用户如果直接回车代表不修改,默认值为0
            if(!(phone==0)){
                house.setPhone(phone);//将用户输入的name覆盖原来的name
            }
            System.out.print("地址:("+house.getAddress()+"):");
            String address=Utility.readString(8,"");
            if(!address.equals("")){
                house.setAddress(address);
            }
            System.out.print("月租:("+house.getRent()+"):");
            int rent=Utility.readInt(0);
            if(!(rent==0)){
                house.setRent(rent);
            }
            System.out.print("状态:("+house.getState()+"):");
            String state=Utility.readString(8,"");
            if(!state.equals("")){
                house.setState(state);//
            }
        }
    }
}

House

package house.model;
/**
 * house类的对象表示一个房屋的信息
 */
public class House {
    //编号  房主  电话  地址  月租  状态(出租/未出租)
    private int id;
    private String name;
    private int phone;
    private String address;
    private int rent;
    private String state;
    public House(int id, String name, int 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 int getPhone() {
        return phone;
    }
    public void setPhone(int 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" +name
                +"\t" +phone
                +"\t" +address
                +"\t\t"+rent
                +"\t\t"+state;
    }
}

App

package house.view;
import house.model.House;
public class App {
    public static void main(String[] args) {
        //创建View对象,并显示主菜单,是整个程序的入口
        new houseView().main_menu();//创建一个虚拟对象调用显示主菜单的方法
        System.out.println("程序已退出");
    }
}

“如何用Java实现房屋出租系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!

返回开发技术教程...