Java實現房屋出租系統詳解

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("程序已退出");
    }
}
 

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!

推薦閱讀: