java room management of small projects
The rooms manage small projects, for java beginners practiced hand. Function, while small, but the content is complete!
Like this article can follow me, I will continue to update, update your concerns are my motivation! For more java learning materials may also private letter I!
I wish my people are concerned about: good health, plenty of money, welfare, such as the East China Sea, many happy returns, Taoyuan County, from not afford to send!
A total of 5 layers, each 10 rooms, digital 101--509 mark; have and check-out, search, quit four simple functions;
public class Hotel {
static final int floor = 5;
static final int order = 10;
private static int countFloor;
private static int countOrder;
private static String[][] rooms = new String[floor][order];
Basic functions // main function on behalf of the hotel, check-in, check-out, query, other;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String temp = null;
while(true){
// alert the user to select the input
System.out.println ( "Please select an input in out search quit:");
temp = scan.next();
int room = 0;
if("in".equals(temp)){
while(true){
System.out.println ( "Please enter the room number:");
room = scan.nextInt();
if(isRightRoom(room,1)){
break;
}
}
System.out.println ( "Enter name:");
String name = scan.next();
if(in(room,name)){
System.out.println ( "Congratulations, stay successful");
System.out.println(room + " : "+name);
}
}else if("out".equals(temp)){
while(true){
System.out.println ( "Please enter the room number:");
room = scan.nextInt();
if(isRightRoom(room,0)){
break;
}
}
if(out(room)){
System.out.println ( "check-out success, welcome to the next visit");
}
}else if("search".equals(temp)){
System.out.println ( "Please enter the room number of (-1 means all)");
room = scan.nextInt();
search(room);
}else if("quit".equals(temp)){
break;
}else{
System.out.println ( "Your input is incorrect, please re-enter!");
}
}
}
// flag: 1 Check the room number and enter the correct unoccupied 0 check only enter the correct room number
private static boolean isRightRoom(int room,int flag) {
// check the room number is entered incorrectly
countFloor = room / 100 - 1;
countOrder = room % 100 - 1;
if(countFloor < 0 || countFloor >= floor || countOrder < 0 || countOrder >= order ){
System.out.println ( "Enter the room number wrong");
return false;
}
if(flag == 1){
if(rooms[countFloor][countOrder] != null){
System.out.println ( "rooms have been living in");
return false;
}
}
return true;
}
// hotel occupancy function realization, in (room, name) countFloor: calculated floor countOrder: the calculated room order
private static boolean in(int room,String name){
rooms[countFloor][countOrder] = name;
return true;
}
// Check the hotel function realization, out (room),
private static boolean out(int room){
// check whether someone check the room number
if(rooms[countFloor][countOrder] == null){
System.out.println ( "never room and check-out wrong");
return false;
}
rooms[countFloor][countOrder] = null;
return true;
}
// hotel search, search (room)
private static void search(int room){
if(room == -1){
int roomNum = 0;
for(int i=0;i<floor;i++){
for(int j=0;j<order;j++){
roomNum = (i + 1) * 100 + j + 1;
System.out.println(roomNum + "->" +
(rooms[i][j] == null ? "empty" : rooms[i][j]));
}
}
}else{
// check the room number is correct
if(!isRightRoom(room,0)){
System.out.println ( "Sorry, wrong room enter!");
}else{
System.out.println(rooms[countFloor][countOrder]);
}
}
}}