In: Computer Science
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked to calculate the sum of all fines in the policeOfficer class from the arraylist i created. I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way? if not please let me know. What I dont understand how to do is access the fineAmountInCAD from the arrayList to sum it all up? Do i use a for loop? if so how do i just get the fineAmount and have it calculate the sum of that specific variable?
Parking Ticket Class
public class ParkingTicket {
private String officerName;
private String officerBadgeNumber;
private String ticketNumber;
private String carLicensePlateNumber;
private double fineAmountInCAD;
public static int
counter = 1000;
public static final String TICKET_PREFIX =
"VAN";
public ParkingTicket(String name, String badgeNumber,
String carLicense, double fine) {
generatTicketNumber();
setOfficerName(name);
setOfficerBadgeNumber(badgeNumber);
setCarLicensePlateNumber(carLicense);
setFineAmountInCAD(fine);
}
private void generatTicketNumber() {
ticketNumber = TICKET_PREFIX +
++counter;
}
public String getOfficerName() {
return officerName;
}
public void setOfficerName(String name) {
if(name != null &&
name.length() > 0) {
this.officerName = name;
} else if (name == null) {
throw new
IllegalArgumentException("officer name must not be null");
} else {
throw new
IllegalArgumentException ("officer name must not be an empty
String");
}
}
public String getOfficerBadgeNumber() {
return officerBadgeNumber;
}
public void setOfficerBadgeNumber(String badgeNumber)
{
if(badgeNumber != null &&
badgeNumber.length() > 0) {
officerBadgeNumber = badgeNumber;
} else if (badgeNumber == null)
{
throw new
IllegalArgumentException("badge number must not be null");
} else {
throw new
IllegalArgumentException ("badge number must not be empty
String");
}
}
public String getCarLicensePlateNumber() {
return carLicensePlateNumber;
}
public void setCarLicensePlateNumber(String
licensePNumber) {
if(licensePNumber != null
&& licensePNumber.length() > 0) {
carLicensePlateNumber = licensePNumber;
} else if (licensePNumber == null)
{
throw new
IllegalArgumentException("car license plate number must not be
null");
} else {
throw new
IllegalArgumentException ("car license plate number must not be
empty String");
}
}
public double getFineAmountInCAD() {
return fineAmountInCAD;
}
public void setFineAmountInCAD(double fine) {
if(fine > 0) {
fineAmountInCAD
= fine;
}else if(fine == 0) {
throw new
IllegalArgumentException("fine amount must not be 0");
}else {
throw new
IllegalArgumentException("fine amount must not be negative");
}
}
public String getTicketNumber() {
return ticketNumber;
}
public void displayDetails() {
System.out.println("Ticket Number:
"+ ticketNumber);
System.out.println("Officer Name:
"+ officerName);
System.out.println("Officer Badge
number: " + officerBadgeNumber);
System.out.println("Car License
Plate Number: "+ carLicensePlateNumber);
System.out.println("Fine amount: "+
fineAmountInCAD);
}
public static void main(String[] args) {
ParkingTicket ticket = new
ParkingTicket("Bob Smith", "RCMP2251", "1A2B3C", 150.0);
ticket.displayDetails();
}
public static void resetCounter() {
counter = 1000;
}
}
Police Officer Class
import java.util.ArrayList;
public class PoliceOfficer
{
public static final int ONE_HOUR_FINE_AMOUNT =
20;
public static final int MINUTES_IN_HOUR =
60;
private String officerName;
private String officerBadgeNumber;
private ArrayList<ParkingTicket>
ticketList;
public PoliceOfficer(String name, String
badgeNumber){
setOfficerName(name);
setOfficerBadgeNumber(badgeNumber);
ticketList = new
ArrayList<ParkingTicket>();
}
public ArrayList<ParkingTicket>
getTicketList(){
return ticketList;
}
public String getOfficerName(){
return
officerName;
}
public String getOfficerBadgeNumber(){
return
officerBadgeNumber;
}
public void setOfficerName(String
officerName){
if(officerName !=null
&& !officerName.isEmpty()){
this.officerName = officerName;
} else if(officerName ==
null){
throw new IllegalArgumentException("officer name must not be
null");
} else
if(officerName.isEmpty()) {
throw new IllegalArgumentException("officer name must not be an
empty String");
}
}
public void setOfficerBadgeNumber(String
officerBadgeNumber){
if(officerBadgeNumber
!=null && !officerBadgeNumber.isEmpty()){
this.officerBadgeNumber = officerBadgeNumber;
} else
if(officerBadgeNumber == null) {
throw new IllegalArgumentException("badge number must not be
null");
} else
if(officerBadgeNumber.isEmpty()){
throw new IllegalArgumentException("badge number must not be empty
String");
}
}
private boolean isParkingTimeExpired(ParkedCar car,
ParkingMeter meter){
if(car.getNumberOfMinutesParked() >
meter.getNumberOfPurchasedMinutes())
{
return true;
} else{
return false;
}
}
private double
calculateFine(ParkedCar car, ParkingMeter meter){
int totalFine = 0;
int minutes = car.getNumberOfMinutesParked() -
meter.getNumberOfPurchasedMinutes();
if(isParkingTimeExpired(car,meter)){
if(minutes % 60 == 0)
totalFine = (minutes/60) *
ONE_HOUR_FINE_AMOUNT;
else
totalFine = ((minutes/60) + 1) *
ONE_HOUR_FINE_AMOUNT;
}
return totalFine;
}
public void issueParkingTicket(ParkedCar
car,ParkingMeter meter){
if(isParkingTimeExpired(car,meter) == true){
ParkingTicket ticket = new
ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
ticket.displayDetails();
ticketList.add(ticket);
}
}
public double
sumAllFines(ArrayList<ParkingTicket> tickets){
}
}
public ParkingTicket
issueParkingTicket(ParkedCar car,ParkingMeter meter){
if(isParkingTimeExpired(car,meter) == true){
ParkingTicket ticket = new
ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
ticket.displayDetails();
return
ticket;
} else
{ return null;
}
}
Question: I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way?
Answer:
Question: What I dont understand how to do is access the fineAmountInCAD from the arrayList to sum it all up? Do i use a for loop? if so how do i just get the fineAmount and have it calculate the sum of that specific variable?
Answer:
Since there were no information about classes ParkedCar and ParkingMeter and how to test the methods, I could not give you test driver class.
All other classes and methods remain same. Just modified sumAllFines method in PoliceOfficer.java File.
import java.util.ArrayList;
public class PoliceOfficer
{
public static final int ONE_HOUR_FINE_AMOUNT = 20;
public static final int MINUTES_IN_HOUR = 60;
private String officerName;
private String officerBadgeNumber;
private ArrayList<ParkingTicket> ticketList;
public PoliceOfficer(String name, String badgeNumber){
setOfficerName(name);
setOfficerBadgeNumber(badgeNumber);
ticketList = new ArrayList<ParkingTicket>();
}
public ArrayList<ParkingTicket> getTicketList(){
return ticketList;
}
public String getOfficerName(){
return officerName;
}
public String getOfficerBadgeNumber(){
return officerBadgeNumber;
}
public void setOfficerName(String officerName){
if(officerName !=null && !officerName.isEmpty()){
this.officerName = officerName;
} else if(officerName == null){
throw new IllegalArgumentException("officer name must not be null");
} else if(officerName.isEmpty()) {
throw new IllegalArgumentException("officer name must not be an empty String");
}
}
public void setOfficerBadgeNumber(String officerBadgeNumber){
if(officerBadgeNumber !=null && !officerBadgeNumber.isEmpty()){
this.officerBadgeNumber = officerBadgeNumber;
} else if(officerBadgeNumber == null) {
throw new IllegalArgumentException("badge number must not be null");
} else if(officerBadgeNumber.isEmpty()){
throw new IllegalArgumentException("badge number must not be empty String");
}
}
private boolean isParkingTimeExpired(ParkedCar car, ParkingMeter meter){
if(car.getNumberOfMinutesParked() > meter.getNumberOfPurchasedMinutes())
{
return true;
} else{
return false;
}
}
private double calculateFine(ParkedCar car, ParkingMeter meter){
int totalFine = 0;
int minutes = car.getNumberOfMinutesParked() - meter.getNumberOfPurchasedMinutes();
if(isParkingTimeExpired(car,meter)){
if(minutes % 60 == 0)
totalFine = (minutes/60) * ONE_HOUR_FINE_AMOUNT;
else
totalFine = ((minutes/60) + 1) * ONE_HOUR_FINE_AMOUNT;
}
return totalFine;
}
public void issueParkingTicket(ParkedCar car, ParkingMeter meter){
if(isParkingTimeExpired(car, meter) == true){
ParkingTicket ticket = new ParkingTicket(officerName, officerBadgeNumber, car.getLicensePlateNumber(), calculateFine(car, meter));
ticket.displayDetails();
ticketList.add(ticket);
}
}
/*public ParkingTicket issueParkingTicket(ParkedCar car, ParkingMeter meter){
if(isParkingTimeExpired(car,meter) == true){
ParkingTicket ticket = new ParkingTicket(officerName, officerBadgeNumber, car.getLicensePlateNumber(),c alculateFine(car,meter));
ticket.displayDetails();
return ticket;
} else {
return null;
}
} */
public double sumAllFines(){
double totalfine = 0;
//Loop through all the tickets and add the fine amount to total
for(ParkingTicket ticket : ticketList){
totalfine += ticket.getFineAmountInCAD();
}
return totalfine;
}
}
******************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Happy Studying!!!
******************************************************************************