房屋出租系统(链表实现)

因为做期末课设必须要用到数据结构

就把原来数组实现的房屋出租系统改成了用链表实现,删减了原先的一部分内容

原文章:房屋出租系统(java)_m0_55997161的博客-CSDN博客_java 房屋出租系统

项目结构

houseView
package house.view;

import house.sevice.house_Sevice;
import house.tool.Utility;
import java.util.Scanner;

/**
 * 1 显示界面
 * 2 接收用户的输入
 * 3 调用houseService完成对房屋信息的各种输人
 */
@SuppressWarnings({"all"})
public class houseView {
    private boolean loop=true;
    private char key=' ';
    Scanner sc=new Scanner(System.in);

    //编写addhouse(),接收输入,创建addhouse对象,调用add方法
    public void addhouse(){
        System.out.println("---------添加房屋-------");
        System.out.print("姓名:");
        String name=sc.next();
        System.out.print("电话:");
        int phone=sc.nextInt();
        System.out.print("地址:");
        String address=sc.next();
        System.out.print("月租:");
        int rent=sc.nextInt();
        System.out.print("状态:");
        String state=sc.next();
        house_Sevice.add(name,phone,address,rent,state);
    }
    /**
     * 查找房屋
     */
    public void find(){
        System.out.println("---------查找房屋-------");
        System.out.print("请输入要查找的房屋手机号:");
        int f=sc.nextInt();
        house_Sevice.search(f);
    }

    /**
     * 删除房屋
     */
    public void delhouse(){
        System.out.println("---------删除房屋-------");
        System.out.print("请输入要删除的房屋手机号(-1退出):");
        int delid=sc.nextInt();
        if(delid==-1){
            System.out.println("---------退出删除-------");
            return;
        }
        house_Sevice.del(delid);
    }
    /**
     * 修改房屋信息
     */
    public void update(){
        System.out.println("---------修改房屋信息-------");
        System.out.print("请输入要修改的房屋手机号(-1退出):");
        int up=sc.nextInt();
        if(up==-1){
            System.out.println("---------退出修改-------");
            return;
        }
        System.out.println("---------请输入修改后的房屋信息-------");
        System.out.print("姓名:");
        String name=sc.next();
        System.out.print("电话:");
        int phone=sc.nextInt();
        System.out.print("地址:");
        String address=sc.next();
        System.out.print("月租:");
        int rent=sc.nextInt();
        System.out.print("状态:");
        String state=sc.next();
        house_Sevice.update(name,phone,address,rent,state,up);
    }
    /**
     * 显示房屋列表
     */
    public  void listHouse(){
        System.out.println("---------房屋出租列表-------");
        house_Sevice.show();
    }

