Link to home
Start Free TrialLog in
Avatar of helpmeoot
helpmeoot

asked on

Urgent - Association Issue - Driving me nuts!!!

I am trying to modify some code which lists the vehicles held by a Hire Company. The Cars are stored in an array and I am trying to include a minibus which holds the same details as the Car (Make, Colour etc) but also no. of seats (Minibus Inherits Car Details).
How do I add to & extract the Car & Minibus details together from the Array?
Do I need a sepearte Array list for minibuses? Do I need to create a separate Association for each?

import javax.swing.JOptionPane;
import java.util.*;
public class VehicleHireCompany{

//instance variables
private String companyname;
private ArrayList owns = new ArrayList();

// Constructor
public VehicleHireCompany(String companyname){
      setCompanyName(companyname);
      }

// Create Text for Output
public String toString() {
String output;
output = "        " + this.companyname + "\n";
output = output + "     Own The Following Vehicles\n";
return output;
}

// Create Association
public void ownsCar(Car owns) {
this.owns.add(owns);
}

public void printDetails(){
String output;
output = toString();

// Extract cars one by one and get details for each
Iterator allCars = this.owns.iterator();
while (allCars.hasNext()) {
      output = output + "\n" + ((Car)(allCars.next())).toString();
}

// Display Message
JOptionPane.showMessageDialog(null,output,"Vehicle List",JOptionPane.INFORMATION_MESSAGE);
}


// Set methods for properties

  public void setCompanyName(String aCompanyName){
    this.companyname = aCompanyName;
  }


} //end class


import javax.swing.JOptionPane;
public class Car{

//instance variables
private String make, colour, registration, motDue;


// Constructor
public Car(String make, String colour, String registration, String motDue){
      setMake(make);
      setColour(colour);
      setRegistration(registration);
      setMot(motDue);
}

// Create Text for Output
public String toString() {
String output;
output = "Make:                             " + make + "\n";
output = output + "Colour:                           " + colour + "\n";
output = output + "Registration:                " + registration + "\n";
output = output + "MOT Due:                      " + motDue + "\n";

return output;
}


public void printDetails(){
String output;

output = toString();
JOptionPane.showMessageDialog(null,output,"Details",JOptionPane.INFORMATION_MESSAGE);
}

//Set methods for properties

  public void setMake(String aMake){
    make = aMake;
  }

  public void setColour(String aColour){
    colour = aColour;
  }

  public void setRegistration(String aRegistration){
    registration = aRegistration;
  }

  public void setMot(String aMot){
    motDue = aMot;
  }

  //Get methods for properties
  public String getMake(){
    return make;
  }

  public String getColour(){
    return colour;
  }

  public String getRegistration(){
    return registration;
  }

  public String getMot(){
       return motDue;
  }

} //end class


import javax.swing.JOptionPane;

public class Minibus extends Car{

//instance variables
private String make, colour, registration, motDue;
private int seats;

// Constructor
public Minibus(String make, String colour, String registration, String motDue, int seats){
     super(make, colour, registration, motDue);
     setSeats(seats);

}


// Create Text for Output
public String toString() {
String output;


output = "Make:                             " + make + "\n";
output = output + "Colour:                           " + colour + "\n";
output = output + "Registration:                " + registration + "\n";
output = output + "MOT Due:                      " + motDue + "\n";
output = output + "No. of Seats:                " + seats + "\n";

return output;
}


public void printDetails(){
String output;

output = toString();
JOptionPane.showMessageDialog(null,output,"Details",JOptionPane.INFORMATION_MESSAGE);
}

  //Set methods for properties

  public void setMake(String aMake){
    make = aMake;
  }

  public void setColour(String aColour){
    colour = aColour;
  }

  public void setRegistration(String aRegistration){
    registration = aRegistration;
  }

  public void setMot(String aMot){
    motDue = aMot;
  }
  public void setSeats(int aSeats){
    seats = aSeats;
  }

   //Get methods for properties

  public String getMake(){
    return make;
  }

  public String getColour(){
    return colour;
  }

  public String getRegistration(){
    return registration;
  }

  public String getMot(){
       return motDue;
  }

  public int getSeats(){
       return seats;
  }
} //end class
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

If you're putting them in the same array, just typecheck them

if (array[n] instanceof Minibus) {
    //
}
else {
    //
}
Your class hierarchy should be

            Vehicle

 |-----------------|---------------|
Car                        Minibus

Using the extends keyword
Avatar of helpmeoot
helpmeoot

ASKER

CEHJ

The Inheritance side is ok. (Public Minibus extends Car)

The issue I think is with the association. I have an association for Car, but do I need the same for Minibus?

Here is the other piece of code I have.

public class Main {
 public static void main (String args[]){

 VehicleHireCompany v1 = new VehicleHireCompany("Edinburgh Vehicle Hire");
 Car c1 = new Car("Vauxhall", "Red", "MD03 PUV", "12th March 2009");
 Car c2 = new Car("Subaru", "Silver", "ST05 HNX", "14th January 2007");
 Minibus m1 = new Minibus("Ford", "White", "ST03 HGF", "14th January 2007", 15);


 // Make Association
 v1.ownsCar(c1);
 v1.ownsCar(c2);
 v1.ownsMinibus(m1);

 // Print List of Cars
 v1.printDetails();



 System.exit(0);
 }
 }
Change

>>
// Make Association
 v1.ownsCar(c1);
 v1.ownsCar(c2);
 v1.ownsMinibus(m1);
>>

to


// Make Association
 v1.ownsVehicle(c1);
 v1.ownsVehicle(c2);
 v1.ownsVehicle(m1);

