============cars.java=========================================== public class Ca
ID: 3628315 • Letter: #
Question
============cars.java===========================================public class Cars {
private String license; // license plate
private int numOfMoves; //number of moves
private String decide; // depart or arrival
/**
*
* @param license
* @param decide
*/
public Cars(String license, String decide)
{
this.license = license;
this.decide = decide;
}
/**
*
* @param license
*/
public Cars(String license)
{
this.license = license;
}
/**
*
* @return number of moves
*/
public int getMoves()
{
return numOfMoves;
}
/**
*
* keep count of moves out of the garage
*/
/**
*
* @return the decision either ARRIVE or DEPART
*/
public String getD()
{
return decide;
}
/**
*
* @return license plate
*/
public String getL()
{
return license;
}
}
===================Garages.java==========================
import java.util.ArrayList;
import java.util.List;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Truegamer4
*/
public class Garages {
private List<Cars> parkingSpaces;
private int laneSpace = 10;
public Garages() {
parkingSpaces = new ArrayList<Cars>(laneSpace);
}
public void addC(Cars a) {
parkingSpaces.add(a);
}
public void printL() {
double addM = 0;
for (int i = 0; i < parkingSpaces.size(); i++) {
Cars vechicle = parkingSpaces.get(i);
boolean arrival = vechicle.getD().equals("ARRIVE");
// boolean depart = vechicle.getD().equals("DEPART");
if (arrival) {
System.out.println(vechicle.getL() + " " + vechicle.getD() + " "
+ ": Car is parked");
} else {
System.out.println(vechicle.getL() + " " + vechicle.getD() + " "
+ ": Departs after having moved "+vechicle.getMoves()+
" times");
}
}
}
========================CarGarageTesters.java=======================
import java.io.File;
import java.util.Scanner;
public class CarGarageTesters {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
Garages newG = new Garages();
Scanner Garages = new Scanner(new File("Garage.txt"));
while (Garages.hasNext())
{
String lie = Garages.next();
String d = Garages.next();
Cars next = new Cars(lie, d);
newG.addC(next);
}
newG.printL();
System.out.println();
}
}
============================garage.txt==============================
JAV001 ARRIVE
JAV002 ARRIVE
JAV003 ARRIVE
JAV004 ARRIVE
JAV005 ARRIVE
JAV001 DEPART
JAV004 DEPART
JAV006 ARRIVE
JAV007 ARRIVE
JAV008 ARRIVE
JAV009 ARRIVE
JAV010 ARRIVE
JAV011 ARRIVE
JAV012 ARRIVE
JAV013 ARRIVE
JAV014 ARRIVE
JAV006 DEPART
JAV014 DEPART
JAV013 DEPART
JAV005 DEPART
JAV015 ARRIVE
JAV010 DEPART
JAV002 DEPART
JAV015 DEPART
JAV013 DEPART
JAV009 DEPART
JAV003 DEPART
JAV008 DEPART
JAV007 DEPART
JAV012 DEPART
JAV011 DEPART
===============================output i am getting==================
JAV001 ARRIVE : Car is parked
JAV002 ARRIVE : Car is parked
JAV003 ARRIVE : Car is parked
JAV004 ARRIVE : Car is parked
JAV005 ARRIVE : Car is parked
JAV001 DEPART : Departs after having moved 0 times
JAV004 DEPART : Departs after having moved 0 times
JAV006 ARRIVE : Car is parked
JAV007 ARRIVE : Car is parked
JAV008 ARRIVE : Car is parked
JAV009 ARRIVE : Car is parked
JAV010 ARRIVE : Car is parked
JAV011 ARRIVE : Car is parked
JAV012 ARRIVE : Car is parked
JAV013 ARRIVE : Car is parked
JAV014 ARRIVE : Car is parked
JAV006 DEPART : Departs after having moved 0 times
JAV014 DEPART : Departs after having moved 0 times
JAV013 DEPART : Departs after having moved 0 times
JAV005 DEPART : Departs after having moved 0 times
JAV015 ARRIVE : Car is parked
JAV010 DEPART : Departs after having moved 0 times
JAV002 DEPART : Departs after having moved 0 times
JAV015 DEPART : Departs after having moved 0 times
JAV013 DEPART : Departs after having moved 0 times
JAV009 DEPART : Departs after having moved 0 times
JAV003 DEPART : Departs after having moved 0 times
JAV008 DEPART : Departs after having moved 0 times
JAV007 DEPART : Departs after having moved 0 times
JAV012 DEPART : Departs after having moved 0 times
JAV011 DEPART : Departs after having moved 0 times
=============================output im suppose to get===============
JAV001 A: parked
JAV002 A: parked
JAV003 A: parked
JAV004 A: parked
JAV005 A: parked
JAV001 D: departs after having moved 0 time(s).
JAV004 D: departs after having moved 0 time(s).
JAV006 A: parked
JAV007 A: parked
JAV008 A: parked
JAV009 A: parked
JAV010 A: parked
JAV011 A: parked
JAV012 A: parked
JAV013 A: turned away
JAV014 A: turned away
JAV006 D: departs after having moved 0 time(s).
JAV014 D: not in the garage
JAV013 D: not in the garage
JAV005 D: departs after having moved 1 time(s).
JAV015 A: parked
JAV010 D: departs after having moved 0 time(s).
JAV002 D: departs after having moved 4 time(s).
JAV015 D: departs after having moved 0 time(s).
JAV013 D: not in the garage
JAV009 D: departs after having moved 2 time(s).
JAV003 D: departs after having moved 6 time(s).
JAV008 D: departs after having moved 3 time(s).
JAV007 D: departs after having moved 4 time(s).
JAV012 D: departs after having moved 1 time(s).
JAV011 D: departs after having moved 2 time(s).
some conditions. this is a single lane car garage that fits up to 10 cars.
when cars try to arrival when its full it gets turn away. if a car try to leave
and its not in the garage, it cant be found. when its the car turn to depart from
the garage needs to produces how many times it was moved while it was still in the garage.
i just need to work on the void printL() to produce those statements. thanks for anyhelp.
Explanation / Answer
You very well could perform all the logic within the printL method. However, that'd defeat the purpose of the design. I suggest to create an arrive and depart method on the garage class or to do more processing in the addC method. You want to use the list to model the parking spaces with it's finite space and it's ordering. You'll want add to the end of the list for arrivals if and only if the size of the list is 10 or less. Otherwise, you'll want to turn the car away. For departures, you'll want to find the car in the list by license. You'll want to remove it from the list (if present) and increment the move count of all car objects ahead of it in the list. When you report the departure of a car, report the car's move count. If you do this, you can put the print statements in the arrive/depart or addC method and you might not need a print method at all. Beware that the ArrayList will expand to fit whatever size necessary. Initializing it to 10 will not cause it to prevent 11 or more entries in the list. Refactor the Garage class and perhaps the tester class, and repost if you get stuck.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.