A. potential classes and their objects
Answer:
A. Potential classes and their objects
Attendant class
This class consists of the accessor and mutator objects for an attendant. An attendant will have an attendant ID, name and availability status thus each of these attributes will have an accessor and mutator object.
Space class
A zone is made up of many spaces. Each space is identified by its space ID and its availability. The space class will have contain the accessor and mutator methods for each of the attribute identifying the space class.
MCP class
This class will contain the main method and will be used to instantiate different classes and methods for the program to run. All the objects will be created using this class. It will also contain a menu class which will help the user to interact with the program.
B. Use case diagram
2 Class diagram
3 Test table
4 outline classes
Attendant
DisabledDriver
import io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Loader {
public Loader(){}
private String [] readFile(String filename) throws IOException {
BufferedReader br=null;
try{
br=new BufferedReader(new FileReader(filename));
StringBuilder sb=new StringBuilder();
String line=br.readLine();
while(line!=null){
sb.append(line);
sb.append("n");
line=br.readLine();
}
String everything=sb.toString();
String [] blocks=everything.split("n");
return blocks;
}catch (Exception e){
e.printStackTrace();
return null;
}finally{
br.close();
}
}
public ArrayList populateData(String filename) throws IOException{
ArrayList data=new ArrayList<>();
String arr[]=readFile(filename);
for (int i=0;i<arr.length;i++){
String[] details=arr[i].split("~");
ArrayList datum=new ArrayList();
for (int j=0;j<details.length;j++){
datum.add(details[j]);
}
data.add(datum);
}
return data;
}
}
Space
public class Space {
private int spaceID;
private boolean avaialability;
public Space(){}
public void setSpaceID(int spaceNO){
this.spaceID=spaceNO;
}
public int getSpaceID(){
return this.spaceID;
}
public void setAvaialability(boolean status){
this.avaialability=status;
}
public boolean getAvailability(){
return this.avaialability;
}
public String toString(){
return this.spaceID+" : "+this.avaialability;
}
}
MCP
import io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class MCP {
private ArrayList vehicleData=new ArrayList<>();
private ArrayList zoneData=new ArrayList<>();
private ArrayList attendantData=new ArrayList<>();
private ArrayList receipts=new ArrayList<>();
public MCP(){
}
public static void main(String[] args) throws IOException {
System.out.println("Welcome to MCP parking");
MCP mcp=new MCP();
mcp.startup();
mcp.menu();
}
public void menu(){
System.out.println("*********************MENU*************");
Scanner s=new Scanner(System.in);
System.out.println("1 Find Parking");
System.out.println("2 Exit parking");
try {
int option = s.nextInt();
if(option==1){
findParking();
}else if(option==2){
exitParking();
}else{
System.out.println("Invalid option");
menu();
}
}catch(NumberFormatException e){
System.out.println("Use numbers only specified in the menu only");
}
}
public void findParking(){
System.out.println("Do you want to be assisted by an attendant? (Y=yes, N=No");
Scanner s=new Scanner(System.in);
String option=s.next();
if(option.equalsIgnoreCase("y")){
Random random=new Random();
int randomNumber = random.nextInt(attendantData.size() - 1) + 1;
Attendant attendant=attendantData.get(randomNumber);
parkVehicle(attendant);
}else if(option.equalsIgnoreCase("n")){
//no attendant
parkVehicle();
}else{
System.out.println("Invalid option");
findParking();
}
}
public void parkVehicle(){
Scanner s=new Scanner(System.in);
System.out.println("Enter the license plate of the vehicle");
String licensePlate=s.next();
System.out.println("Do you have any disability? (Y=Yes, N=No");
String option=s.next();
boolean opt=false;
if(option.equalsIgnoreCase("y")){
opt=true;
}else if(option.equalsIgnoreCase("n")){
opt=false;
}else{
System.out.println("Invalid option");
System.out.println("Do you have any disability? (Y=Yes, N=No");
option=s.next();
if(option.equalsIgnoreCase("y")){
opt=true;
}else if(option.equalsIgnoreCase("n")){
opt=false;
}
}
DisabledDriver driver=new DisabledDriver();
driver.setlicensePlate(licensePlate);
driver.setStatus(opt);
System.out.println("Select you vehicle type");
Vehicle driverVehicle=null;
for(int i=0;i<vehicleData.size();i++){
System.out.println(i+" "+vehicleData.get(i).getType());
}
int selectedVehicle;
try {
selectedVehicle = s.nextInt();
if (selectedVehicle>=0 && selectedVehicle<=vehicleData.size()){
driverVehicle=vehicleData.get(selectedVehicle);
}else{
System.out.println("Invalid option Select vehicle");
for(int i=0;i<vehicleData.size();i++){
System.out.println(i+" "+vehicleData.get(i).getType());
}
selectedVehicle = s.nextInt();
if (selectedVehicle>=0 && selectedVehicle<=vehicleData.size()){
driverVehicle=vehicleData.get(selectedVehicle);
}else{
System.out.println("Invalid option Select vehicle");
for(int i=0;i<vehicleData.size();i++){
System.out.println(i+" "+vehicleData.get(i).getType());
}
}
}
}catch (NumberFormatException e){
System.out.println("Use numbers only");
}
showParkingLots();
//show parking lots
System.out.println("Select parking lot option");
Zone customerZone=null;
Space customerSpace=null;
try{
int selectedZone=s.nextInt();
if(selectedZone==0){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if(selectedZone==1){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if(selectedZone==2){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if (selectedZone==3){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if(selectedZone==4){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if (selectedZone==5){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else{
System.out.println("Invalid option, select zone using the specified zoneID");
}
}catch(NumberFormatException e){
System.out.println("Only numbers are allowed");
}
Receipt receipt=new Receipt();
Random rand = new Random();
int receiptNO = rand.nextInt(500000) + 1;
receipt.setReceiptNO(String.valueOf(receiptNO));
receipt.setSpace(customerSpace);
receipt.setZone(customerZone);
receipt.setArrivalTime(String.valueOf(System.currentTimeMillis()));
receipt.setDriver(driver);
receipt.setUserVehicle(driverVehicle);
receipt.setAttendant(null);
receipts.add(receipt);
receipt.displayReceipt();
menu();
}
public void parkVehicle(Attendant attendant){
Scanner s=new Scanner(System.in);
System.out.println("Enter the license plate of the vehicle");
String licensePlate=s.next();
System.out.println("Do you have any disability? (Y=Yes, N=No");
String option=s.next();
boolean opt=false;
if(option.equalsIgnoreCase("y")){
opt=true;
}else if(option.equalsIgnoreCase("n")){
opt=false;
}else{
System.out.println("Invalid option");
System.out.println("Do you have any disability? (Y=Yes, N=No");
option=s.next();
if(option.equalsIgnoreCase("y")){
opt=true;
}else if(option.equalsIgnoreCase("n")){
opt=false;
}
}
DisabledDriver driver=new DisabledDriver();
driver.setlicensePlate(licensePlate);
driver.setStatus(opt);
System.out.println("Select you vehicle type");
Vehicle driverVehicle=null;
for(int i=0;i<vehicleData.size();i++){
System.out.println(i+" "+vehicleData.get(i).getType());
}
int selectedVehicle;
try {
selectedVehicle = s.nextInt();
if (selectedVehicle>=0 && selectedVehicle<=vehicleData.size()){
driverVehicle=vehicleData.get(selectedVehicle);
}else{
System.out.println("Invalid option Select vehicle");
for(int i=0;i<vehicleData.size();i++){
System.out.println(i+" "+vehicleData.get(i).getType());
}
selectedVehicle = s.nextInt();
if (selectedVehicle>=0 && selectedVehicle<=vehicleData.size()){
driverVehicle=vehicleData.get(selectedVehicle);
}else{
System.out.println("Invalid option Select vehicle");
for(int i=0;i<vehicleData.size();i++){
System.out.println(i+" "+vehicleData.get(i).getType());
}
}
}
}catch (NumberFormatException e){
System.out.println("Use numbers only");
}
showParkingLots();
//show parking lots
System.out.println("Select parking lot option");
Zone customerZone=null;
Space customerSpace=null;
try{
int selectedZone=s.nextInt();
if(selectedZone==0){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if(selectedZone==1){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if(selectedZone==2){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if (selectedZone==3){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if(selectedZone==4){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else if (selectedZone==5){
customerZone=zoneData.get(selectedZone);
customerSpace=findEmptyParkingSpace(customerZone);
}else{
System.out.println("Invalid option, select zone using the specified zoneID");
}
}catch(NumberFormatException e){
System.out.println("Only numbers are allowed");
}
Receipt receipt=new Receipt();
Random rand = new Random();
int receiptNO = rand.nextInt(500000) + 1;
receipt.setReceiptNO(String.valueOf(receiptNO));
receipt.setSpace(customerSpace);
receipt.setZone(customerZone);
receipt.setArrivalTime(String.valueOf(System.currentTimeMillis()));
receipt.setDriver(driver);
receipt.setUserVehicle(driverVehicle);
receipt.setAttendant(attendant);
receipts.add(receipt);
receipt.displayReceipt();
attendantData.remove(attendant);
menu();
}
public Space findEmptyParkingSpace(Zone zone){
Space value=null;
for(int i=0;i<50;i++){
Space space= (Space) zone.getSpaces().get(i);
if(space.getAvailability()==true){
value=space;
space.setAvaialability(false);
break;
}
}
zone.getSpaces().remove(value);
zone.getSpaces().add(value);
zoneData.remove(zone);
zoneData.add(zone);
return value;
}
public void showParkingLots(){
for (int i=0;i<zoneData.size();i++){
System.out.println("Option: "+i);
System.out.println("Zone ID: "+zoneData.get(i).getZoneID()+" "+zoneData.get(i).getType());
System.out.println("Type of vehicles accepted: ");
for(int k=0;k<zoneData.get(i).getVehicles().size();k++){
Vehicle vehicle= (Vehicle) zoneData.get(i).getVehicles().get(k);
System.out.append(vehicle.getType()+" n");
}
System.out.println("Spaces (True means the space is available and false means its not)");
for(int j=0;j<zoneData.get(i).getSpaces().size();j++){
if(j%10==0){
System.out.println();
}
System.out.print("t "+zoneData.get(i).getSpaces().get(j).toString());
}
System.out.println("n");
}
}
public void exitParking(){
System.out.println("Enter your receiptNO (Token)");
Scanner s=new Scanner(System.in);
String token=s.next();
Receipt receipt=findReceipt(token);
if(receipt==null){
System.out.println("invaid receipt no or token..Refer to your receipt");
menu();
}else{
receipt.seExitTime(String.valueOf(System.currentTimeMillis()));
long timeDiff=Long.parseLong(receipt.getExitTime())-Long.parseLong(receipt.getArrivalTime());
long diffInSeconds=timeDiff/1000;
Double amount= Double.valueOf(0);
if(diffInSeconds%3600==0){
amount=(diffInSeconds/3600)*getRate(receipt.getUserVehicle());
}else{
amount=(diffInSeconds/3600)*getRate(receipt.getUserVehicle())+1*getRate(receipt.getUserVehicle());
}
receipt.setAmount(amount);
System.out.println("Charges for your parking ="+receipt.getAmount() );
System.out.println("Enter your payment (should be the exact as specified)");
Double userAmount;
try {
userAmount=s.nextDouble();
if(userAmountamount){
System.out.println("Incorrect amount");
System.out.println("Charges for your parking ="+receipt.getAmount() );
System.out.println("Enter your payment (should be the exact as specified)");
userAmount=s.nextDouble();
if(userAmountamount){
System.out.println("Incorrect amount");
System.out.println("Charges for your parking ="+receipt.getAmount() );
System.out.println("Enter your payment (should be the exact as specified)");
userAmount=s.nextDouble();
}else{
System.out.println("Payment made");
}
}else{
System.out.println("Payment made");
}
}catch(Exception e){
System.out.println("Use numbers only");
}
receipt.displayReceipt();
System.out.println("Exit parking within the next fifteen minutes");
System.out.println("Press 1 to raise barrier");
String exitOption=s.next();
if(exitOption.equalsIgnoreCase("1")){
long currentTime=System.currentTimeMillis();
long diff=(currentTime-Long.parseLong(receipt.getExitTime()))/1000;
if(diff>15*60){
System.out.println("You have exceeded 15 minutes from the time you paid..Seek assistance from an attendant");
}else{
System.out.println("Raising barrier..Bye");
}
}
menu();
}
}
public Double getRate(Vehicle vehicle){
Double rate=0.0;
for(int i=0;i<vehicleData.size();i++){
if(vehicleData.get(i)==vehicle){
rate=vehicleData.get(i).getRate();
}
}
return rate;
}
public Receipt findReceipt(String receiptNO){
Receipt receipt=null;
for(int i=0;i<receipts.size();i++){
if(receipts.get(i).getReceiptNO().equalsIgnoreCase(receiptNO)){
receipt=receipts.get(i);
}
}
return receipt;
}
public void startup() throws IOException {
Loader loader=new Loader();
//loading the vehicle types
ArrayList vehicles=loader.populateData("./src/vehicles.txt");
for (int i=0;i<vehicles.size();i++){
Vehicle vehicle=new Vehicle();
vehicle.setVehicleID(Integer.parseInt(vehicles.get(i).get(0).toString()));
vehicle.setType(vehicles.get(i).get(1).toString());
vehicle.setRate(Double.parseDouble(vehicles.get(i).get(2).toString()));
vehicleData.add(vehicle);
}
//loading the zone types
ArrayList zones=loader.populateData("./src/zones.txt");
for (int i=0;i<zones.size();i++){
Zone zone=new Zone();
zone.setZoneID(Integer.parseInt(zones.get(i).get(0).toString()));
zone.setType(zones.get(i).get(1).toString());
String[] cars=zones.get(i).get(2).toString().split(",");
for (int k=0;k<cars.length;k++){
for(int l=0;l<vehicleData.size();l++){
if(Integer.toString(vehicleData.get(l).getVehicleID()).equals(cars[k])){
zone.setVehicles(vehicleData.get(l));
}
}
}
for(int l=0;l<50;l++){
Space space=new Space();
space.setSpaceID(l);
space.setAvaialability(true);
zone.setSpaces(space);
}
zoneData.add(zone);
}
//
//loading space
ArrayList attendants=loader.populateData("./src/attendant.txt");
for (int i=0;i<attendants.size();i++){
Attendant attendant=new Attendant();
attendant.setAttendantID(Integer.parseInt(attendants.get(i).get(0).toString()));
attendant.setName(attendants.get(i).get(1).toString());
attendant.setAvailability(true);
attendantData.add(attendant);
}
}
}