    /**
     * 主菜单
     */
    public void main(){
        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("请输入你的选择:");
            Scanner sc=new Scanner(System.in);
            key= sc.next().charAt(0);
            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;

/**
 * house_Sevice.java<=> 类 [业务层]
 * 1 完成对房屋信息的各种操作(增删改查)
 */
@SuppressWarnings({"all"})
public class house_Sevice {
    //静态代码块只执行一次用于初始化列表
    static{
        Manage.add(new Node("morty",111,"纽约",3000,"未出租"));
    }
    public static void add(String name, int phone, String address, int rent, String state){
        Manage.add(new Node(name,phone,address,rent,state));
        System.out.println("---------添加成功-------");
    }
    public static void update(String name, int phone, String address, int rent, String state,int up){
        Manage.updata(new Node(name,phone,address,rent,state),up);
    }
    public static void del(int phone){
        Manage.del(phone);
    }
    public static void show(){
        Manage.show();
    }
    public static void search(int phone){
        Manage.search(phone);
    }
}
@SuppressWarnings({"all"})
class Manage{
    //初始化头结点
    private static Node head=new Node("",0,"",0,"");

    /**
     * 添加房屋
     * @param node
     */
    public static void add(Node node){
        Node temp=head;
        while(true){
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        temp.next=node;
    }

    /**
     * 修改房屋信息
     * @param node
     */
    public static void updata(Node node,int phone){
        Node temp=head;
        boolean flag=false;
        while(true){
            if (temp.phone==phone){
                flag=true;
                break;
            }
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        if (flag){
            temp.name= node.name;
            temp.phone= node.phone;
            temp.address= node.address;
            temp.rent=node.rent;
            temp.state= node.state;
            System.out.println("---------修改成功-------");
        }else{
            System.out.println("未找到"+node.phone);
        }
    }
    /**
     * 删除房屋,根据房屋查找
     * @param phone
     */
    public static void del(int phone){
        Node temp=head;;
        boolean flag=false;
        while (true){
            if (temp.next==null){
                break;
            }
            if (temp.next.phone==phone){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if (flag){
            temp.next=temp.next.next;
            System.out.println("---------删除成功-------");
        }else{
            System.out.println("未找到"+phone);
        }
    }

    /**
     * 查找房屋,根据手机号
     * @param phone
     */
    public static void search(int phone){
        Node temp=head;;
        boolean flag=false;
        while (true){
            if (temp.phone==phone){
                flag=true;
                break;
            }
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        if (flag){
            System.out.println(temp);
        }else{
            System.out.println("未找到"+phone);
        }
    }
    /**
     * 显示房屋信息
     */
    public static void show(){
        //判断是否为空
        if (head.next==null){
            System.out.println("房屋列表为空");
            return;
        }
        Node temp=head.next;
        while(true){
            if (temp==null){
                break;
            }
            System.out.println(temp);
            temp=temp.next;
        }
    }
}
class Node{
    public String name;
    public int phone;
    public String address;
    public int rent;
    public String state;
    public Node next;

    public Node(String name, int phone, String address, int rent, String state) {
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.rent = rent;
        this.state = state;
    }

    @Override
    public String toString() {
        return "房主='" + name + '\'' +
                ", 电话=" + phone +
                ", 地址='" + address + '\'' +
                ", 月租='" + rent + '\'' +
                ", 状态='" + state + '\'' ;
    }
}
Utility
package house.tool;
 
import java.util.*;
@SuppressWarnings({"all"})
/**
 工具类:处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
 */
public class Utility {
    //静态属性。。。
    private static Scanner scanner = new Scanner(System.in);
 
    /**
     * 功能:读取键盘输入的确认选项,Y或N
     * 将小的功能,封装到一个方法中.
     * @return Y或N
     */
    public static char readConfirmSelection() {
        System.out.print("是否确定退出(Y/N)");
        char c;
        for (; ; ) {//无限循环
            //在这里,将接受到字符,转成了大写字母
            //y => Y n=>N
        	//从键盘上读取参数长度为1的内容 将小写字母转成了大写字母
        	String str = readKeyBoard(1).toUpperCase();
            c = str.charAt(0);	//返回str中的第一个字母
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");
            }
        }
        return c;
    }
 
    /**
     * 功能: 读取一个字符串
     * @param limit 读取的长度
     * @param blankReturn 如果为true ,表示 可以读空字符串。
     * @return
     */
    private static String readKeyBoard(int limit) {
 
        //定义了字符串
        String line = "";
 
        //scanner.hasNextLine() 判断有没有下一行
        while (scanner.hasNextLine()) {	//读取键盘上输入的全部内容
            line = scanner.nextLine();//读取这一行
 
            //如果line.length=0, 即用户没有输入任何内容,直接回车
            if (line.length() == 0) {
                return line;//直接可以返回空串
            }
            break;
        }
        return line;
    }
}
App
package house.view;

public class App {
    public static void main(String[] args) {

        //创建View对象,并显示主菜单,是整个程序的入口
        new houseView().main();//创建一个虚拟对象调用显示主菜单的方法
        System.out.println("程序已退出");
    }
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值