Need help writing GarageExitBag class...Provided is the UML and GarageExitBag re
ID: 3599121 • Letter: N
Question
Need help writing GarageExitBag class...Provided is the UML and GarageExitBag requirements. CarDataNode class is included in UML just to show you how they relate to each other. You can disregard GarageSet, as it doesnt directly matter
GarageExitBag
*Should only have a head, not a tail
*Nodes that are removed from the GarageSet should be added to the GarageExitBag
*The method, dumpOutputFile, should print the contents of the exit bag to a text file named using the current date. I used SimpleDateFormat outFileFormat = new SimpleDateFormat("'GarageDump'E_yyyy_MM_ddhh_mm_ss"); as the SimpleDateFormat object, then got the file name with outFileFormat.format((new Date())) + ".txt".
Explanation / Answer
Below are the required files according to the give UML diagram and requirements:
CarDataNode.java:
public class CarDataNode {
private String licensePlateNumber;
private Date checkIn,checkOut;
private CarDataNode next;
public CarDataNode(String lpn) {
this.licensePlateNumber = lpn;
this.setNext(null);
}
public String getLPN() {
return licensePlateNumber;
}
public void setLPN(String licensePlateNumber) {
this.licensePlateNumber = licensePlateNumber;
}
public Date getCheckIn() {
return checkIn;
}
public void setCheckIn(Date checkIn) {
this.checkIn = checkIn;
}
public Date getCheckOut() {
return checkOut;
}
public void setCheckOut(Date checkOut) {
this.checkOut = checkOut;
}
public CarDataNode getNext() {
return next;
}
public void setNext(CarDataNode next) {
this.next = next;
}
@Override
public String toString() {
return this.licensePlateNumber+", check in: "+ checkIn + ", check out: " + checkOut;
}
}
GarageExitBag.java:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GarageExitBag {
private CarDataNode head;
public GarageExitBag() {
this.head = null;
}
public void addNodeToBag(CarDataNode node) {
CarDataNode temp = head;
CarDataNode prev = head;
if (head == null) {
head = node;
} else {
while (temp != null) {
prev = temp;
temp = temp.getNext();
}
prev.setNext(node);
}
}
public void dumpOutputFile() {
SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
try {
BufferedWriter writer = new BufferedWriter(
new FileWriter("GarageDump" + format.format(new Date()) + ".txt"));
CarDataNode temp = head;
while(temp!=null) {
writer.write(temp.toString()+" ");
temp = temp.getNext();
}
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.