m0_74904829 2023-04-12 10:15 采纳率: 50%
浏览 169
已结题

java 学生宿舍管理系统设计

学生宿舍管理系统设计 
功能:实现简单的学生宿舍基本信息管理,宿舍的基本信息包括楼号、房间号、面积、所容纳人数、已入住人数等 
基本要求: 
1设计简单的菜单,能够进行系统功能选择。 
2实现信息的录入功能。 
3在已有信息的基础上添加新的记录。 
4删除指定房间号的记录。 
5修改指定房间号的记录 
6实现信息的浏览功能 
7按房间号查询功能 
8按已入住人数排序功能

要运用数据库处理,用MySQL5.7.3

  • 写回答

6条回答 默认 最新

  • Leodong. 2023-04-12 10:32
    关注

    该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
    下面是一个简单的学生宿舍管理系统设计的Java代码,实现了上述功能:
    在下面的代码中,我们定义了一个Dormitory类来表示一间宿舍,有楼号、房间号、面积、所容纳人数和已入住人数等属性。在DormitoryManagement类中,我们定义了一些方法来实现对宿舍信息的录入、添加、删除、修改、浏览、查询和排序等操作。

    在main方法中,我们通过Scanner类来实现用户输入,并通过switch语句来根据用户的选择执行不同的操作。例如,当用户输入1时,程序会调用DormitoryManagement类的input方法来录入宿舍信息;当用户输入5时,程序会调用DormitoryManagement类的showAll方法来浏览所有宿舍信息。

    此外,在DormitoryManagement类中,我们还定义了一些辅助方法来实现具体的操作,例如findDormitory方法用于根据房间号查找宿舍信息,sortByOccupied方法用于按已入住人数排序等。这些方法的实现细节可以参考代码中的注释。

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
    
    class Dormitory {
        private int buildingNumber;
        private int roomNumber;
        private double area;
        private int capacity;
        private int occupied;
    
        public Dormitory(int buildingNumber, int roomNumber, double area, int capacity, int occupied) {
            this.buildingNumber = buildingNumber;
            this.roomNumber = roomNumber;
            this.area = area;
            this.capacity = capacity;
            this.occupied = occupied;
        }
    
        public int getBuildingNumber() {
            return buildingNumber;
        }
    
        public int getRoomNumber() {
            return roomNumber;
        }
    
        public double getArea() {
            return area;
        }
    
        public int getCapacity() {
            return capacity;
        }
    
        public int getOccupied() {
            return occupied;
        }
    }
    
    public class DormitoryManagement {
        private ArrayList<Dormitory> dormitories;
    
        public DormitoryManagement() {
            dormitories = new ArrayList<>();
        }
    
        public void addDormitory() {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入楼号:");
            int buildingNumber = scanner.nextInt();
            System.out.print("请输入房间号:");
            int roomNumber = scanner.nextInt();
            System.out.print("请输入面积:");
            double area = scanner.nextDouble();
            System.out.print("请输入所容纳人数:");
            int capacity = scanner.nextInt();
            System.out.print("请输入已入住人数:");
            int occupied = scanner.nextInt();
            Dormitory dormitory = new Dormitory(buildingNumber, roomNumber, area, capacity, occupied);
            dormitories.add(dormitory);
            System.out.println("添加成功!");
        }
    
        public void addNewRecord() {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入要添加记录的房间号:");
            int roomNumber = scanner.nextInt();
            Dormitory dormitory = findDormitory(roomNumber);
            if (dormitory != null) {
                System.out.println("该房间已存在,无法添加!");
                return;
            }
            System.out.print("请输入楼号:");
            int buildingNumber = scanner.nextInt();
            System.out.print("请输入面积:");
            double area = scanner.nextDouble();
            System.out.print("请输入所容纳人数:");
            int capacity = scanner.nextInt();
            System.out.print("请输入已入住人数:");
            int occupied = scanner.nextInt();
            dormitory = new Dormitory(buildingNumber, roomNumber, area, capacity, occupied);
            dormitories.add(dormitory);
            System.out.println("添加成功!");
        }
    
        public void deleteRecord() {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入要删除记录的房间号:");
            int roomNumber = scanner.nextInt();
            Dormitory dormitory = findDormitory(roomNumber);
            if (dormitory == null) {
                System.out.println("该房间不存在,无法删除!");
                return;
            }
            dormitories.remove(dormitory);
            System.out.println("删除成功!");
        }
    
        public void modifyRecord() {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入要修改记录的房间号:");
            int roomNumber = scanner.nextInt();
            Dormitory dormitory = findDormitory(roomNumber);
            if (dormitory == null) {
                System.out.println("该房间不存在,无法修改!");
                return;
            }
            System.out.print("请输入新的楼号(原楼号为 " + dormitory.getBuildingNumber() + "):");
            int buildingNumber = scanner.nextInt();
            System.out.print("请输入新的面积(原面积为 " + dormitory.getArea() + "):");
            double area = scanner.nextDouble();
            System.out.print("请输入新的所容纳人数(原所容纳人数为 " + dormitory.getCapacity() + "):");
            int capacity = scanner.nextInt();
            System.out.print("请输入新的已入住人数(原已入住人数为 " + dormitory.getOccupied() + "):");
            int occupied = scanner.nextInt();
            dormitory = new Dormitory(buildingNumber, roomNumber, area, capacity, occupied);
            dormitories.set(dormitories.indexOf(findDormitory(roomNumber)), dormitory);
            System.out.println("修改成功!");
        }
    
        public void browseRecords() {
            if (dormitories.isEmpty()) {
                System.out.println("没有记录!");
                return;
            }
            System.println("楼号\t房间号\t面积\t所容纳人数\t已入住人数");
    for (Dormitory dormitory : dormitories) {
    System.out.println(dormitory.getBuildingNumber() + "\t" + dormitory.getRoomNumber() + "\t" +
    dormitory.getArea() + "\t" + dormitory.getCapacity() + "\t" + dormitory.getOccupied());
    }
    }
    
    public void searchByRoomNumber() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要查询的房间号:");
        int roomNumber = scanner.nextInt();
        Dormitory dormitory = findDormitory(roomNumber);
        if (dormitory == null) {
            System.out.println("该房间不存在!");
            return;
        }
        System.out.println("楼号\t房间号\t面积\t所容纳人数\t已入住人数");
        System.out.println(dormitory.getBuildingNumber() + "\t" + dormitory.getRoomNumber() + "\t" +
                dormitory.getArea() + "\t" + dormitory.getCapacity() + "\t" + dormitory.getOccupied());
    }
    
    public void sortByOccupied() {
        if (dormitories.isEmpty()) {
            System.out.println("没有记录!");
            return;
        }
        Collections.sort(dormitories, Comparator.comparing(Dormitory::getOccupied));
        System.out.println("楼号\t房间号\t面积\t所容纳人数\t已入住人数");
        for (Dormitory dormitory : dormitories) {
            System.out.println(dormitory.getBuildingNumber() + "\t" + dormitory.getRoomNumber() + "\t" +
                    dormitory.getArea() + "\t" + dormitory.getCapacity() + "\t" + dormitory.getOccupied());
        }
    }
    
    private Dormitory findDormitory(int roomNumber) {
        for (Dormitory dormitory : dormitories) {
            if (dormitory.getRoomNumber() == roomNumber) {
                return dormitory;
            }
        }
        return null;
    }
    
    public static void main(String[] args) {
        DormitoryManagement management = new DormitoryManagement();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            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("7.按已入住人数排序");
            System.out.println("0.退出系统");
            int option = scanner.nextInt();
            switch (option) {
                case 0:
                    System.out.println("谢谢使用!");
                    System.exit(0);
                    break;
                case 1:
                    management.input();
                    break;
                case 2:
                    management.add();
                    break;
                case 3:
                    management.delete();
                    break;
                case 4:
                    management.modify();
                    break;
                case 5:
                    management.showAll();
                    break;
                case 6:
                    management.searchByRoomNumber();
                    break;
                case 7:
                    management.sortByOccupied();
                    break;
                default:
                    System.out.println("选项不正确,请重新输入!");
                    break;
            }
        }
    }
    }
    
    class Dormitory {
    private int buildingNumber; // 楼号
    private int roomNumber; // 房间号
    private double area; // 面积
    private int capacity; // 所容纳人数
    private int occupied; // 已入住人数
    
    public Dormitory(int buildingNumber, int roomNumber, double area, int capacity, int occupied) {
        this.buildingNumber = buildingNumber;
        this.roomNumber = roomNumber;
        this.area = area;
        this.capacity = capacity;
        this.occupied = occupied;
    }
    
    public int getBuildingNumber() {
        return buildingNumber;
    }
    
    public void setBuildingNumber(int buildingNumber) {
        this.buildingNumber = buildingNumber;
    }
    
    public int getRoomNumber() {
        return roomNumber;
    }
    
    public void setRoomNumber(int roomNumber) {
        this.roomNumber = roomNumber;
    }
    
    public double getArea() {
        return area;
    }
    
    public void setArea(double area) {
        this.area = area;
    }
    public int getCapacity() {
            return capacity;
        }
    
        public void setCapacity(int capacity) {
            this.capacity = capacity;
        }
    
        public int getOccupied() {
            return occupied;
        }
    
        public void setOccupied(int occupied) {
            this.occupied = occupied;
        }
    }
    

    如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 4月21日
  • 已采纳回答 4月13日
  • 修改了问题 4月12日
  • 修改了问题 4月12日
  • 展开全部

悬赏问题

  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题