package com.lut.javase.array; import java.util.Objects; /* * 第二题:(java软件工程师人生路上第一个小型项目。锻炼一下面向对象。) 为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。 1、该系统的用户是:酒店前台。 2、酒店使用一个二维数组来模拟。“Room[][] rooms;” 3、酒店中的每一个房间应该是一个java对象:Room 4、每一个房间Room应该有:房间编号、房间类型、房间是否空闲. 5、系统应该对外提供的功能: 可以预定房间:用户输入房间编号,订房。 可以退房:用户输入房间编号,退房。 可以查看所有房间的状态:用户输入某个指令应该可以查看所有房间状态。 * 声明一个类的时候,要重写equals方法以及toString方法,并对其属性进行封装*/ public class HomeWork02 { public static void main(String[] args) { /* Room room = new Room(123, "套房", true); System.out.println(room);*/ Hotel hotel = new Hotel(); hotel.printHotel(); hotel.order(101); hotel.exit(101); } } class Room{ private int roomId; private String type; private boolean status; public int getRoomId() { return roomId; } public void setRoomId(int roomId) { this.roomId = roomId; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean getStatus() { return status; } public void setStatus(boolean status) { this.status = status; } public Room(int roomId, String type, boolean status) { this.roomId = roomId; this.type = type; this.status = status; } public Room() { } public String toString() { return "["+"Id=" + roomId + ", type=" + type + ", status=" + status+"]"+" "; } public boolean equals(Object obj){ //equals方法用来比较两个对象是否相同, if(obj==null || !(obj instanceof Room)) return false; if(this == obj) return true; Room room = (Room)obj; return this.getRoomId() == room.getRoomId(); } } class Hotel{ Room[][] rooms;; public Hotel() { rooms = new Room[3][10]; for(int i=0; i < rooms.length; i++){ for(int j=0; j < rooms[i].length; j++){ if(i == 0) rooms[i][j] = new Room((i+1)*100+j, "single",true); if(i == 1) rooms[i][j] = new Room((i+1)*100+j, "standard",true); if(i == 2) rooms[i][j] = new Room((i+1)*100+j, "suit",true); } } } public void printHotel(){ for(int i = 0; i < rooms.length; i++){ for(int j = 0; j < rooms[i].length; j++){ Room room = rooms[i][j]; System.out.print(room); } System.out.println(); } } public void order(int orderNum){ Room room = rooms[orderNum / 100 - 1][orderNum % 100 - 1]; room.setStatus(false); System.out.println("order"+room.getRoomId()+"success"); } public void exit(int exitNum){ Room room = rooms[exitNum / 100 - 1][exitNum % 100 - 1]; room.setStatus(true); System.out.println("exit"+room.getRoomId()+"success"); } }
原创文章,作者:254126420,如若转载,请注明出处:https://blog.ytso.com/272439.html