1.11M Members

Calling a arraylist method in another class

 
0
 

I am designing a cinema system

I want to call a another class which has a arraylist method and then display the elements inside the arraylist.

Please help

I made two sample classes

Class cinema()
        .
        .
        .
        .

        public ArrayList<time> getTime()
        {
                for (Show show : getTime){
                }
                return getTime;
    
        }

}
Class 2
Class System()
.
.
.
.
.
private void welcome()
{

Cinema listOftime = new Cinema();
// call and display arraylist
}

I don't known how to call & display the arraylist.

hope that helps

Thanks

 
0
 
import java.util.ArrayList;
import java.util.List;

public class CinemaExample {

    public static void main(String[] args) {
        List<Cinema> cinemas = new ArrayList<Cinema>();
        // fill the cinemaList
        Cinema cinema1 = new Cinema("Paradise");
        cinemas.add(cinema1);
        Cinema cinema2 = new Cinema("Zodiac");
        cinemas.add(cinema2);

        Movie movie1 = new Movie("Zorro");
        Movie movie2 = new Movie("Titanic");
        Movie movie3 = new Movie("Matrix");


        cinema1.setMovie(new Projection(movie1, 18));
        cinema1.setMovie(new Projection(movie2, 20));
        cinema1.setMovie(new Projection(movie3, 24));

        cinema2.setMovie(new Projection(movie1, 14));
        cinema2.setMovie(new Projection(movie3, 16));

        // results
        System.out.println("CINEMA LIST:");
        for (Cinema c : cinemas) {
            System.out.println(c.toString());
        }

    }
}

class Movie {

    String movieName;

    Movie(String movieName) {
        this.movieName = movieName;
    }

    public String toString() {
        return movieName;
    }
}

class Cinema {

    String cinemaName;
    List<Projection> listOfProjections;

    Cinema(String cinemaName) {
        this.cinemaName = cinemaName;
        listOfProjections = new ArrayList<Projection>();

    }

    void setMovie(Projection projection) {
        listOfProjections.add(projection);
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append("CinemaName:" + cinemaName);
        sb.append("\n");

        for (Projection p : listOfProjections) {
            sb.append(p.toString());
        }

        return sb.toString();
    }
}

class Projection {

    Movie movie;
    int timeofProjection;

    Projection(Movie movie, int timeofProjection) {
        this.movie = movie;
        this.timeofProjection = timeofProjection;
    }

    public String toString() {
        return ("movie=" + movie + ", timeofProjection=" + timeofProjection + "\n");
    }
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article