-2
import java.util.Scanner;


public class MainApp 
{
    private Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args)
    {
        new MainApp().start();  
    }

    public void start()
    {
        Airline airline1 = new Airline("AerLingus");
        PlaneStore planeStore = new PlaneStore("Aer Lingus");

        Flight p1 = new Flight("Aer Lingus","A01", 150.5, 10.5, 500, Flight.AIRPLANETYPE.AIRBUS);
        Flight p2 = new Flight("Aer Lingus","B01", 50.3, 1.5, 91, Flight.AIRPLANETYPE.CORPORATE);
        Flight p3 = new Flight("Aer Lingus","C01", 12.2, -3.1, 56, Flight.AIRPLANETYPE.AIRBUS);


        Flight p4 = new Flight("Ryan Air","D01", 10.5, 1.5, 430, Flight.AIRPLANETYPE.PRIVATE);
        Flight p5 = new Flight("Ryan Air","E01", 0.3, 2.1, 101, Flight.AIRPLANETYPE.CORPORATE);
        Flight p6 = new Flight("Ryan Air","F01", 2.2, -3, 291, Flight.AIRPLANETYPE.AIRBUS);
        planeStore.add(p1);
        planeStore.add(p2);
        planeStore.add(p3);
        planeStore.print();

        airline1.add(planeStore);
        airline1.add(planeStore);
        airline1.add(planeStore);

        airline1.printPlane();
    }

}

import java.util.HashMap;


public class Airline 
{
    private String airlineName;
    private HashMap<String, PlaneStore> map;

    public Airline(String airlineName)
    {
        this.airlineName = "";
        map = new HashMap<String, PlaneStore>();
    }
    public void add(PlaneStore plane)
    {
        map.put(airlineName, plane);
    }
    public void remove(String flight)
    {
        map.remove(flight);
    }
    public void printPlane()
    {
        System.out.println("\n********Flight List********");
        for (PlaneStore plane: map.values()) {
             //System.out.println(plane);
            // class
            // or:
            System.out.println(airlineName);
            System.out.println(plane.toString());

        }

    }

}

Hey i want to print out the plane store. The plane store contains a hashmap of strings and flights. That is where the flight.add/print come into the main app. But i now want The airline and flights to print parallel so i put a airline store and im trying to print out the planeStore along with airline but all im getting is a memory address can anybody help me with this.

Pendo826
  • 1,002
  • 18
  • 47

4 Answers4

1

If you get something like MyClass@abcdef, then you need to override toString in MyClass like so:

public class MyClass {

    ...

    @Override
    public String toString() {
        return "My class"; // Make a string here that you want to display
    }
}

So for example, if PlaneStore has int planeCount and String name for fields, then you can do something like:

@Override
public String toString() {
    return "Plane store | Name: " + name + ", planes: " + planeCount;
}
Brian
  • 17,079
  • 6
  • 43
  • 66
0

You could do that by:

System.out.println(map.toString());

which is the same as

System.out.println(map);

Then, to avoid the "memory adress" you mentioned:
add to PlainStore a method

public String toString() {
    return this.storeName; // or similar, 
}

This works because the Map can printout its content, by calling the toString() method of each key and value.

If this output is not nice enough, you must iterate for yourself: Use the map.entrySet() and iterate over that.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • I dont understand what would go in where storeName is. I tried putting the map name but theres an error. – Pendo826 Dec 10 '12 at 20:20
  • Each class shall have a toString() method: you want to output the map which contains PlainStore objects, so in PlainStore or any other class you want to print: add an toString() method which returns a String which represents the data of that class; Post the code of PlainStore. – AlexWien Dec 10 '12 at 20:26
0

Try using the Apache ToStringBuilder class. But ofcourse toString() will also help. More information on ToStringBuilder

sathish_at_madison
  • 823
  • 11
  • 34
0

Because you seem to be new to programming, I'm going to hand you he answer:

public class PlaneStore {

    private String name;
    ...

    public String toString() {
        return name;
    }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722