8 Replies - 5436 Views - Last Post: 30 November 2013 - 05:36 AM Rate Topic: -----

#1 xfire   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 12

Parking Ticket Simulator, adding Linked List

Posted 27 November 2013 - 08:10 PM

I have been working on a program but unsure of how i would make and implement a linked list for different cars that would show ticket(s) for each car(ie. car 1 has 2 tickets - ticket 1 - $34, ticket 2 - $25; car 2 has 1 ticket - ticket 1 - $25; etc and etc) I am unsure of how to do add a linked list that could account for each car and it's ticket amount and it doesn't have to have to seperate the tickets, it could be just car 1 has 4 tickets totaling $90 or something like that.

Program after complied:
Attached File  Parking Simulator.zip (15.83K)
Number of downloads: 254

Parked car class:
public class ParkedCar {
 
	private String make;
	private String model;
	private String color;
	private String license;
	private static int minutes;
 
	public ParkedCar() {
 
	}
 
	public ParkedCar(String carMake, String carModel, String carColor, String carLicense, int carMinutes) {
 
		make = carMake;
		model = carModel;
		color = carColor;
		license = carLicense;
		minutes = carMinutes;
	}
 
	public String getMake() {
		return make;
	}
	public String getModel() {
		return model;
	}
	public String getColor() {
		return color;
	}
	public String getLicense() {
		return license;
	}
	public static int getMinutes() {
		return minutes;
	}
 
	public String toString() {
 
		String string = "Make: " + make
				+ "\nModel: " + model
				+ "\nColor: " + color
				+ "\nLicense Plate: " + license;
			return string;
 
	}
 
 
}


Parking Meter class
public class ParkingMeter {
 
	private static int minPurchased;
 
	public ParkingMeter() {
 
	}
 
	public ParkingMeter(int carMinPurchased) {
 
		minPurchased = carMinPurchased;
	}
 
	public static int getMinPurchased() {
		return minPurchased;
	}
 
	public String toString() {
		String string = "Minutes Purchased: " + minPurchased;
		return string;
	}
} 


Parking Ticket class:
public class ParkingTicket {
 
		private ParkedCar vehicle;
		private PoliceOfficer copster;
		private double fine;
		private int minutes;
		private double firstFine = 25;
		private double moreFine = 10;
 
		public ParkingTicket(ParkedCar car, PoliceOfficer cop, double guyFine, int mins) {
 
			vehicle = car;
			copster = cop;
			fine = guyFine;
			minutes = mins;
		}
 
		public void getTotalFine() {
			int  time = ParkedCar.getMinutes() - ParkingMeter.getMinPurchased();
 
			if(time <= 60) {
				fine = firstFine;
			}
			else {
				fine = firstFine + moreFine * (time / 60);
			}
		}
 
		public double getFirstFine() {
			return firstFine;
		}
		public double getMoreFine() {
			return moreFine;
		}
		public ParkedCar getVehicle() {
			return vehicle;
		}
		public PoliceOfficer getCopster() {
			return copster;
		}
		public int getMinutes() {
			return minutes;
		}
		public double getFine() {
			return fine;
		}
 
 
	}


Police officer class
public class PoliceOfficer {
 
	private String name;
	private int badge;
	private static double ticket;
 
	public PoliceOfficer() {
 
	}
 
	public PoliceOfficer(String poName, int poBadge) {
 
		name = poName;
		badge = poBadge;
	}
 
	public String getName() {
		return name;
	}
	public int getBadge() {
		return badge;
	}
 
static double search(ParkedCar car, ParkingMeter meter) {
 
		int  time = ParkedCar.getMinutes() - ParkingMeter.getMinPurchased();
 
		if(ParkedCar.getMinutes() > ParkingMeter.getMinPurchased()) {
			if(time <= 60) {
				ticket = 25;
 
			}
 
		else {
			ticket = 25 + (10 * (time/60));
		}
		}
		return ticket;
 
}


Parking Simulator (main class) :
package parkingsimulator;

public class ParkingSimulator {

