Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this java program. Read from a file the design and the occupancy information of a parking lot, and perform some basic

Please help me with this java program.

Read from a file the design and the occupancy information of a parking lot, and perform some basic operations, for example, parking a car at a certain spot in the lot. What you need to do in this assignment is illustrated with an example. Suppose we have a file named parking.inf. S, S, S, S, N

R, R, L, L, E

R, R, L, L, E

S, S, S, S, N

###

0, 1, S, ABC

1, 2, L, ABD

3, 3, S, ABX

Structure of the input file

First part specifies the lot design. The rows in the lot design are separated by new lines. The spots in an individual row are separated by commas (,)

### separates the lot design (above) from the occupancy information (below)

Second part is the occupancy information. Each row represents one Car instance and is formatted as follows: row index, spot-within-row index, car type, plate number For example, the first row (0, 1, S, ABC) means that a small (S) car with plate number ABC is parked at position (0,1), i.e., at occupancy[0][1]

Rules for parking:

No car can be parked at a spot that is designated NA in the lot design;

A car cannot be parked at an out-of-bound position;

At most one car can be parked at any given (non-NA) spot;

An electric car is allowed to park at any (non-NA) spot: ELECTRIC, SMALL, REGULAR, LARGE;

A small car is allowed to park only at the following spot types: SMALL, REGULAR, LARGE;

A regular car is allowed to park only at the following spot types: REGULAR, LARGE;

A large car is allowed to park only at the following spot type: LARGE; Implementation: Car(program that need to fill in):

public class Car { private CarType type; private String plateNum; public CarType getType() { // WRITE YOUR CODE HERE! return null; // REMOVE THIS STATEMENT AFTER IMPLEMENTING THIS METHOD } public void setType(CarType type) { // WRITE YOUR CODE HERE! } public String getPlateNum() { // WRITE YOUR CODE HERE! return null; // REMOVE THIS STATEMENT AFTER IMPLEMENTING THIS METHOD } public void setPlateNum(String plateNum) { // WRITE YOUR CODE HERE! } public Car(CarType type, String plateNum) { // WRITE YOUR CODE HERE! } public String toString() { // NOTE: The implementation of this method is complete. You do NOT need to // change it for the assignment. return Util.getLabelByCarType(type) + '(' + plateNum + ')'; } }

Parking lot(program that need to fill in):

import java.io.File; import java.util.Scanner;

public class ParkingLot { private static final String SEPARATOR = ","; private static final String SECTIONER = "###"; private int numRows; private int numSpotsPerRow; private CarType[][] lotDesign; private Car[][] occupancy; public ParkingLot(String strFilename) throws Exception {

if (strFilename == null) { System.out.println("File name cannot be null."); return; }

// determine numRows and numSpotsPerRow; you can do so by // writing your own code or alternatively completing the // private calculateLotDimensions(...) that I have provided calculateLotDimensions(strFilename);

// instantiate the lotDesign and occupancy variables! // WRITE YOUR CODE HERE!

// populate lotDesign and occupancy; you can do so by // writing your own code or alternatively completing the // private populateFromFile(...) that I have provided populateFromFile(strFilename); } public void park(int i, int j, Car c) { // WRITE YOUR CODE HERE! } public Car remove(int i, int j) { // WRITE YOUR CODE HERE! return null; // REMOVE THIS STATEMENT AFTER IMPLEMENTING THIS METHOD

} public boolean canParkAt(int i, int j, Car c) { // WRITE YOUR CODE HERE! return false; // REMOVE THIS STATEMENT AFTER IMPLEMENTING THIS METHOD

} public int getTotalCapacity() { // WRITE YOUR CODE HERE! return -1; // REMOVE THIS STATEMENT AFTER IMPLEMENTING THIS METHOD

} public int getTotalOccupancy() { // WRITE YOUR CODE HERE! return -1; // REMOVE THIS STATEMENT AFTER IMPLEMENTING THIS METHOD }

private void calculateLotDimensions(String strFilename) throws Exception {

Scanner scanner = new Scanner(new File(strFilename));

while (scanner.hasNext()) { String str = scanner.nextLine(); // WRITE YOUR CODE HERE! }

scanner.close(); }

private void populateFromFile(String strFilename) throws Exception {

Scanner scanner = new Scanner(new File(strFilename));

// YOU MAY NEED TO DEFINE SOME LOCAL VARIABLES HERE!

// while loop for reading the lot design while (scanner.hasNext()) { String str = scanner.nextLine(); // WRITE YOUR CODE HERE! }

// while loop for reading occupancy data while (scanner.hasNext()) { String str = scanner.nextLine(); // WRITE YOUR CODE HERE! }

scanner.close(); } public String toString() { // NOTE: The implementation of this method is complete. StringBuffer buffer = new StringBuffer(); buffer.append("==== Lot Design ====").append(System.lineSeparator());

for (int i = 0; i < lotDesign.length; i++) { for (int j = 0; j < lotDesign[0].length; j++) { buffer.append((lotDesign[i][j] != null) ? Util.getLabelByCarType(lotDesign[i][j]) : Util.getLabelByCarType(CarType.NA)); if (j < numSpotsPerRow - 1) { buffer.append(", "); } } buffer.append(System.lineSeparator()); }

buffer.append(System.lineSeparator()).append("==== Parking Occupancy ====").append(System.lineSeparator());

for (int i = 0; i < occupancy.length; i++) { for (int j = 0; j < occupancy[0].length; j++) { buffer.append( "(" + i + ", " + j + "): " + ((occupancy[i][j] != null) ? occupancy[i][j] : "Unoccupied")); buffer.append(System.lineSeparator()); }

} return buffer.toString(); }

public static void main(String args[]) throws Exception {

StudentInfo.display();

System.out.print("Please enter the name of the file to process: ");

Scanner scanner = new Scanner(System.in);

String strFilename = scanner.nextLine();

ParkingLot lot = new ParkingLot(strFilename);

System.out.println("Total number of parkable spots (capacity): " + lot.getTotalCapacity());

System.out.println("Number of cars currently parked in the lot: " + lot.getTotalOccupancy());

System.out.print(lot);

} }

Util(do not need to change):

public class Util {

public static CarType getCarTypeByLabel(String label) {

if (label.equals("E")) return CarType.ELECTRIC;

else if (label.equals("S")) return CarType.SMALL;

else if (label.equals("R")) return CarType.REGULAR;

else if (label.equals("L")) return CarType.LARGE;

else return CarType.NA; }

public static String getLabelByCarType(CarType type) {

if (type == CarType.ELECTRIC) return "E";

else if (type == CarType.SMALL) return "S";

else if (type == CarType.REGULAR) return "R";

else if (type == CarType.LARGE) return "L";

else return "N"; } }

CarType(do not need to change):

public enum CarType { ELECTRIC, SMALL, REGULAR, LARGE, NA; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions