You are implementing a system that manages parking lot using object-oriented pri
ID: 3707211 • Letter: Y
Question
You are implementing a system that manages parking lot using object-oriented principles The parking lot has multiple levels. Each level has a multiple rows of spots Motorcycles, cars and buses can park. It has motorcycle spot, compact spot and large spoft. A motorcycle can park in any spot. A car can park in either a single compact spot or a single large spot. A bus can park in five large spots that are consecutive and with in the same row. It cannot park in small spot. A) Draw a UML class diagram. Identify classes and the relation between them (for example inheritance or aggregation) Use a standard tool to draw UML diagram such as starUML which you can download it from http://staruml.io/download. Manual UML class diagrams are not accepted B) Convert the class diagram to java classes Different version of the Game 1. Only Park vehicles until no spot available (Mandatory for this project) . You can find a first/random available spot and park the vehicle 2. Park and remove vehicles (Optional for this project extra credit . You can find a first/random available spot and park/remove the vehicle Note: Here are some default values NUM OF LEVELS= 5 SPOTS PER LEVEL=30 SPOTS PER ROW= 10 . 20% of spots are large (per level) and they are consecutive with the same row 20% of spots are motorcycle spots (per level) and they are consecutive with the same row You need to ask user when you start the program to provide these numbers or use the above default values otherwise ** You can find a sample run at the end of this fileExplanation / Answer
Below is your code: -
Vehicle.java
public class Vehicle {
static int numOfVehicles = 0;
// Each time a new vehicle is added, numOfVehicles is incremented.
Vehicle() {
numOfVehicles++;
}
}
ParkingMain.java
public class ParkingMain {
public static void main(String[] args) {
ParkingSpot spot = new ParkingSpot();
int total_level = 0;
int spot_row = 0;
int spot_level = 0;
int level = 0;
String whatType = "a";
boolean isNumber;
// Getting the inputs required
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
// Input checking to make sure only integers are entered.
do {
System.out.print("How many levels do you want in your parking lot? ");
if (input.hasNextInt()) {
isNumber = true;
total_level = input.nextInt();
} else {
System.out.println("Sorry, invalid input: Please try again:");
isNumber = false;
input.next();
}
} while (isNumber == false);
do {
System.out.print("How many spots do you want in each level? ");
if (input.hasNextInt()) {
isNumber = true;
spot_level = input.nextInt();
} else {
System.out.println("Sorry, invalid input: Please try again:");
isNumber = false;
input.next();
}
} while (isNumber == false);
do {
System.out.print("How many spots do you want in each row? ");
if (input.hasNextInt()) {
isNumber = true;
spot_row = input.nextInt();
} else {
System.out.println("Sorry, invalid input: Please try again:");
isNumber = false;
input.next();
}
} while (isNumber == false);
// Setting the new inputs. If invalid input, default is used.
if (total_level >= 0 && spot_row >= 0 && spot_level >= 0) {
spot.setRow(spot_row);
spot.setLevel(spot_level);
spot.setTotalLevel(total_level);
}
spot.setLarge();
spot.setCompact();
spot.setMotorcycle();
// System.out.print("Total level: " + spot.getNumOfLevel());
// System.out.println("Total row: " + spot.getTotalLevel());
// Creating the lot
spot.createLot();
// spot.printLot();
// Asking the user what level they want to park on
while (level != -1) {
System.out.print("What level would you like to park in?[-1 to end]: ");
level = input.nextInt();
// spot.setParkLevel(level);
if (level < 0 || level > total_level) {
System.out.print("Invalid level, ending program...");
System.exit(0);
}
// Getting the type of car
System.out.println("Choose your car type: Bus: B Car: C Motorcycle: M");
System.out.print("Please enter your choice: ");
whatType = input.next();
whatType = whatType.trim();
whatType = whatType.toLowerCase();
char[][] lot = spot.getLot();
// System.out.println("Here's the ARRAY!");
// System.out.println(Arrays.deepToString(lot));
if (whatType.charAt(0) == 'b') {
Bus b = new Bus();
lot = b.parkBus(lot, level, spot_row);
}
else if (whatType.charAt(0) == 'c') {
Car c = new Car();
lot = c.parkCar(lot, level);
}
else if (whatType.charAt(0) == 'm') {
Motorcycle m = new Motorcycle();
lot = m.parkMotorcycle(lot, level);
}
spot.setLot(lot);
spot.printLot();
}
}
}
ParkingSpot,java
//Removing spot: extra point
public class ParkingSpot {
// Multiple levels, each level has multiple spots
private int NUM_OF_LEVELS = 5;
private int SPOTS_PER_LEVEL = 30;
private int SPOTS_PER_ROW = 10;
private int TOTAL_SPOT = NUM_OF_LEVELS * SPOTS_PER_LEVEL;
private int LEVEL_TO_PARK = 0;
private char[][] spots;;
public int large_spot = (int) (getTotalLevel() * 0.2);
public int compact_spot = (int) (getTotalLevel() * 0.6);
public int motorcycle_spot = (int) (getTotalLevel() * 0.2);
ParkingSpot() {
}
public void createLot() {
// System.out.print(motorcycle.getSpots());
// int motor = motorcycle.getSpots();
// int comp = compact.getSpots();
// int motor = motorcycle.motorcycle_spot;
// int comp = compact.compact_spot;
spots = new char[NUM_OF_LEVELS][SPOTS_PER_LEVEL];
int motor = motorcycle_spot;
int comp = compact_spot;
// Setting the motorcycle spots
for (int i = 0; i < NUM_OF_LEVELS; i++) {
for (int j = 0; j < motor; j++) {
spots[i][j] = 'm';
}
}
// Setting the compact car spots
for (int i1 = 0; i1 < NUM_OF_LEVELS; i1++) {
for (int j = motor; j < comp + motor; j++) {
spots[i1][j] = 'c';
}
}
// Setting large spots
for (int i2 = 0; i2 < NUM_OF_LEVELS; i2++) {
for (int j = comp + motor; j < this.SPOTS_PER_LEVEL; j++) {
spots[i2][j] = 'l';
}
}
}
public void printLot() {
for (int i = 0; i < NUM_OF_LEVELS; i++) {
for (int j = 0; j < SPOTS_PER_LEVEL; j++) {
if (j == 0) {
System.out.print("Level " + i + ": ");
}
if (j % SPOTS_PER_ROW == 0) {
System.out.print(" ");
}
System.out.print(spots[i][j]);
}
System.out.println();
}
}
public boolean isFilled(int level, Vehicle type) {
boolean isFilled = true;
// If the vehicle type can't fit on this row, return false
return isFilled;
}
public int getNumOfRow() {
return SPOTS_PER_ROW;
}
public int levelToPark() {
return LEVEL_TO_PARK;
}
public int getTotalLevel() {
return SPOTS_PER_LEVEL;
}
public int getNumOfLevel() {
return NUM_OF_LEVELS;
}
public int getTotalSpot() {
return TOTAL_SPOT;
}
public char[][] getLot() {
return spots;
}
public void setRow(int spot_row) {
SPOTS_PER_ROW = spot_row;
}
public void setTotalLevel(int total_level) {
NUM_OF_LEVELS = total_level;
}
public void setLevel(int spot_level) {
SPOTS_PER_LEVEL = spot_level;
}
public void setParkLevel(int levelToPark) {
LEVEL_TO_PARK = levelToPark;
}
public void setLot(char[][] lot) {
// spots = lot;
for (int i = 0; i < lot.length; i++) {
for (int j = 0; j < lot[i].length; j++) {
spots[i][j] = lot[i][j];
}
}
}
public void setLarge() {
large_spot = (int) (SPOTS_PER_LEVEL * 0.2);
}
public void setCompact() {
compact_spot = (int) (SPOTS_PER_LEVEL * 0.6);
}
public void setMotorcycle() {
motorcycle_spot = (int) (SPOTS_PER_LEVEL * 0.2);
}
public int getLarge() {
return large_spot;
}
}
Bus.java
public class Bus extends Vehicle {
private char name = 'B';
private int numOfBus = 0;
private int spotsNeeded = 5;
boolean filled = true;
ParkingSpot s = new ParkingSpot();
// Every time a bus is added, numOfBus is incremented.
Bus() {
super(); // Calls the super class constructor each time so numOfVehicles
// can be incremented.
numOfBus++;
}
// Getter variable to return the number of bus.
public int getNumOfBus() {
return numOfBus;
}
public char[][] parkBus(char[][] lot, int level, int row) {
// char[][] lot= new char[s.getTotalLevel()][s.getTotalSpot()];
// System.out.print("Total Spot: " + s.getTotalLevel());
for (int i = 0; i < lot[0].length; i++) {
// System.out.print("Total Spot: " + s.getTotalLevel());
if (i + 5 <= lot[0].length) { // This <=
// if ((lot[0].length*.2)
if (row >= 5) {
if (lot[level][i] == 'l' && lot[level][i + 1] == 'l' && lot[level][i + 2] == 'l'
&& lot[level][i + 3] == 'l' && lot[level][i + 4] == 'l') {
filled = false;
lot[level][i] = name;
lot[level][i + 1] = name;
lot[level][i + 2] = name;
lot[level][i + 3] = name;
lot[level][i + 4] = name;
return lot;
}
}
}
// System.out.println("Here's the ARRAY!");
// System.out.println(Arrays.deepToString(lot));
}
// s.setLot(lot);
if (filled == true) {
System.out.println("Sorry!, There are no spaces available!");
return lot;
}
return lot;
}
// Removing bus
public char[][] removeBus(char[][] lot, int level) {
for (int i = 0; i < lot[0].length; i++) {
if (lot[level][i] == name && lot[level][i + 1] == name && lot[level][i + 2] == name
&& lot[level][i + 3] == name && lot[level][i + 4] == name) {
filled = true;
lot[level][i] = 'l';
lot[level][i + 1] = 'l';
lot[level][i + 2] = 'l';
lot[level][i + 3] = 'l';
lot[level][i + 4] = 'l';
return lot;
}
}
filled = false;
if (filled == false) {
System.out.println("Sorry! There are no buses to remove.");
}
return lot;
}
}
Car.java
//60% per level
public class Car extends Vehicle {
private char name = 'C';
private int numOfCar = 0;
private int spotsNeeded = 1;
boolean filled = true;
ParkingSpot s = new ParkingSpot();
// Every time a car is added, numOfCar is incremented.
Car() {
super(); // Calls the super class constructor each time so numOfVehicles
// can be incremented.
numOfCar++;
}
// Getter variable to return the number of car.
public int getNumOfCar() {
return numOfCar;
}
public char[][] parkCar(char[][] lot, int level) {
// char[][] lot= new char[s.getTotalLevel()][s.getTotalSpot()];
for (int i = 0; i < lot[0].length; i++) {
if (lot[level][i] == 'c' || lot[level][i] == 'l') {
filled = false;
lot[level][i] = name;
return lot;
}
}
// s.setLot(lot);
if (filled == true) {
System.out.println("Sorry!, There are no spaces available!");
return lot;
}
return lot;
}
// Removing car
public char[][] removeCar(char[][] lot, int level) {
int large = (int) (lot[0].length * 0.2);
int comp = (int) (lot[0].length * 0.6);
int motor = (int) (lot[0].length * 0.2);
for (int i = 0; i < lot[0].length; i++) {
if (lot[level][i] == name) {
filled = true;
if (i >= motor && i < comp + motor) {
lot[level][i] = 'c';
return lot;
}
if (i >= comp + motor && i < lot.length) {
lot[level][i] = 'l';
return lot;
}
}
}
filled = false;
if (filled == false) {
System.out.println("Sorry! There are no cars to remove.");
}
return lot;
}
}
MotorCycle.java
//20% Per level
public class Motorcycle extends Vehicle {
private char name = 'M';
private int numOfMotorcycle = 0;
private int spotsNeeded = 1;
boolean filled = true;
ParkingSpot s = new ParkingSpot();
// Every time a motorcycle is added, numOfMotorcycle is incremented.
Motorcycle() {
super(); // Calls the super class constructor each time so numOfVehicles
// can be incremented.
numOfMotorcycle++;
}
// Getter variable to return the number of motorcycles.
public int getNumOfMotorcycle() {
return numOfMotorcycle;
}
public char[][] parkMotorcycle(char[][] lot, int level) {
// char[][] lot= new char[s.getTotalLevel()][s.getTotalSpot()];
for (int i = 0; i < lot[0].length; i++) {
if (lot[level][i] == 'm' || lot[level][i] == 'c' || lot[level][i] == 'l') {
filled = false;
lot[level][i] = name;
return lot;
}
// We need to return the lot and get main function to set it.
}
// s.setLot(lot);
if (filled == true) {
System.out.println("Sorry!, There are no spaces available!");
return lot;
}
return lot;
}
// Removing motorcycle
public char[][] removeMotorcycle(char[][] lot, int level) {
int large = (int) (lot[0].length * 0.2);
int comp = (int) (lot[0].length * 0.6);
int motor = (int) (lot[0].length * 0.2);
for (int i = 0; i < lot[0].length; i++) {
if (lot[level][i] == name) {
filled = true;
if (i < motor) {
lot[level][i] = 'm';
return lot;
}
if (i >= motor && i < comp + motor) {
lot[level][i] = 'c';
return lot;
}
if (i >= comp + motor && i < lot.length) {
lot[level][i] = 'l';
return lot;
}
}
}
filled = false;
if (filled == false) {
System.out.println("Sorry! There are no motorcycles to remove.");
}
return lot;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.