客户端:
package HotelSystem;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT;
public class Client {
public static void main(String[] args) throws IOException {
Hotel hotel = new Hotel();
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("请输入你要选择的功能: 1 查看房间 2 房间预订 3 退房 0 退出");
int a = s.nextInt();
switch (a) {
case 1:
hotel.show();
break;
case 2:
System.out.println("请输入房间号");
int b = s.nextInt();
hotel.order(b);
break;
case 3:
System.out.println("请输入房间号");
int c = s.nextInt();
hotel.checkOut(c);
break;
case 0:
return;
}
}
}
}
房间类:
package HotelSystem;
import java.io.Serializable;
public class Room implements Serializable {
/**
* 如果不加 每次更改类,UID就变了,需要重新序列化
*
* 目的 : 控制序列化对象的版本,如果新版本兼容旧版本,这个值 就不用改 ,如果不兼容 就把值更改,原来的序列化对象就不能用了
*
* 版本号
*/
private static final long serialVersionUID = 1L;
private int number;
private String type;
private boolean flab;
//getSet方法
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isFlab() {
return flab;
}
public void setFlab(boolean flab) {
this.flab = flab;
}
// 构造方法
public Room(int number, String type, boolean flab) {
super();
this.number = number;
this.type = type;
this.flab = flab;
}
@Override
public String toString() {
return "Room ["+ number +" "+ type +" "+ (flab==true?"空闲":"占用") + "]";
}
}
酒店类:
package HotelSystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.PublicKey;
public class Hotel {
private Room[][] rooms;
public Hotel(){
File file=new File("./src/hotel");
if(!file.exists()){
//创建酒店房间的个数
rooms=new Room[5][10];
//对酒店进行初始化
for(int i=0;i<rooms.length;i++){
for(int j=0;j<rooms[i].length;j++){
if(i<=2){
rooms[i][j]=new Room((i+1)*100+j+1, "标准间", true);
}
if(i>2&&i<4){
rooms[i][j]=new Room((i+1)*100+j+1, "双人间", true);
}
if(i==4){
rooms[i][j]=new Room((i+1)*100+j+1, "豪华间", true);
}
}
}
}else {
try(//反序列化
FileInputStream fis=new FileInputStream("./src/hotel");
ObjectInputStream ois =new ObjectInputStream(fis);
) {
Object object=ois.readObject();
rooms=(Room[][])object;
} catch (Exception e) {
// TODO: handle exception
}
}
}
// 序列化
public void m1(){
try ( FileOutputStream fos=new FileOutputStream("./src/hotel");
ObjectOutputStream oos=new ObjectOutputStream(fos);
){
oos.writeObject(rooms);
} catch (Exception e) {
e.printStackTrace();
}
}
//房间展示
public void show(){
for(int i=0;i<rooms.length;i++){
for(int j=0;j<rooms[i].length;j++){
System.out.print(rooms[i][j]+" ");
}
System.out.println();
}
}
//房间预订
public void order(int num){
if (num/100-1>4 || num%100-1>9){
System.err.println("该房间不存在,请输入正确的房间号!");
}else {
if(num==rooms[num/100-1][num%100-1].getNumber()){
if(rooms[num/100-1][num%100-1].isFlab()==false){
System.err.println("该房间已被占用,请选择其他房间");
}else {
rooms[num/100-1][num%100-1].setFlab(false);
System.out.println("预订成功!!!");
}
}
}
m1();
}
//退房
public void checkOut(int num){
if (num/100-1>4 || num%100-1>9){
System.err.println("该房间不存在,请输入正确的房间号!");
}else {
if(rooms[num/100-1][num%100-1].isFlab()==true){
System.err.println("该房间无需退房!!!");
}else {
rooms[num/100-1][num%100-1].setFlab(true);
System.out.println("退房成功!!!");
}
}
}
}