day08(Hotel优化、静态代码块)
People:
package testPackage;
public class People {
private int age;
public String name;
protected String idCard; //身份证号
String sex;
}
Customer:
/**
- 存放客户个人信息
- @author Administrator
*/
public class Customer {
private String name; //客户姓名
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Room:
public class Room {
private String roomNo; //存放房间号
private boolean empty; //判断是否有人住
private Customer customer; //存放客户信息
public String getRoomNo() {
return roomNo;
}
public void setRoomNo(String roomNo) {
this.roomNo = roomNo;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Room()
{
}
public Room(String roomNo)
{
this.roomNo = roomNo;
this.empty = true;
}
/**
* 房间入住
* 1 判断是否有人住
* 2 输入入住者姓名
* @param customer
*/
public void getInMethod(Customer customer)
{
this.empty = false;
this.customer = customer;
}
/**
* 退房
* 1 房间状态为empty
* 2 入住者姓名为空
*/
public void outOfRoomMethod()
{
this.empty = true;
this.customer = null;
}
}
RoomManager:
/**
- 类似首页
- 第一个展示在用户面前的界面
- @author Administrator
*/
public class RoomManageSystem {
//引用数据类型
private Hotel hotel;
public RoomManageSystem() {
}
public RoomManageSystem(Hotel hotel) {
this.hotel = hotel;
}
public void start() {
Scanner sc = new Scanner(System.in);
boolean isRunning = true;
while(isRunning)
{
printMenu();
int choice = sc.nextInt();
switch (choice) {
case 1:
hotel.searchStatus();
break;
case 2:
System.out.println("请输入房间号");
int roomNo = sc.nextInt();
System.out.println("请输入客户姓名");
String customerName = sc.next();
hotel.getInRoom(roomNo, customerName);
break;
case 3:
System.out.println("请输入房间号");
int outRoomNo = sc.nextInt();
hotel.outOfRoom(outRoomNo);
break;
case 4:
System.out.println("再见");
break;
default:
break;
}
}
}
public void printMenu()
{
System.out.println("-----菜单-------");
System.out.println("1-查询所有房间状态");
System.out.println("2-入住");
System.out.println("3-退房");
System.out.println("4-退出系统");
System.out.println("---------------");
}
}
Hotel:
public class Hotel {
private Room rooms[][];
/**
* 酒店房间的初始化
*/
public Hotel()
{
rooms = new Room[10][12];
for(int i = 0; i < rooms.length;i ++)
{
for(int j = 0 ; j < rooms[i].length ; j++ )
{
String roomNo = "";
//层的设置
roomNo += i<9 ? "0"+(i+1):(i+1);
//房间号设置
roomNo += j<9 ? "0"+(j+1):(j+1);
rooms[i][j] = new Room(roomNo);
}
}
}
/**
* 查询房间状态
*/
public void searchStatus()
{
for(int i = 0; i < rooms.length;i ++)
{
for(int j = 0 ; j < rooms[i].length ; j++ )
{
//取其中一个房间
Room r = rooms[i][j];
System.out.print(r.getRoomNo()+","+r.isEmpty());
if(!r.isEmpty())
{
System.out.print(","+r.getCustomer().getName());
}
System.out.print("\t");
}
System.out.println();
}
}
/**
* 房间入住
*/
public void getInRoom(int roomNo, String customer)
{
int i = roomNo/100 -1;
int j = roomNo%100 -1;
Room r = rooms[i][j];
if(r.isEmpty())
{
Customer c = new Customer();
//如果是空 设置用户姓名
c.setName(customer);
r.getInMethod(c);
}
else
{
System.err.println("入住失败!");
}
}
/**
* 退房
*/
public void outOfRoom(int roomNo)
{
int i = roomNo / 100 -1;
int j = roomNo % 100 -1;
Room r = rooms[i][j];
if(!r.isEmpty())
{
r.outOfRoomMethod();
}
else {
System.out.println("退房失败");
}
}
}
Person:
public class Person{
public int age;
public String name;
protected String idCard; //身份证号
String sex;
// public Person()
// {
// System.out.println("person的无参构造");
// }
public Person(int age,String name)
{
this.age = age;
this.name = name;
System.out.println("person的有参构造");
}
public void eat()
{
System.out.println("吃饭");
}
public void run()
{
System.out.println("跑步");
}
public void drink()
{
System.out.println("喝水");
}
}
Test01:
public class Test01 extends People{
private final static double PI = 3.14;
// private static int age;
public static void main(String[] args) {
/*
final Test02 test02 = new Test02();
test02.setName("zhangsan");
System.out.println(test02.getName());
test02.setName("lisi");
System.out.println(test02.getName());
Test01 test01 = new Test01();
test01.getMethod();
*/
Person person = new Person(1000, "lisi");
Student student = new Student();
student.eat();
System.out.println(person.name);
System.out.println(person.age);
// student.idCard = "341124....";
// student.name = "zhangsan";
// student.sex = "男";
// student.age = 10;
/*
//不同包且没有继承关系
People p = new People();
p.name = "zhangsan";
p.sex = "nan";
p.idCard = "546546";
p.age = 10;
*/
/*
//不同包但有继承关系
Test01 test01 = new Test01();
test01.name = "zhangsan";
test01.sex = "nan";
test01.idCard = "546546";
test01.age = 10;
*/
}
/**
* 代码块
*/
{
//创建对象时执行
}
/**
* 静态代码块
*/
static{
//类加载时执行 且只执行一次
}
public void getMethod()
{}
public static void someMethod()
{
}
}
Test02:
public class Test02{
private String name;
private int age;
private static String sex;
{
System.out.println("代码块");
}
static {
System.out.println("静态代码块");
}
public Test02()
{
System.out.println("构造函数");
}
public static String getSex() {
return sex;
}
public static void setSex(String sex) {
Test02.sex = sex;
}
public static void main(String[] args) {
Test02.getSth();
}
public static void getSth()
{
System.out.println("something");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}