    public static void main(String[] args) {
        ParkedCar car = new ParkedCar("Honda", "Accord", "Black", "452-BTS", 78); //car
        ParkingMeter meter = new ParkingMeter(60); //meter
        PoliceOfficer john = new PoliceOfficer("John Doe", 1337); //officer
        double ticket = PoliceOfficer.search(car, meter); //ticket 
        if(ticket != 0.0) {
                System.out.println(ticket);
        }
        else {
                System.out.println("Car is not doing anything wrong!");
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Parking Ticket Simulator, adding Linked List

#2 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3132
  • View blog
  • Posts: 11,937
  • Joined: 03-December 12

Re: Parking Ticket Simulator, adding Linked List

Posted 27 November 2013 - 08:25 PM

Why a linked list? You could make this attributes of the parkedcar class.
Was This Post Helpful? 0
  • +
  • -

#3 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Parking Ticket Simulator, adding Linked List

Posted 27 November 2013 - 10:42 PM

You in my class? lol You go to Roosevelt?
Was This Post Helpful? 0
  • +
  • -

#4 xfire   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 12

Re: Parking Ticket Simulator, adding Linked List

Posted 28 November 2013 - 05:46 PM

View Postastonecipher, on 27 November 2013 - 08:25 PM, said:

Why a linked list? You could make this attributes of the parkedcar class.


Well that or something that can help with more than one car with more than one ticket(or one or the other)

View Postjphoc13, on 27 November 2013 - 10:42 PM, said:

You in my class? lol You go to Roosevelt?


My teacher sent my class an idea, so I am unsure if he might have gotten the idea from there or not.
Was This Post Helpful? 0
  • +
  • -

#5 xfire   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 12

Re: Parking Ticket Simulator, adding Linked List

Posted 29 November 2013 - 05:21 PM

a little bump
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,926
  • Joined: 27-December 08

Re: Parking Ticket Simulator, adding Linked List

Posted 29 November 2013 - 06:13 PM

Please avoid bumping your threads without continued effort.

I would go for the following design:
public class ParkingTicket{

}

public class Car{
   private List<ParkingTicket> tickets;

}

public class CarCollection{
   private List<Car> cars;
}



This improves on your design by avoiding coupling between the Car and ParkingTicket class.
Was This Post Helpful? 0
  • +
  • -

#7 xfire   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 12

Re: Parking Ticket Simulator, adding Linked List

Posted 29 November 2013 - 09:07 PM

How exactly would I go about doing each part? I am still kind of new to data structures, and not sure about how to invoke each part correctly.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15692
  • View blog
  • Posts: 62,848
  • Joined: 12-June 08

Re: Parking Ticket Simulator, adding Linked List

Posted 29 November 2013 - 09:22 PM

Reread up on classes and objects.

http://www.dreaminco...igning-classes/
http://www.dreaminco...ing-classes-ii/
http://www.dreaminco...bjects-in-java/
Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Parking Ticket Simulator, adding Linked List

Posted 30 November 2013 - 05:36 AM

I see static in your code. private static int minutes;,private static int minPurchased;. This is simply wrong.

Alas, your download is just this source code. There is no example of what you're actually after.

So, you have the concept of ParkingMeter? For your simulator to work, I would assume you'd need a concept of time.

How do you see ParkingSimulator working? How does time pass? What does it show?

Hmm... just brainstorming a little.

interface TimeServer { int getTime(); }

interface ParkingMeter {
	int getId();
	boolean addCar(Car car, int minutes);
	boolean hasCar();
	Car getCar();
	boolean isExpired();
	Ticket giveTicket(PoliceOfficer officer);
	boolean hasTicket();
}

interface Ticket  {
	int getId();
	ParkingMeter getParkingMeter();
	Car getCar();
	PoliceOfficer getOfficer();
}

interface ParkingSimulation extends TimeServer {
	int getTime();
	void addTime(int minutes);
	Iterable<PoliceOfficer> getOfficers();
	Iterable<Car> getCars();
	Iterable<ParkingMeter> getMeters();
	Iterable<Ticket> getTickets();
	void addTicket(Ticket ticket);
	Car getCarNeedsPark();
	ParkingMeter getAvailableMeter();
}



Your simulation would start with Cars, Meters, and PoliceOfficer loaded. The addTime is essentially your game loop. Cars park, cars leave, if cars don't leave soon enough, they get a ticket. You have reports on various elements of your simulation. You might want to pay tickets, etc.

Since a ticket must have a car, but a car needn't have a ticket, I'd make ticket hold car rather than the other way around. There's no single best way to do this, though. Having cars hold tickets makes one aspect of the program easier, but others hard. This is the same, only the other way around.

Note, the idea behind TimeServer is this:
class ParkingMeter {
	private final int id;
	private final TimeServer ts;
	private Car car;
	private int carExpires;
	public ParkingMeter(int id, TimeServer ts) { 
		this.id = id;
		this.ts = ts;
	}
	public boolean addCar(Car car, int minutes) {
		if (hasCar()) { return false; }
		this.car = car;
		this.carExpires = ts.getTime() + minutes;
		return true;
	}
	
	public boolean isExpired() {return hasCar() && (carExpires < ts.getTime()); }



Looks like fun.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1