lab 9 close #7
This commit is contained in:
124
lab/lab9/Question2/Car.java
Normal file
124
lab/lab9/Question2/Car.java
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user