完整代码
public class Main {
public static class Client {
private int customerID;
private String name;
private Dates arrive;
private Dates leave;
private Room bedroom;
public Client(int id, String c, Dates dateIn, Dates dateOut, Room r) {
setCustomerID(id);
setName(c);
setArrive(dateIn);
setLeave(dateOut);
setBedroom(r);
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dates getArrive() {
return arrive;
}
public void setArrive(Dates arrive) {
this.arrive = arrive;
}
public Dates getLeave() {
return leave;
}
public void setLeave(Dates leave) {
this.leave = leave;
}
public Room getBedroom() {
return bedroom;
}
public void setBedroom(Room bedroom) {
this.bedroom = bedroom;
}
public void bill() {
int nightsStayed = Dates.StayDays(arrive, leave);
double totalCost = nightsStayed * bedroom.getPrice();
System.out.println("Client's Name: " + getName());
System.out.println("Room Number: " + bedroom.getRoomNumber());
System.out.println("Arrival Date: " + arrive.getDay() + "/" + arrive.getMonth() + "/" + arrive.getYear());
System.out.println("Departure Date: " + leave.getDay() + "/" + leave.getMonth() + "/" + leave.getYear());
System.out.println("Total Nights Stayed: " + nightsStayed);
System.out.println("Total Cost: " + totalCost);
}
}
public class GClient extends Client {
private String groupName;
public GClient(int id, String c, Dates dateIn, Dates dateOut, Room r, String groupName) {
super(id, c, dateIn, dateOut, r);
this.groupName = groupName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
public static class Dates {
private int day;
private int month;
private int year;
public Dates(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static int StayDays(Dates x, Dates y) {
// Calculate the number of days between two provided dates (x and y)
int daysInX = x.getDay() + x.getMonth() * 30 + x.getYear() * 365;
int daysInY = y.getDay() + y.getMonth() * 30 + y.getYear() * 365;
return Math.abs(daysInX - daysInY);
}
}
public static class Room {
private int roomNumber;
private int beds;
private double price;
private boolean empty;
public Room(int roomNumber, int beds, double price, boolean empty) {
this.roomNumber = roomNumber;
this.beds = beds;
this.price = price;
this.empty = empty;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
public static class Group {
private String name; // name of group
private int number; // number of rooms allocated to the group
int[] gRooms; // array to hold room numbers allocated to the group
public Group(String name, int number) {
this.name = name;
this.number = number;
gRooms = new int[number]; // array to hold room numbers allocated to the group
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
public double bill() {
// Calculate the total bill for the group's allocated rooms for one day
double totalCost = 0;
for(int i=0; i<number; i++){
for(int j=0; j<100; j++){
if(gRooms[i] == Hotel.getAllRooms()[j].getRoomNumber()){
totalCost += Hotel.getAllRooms()[j].getPrice();
break;
}
}
}
return totalCost;
}
}
public static class Hotel {
private static Room[] allRooms; // Assume this array contains all the available rooms in the hotel
public static Room[] getAllRooms(){
return allRooms;
}
public int[] findRooms() {
int[] emptyTwoRooms = new int[100];
int index = 0;
for (int i=0; i<allRooms.length; i++) {
if (allRooms[i].isEmpty() && allRooms[i].getBeds() == 2) {
emptyTwoRooms[index] = allRooms[i].getRoomNumber();
index++;
}
}
return emptyTwoRooms;
}
public void allocateRooms(Group group){
int[] emptyTwoRooms = new int[100];
emptyTwoRooms = findRooms();
int index = 0;
for(int i=0; i<emptyTwoRooms.length; i++){
if(index<group.getNumber() && emptyTwoRooms[i] != 0){
allRooms[emptyTwoRooms[i]].empty = true;
group.gRooms[index] = emptyTwoRooms[i];
index = index+1;
}
}
if(index < group.getNumber()){
System.out.println("empty room is not sufficient");
}else{
for(int i=0; i<group.gRooms.length; i++){
System.out.print(group.gRooms[i]+" ");
}
}
}
}
public static void main(String[] args) {
Room[] rooms = new Room[100];
for (int i = 0; i < 100; i++) {
boolean isEmpty = i % 2 == 0;
rooms[i] = new Room(i + 1, 2, 100.0, isEmpty);
}
Hotel hotel = new Hotel();
hotel.allRooms = rooms;
// Testing the findRooms() method
System.out.println("\nTesting the findRooms() method======================");
int[] emptyTwoBedRooms = hotel.findRooms();
System.out.println("Empty rooms with 2 beds:");
for (int roomNumber : emptyTwoBedRooms) {
if (roomNumber != 0) {
System.out.print(roomNumber + " ");
}
}
System.out.println();
// Testing the bill() method
System.out.println("\nTesting the bill() method======================");
Dates arrivalDate = new Dates(1, 11, 2023);
Dates departureDate = new Dates(10, 11, 2023);
Room clientRoom = new Room(101, 2, 120.0, false);
Client client = new Client(1, "John Doe", arrivalDate, departureDate, clientRoom);
client.bill();
// Testing the allocateRooms() method
System.out.println("\nTesting the allocateRooms() method======================");
Group group = new Group("Happy Travellers", 15);
hotel.allocateRooms(group);
System.out.println();
// Testing the Group.bill() method
System.out.println("\nTesting the Group.bill() method======================");
System.out.println(group.bill());
}
}
14(d) findRooms()
public class Main {
public static class Client {
private int customerID;
private String name;
private Dates arrive;
private Dates leave;
private Room bedroom;
public Client(int id, String c, Dates dateIn, Dates dateOut, Room r) {
setCustomerID(id);
setName(c);
setArrive(dateIn);
setLeave(dateOut);
setBedroom(r);
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dates getArrive() {
return arrive;
}
public void setArrive(Dates arrive) {
this.arrive = arrive;
}
public Dates getLeave() {
return leave;
}
public void setLeave(Dates leave) {
this.leave = leave;
}
public Room getBedroom() {
return bedroom;
}
public void setBedroom(Room bedroom) {
this.bedroom = bedroom;
}
public void bill() {
}
}
public class GClient extends Client {
private String groupName;
public GClient(int id, String c, Dates dateIn, Dates dateOut, Room r, String groupName) {
super(id, c, dateIn, dateOut, r);
this.groupName = groupName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
public static class Dates {
private int day;
private int month;
private int year;
public Dates(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static int StayDays(Dates x, Dates y) {
// Calculate the number of days between two provided dates (x and y)
int daysInX = x.getDay() + x.getMonth() * 30 + x.getYear() * 365;
int daysInY = y.getDay() + y.getMonth() * 30 + y.getYear() * 365;
return Math.abs(daysInX - daysInY);
}
}
public static class Room {
private int roomNumber;
private int beds;
private double price;
private boolean empty;
public Room(int roomNumber, int beds, double price, boolean empty) {
this.roomNumber = roomNumber;
this.beds = beds;
this.price = price;
this.empty = empty;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
public static class Group {
private String name; // name of group
private int number; // number of rooms allocated to the group
int[] gRooms; // array to hold room numbers allocated to the group
public Group(String name, int number) {
this.name = name;
this.number = number;
gRooms = new int[number]; // array to hold room numbers allocated to the group
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
public double bill() {
return 0.0;
}
}
public static class Hotel {
private static Room[] allRooms; // Assume this array contains all the available rooms in the hotel
public static Room[] getAllRooms(){
return allRooms;
}
public int[] findRooms() {
int[] emptyTwoRooms = new int[100];
int index = 0;
for (int i=0; i<allRooms.length; i++) {
if (allRooms[i].isEmpty() && allRooms[i].getBeds() == 2) {
emptyTwoRooms[index] = allRooms[i].getRoomNumber();
index++;
}
}
return emptyTwoRooms;
}
public void allocateRooms(Group group){
}
}
public static void main(String[] args) {
Room[] rooms = new Room[100];
for (int i = 0; i < 100; i++) {
boolean isEmpty = i % 2 == 0;
rooms[i] = new Room(i + 1, 2, 100.0, isEmpty);
}
Hotel hotel = new Hotel();
hotel.allRooms = rooms;
// Testing the findRooms() method
System.out.println("Testing the findRooms() method======================");
int[] emptyTwoBedRooms = hotel.findRooms();
System.out.println("Empty rooms with 2 beds:");
for (int roomNumber : emptyTwoBedRooms) {
if (roomNumber != 0) {
System.out.print(roomNumber + " ");
}
}
}
}
14(e) client.bill()
public class Main {
public static class Client {
private int customerID;
private String name;
private Dates arrive;
private Dates leave;
private Room bedroom;
public Client(int id, String c, Dates dateIn, Dates dateOut, Room r) {
setCustomerID(id);
setName(c);
setArrive(dateIn);
setLeave(dateOut);
setBedroom(r);
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dates getArrive() {
return arrive;
}
public void setArrive(Dates arrive) {
this.arrive = arrive;
}
public Dates getLeave() {
return leave;
}
public void setLeave(Dates leave) {
this.leave = leave;
}
public Room getBedroom() {
return bedroom;
}
public void setBedroom(Room bedroom) {
this.bedroom = bedroom;
}
public void bill() {
int nightsStayed = Dates.StayDays(arrive, leave);
double totalCost = nightsStayed * bedroom.getPrice();
System.out.println("Client's Name: " + getName());
System.out.println("Room Number: " + bedroom.getRoomNumber());
System.out.println("Arrival Date: " + arrive.getDay() + "/" + arrive.getMonth() + "/" + arrive.getYear());
System.out.println("Departure Date: " + leave.getDay() + "/" + leave.getMonth() + "/" + leave.getYear());
System.out.println("Total Nights Stayed: " + nightsStayed);
System.out.println("Total Cost: " + totalCost);
}
}
public class GClient extends Client {
private String groupName;
public GClient(int id, String c, Dates dateIn, Dates dateOut, Room r, String groupName) {
super(id, c, dateIn, dateOut, r);
this.groupName = groupName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
public static class Dates {
private int day;
private int month;
private int year;
public Dates(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static int StayDays(Dates x, Dates y) {
// Calculate the number of days between two provided dates (x and y)
int daysInX = x.getDay() + x.getMonth() * 30 + x.getYear() * 365;
int daysInY = y.getDay() + y.getMonth() * 30 + y.getYear() * 365;
return Math.abs(daysInX - daysInY);
}
}
public static class Room {
private int roomNumber;
private int beds;
private double price;
private boolean empty;
public Room(int roomNumber, int beds, double price, boolean empty) {
this.roomNumber = roomNumber;
this.beds = beds;
this.price = price;
this.empty = empty;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
public static class Group {
private String name; // name of group
private int number; // number of rooms allocated to the group
int[] gRooms; // array to hold room numbers allocated to the group
public Group(String name, int number) {
this.name = name;
this.number = number;
gRooms = new int[number]; // array to hold room numbers allocated to the group
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
public double bill() {
return 0.0;
}
}
public static class Hotel {
private static Room[] allRooms; // Assume this array contains all the available rooms in the hotel
public static Room[] getAllRooms(){
return allRooms;
}
public int[] findRooms() {
int[] emptyTwoRooms = new int[100];
int index = 0;
for (int i=0; i<allRooms.length; i++) {
if (allRooms[i].isEmpty() && allRooms[i].getBeds() == 2) {
emptyTwoRooms[index] = allRooms[i].getRoomNumber();
index++;
}
}
return emptyTwoRooms;
}
public void allocateRooms(Group group){
}
}
public static void main(String[] args) {
Room[] rooms = new Room[100];
for (int i = 0; i < 100; i++) {
boolean isEmpty = i % 2 == 0;
rooms[i] = new Room(i + 1, 2, 100.0, isEmpty);
}
Hotel hotel = new Hotel();
hotel.allRooms = rooms;
// Testing the client.bill() method
System.out.println("Testing the client.bill() method======================");
Dates arrivalDate = new Dates(1, 11, 2023);
Dates departureDate = new Dates(10, 11, 2023);
Room clientRoom = new Room(101, 2, 120.0, false);
Client client = new Client(1, "John Doe", arrivalDate, departureDate, clientRoom);
client.bill();
}
}
15(b) allocateRooms()
public class Main {
public static class Client {
private int customerID;
private String name;
private Dates arrive;
private Dates leave;
private Room bedroom;
public Client(int id, String c, Dates dateIn, Dates dateOut, Room r) {
setCustomerID(id);
setName(c);
setArrive(dateIn);
setLeave(dateOut);
setBedroom(r);
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dates getArrive() {
return arrive;
}
public void setArrive(Dates arrive) {
this.arrive = arrive;
}
public Dates getLeave() {
return leave;
}
public void setLeave(Dates leave) {
this.leave = leave;
}
public Room getBedroom() {
return bedroom;
}
public void setBedroom(Room bedroom) {
this.bedroom = bedroom;
}
public void bill() {
/*
YOUR CODE HERE
*/
}
}
public class GClient extends Client {
private String groupName;
public GClient(int id, String c, Dates dateIn, Dates dateOut, Room r, String groupName) {
super(id, c, dateIn, dateOut, r);
this.groupName = groupName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
public static class Dates {
private int day;
private int month;
private int year;
public Dates(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static int StayDays(Dates x, Dates y) {
// Calculate the number of days between two provided dates (x and y)
int daysInX = x.getDay() + x.getMonth() * 30 + x.getYear() * 365;
int daysInY = y.getDay() + y.getMonth() * 30 + y.getYear() * 365;
return Math.abs(daysInX - daysInY);
}
}
public static class Room {
private int roomNumber;
private int beds;
private double price;
private boolean empty;
public Room(int roomNumber, int beds, double price, boolean empty) {
this.roomNumber = roomNumber;
this.beds = beds;
this.price = price;
this.empty = empty;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
public static class Group {
private String name; // name of group
private int number; // number of rooms allocated to the group
int[] gRooms; // array to hold room numbers allocated to the group
public Group(String name, int number) {
this.name = name;
this.number = number;
gRooms = new int[number]; // array to hold room numbers allocated to the group
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
public double bill() {
/*
YOUR CODE HERE
*/
return 0.0;
}
}
public static class Hotel {
private static Room[] allRooms; // Assume this array contains all the available rooms in the hotel
public static Room[] getAllRooms(){
return allRooms;
}
public int[] findRooms() {
int[] emptyTwoRooms = new int[100];
int index = 0;
for (int i=0; i<allRooms.length; i++) {
if (allRooms[i].isEmpty() && allRooms[i].getBeds() == 2) {
emptyTwoRooms[index] = allRooms[i].getRoomNumber();
index++;
}
}
return emptyTwoRooms;
}
public void allocateRooms(Group group){
int[] emptyTwoRooms = new int[100];
emptyTwoRooms = findRooms();
int index = 0;
for(int i=0; i<emptyTwoRooms.length; i++){
if(index<group.getNumber() && emptyTwoRooms[i] != 0){
allRooms[emptyTwoRooms[i]].empty = true;
group.gRooms[index] = emptyTwoRooms[i];
index = index+1;
}
}
if(index < group.getNumber()){
System.out.println("empty room is not sufficient");
}else{
for(int i=0; i<group.gRooms.length; i++){
System.out.print(group.gRooms[i]+" ");
}
}
}
}
public static void main(String[] args) {
Room[] rooms = new Room[100];
for (int i = 0; i < 100; i++) {
boolean isEmpty = i % 2 == 0;
rooms[i] = new Room(i + 1, 2, 100.0, isEmpty);
}
Hotel hotel = new Hotel();
hotel.allRooms = rooms;
// Testing the allocateRooms() method
System.out.println("Testing the allocateRooms() method======================");
Group group = new Group("Happy Travellers", 15);
hotel.allocateRooms(group);
}
}
15(c) Group.bill()
public class Main {
public static class Client {
private int customerID;
private String name;
private Dates arrive;
private Dates leave;
private Room bedroom;
public Client(int id, String c, Dates dateIn, Dates dateOut, Room r) {
setCustomerID(id);
setName(c);
setArrive(dateIn);
setLeave(dateOut);
setBedroom(r);
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dates getArrive() {
return arrive;
}
public void setArrive(Dates arrive) {
this.arrive = arrive;
}
public Dates getLeave() {
return leave;
}
public void setLeave(Dates leave) {
this.leave = leave;
}
public Room getBedroom() {
return bedroom;
}
public void setBedroom(Room bedroom) {
this.bedroom = bedroom;
}
public void bill() {
/*YOUR CODE HERE*/
}
}
public class GClient extends Client {
private String groupName;
public GClient(int id, String c, Dates dateIn, Dates dateOut, Room r, String groupName) {
super(id, c, dateIn, dateOut, r);
this.groupName = groupName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
public static class Dates {
private int day;
private int month;
private int year;
public Dates(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static int StayDays(Dates x, Dates y) {
// Calculate the number of days between two provided dates (x and y)
int daysInX = x.getDay() + x.getMonth() * 30 + x.getYear() * 365;
int daysInY = y.getDay() + y.getMonth() * 30 + y.getYear() * 365;
return Math.abs(daysInX - daysInY);
}
}
public static class Room {
private int roomNumber;
private int beds;
private double price;
private boolean empty;
public Room(int roomNumber, int beds, double price, boolean empty) {
this.roomNumber = roomNumber;
this.beds = beds;
this.price = price;
this.empty = empty;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
public static class Group {
private String name; // name of group
private int number; // number of rooms allocated to the group
int[] gRooms; // array to hold room numbers allocated to the group
public Group(String name, int number) {
this.name = name;
this.number = number;
gRooms = new int[number]; // array to hold room numbers allocated to the group
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
public double bill() {
// Calculate the total bill for the group's allocated rooms for one day
double totalCost = 0;
for(int i=0; i<number; i++){
for(int j=0; j<100; j++){
if(gRooms[i] == Hotel.getAllRooms()[j].getRoomNumber()){
totalCost += Hotel.getAllRooms()[j].getPrice();
break;
}
}
}
return totalCost;
}
}
public static class Hotel {
private static Room[] allRooms; // Assume this array contains all the available rooms in the hotel
public static Room[] getAllRooms(){
return allRooms;
}
public int[] findRooms() {
int[] emptyTwoRooms = new int[100];
int index = 0;
for (int i=0; i<allRooms.length; i++) {
if (allRooms[i].isEmpty() && allRooms[i].getBeds() == 2) {
emptyTwoRooms[index] = allRooms[i].getRoomNumber();
index++;
}
}
return emptyTwoRooms;
}
public void allocateRooms(Group group){
int[] emptyTwoRooms = new int[100];
emptyTwoRooms = findRooms();
int index = 0;
for(int i=0; i<emptyTwoRooms.length; i++){
if(index<group.getNumber() && emptyTwoRooms[i] != 0){
allRooms[emptyTwoRooms[i]].empty = true;
group.gRooms[index] = emptyTwoRooms[i];
index = index+1;
}
}
// if(index < group.getNumber()){
// System.out.println("empty room is not sufficient");
// }else{
// for(int i=0; i<group.gRooms.length; i++){
// System.out.print(group.gRooms[i]+" ");
// }
// }
}
}
public static void main(String[] args) {
Room[] rooms = new Room[100];
for (int i = 0; i < 100; i++) {
boolean isEmpty = i % 2 == 0;
rooms[i] = new Room(i + 1, 2, 100.0, isEmpty);
}
Hotel hotel = new Hotel();
hotel.allRooms = rooms;
// Testing the Group.bill() method
Group group = new Group("Happy Travellers", 15);
hotel.allocateRooms(group);
System.out.println("Testing the Group.bill() method======================");
System.out.println(group.bill());
}
}
17题完整代码
import java.util.Comparator;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
public class Main {
public static class Dates {
private int day;
private int month;
private int year;
public Dates(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static int StayDays(Dates x, Dates y) {
// Calculate the number of days between two provided dates (x and y)
int daysInX = x.getDay() + x.getMonth() * 30 + x.getYear() * 365;
int daysInY = y.getDay() + y.getMonth() * 30 + y.getYear() * 365;
return Math.abs(daysInX - daysInY);
}
public static Dates compareDate(Dates x, Dates y) {
if (x.year < y.year) {
return x;
} else if (x.year > y.year) {
return y;
} else {
if (x.month < y.month) {
return x;
} else if (x.month > y.month) {
return y;
} else {
if (x.day <= y.day) { // Updated condition to handle equal days
return x;
} else {
return y;
}
}
}
}
public static boolean equalDate(Dates x, Dates y) {
return (x.year == y.year && x.month == y.month && x.day == y.day);
}
}
public static class Room {
private int roomNumber;
private int beds;
private double price;
private boolean empty;
public Room(int roomNumber, int beds, double price, boolean empty) {
this.roomNumber = roomNumber;
this.beds = beds;
this.price = price;
this.empty = empty;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
public static class Client {
private int customerID;
private String name;
private Dates arrive;
private Dates leave;
private Room bedroom;
public Client(int id, String c, Dates dateIn, Dates dateOut, Room r) {
setCustomerID(id);
setName(c);
setArrive(dateIn);
setLeave(dateOut);
setBedroom(r);
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dates getArrive() {
return arrive;
}
public void setArrive(Dates arrive) {
this.arrive = arrive;
}
public Dates getLeave() {
return leave;
}
public void setLeave(Dates leave) {
this.leave = leave;
}
public Room getBedroom() {
return bedroom;
}
public void setBedroom(Room bedroom) {
this.bedroom = bedroom;
}
public void bill() {
int nightsStayed = Dates.StayDays(arrive, leave);
double totalCost = nightsStayed * bedroom.getPrice();
System.out.println("Client's Name: " + getName());
System.out.println("Room Number: " + bedroom.getRoomNumber());
System.out.println("Arrival Date: " + arrive.getDay() + "/" + arrive.getMonth() + "/" + arrive.getYear());
System.out.println("Departure Date: " + leave.getDay() + "/" + leave.getMonth() + "/" + leave.getYear());
System.out.println("Total Nights Stayed: " + nightsStayed);
System.out.println("Total Cost: " + totalCost);
}
}
public static class Hotel {
private LinkedList<Client> bookings;
public Hotel() {
bookings = new LinkedList<>();
}
public void newClient(Client newClient) {
if(bookings.isEmpty() == true){
bookings.addFirst(newClient);
}else{
ListIterator<Client> iterator = bookings.listIterator();
int index = 0;
boolean found = false;
while(iterator.hasNext() && found == false){
Client current = iterator.next();
if(Dates.equalDate(Dates.compareDate(newClient.getArrive(), current.getArrive()), newClient.getArrive())
|| Dates.equalDate(newClient.getArrive(), current.getArrive())){
bookings.add(index, newClient);
found = true;
}else{
index = index+1;
}
}
if(found == false) bookings.addLast(newClient);
}
}
public Client[] todayClients(Dates today) {
Client[] arrivalsToday = new Client[bookings.size()]; // 创建一个数组来存储今日到达的客户
int lengthOfArray = 0;
ListIterator<Client> iterator = bookings.listIterator();
while (iterator.hasNext()) {
Client current = iterator.next();
if (Dates.equalDate(current.getArrive(), today)) {
iterator.remove(); // 移除当前元素
int i;
for (i = lengthOfArray - 1; i >= 0; i--) {
if (Dates.equalDate(Dates.compareDate(arrivalsToday[i].getLeave(),
current.getLeave()), current.getLeave())) {
arrivalsToday[i + 1] = arrivalsToday[i];
} else {
break;
}
}
arrivalsToday[i + 1] = current; // 插入新的到达客户
lengthOfArray = lengthOfArray + 1;
}
}
Client[] arrivalsArray = new Client[lengthOfArray]; // 创建一个新的数组来存储今日到达的客户
System.arraycopy(arrivalsToday, 0, arrivalsArray, 0, lengthOfArray); // 将结果复制到新数组中
return arrivalsArray;
}
}
public static void main(String[] args) {
Hotel hotel = new Hotel();
// Create dates
Dates date1 = new Dates(1, 1, 2023);
Dates date2 = new Dates(2, 1, 2023);
Dates date3 = new Dates(3, 1, 2023);
Dates date4 = new Dates(4, 1, 2023);
Dates date5 = new Dates(5, 1, 2023);
Dates date6 = new Dates(6, 1, 2023);
// Create rooms
Room room1 = new Room(101, 2, 100, true);
Room room2 = new Room(102, 3, 150, true);
Room room3 = new Room(103, 2, 120, true);
Room room4 = new Room(104, 2, 110, true);
Room room5 = new Room(105, 1, 90, true);
// Create clients
Client client1 = new Client(1, "Alice", date2, date2, room1);
Client client2 = new Client(2, "Bob", date2, date3, room2);
Client client3 = new Client(3, "Charlie", date3, date3, room3);
Client client4 = new Client(4, "David", date2, date4, room4);
Client client5 = new Client(5, "Emma", date4, date5, room5);
Client client6 = new Client(5, "Lily", date2, date4, room4);
// Add clients to the hotel
hotel.newClient(client1);
hotel.newClient(client2);
hotel.newClient(client3);
hotel.newClient(client4);
hotel.newClient(client5);
// Display all clients after sorting
System.out.println("Clients after sorting:");
for (Client client : hotel.bookings) {
System.out.println("Name: " + client.getName() + ", Arrival: " +
client.getArrive().getDay() + "/" + client.getArrive().getMonth() + "/" + client.getArrive().getYear());
}
Dates today = new Dates(2, 1, 2023); // Assuming today's date is 1st Jan 2023
// Display clients today's arrivals
System.out.println("\nClients today's arrivals:");
for (Client client : hotel.todayClients(today)) {
System.out.println("Name: " + client.getName() + ", Arrival: " +
client.getArrive().getDay() + "/" + client.getArrive().getMonth() + "/" + client.getArrive().getYear()+
", Leave: " +
client.getLeave().getDay() + "/" + client.getLeave().getMonth() + "/" + client.getLeave().getYear());
}
}
}
17(a) newClient()
import java.util.Comparator;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
public class Main {
public static class Dates {
private int day;
private int month;
private int year;
public Dates(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static int StayDays(Dates x, Dates y) {
// Calculate the number of days between two provided dates (x and y)
int daysInX = x.getDay() + x.getMonth() * 30 + x.getYear() * 365;
int daysInY = y.getDay() + y.getMonth() * 30 + y.getYear() * 365;
return Math.abs(daysInX - daysInY);
}
public static Dates compareDate(Dates x, Dates y) {
if (x.year < y.year) {
return x;
} else if (x.year > y.year) {
return y;
} else {
if (x.month < y.month) {
return x;
} else if (x.month > y.month) {
return y;
} else {
if (x.day <= y.day) { // Updated condition to handle equal days
return x;
} else {
return y;
}
}
}
}
public static boolean equalDate(Dates x, Dates y) {
return (x.year == y.year && x.month == y.month && x.day == y.day);
}
}
public static class Room {
private int roomNumber;
private int beds;
private double price;
private boolean empty;
public Room(int roomNumber, int beds, double price, boolean empty) {
this.roomNumber = roomNumber;
this.beds = beds;
this.price = price;
this.empty = empty;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
public static class Client {
private int customerID;
private String name;
private Dates arrive;
private Dates leave;
private Room bedroom;
public Client(int id, String c, Dates dateIn, Dates dateOut, Room r) {
setCustomerID(id);
setName(c);
setArrive(dateIn);
setLeave(dateOut);
setBedroom(r);
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dates getArrive() {
return arrive;
}
public void setArrive(Dates arrive) {
this.arrive = arrive;
}
public Dates getLeave() {
return leave;
}
public void setLeave(Dates leave) {
this.leave = leave;
}
public Room getBedroom() {
return bedroom;
}
public void setBedroom(Room bedroom) {
this.bedroom = bedroom;
}
public void bill() {
int nightsStayed = Dates.StayDays(arrive, leave);
double totalCost = nightsStayed * bedroom.getPrice();
System.out.println("Client's Name: " + getName());
System.out.println("Room Number: " + bedroom.getRoomNumber());
System.out.println("Arrival Date: " + arrive.getDay() + "/" + arrive.getMonth() + "/" + arrive.getYear());
System.out.println("Departure Date: " + leave.getDay() + "/" + leave.getMonth() + "/" + leave.getYear());
System.out.println("Total Nights Stayed: " + nightsStayed);
System.out.println("Total Cost: " + totalCost);
}
}
public static class Hotel {
private LinkedList<Client> bookings;
public Hotel() {
bookings = new LinkedList<>();
}
public void newClient(Client newClient) {
if(bookings.isEmpty() == true){
bookings.addFirst(newClient);
}else{
ListIterator<Client> iterator = bookings.listIterator();
int index = 0;
boolean found = false;
while(iterator.hasNext() && found == false){
Client current = iterator.next();
if(Dates.equalDate(Dates.compareDate(newClient.getArrive(), current.getArrive()), newClient.getArrive())
|| Dates.equalDate(newClient.getArrive(), current.getArrive())){
bookings.add(index, newClient);
found = true;
}else{
index = index+1;
}
}
if(found == false) bookings.addLast(newClient);
}
}
public Client[] todayClients(Dates today) {
Client[] arrivalsToday = new Client[bookings.size()]; // 创建一个数组来存储今日到达的客户
return arrivalsToday;
}
}
public static void main(String[] args) {
Hotel hotel = new Hotel();
// Create dates
Dates date1 = new Dates(1, 1, 2023);
Dates date2 = new Dates(2, 1, 2023);
Dates date3 = new Dates(3, 1, 2023);
Dates date4 = new Dates(4, 1, 2023);
Dates date5 = new Dates(5, 1, 2023);
Dates date6 = new Dates(6, 1, 2023);
// Create rooms
Room room1 = new Room(101, 2, 100, true);
Room room2 = new Room(102, 3, 150, true);
Room room3 = new Room(103, 2, 120, true);
Room room4 = new Room(104, 2, 110, true);
Room room5 = new Room(105, 1, 90, true);
// Create clients
Client client1 = new Client(1, "Alice", date2, date2, room1);
Client client2 = new Client(2, "Bob", date2, date3, room2);
Client client3 = new Client(3, "Charlie", date3, date3, room3);
Client client4 = new Client(4, "David", date2, date4, room4);
Client client5 = new Client(5, "Emma", date4, date5, room5);
Client client6 = new Client(5, "Lily", date2, date4, room4);
// Add clients to the hotel
hotel.newClient(client1);
hotel.newClient(client2);
hotel.newClient(client3);
hotel.newClient(client4);
hotel.newClient(client5);
// Display all clients after sorting
System.out.println("Clients after sorting:");
for (Client client : hotel.bookings) {
System.out.println("Name: " + client.getName() + ", Arrival: " +
client.getArrive().getDay() + "/" + client.getArrive().getMonth() + "/" + client.getArrive().getYear());
}
}
}
17(b) todayClients
import java.util.Comparator;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
public class Main {
public static class Dates {
private int day;
private int month;
private int year;
public Dates(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static int StayDays(Dates x, Dates y) {
// Calculate the number of days between two provided dates (x and y)
int daysInX = x.getDay() + x.getMonth() * 30 + x.getYear() * 365;
int daysInY = y.getDay() + y.getMonth() * 30 + y.getYear() * 365;
return Math.abs(daysInX - daysInY);
}
public static Dates compareDate(Dates x, Dates y) {
if (x.year < y.year) {
return x;
} else if (x.year > y.year) {
return y;
} else {
if (x.month < y.month) {
return x;
} else if (x.month > y.month) {
return y;
} else {
if (x.day <= y.day) { // Updated condition to handle equal days
return x;
} else {
return y;
}
}
}
}
public static boolean equalDate(Dates x, Dates y) {
return (x.year == y.year && x.month == y.month && x.day == y.day);
}
}
public static class Room {
private int roomNumber;
private int beds;
private double price;
private boolean empty;
public Room(int roomNumber, int beds, double price, boolean empty) {
this.roomNumber = roomNumber;
this.beds = beds;
this.price = price;
this.empty = empty;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
public static class Client {
private int customerID;
private String name;
private Dates arrive;
private Dates leave;
private Room bedroom;
public Client(int id, String c, Dates dateIn, Dates dateOut, Room r) {
setCustomerID(id);
setName(c);
setArrive(dateIn);
setLeave(dateOut);
setBedroom(r);
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dates getArrive() {
return arrive;
}
public void setArrive(Dates arrive) {
this.arrive = arrive;
}
public Dates getLeave() {
return leave;
}
public void setLeave(Dates leave) {
this.leave = leave;
}
public Room getBedroom() {
return bedroom;
}
public void setBedroom(Room bedroom) {
this.bedroom = bedroom;
}
public void bill() {
int nightsStayed = Dates.StayDays(arrive, leave);
double totalCost = nightsStayed * bedroom.getPrice();
System.out.println("Client's Name: " + getName());
System.out.println("Room Number: " + bedroom.getRoomNumber());
System.out.println("Arrival Date: " + arrive.getDay() + "/" + arrive.getMonth() + "/" + arrive.getYear());
System.out.println("Departure Date: " + leave.getDay() + "/" + leave.getMonth() + "/" + leave.getYear());
System.out.println("Total Nights Stayed: " + nightsStayed);
System.out.println("Total Cost: " + totalCost);
}
}
public static class Hotel {
private LinkedList<Client> bookings;
public Hotel() {
bookings = new LinkedList<>();
}
public void newClient(Client newClient) {
if(bookings.isEmpty() == true){
bookings.addFirst(newClient);
}else{
ListIterator<Client> iterator = bookings.listIterator();
int index = 0;
boolean found = false;
while(iterator.hasNext() && found == false){
Client current = iterator.next();
if(Dates.equalDate(Dates.compareDate(newClient.getArrive(), current.getArrive()), newClient.getArrive())
|| Dates.equalDate(newClient.getArrive(), current.getArrive())){
bookings.add(index, newClient);
found = true;
}else{
index = index+1;
}
}
if(found == false) bookings.addLast(newClient);
}
}
public Client[] todayClients(Dates today) {
Client[] arrivalsToday = new Client[bookings.size()]; // 创建一个数组来存储今日到达的客户
int lengthOfArray = 0;
ListIterator<Client> iterator = bookings.listIterator();
while (iterator.hasNext()) {
Client current = iterator.next();
if (Dates.equalDate(current.getArrive(), today)) {
iterator.remove(); // 移除当前元素
int i;
for (i = lengthOfArray - 1; i >= 0; i--) {
if (Dates.equalDate(Dates.compareDate(arrivalsToday[i].getLeave(),
current.getLeave()), current.getLeave())) {
arrivalsToday[i + 1] = arrivalsToday[i];
} else {
break;
}
}
arrivalsToday[i + 1] = current; // 插入新的到达客户
lengthOfArray = lengthOfArray + 1;
}
}
Client[] arrivalsArray = new Client[lengthOfArray]; // 创建一个新的数组来存储今日到达的客户
System.arraycopy(arrivalsToday, 0, arrivalsArray, 0, lengthOfArray); // 将结果复制到新数组中
return arrivalsArray;
}
}
public static void main(String[] args) {
Hotel hotel = new Hotel();
// Create dates
Dates date1 = new Dates(1, 1, 2023);
Dates date2 = new Dates(2, 1, 2023);
Dates date3 = new Dates(3, 1, 2023);
Dates date4 = new Dates(4, 1, 2023);
Dates date5 = new Dates(5, 1, 2023);
Dates date6 = new Dates(6, 1, 2023);
// Create rooms
Room room1 = new Room(101, 2, 100, true);
Room room2 = new Room(102, 3, 150, true);
Room room3 = new Room(103, 2, 120, true);
Room room4 = new Room(104, 2, 110, true);
Room room5 = new Room(105, 1, 90, true);
// Create clients
Client client1 = new Client(1, "Alice", date2, date2, room1);
Client client2 = new Client(2, "Bob", date2, date3, room2);
Client client3 = new Client(3, "Charlie", date3, date3, room3);
Client client4 = new Client(4, "David", date2, date4, room4);
Client client5 = new Client(5, "Emma", date4, date5, room5);
Client client6 = new Client(5, "Lily", date2, date4, room4);
// Add clients to the hotel
hotel.newClient(client1);
hotel.newClient(client2);
hotel.newClient(client3);
hotel.newClient(client4);
hotel.newClient(client5);
Dates today = new Dates(2, 1, 2023); // Assuming today's date is 1st Jan 2023
// Display clients today's arrivals
System.out.println("Clients today's arrivals:");
for (Client client : hotel.todayClients(today)) {
System.out.println("Name: " + client.getName() + ", Arrival: " +
client.getArrive().getDay() + "/" + client.getArrive().getMonth() + "/" + client.getArrive().getYear()+
", Leave: " +
client.getLeave().getDay() + "/" + client.getLeave().getMonth() + "/" + client.getLeave().getYear());
}
}
}