Question

How can the java code be edited that when someone exits the lot, they receive an...

How can the java code be edited that when someone exits the lot, they receive an exiting timestamp. And also i would need that exit timestamp to be subtracted from the timestamp the driver gets when entering the lot to find out the total amount of time they were in the parking lot. please explain. should only need a couple of lines added.

//////////////////////////////ParkingCarInfo.java///////////////////////////////////////////

package test;

import java.sql.Timestamp;

//Class: ParkingCarInfo
public class ParkingCarInfo {
private String name; //name
private Timestamp timestamp; //timestamp

//constructor
public ParkingCarInfo(String name){
this.name = name;
//create a new timestamp for current time
this.timestamp = new Timestamp(System.currentTimeMillis());
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Timestamp getTimestamp() {
return timestamp;
}

public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}

@Override
//toString method
public String toString() {
return "ParkingCarInfo [name=" + name + ", timestamp=" + timestamp + "]";
}
}

//////////////////////////TestParking.java///////////////////////////////////////

package test;


import java.util.LinkedList;
import java.util.Scanner;

public class TestParking {

public static void main(String[] args) {
ParkingLot pl = new ParkingLot(5);
Scanner sc = new Scanner(System.in);
System.out.println("Capacity of parking lot = " + pl.getCapacity());

  
String name;

for(int j=0;j<10;j++) {

System.out.println("\nEnter 'exit' to Exit the Parking System");
System.out.println("Attempt to park! Enter name:");
name = sc.nextLine();

if(name.compareToIgnoreCase("exit")==0) {
System.out.println("\nThe number of cars parked at this moment : " + pl.getSize());
System.out.println("Exiting the Parking System");
break;
}
LinkedList<ParkingCarInfo> info = pl.getCars();
boolean flag = true;
for (int i = 0; i < info.size(); i++) {

if (info.get(i).getName().compareTo(name) == 0) {
pl.leaveFromParking(info.get(i));
flag = false;
break;
}
}

if (flag)
pl.parkACar(name);

System.out.println("\nThe number of cars parked at this moment : " + pl.getSize());

  

}

sc.close();
}

}
//////////////////////ParkingLot.java//////////////////////////

package test.parkinglot;

