What I'm trying to do here is apply id's to all of the Vehicle's objects in my program.
When a vehicle is added the counter should turn to 1... and increment every time a vehicle is added.
So if I add a Car the counter should increment by one. Same for lorry and same for coach...
However, each of my Vehicles (Car, Lorry and Coach) seem to have counters of their own. For instance... If I add a bunch of Cars, lets say 4 cars and I print the counters. It prints... 1,2,3,4 to the console.
Now, if I say three lorries and print the lorries counters, it prints 1,2,3...
Same for coaches, two coaches, prints 1,2...
What I really want to acheive is for all of my vehicles to have a unique counter, so... If the code actually worked, then what I have described above should print:
1,2,3,4,5,6,7,8,9... and so forth... It should be unique...
The code is below, forgive me if I've added the wrong code tags in
Help would greatly be appreciated!
Thanks guys
package backend; import java.io.Serializable; import javax.swing.ImageIcon; public abstract class Vehicle implements Serializable{ protected double charge; protected String regNumber; private static int count = 1; private int id; public Vehicle() { this.charge = 0; this.regNumber = ""; } public Vehicle(String regNumber) { this.regNumber = regNumber; this.id = count; count++; } public Vehicle(int id, String regNumber) { this(regNumber); this.id = id; } public abstract int getId(); public abstract void setId(int id); public double getCharge() { return charge; } public void setCharge(double charge) { this.charge = charge; } public abstract ImageIcon getImage(); public abstract double calcCharge(); @Override public String toString() { return "{" + regNumber + ", " + charge + "}"; } }
[CODE]package backend;
import javax.swing.ImageIcon; public class Lorry extends Vehicle{ private double weight; private int numOfDays; ImageIcon icon = new ImageIcon((getClass().getResource("/images/lorry.png"))); private static int count = 1; private int id; public Lorry() { this.weight = 0; this.numOfDays = 0; } public Lorry(String regNum, int weight, int numOfDays) { this.regNumber = regNum; this.weight = weight; this.numOfDays = numOfDays; this.id = count; count++; } public Lorry(int id, String regNum, int weight, int numOfDays) { this(regNum, weight, numOfDays); this.id = id; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public ImageIcon getImage() { return icon; } @Override public String toString() { return "{" + weight + ", " + numOfDays + "}"; } @Override public double calcCharge() { // TODO Auto-generated method stub return 0; } @Override public int getId() { return id; } @Override public void setId(int id) { this.id = id; } }package backend; import javax.swing.ImageIcon; public class Coach extends Vehicle { private int numOfPassengers; private boolean touristOperator; private String regNum; ImageIcon icon = new ImageIcon((getClass().getResource("/images/coach.png"))); private static int count = 1; private int id; public Coach() { this.numOfPassengers = 0; this.touristOperator = false; } public Coach(String regNum, int numOfPassengers, boolean touristOperator) { this.regNum = regNum; this.numOfPassengers = numOfPassengers; this.touristOperator = touristOperator; this.id = count; count++; } public Coach(int id, String regNum, int numOfPassengers, boolean touristOperator) { this(regNum, numOfPassengers, touristOperator); this.id = id; } public ImageIcon getImage() { return icon; } @Override public String toString() { return "{Reg: " + regNum + ", Charge: " + calcCharge() + ", No. Passengers: " + numOfPassengers + "}"; } @Override public double calcCharge() { // TODO Auto-generated method stub return 0; } @Override public int getId() { // TODO Auto-generated method stub return id; } @Override public void setId(int id) { this.id = id; } }
package backend; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.JTextField; public class Car extends Vehicle { private double length; private boolean disabledBadge; private int numOfHours; private String regNum; private double charge; private static int count = 1; private int id; ImageIcon icon = new ImageIcon((getClass().getResource("/images/car.png"))); public Car() { length = 0.0; disabledBadge = false; numOfHours = 0; } public Car(String regNum, double length, boolean disabledBadge, int numOfHours) { this.regNum = regNum; this.length = length; this.disabledBadge = disabledBadge; this.numOfHours = numOfHours; this.id = count; count++; } public Car(int id, String regNum, double length, boolean disabledBadge, int numOfHours) { this( regNum, length, disabledBadge,numOfHours); this.id = id; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public boolean isDisabledBadge() { return disabledBadge; } public void setDisabledBadge(boolean disabledBadge) { this.disabledBadge = disabledBadge; } public int getNumOfHours() { return numOfHours; } public void setNumOfHours(int numOfHours) { this.numOfHours = numOfHours; } public ImageIcon getImage() { return icon; } @Override public String toString() { return "{" + regNum + ", " + length + ", " + disabledBadge + ", " + numOfHours + "}"; } @Override public double calcCharge() { // TODO Auto-generated method stub return 0; } @Override public int getId() { return id; } @Override public void setId(int id) { this.id = id; } }