?
Solved

Urgent - Association Issue - Driving me nuts!!!

Posted on 2006-05-03
17
Medium Priority
?
225 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:helpmeoot
  • 9
  • 6
  • 2
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16600499
If you're putting them in the same array, just typecheck them

if (array[n] instanceof Minibus) {
    //
}
else {
    //
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16600543
Your class hierarchy should be

            Vehicle

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

Using the extends keyword
0
 

Author Comment

by:helpmeoot
ID: 16600613
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);
 }
 }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 16600645
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);
}

0
 

Author Comment

by:helpmeoot
ID: 16600731
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();
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16600759
Yes - you need to make a Vehicle class as i mentioned in my crude ascii art diagram

public abstract class Vehicle {
}
0
 

Author Comment

by:helpmeoot
ID: 16600903
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);
 }
 }

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16601034
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();

      }
}
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16601093
What you already had was fine, you just need to change:

 v1.ownsMinibus(m1);

to:

 v1.ownsCar(m1);

(as a MiniBus *is a* Car)
0
 

Author Comment

by:helpmeoot
ID: 16601200
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16601271
>>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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16601275
Both Car and Minibus should extend Vehicle
0
 
LVL 92

Expert Comment

by:objects
ID: 16601302
> 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
0
 

Author Comment

by:helpmeoot
ID: 16601397
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16603196
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
0
 

Author Comment

by:helpmeoot
ID: 16635079
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16636991
>>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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question