Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Id counter

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Id counter

    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;
     
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Id counter

    However, each of my Vehicles (Car, Lorry and Coach) seem to have counters of their own.
    Yes, they do. The solution is to remove the declaration of the count variable from each of the child classes and change the access modifier in Vehicle to protected. This class variable concept is discussed in the Bicycle class example in the Oracle Tutorials.

Similar Threads

  1. Digit counter
    By Rexshine in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 16th, 2013, 06:53 PM
  2. [SOLVED] Counter
    By mssim in forum Java Theory & Questions
    Replies: 2
    Last Post: November 7th, 2012, 05:32 AM
  3. Help With Counter
    By Catgroove in forum Java Theory & Questions
    Replies: 7
    Last Post: February 10th, 2011, 08:50 AM
  4. String counter
    By chkang0130 in forum Algorithms & Recursion
    Replies: 9
    Last Post: December 8th, 2009, 11:56 AM