import java.util.LinkedList;
//Class: ParkingLot
public class ParkingLot {
//the linked list will hold all ParkingCarInfo currently parked
private LinkedList<ParkingCarInfo> cars = null;
private int size = 0; //number of currently parked cars
private int capacity = 0; //maximum capacity of parking lot
  
//constructor
public ParkingLot(int capacity){
this.capacity = capacity;
this.cars = new LinkedList<ParkingCarInfo>();
this.size = 0;
}
  
  
//getter for cars
public LinkedList<ParkingCarInfo> getCars() {
return cars;
}


//getter for size
public int getSize() {
return size;
}


//getter for capacity
public int getCapacity() {
return capacity;
}


/**
* Park a Car method with the given name
* @param name
*/
public void parkACar(String name){
if(this.size == this.capacity){ //if max capacity is filled
System.out.println("Parking Lot is already full. The car cannot be admitted!");
}else{ //otherwise if there is space left for parking
ParkingCarInfo parkingCar = new ParkingCarInfo(name); //create a ParkingCarInfo instance
cars.add(parkingCar); //add it to cars
System.out.println("A new car is parked..");
System.out.println(parkingCar);
this.size++; //increase the size
}
  
}
  
/**
* method to handle the situation when a car leaves the parking
* @param pc
*/
public void leaveFromParking(ParkingCarInfo pc){
boolean isLeft = cars.remove(pc); //if Car with given ParkingCarInfo, leaves
if(isLeft){ //if successful
this.size--;//cecrease the size
System.out.println("The car having "+pc+" has left the parking lot.");
}else{
System.out.println("Sorry! No Parking info for the car is found.");
}
  
}
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code in bold are the lines which are added

Code:

package test;

import java.sql.Timestamp;

//Class: ParkingCarInfo
public class ParkingCarInfo {
private String name; //name
private Timestamp timestamp; //timestamp

//constructor
public ParkingCarInfo(String name){
this.name = name;
//create a new timestamp for current time
this.timestamp = new Timestamp(System.currentTimeMillis());
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Timestamp getTimestamp() {
return timestamp;
}

public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}

@Override
//toString method
public String toString() {
return "ParkingCarInfo [name=" + name + ", timestamp=" + timestamp + "]";
}
}

//////////////////////////TestParking.java///////////////////////////////////////

package test;


import java.util.LinkedList;
import java.util.Scanner;

public class TestParking {

public static void main(String[] args) {
ParkingLot pl = new ParkingLot(5);
Scanner sc = new Scanner(System.in);
System.out.println("Capacity of parking lot = " + pl.getCapacity());

  
String name;

for(int j=0;j<10;j++) {

System.out.println("\nEnter 'exit' to Exit the Parking System");
System.out.println("Attempt to park! Enter name:");
name = sc.nextLine();

if(name.compareToIgnoreCase("exit")==0) {
System.out.println("\nThe number of cars parked at this moment : " + pl.getSize());
System.out.println("Exiting the Parking System");
break;
}
LinkedList<ParkingCarInfo> info = pl.getCars();
boolean flag = true;
for (int i = 0; i < info.size(); i++) {

if (info.get(i).getName().compareTo(name) == 0) {
pl.leaveFromParking(info.get(i));
flag = false;
break;
}
}

if (flag)
pl.parkACar(name);

System.out.println("\nThe number of cars parked at this moment : " + pl.getSize());

  

}

sc.close();
}

}
//////////////////////ParkingLot.java//////////////////////////

package test.parkinglot;

import java.util.LinkedList;
//Class: ParkingLot
public class ParkingLot {
//the linked list will hold all ParkingCarInfo currently parked
private LinkedList<ParkingCarInfo> cars = null;
private int size = 0; //number of currently parked cars
private int capacity = 0; //maximum capacity of parking lot

  
//constructor
public ParkingLot(int capacity){
this.capacity = capacity;
this.cars = new LinkedList<ParkingCarInfo>();
this.size = 0;
}
  
  
//getter for cars
public LinkedList<ParkingCarInfo> getCars() {
return cars;
}


//getter for size
public int getSize() {
return size;
}


//getter for capacity
public int getCapacity() {
return capacity;
}


/**
* Park a Car method with the given name
* @param name
*/
public void parkACar(String name){
if(this.size == this.capacity){ //if max capacity is filled
System.out.println("Parking Lot is already full. The car cannot be admitted!");
}else{ //otherwise if there is space left for parking
ParkingCarInfo parkingCar = new ParkingCarInfo(name); //create a ParkingCarInfo instance
cars.add(parkingCar); //add it to cars
System.out.println("A new car is parked..");
System.out.println(parkingCar);
this.size++; //increase the size
}
  
}
  
/**
* method to handle the situation when a car leaves the parking
* @param pc
*/
public void leaveFromParking(ParkingCarInfo pc){
boolean isLeft = cars.remove(pc); //if Car with given ParkingCarInfo, leaves
//Calculate the elapsed time in the parking lot
long timeDiff = System.currentTimeMillis() - pc.getTimestamp().getTime();

if(isLeft){ //if successful
this.size--;//cecrease the size
System.out.println("The car having "+pc+" has left the parking lot. total time in parking lot is "+timeDiff+" milliseconds");
}else{
System.out.println("Sorry! No Parking info for the car is found.");
}
  
}
  
}

Add a comment
Know the answer?
Add Answer to:
How can the java code be edited that when someone exits the lot, they receive an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I would like someone to check my code and help with my for loop to print...

    I would like someone to check my code and help with my for loop to print the recipe. It is incorrect. package SteppingStones; import java.util.Scanner; //Scanner class// import java.util.ArrayList; //ArrayList// import ingredients.Ingredient; /**Gets from package ingredients and class Ingredient * * @author kimbe */ public class SteppingStone5_Recipe {    //Instance Variables// private ArrayList recipeIngredients; private String recipeName; private int servings; private double totalRecipeCalories;                   //Setter and Getters//    public ArrayList getrecipeIngredients() { return recipeIngredients; }...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • I need a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public...

    I need a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public class ItemToPurchase {    // instance variables    private String itemName;    private String itemDescription;    private int itemPrice;    private int itemQuantity;    // default constructor    public ItemToPurchase() {        this.itemName = "none";        this.itemDescription = "none";        this.itemPrice = 0;        this.itemQuantity = 0;    }    public ItemToPurchase(String itemName, int itemPrice, int itemQuantity,String itemDescription) {   ...

  • Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies...

    Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies a utility method to reverse the entries in a linked list. Then, test your code using the tester class, Reverse Tester.java, given below. ListUtil.java import java.util.LinkedList; /** This class supplies a utility method to reverse the entries in a linked list. */ public class ListUtil { /** Reverses the elements in a linked list @param strings the linked list to reverse public static void...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) {...

    import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • Take the following code and add a joption pane to make it eaiser to read. Can...

    Take the following code and add a joption pane to make it eaiser to read. Can anyone tell me whats wrong with my code? It wont compile JAVA. import java.util.Scanner; import java.util.Queue; import java.util.LinkedList; class que { public static void main(String args[]) { int count=0,min=0,hr=0; String a; Scanner inp=new Scanner(System.in); Queue q=new LinkedList<>(); while(count<10) { System.out.println("Enter your name:"); a=inp.nextLine(); q.add(a); count=count+1; min=min+5; if(min>=60) { hr=hr+1; } System.out.println("You are "+count+" in the queue"); System.out.println("Average wait time is "+hr+" hours and "+min+"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT