221 lines
5.2 KiB
Java
221 lines
5.2 KiB
Java
/*
|
|
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
|
* Date: 2022-04-21
|
|
* Description: This is the class of Door.
|
|
*/
|
|
|
|
public class Car {
|
|
private String name;
|
|
private Door[] doors;
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param name The name of the car.
|
|
* @param numberOfDoors The number of doors.
|
|
* @throws BadCarException If the number of doors is less than 1.
|
|
*/
|
|
public Car(String name, int numberOfDoors) throws BadCarException {
|
|
if (numberOfDoors < 1) {
|
|
throw new BadCarException("A car must have at least one door!");
|
|
}
|
|
|
|
this.name = name;
|
|
|
|
// initialise the doors array
|
|
doors = new Door[numberOfDoors];
|
|
for (int i = 0; i < numberOfDoors; i++) {
|
|
doors[i] = new Door();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prints each door of the car.
|
|
*/
|
|
public void listDoors() {
|
|
for (Door door : doors) {
|
|
System.out.println(String.format("%s: door is %s", name, door.isOpen() ? "open" : "closed"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Count the numer of open doors.
|
|
*
|
|
* @return The number of open doors.
|
|
*/
|
|
public int countOpenDoors() {
|
|
int count = 0;
|
|
for (Door door : doors) {
|
|
if (door.isOpen()) {
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/**
|
|
* Open one door with number start from 1.
|
|
*
|
|
* @param doorNumber The number of the door to open start from 1.
|
|
* @throws BadDoorException If try to open a non-existed door.
|
|
*/
|
|
public void openOneDoor(int doorNumber) throws BadDoorException {
|
|
doorNumber--;
|
|
if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) {
|
|
throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1));
|
|
}
|
|
|
|
doors[doorNumber].open();
|
|
}
|
|
|
|
/**
|
|
* Open all the closed doors and closes all the open doors.
|
|
*/
|
|
public void changeAllDoors() {
|
|
for (Door door : doors) {
|
|
if (door.isOpen()) {
|
|
door.close();
|
|
} else {
|
|
door.open();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Replaces the existing car door with a completely new door.
|
|
*
|
|
* @param doorNumber The number of the door to replace start from 1.
|
|
* @throws BadDoorException if replace a non-existed door.
|
|
*/
|
|
public void replaceDoor(int doorNumber) throws BadDoorException {
|
|
doorNumber--;
|
|
if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) {
|
|
throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1));
|
|
}
|
|
|
|
// replace the door.
|
|
doors[doorNumber] = new Door();
|
|
}
|
|
|
|
/**
|
|
* Replace all doors.
|
|
*/
|
|
public void replaceAllDoors() {
|
|
for (int i = 0; i < doors.length; i++) {
|
|
doors[i] = new Door();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Replace existing doors start from the first door with the number of doors.
|
|
*
|
|
* @param numOfDoorsToReplace The number of doors to replace.
|
|
*/
|
|
public void replaceManyDoors(int numOfDoorsToReplace) {
|
|
for (int i = 0; i < numOfDoorsToReplace; i++) {
|
|
doors[i] = new Door();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds two new doors to the car.
|
|
*/
|
|
public void expandCar() {
|
|
Door[] newDoors = new Door[doors.length + 2];
|
|
|
|
// copy the old doors to the new array.
|
|
for (int i = 0; i < doors.length; i++) {
|
|
newDoors[i] = doors[i];
|
|
}
|
|
|
|
// initialise the new doors.
|
|
for (int i = doors.length; i < newDoors.length; i++) {
|
|
newDoors[i] = new Door();
|
|
}
|
|
|
|
doors = newDoors;
|
|
}
|
|
|
|
/**
|
|
* Test.
|
|
*/
|
|
public static void testCar() {
|
|
try {
|
|
Car brokencar = new Car("Broken", 0);
|
|
} catch (BadCarException ex) {
|
|
System.out.println(ex.getMessage() == "A car must have at least one door!");
|
|
}
|
|
Car c = null;
|
|
try {
|
|
c = new Car("Biggy", 7);
|
|
} catch (BadCarException ex) {
|
|
System.out.println("BUG! This must never happen!");
|
|
}
|
|
c.listDoors();
|
|
System.out.println(c.countOpenDoors() == 0);
|
|
c.changeAllDoors();
|
|
System.out.println(c.countOpenDoors() == 7);
|
|
c.listDoors();
|
|
c.changeAllDoors();
|
|
System.out.println(c.countOpenDoors() == 0);
|
|
try {
|
|
c.openOneDoor(8);
|
|
} catch (BadDoorException ex) {
|
|
System.out.println(ex.getMessage().equals("Door 8 does not exist!"));
|
|
}
|
|
try {
|
|
c.openOneDoor(3);
|
|
} catch (BadDoorException ex) {
|
|
System.out.println("BUG! This must never happen!");
|
|
}
|
|
System.out.println(c.countOpenDoors() == 1);
|
|
|
|
c.changeAllDoors();
|
|
System.out.println(c.countOpenDoors() == 6);
|
|
c.listDoors();
|
|
c.changeAllDoors();
|
|
System.out.println(c.countOpenDoors() == 1);
|
|
c.listDoors();
|
|
try {
|
|
c.replaceDoor(8);
|
|
} catch (BadDoorException ex) {
|
|
|
|
System.out.println(ex.getMessage().equals("Door 8 does not exist!"));
|
|
}
|
|
|
|
try {
|
|
c.replaceDoor(3);
|
|
} catch (BadDoorException ex) {
|
|
System.out.println("BUG! This must never happen!");
|
|
}
|
|
System.out.println(c.countOpenDoors() == 0);
|
|
|
|
c.listDoors();
|
|
c.changeAllDoors();
|
|
System.out.println(c.countOpenDoors() == 7);
|
|
c.listDoors();
|
|
c.replaceAllDoors();
|
|
System.out.println(c.countOpenDoors() == 0);
|
|
c.listDoors();
|
|
c.changeAllDoors();
|
|
System.out.println(c.countOpenDoors() == 7);
|
|
c.listDoors();
|
|
c.replaceManyDoors(4);
|
|
System.out.println(c.countOpenDoors() == 3);
|
|
c.listDoors();
|
|
try {
|
|
c.replaceManyDoors(20);
|
|
} catch (ArrayIndexOutOfBoundsException ex) {
|
|
System.out.println(ex.getMessage().equals("Index 7 out of bounds for length 7"));
|
|
}
|
|
|
|
System.out.println(c.countOpenDoors() == 0);
|
|
c.listDoors();
|
|
|
|
c.expandCar();
|
|
c.changeAllDoors();
|
|
System.out.println(c.countOpenDoors() == 9);
|
|
c.listDoors();
|
|
}
|
|
}
|