it doesn't need a separate method

public void ownsVehicle(Vehicle owns) {
    this.owns.add(owns);
}

I get the following error.

Desktop\Java1\VehicleHireCompany.java:28: cannot resolve symbol
symbol  : class Vehicle
location: class VehicleHireCompany
public void ownsVehicle(Vehicle owns) {
                        ^
1 error

Tool completed with exit code 1


Do I not have to change (Car) in the following code?

// Extract cars one by one and get details for each
Iterator allCars = this.owns.iterator();
while (allCars.hasNext()) {
      output = output + "\n" + ((Car)(allCars.next())).toString();
}
Yes - you need to make a Vehicle class as i mentioned in my crude ascii art diagram

public abstract class Vehicle {
}
OK, Thanks for the help so far. I am very new to this (As you can probably tell!!)

I now have this and it won't run.



import javax.swing.JOptionPane;
import java.util.*;
public class VehicleHireCompany{

//instance variables
private String companyname;
private ArrayList owns = new ArrayList();

// Constructor
public VehicleHireCompany(String companyname){
      setCompanyName(companyname);
      }

// Create Text for Output
public String toString() {
String output;
output = "        " + this.companyname + "\n";
output = output + "     Own The Following Vehicles\n";
return output;
}

// Create Association
public void ownsVehicle(Vehicle owns) {
this.owns.add(owns);
}

public void printDetails(){
String output;
output = toString();

// Extract vehicles one by one and get details for each
Iterator allVehicles = this.owns.iterator();
while (allVehicles.hasNext()) {
      output = output + "\n" + ((Vehicle)(allVehicles.next())).toString();
}

// Display Message
JOptionPane.showMessageDialog(null,output,"Vehicle List",JOptionPane.INFORMATION_MESSAGE);
}


// Set methods for properties

  public void setCompanyName(String aCompanyName){
    this.companyname = aCompanyName;
  }


} //end class


public abstract class Vehicle {
}

// end class

public class Main {
 public static void main (String args[]){

 VehicleHireCompany v1 = new VehicleHireCompany("Vehicle Hire");
 Car c1 = new Car("Vauxhall", "Red", "MD03 PUV", "12th March 2009");
 Car c2 = new Car("Subaru", "Silver", "ST05 HNX", "14th January 2007");
 Minibus m1 = new Car("Ford", "White", "ST03 HGF", "14th January 2007", 15);


 // Make Association
 v1.ownsVehicle(c1);
 v1.ownsVehicle(c2);
 v1.ownsVehicle(m1);

 // Print List of Cars
 v1.printDetails();



 System.exit(0);
 }
 }

This is how class Main should look - now try to fit in the other classes around it


public class Main {
      public static void main(String args[]) {

            VehicleHireCompany vhc = new VehicleHireCompany("Vehicle Hire");
            Vehicle v1 = new Car("Vauxhall", "Red", "MD03 PUV", "12th March 2009");
            Vehicle v2 = new Car("Subaru", "Silver", "ST05 HNX", "14th January 2007");
            Vehicle v3 = new Minibus("Ford", "White", "ST03 HGF", "14th January 2007", 15);

            // Make Association
            vhc.ownsVehicle(v1);
            vhc.ownsVehicle(v2);
            vhc.ownsVehicle(v3);

            // Print List of Cars
            vhc.printDetails();

      }
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Right, now I am completely lost.

I know there is no such thing as an instance of the class Vehicle, as any vehicle belongs to one of the subclasses.

Should the abstract class contain any code?

I have looked at both the Car and Minibus Classes and can't see anything I would need to change.
>>Should the abstract class contain any code?

That's something you can take a view on. It may, say, contain colour-related code (i can't think of many transparent vehicles) but for the moment you can leave it as is
Both Car and Minibus should extend Vehicle
> Should the abstract class contain any code?

You don't need an abstract class

> I know there is no such thing as an instance of the class Vehicle, as any vehicle belongs to one of the subclasses.

Thats right you don't need one (or want one)

> I have looked at both the Car and Minibus Classes and can't see anything I would need to change.

There isn't. Though you seem to unnecessarily override a lot of methods it could just be

import javax.swing.JOptionPane;

public class Minibus extends Car{

//instance variables
private String make, colour, registration, motDue;
private int seats;

// Constructor
public Minibus(String make, String colour, String registration, String motDue, int seats){
     super(make, colour, registration, motDue);
     setSeats(seats);

}


// Create Text for Output
public String toString() {
String output = super.toString();


output = output + "No. of Seats:                " + seats + "\n";

return output;
}

  public void setSeats(int aSeats){
    seats = aSeats;
  }

  public int getSeats(){
      return seats;
  }
} //end class

All the rest gets inherited fromn Car
Objects, Thanks for answering all my questions. It's greatly appreciated. I see what you mean about the Minibus (and also my Truck class), I will remove the unnecessary code!!

CEHJ thanks for your help also. Your solution was getting way above my head unfortunately.
There's no unnecessary code - a minibus is not a car - ask your local vehicle licensing department ;-) Also if you wanted to start selling, e.g. mopeds, you'd have no base class to descend from. Your class hierarchy is unfortunately wrong
CEHJ, I can appreciate what you mean now.

If I start a new question, can you guide me through how I achieve this. I have never done any work on abstract classes before.

I presume most of the code should be ok to use?
>>CEHJ, I can appreciate what you mean now.

Good. The first thing that you should do is address the question of having accepted the wrong answer. I can ask for the question to be reopened if you wish