简体   繁体   中英

i need help seperate this main method to a class

I have this movie ticket program, which I want to have a class so it becomes a main method and a class and it's aching my mind.

    import java.util.Scanner;
    public class MovieTicket {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String[] movielist ={"1) Shutter Island","2) Devil's Advocate","3) Pulp Fiction","4) Resvouar Dogs"};
        Ticket obj=new Ticket();
         
        System.out.println("Welcome to Vox Cinemas");
        
        System.out.println("Please ,Selcet a movie please");
        for (int i =0; i<movielist.length;i++){
            System.out.println(movielist[i]);
        }
        int number = sc.nextInt();
        System.out.println("The movie you selected is:"+" "+movielist[number-1]);         
         
        System.out.println("How many seats would you lie:");
        String seats = sc.next();
        System.out.println("You've selected:"+seats+"Seats");
     }
}

You need to create separate classes for Theatre, Movie, Ticket etc.
Something like below.

class Movie{
    private String name;
    private int runtime;
    //add other fields like producer/actors/genre
    
    public Movie(String name, int runtime) {
        this.name = name;
        this.runtime = runtime;
    }
    
    public String getName() {
        return name;
    }

    public int getRuntime() {
        return runtime;
    }

}

class Ticket{
    private int number;
    private Movie movie;
    private String screen; //Screen 1
    private String seat;  //A14
    
    public Ticket(int number, Movie movie, String screen, String seat) {
        //.. initiate variables here.
    }
    
    //write getters here
}

class VoxCinema{
    private List<Movie> movies = new ArrayList<>();
    
    private String address;
    //theater details like name/timings etc.
    
    public VoxCinema() {
        
    }
    
    public void addMovie(Movie movie) {
        this.movies.add(movie);
    }
    
    public List<Movie> getAllMovies() {
        return this.movies;
    }
    
    //write getters and setters
}

Then use them in your application like this:

public class TicketBookingApplication {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        VoxCinema vc = new VoxCinema();
        vc.addMovie(new Movie("Shutter Island", 130));
        vc.addMovie(new Movie("Devil's Advocate", 180));
        /// add all movies

        System.out.println("Welcome to Vox Cinemas");

        System.out.println("Please ,Selcet a movie please");
        List<Movie> movies = vc.getAllMovies();
        for (int i = 0; i < movies.size(); i++) {
            System.out.println(i + ") " + movies.get(i).getName());
        }
        int number = sc.nextInt();

        Movie selectedMovie = movies.get(number);
        System.out.println("The movie you selected is: " + selectedMovie.getName());

        System.out.println("How many seats would you lie:");
        String seats = sc.next();
        System.out.println("You've selected:" + seats + "Seats");

        // create tickets
        Ticket t = new Ticket(3341, selectedMovie, "Screen 4", "D15");
        //store tickets in VoxCinema object
    }
}

Implement remaining parts or modify code as per your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM