Files
oop/lab/lab9/Question7/Car.java
2022-04-23 00:21:49 +08:00

213 lines
5.1 KiB
Java

/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-21
* Description: This is the class of Door.
*/
import java.util.ArrayList;
public class Car {
private String name;
private ArrayList 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 ArrayList();
for (int i = 0; i < numberOfDoors; i++) {
doors.add(new Door());
}
}
/**
* Prints each door of the car.
*/
public void listDoors() {
for (int i = 0; i < doors.size(); i++) {
Door door = (Door) doors.get(i);
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 (int i = 0; i < doors.size(); i++) {
Door door = (Door) doors.get(i);
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.size() || doors.get(doorNumber) == null) {
throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1));
}
Door door = (Door) doors.get(doorNumber);
door.open();
}
/**
* Open all the closed doors and closes all the open doors.
*/
public void changeAllDoors() {
for (int i = 0; i < doors.size(); i++) {
Door door = (Door) doors.get(i);
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.size() || doors.get(doorNumber) == null) {
throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1));
}
// replace the door.
doors.set(doorNumber, new Door());
}
/**
* Replace all doors.
*/
public void replaceAllDoors() {
for (int i = 0; i < doors.size(); i++) {
doors.set(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.set(i, new Door());
}
}
/**
* Adds two new doors to the car.
*/
public void expandCar() {
doors.add(new Door());
doors.add(new Door());
}
/**
* 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 (IndexOutOfBoundsException 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();
}
}