package day04hw; import java.util.Scanner; public class HotelMgmt { private final static int FLOOR = 10; private final static int ROOM = 8; private String[][] rooms; public HotelMgmt() { rooms = new String[HotelMgmt.FLOOR][HotelMgmt.ROOM]; } //查询房间 public void search() { //rooms.length FLOOR //rooms[i].length ROOM System.out.print(" \t"); for (int i=0; i<HotelMgmt.ROOM; i++) { System.out.print((i+1)+"\t"); } System.out.println(); for (int i=HotelMgmt.FLOOR-1; i>=0; i--) { for (int j=0; j<HotelMgmt.ROOM; j++) { if (j==0) { System.out.print((i+1)+"\t"); } System.out.print((rooms[i][j]==null?"-":rooms[i][j])+"\t"); } System.out.println(); } } //入住 public void in(int floor, int room, String username) { if (floor > HotelMgmt.FLOOR || floor <= 0 || room > HotelMgmt.ROOM || room <=0) { System.out.println("您所输入的房间不存在!"); } else { if (rooms[floor-1][room-1] != null) { System.out.println("您所输入的房间已入住,入住失败!"); } else { System.out.println(username+"入住 ["+floor+"] 层 " + "["+room+"] 号房间!"); rooms[floor-1][room-1] = username; } } } //退房 public void out(int floor, int room) { if (floor > HotelMgmt.FLOOR || floor <= 0 || room > HotelMgmt.ROOM || room <=0) { System.out.println("您所输入的房间不存在!"); } else { if (rooms[floor-1][room-1] == null) { System.out.println("您所输入的房间未入住,退房失败!"); } else { System.out.println(rooms[floor-1][room-1]+"退房 ["+floor+"] 层 " + "["+room+"] 号房间!"); rooms[floor-1][room-1] = null; } } } public void showCommand() { System.out.println("command :查询所有命令"); System.out.println("search :查询所有房间的状态"); System.out.println("in 1202 tangliang :姓名为tangliang的客人入住1202房间"); System.out.println("out 1202:1202房间退房"); System.out.println("quit:退出程序"); } public void console() { Scanner scanner = new Scanner(System.in); System.out.println("欢迎使用山寨版酒店管理系统!"); showCommand(); while (true) { System.out.println("\n请输入命令:"); String command = scanner.nextLine(); String[] commands = command.split(" "); if (commands[0].equalsIgnoreCase("command")) { showCommand(); } else if (commands[0].equalsIgnoreCase("search")) { search(); } else if (commands[0].equalsIgnoreCase("in")) { String roomInfo = commands[1]; String username = commands[2]; int floor = Integer.parseInt(roomInfo.substring(0,2)); int room = Integer.parseInt(roomInfo.substring(2)); in(floor, room, username); } else if (commands[0].equalsIgnoreCase("out")) { String roomInfo = commands[1]; int floor = Integer.parseInt(roomInfo.substring(0,2)); int room = Integer.parseInt(roomInfo.substring(2)); out(floor, room); } else if (commands[0].equalsIgnoreCase("quit")) { System.out.println("谢谢使用,再见!"); break; } else { System.out.println("您所输入的命令不存在! 请输入command进行命令查询!"); } } } public static void main(String[] args) { HotelMgmt system = new HotelMgmt(); system.console(); } }
package tarena; import java.io.*; import java.util.Arrays; //自己写的 public class Hotel { /** * @param args */ private String[][] rooms; private final static int FLOOR = 20; private final static int ROOM = 10; public Hotel() { rooms = new String[Hotel.FLOOR][Hotel.ROOM]; for (int i = 0; i < rooms.length; i++) Arrays.fill(rooms[i], "_"); } public void search() { System.out.print("\t"); for (int i = 0; i < rooms[0].length; i++) System.out.print((i + 1) + "\t"); System.out.println(); for (int i = rooms.length - 1; i >= 0; i--) { for (int j = 0; j < rooms[i].length; j++) { if (j == 0) { System.out.print((i + 1) + "\t"); } System.out.print(rooms[i][j] + "\t"); } System.out.println(); System.out.println(); } } public void in(int floor, int room, String name) { if (floor > (rooms.length) || floor <= 0 || room > (rooms[1].length) || room <= 0) { System.out.println("房间不存在!"); } else { if (rooms[floor - 1][room - 1] != "_") { System.out.println("房间已入住,入住失败!"); } else { System.out.println(name + "入住 [" + floor + "] 层 " + "[" + room + "] 号房间!"); rooms[floor - 1][room - 1] = name; } } } public void out(int floor, int room) { if (floor > (rooms.length) || floor <= 0 || room > (rooms[1].length) || room <= 0) { System.out.println("房间不存在!"); } else { if (rooms[floor - 1][room - 1].equals("_")) { System.out.println(floor + "层" + room + "号房间空,退房失败"); } else { System.out.println("退" + floor + "层" + room + "号房间"); rooms[floor - 1][room - 1] = "_"; } } } public void console() { System.out.println("欢迎使用酒店管理系统!"); while (true) { System.out.println("************************************************************"); System.out.println("search :查询所有房间的状态"); System.out.println("in 1202 tangliang :姓名为tangliang的客人入住1202房间"); System.out.println("out 1202:1202房间退房"); System.out.println("quit:退出程序"); System.out.println("\n请输入命令:"); String str = null; try { BufferedReader buf; buf = new BufferedReader(new InputStreamReader(System.in)); str = buf.readLine(); //System.out.println(str); } catch (Exception e) { System.out.println("error"); } /////////////////////////////////////////////////////////////// String[] s = str.split(" ");//字符拆分借鉴老师代码 if (s[0].equalsIgnoreCase("search")) { search(); } else if (s[0].equalsIgnoreCase("in")) { String roomInfo = s[1]; String username = s[2]; int floor = Integer.parseInt(roomInfo.substring(0, 2)); int room = Integer.parseInt(roomInfo.substring(2)); in(floor, room, username); } else if (s[0].equalsIgnoreCase("out")) { String roomInfo = s[1]; int floor = Integer.parseInt(roomInfo.substring(0, 2)); int room = Integer.parseInt(roomInfo.substring(2)); out(floor, room); } else if (s[0].equalsIgnoreCase("quit")) { System.out.println("系统安全退出!"); break; } else { System.out.println("您所输入的命令不存在! "); } /////////////////////////////////////////////////////////////// } } public static void main(String[] args) { Hotel ren = new Hotel(); ren.console(); } }