Jedna metoda w interfejsie dla dwóch różnych obiektów różnych klas

0

Czy jest możliwość wykombinowania jakiegoś parametru dla metody interfejsu serialization(), który umożliwiałby podanie jako parametr dowolny typ obiektu w zalezności dla której klasy go używamy?
Mam taki interfejs

/** The interface for objects production that is Movie / Series */
public interface Production {
	
	public void addToTheDatabase();
	
	public void serialization();
}

I dwie klasy

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner; // A class for reading data from the input

/**  Object class film */
public class Movie implements Production{
	
	/**  Title of the movie */
	private String title;
	/**  Movie description */
	private String description;
	/**  The film's director */
	private String director; 
	/**  Movie Release Date */
	private String releaseDate;
	/**  Rating movie */
	private int rate;
	/**  The film's budget */
	private int budget;
	
	/**  Genres movies */
	private String[] genres = {"Comedy", "Adventure", "Fantasy", "Animation", "Horror", "Thriller", "Action"};
	/**  Country film production */
	private String[] country = {"USA", "UK", "Poland"};
	
	@Override
	public void addToTheDatabase() {
		
		Movie movie = new Movie();
		
		Scanner input = new Scanner(System.in);

		System.out.println("Title: ");
		movie.title = input.nextLine();
		System.out.println("Description: ");
		movie.description = input.nextLine();
		System.out.println("Director: ");
		movie.director = input.nextLine();
		System.out.println("Release date: ");
		movie.releaseDate = input.nextLine();
		System.out.println("Budget: ");
		movie.budget = input.nextInt();
		
		serialization(movie);
	}

	@Override
	public void serialization() {
		
		
	} 
	
	
	
/**  Object class series */
public class Series implements Production{
	
	/**  The title of the show */
	private String title;
	/**  Description of the show */
	private String description;
	/**  Director of the show */
	private String director; 
	/**  Release date of the show. */
	private String releaseDate;
	/**  Rating series */
	private int rate;
	
	/**  Genres serials */
	private String[] genres = {"Comedy", "Adventure", "Fantasy", "Animation", "Horror", "Thriller", "Action"};
	/**  Country of production series */
	private String[] country = {"USA", "UK", "Poland"};
	
	@Override
	public void addToTheDatabase() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void serialization() {
		// TODO Auto-generated method stub
		
	}  
}
0

Po co chcesz coś takiego robić?

Najpierw przeprojektuj swoje klasy to zobaczysz, że nie będziesz miał takiego problemu.

Dlaczego klasa Movie, która reprezentuje pojedynczy film ma jakąś metodę do dodawania do bazy i przechowuje listy typów filmów i kraje w których Ostały wyprodukowane?

0
public interface Production<T> {
 
    public void addToTheDatabase(T obj);
 
    public void serialization();
} 
 public class Movie implements Production<Movie>{

    @Override
    public void addToTheDatabase(Movie movie) {
        serialization(movie);
    }
 
    @Override
    public void serialization() {
 
 
    } 
 
 

Tak? Pewnie chodzi Ci o typy generyczne.

@Edit

Ale te metody nie powinny należeć do klasy Movie :)

0

Mam jeszcze główną klasę. Tam mam przenieść te metody?

import java.io.IOException; // Signals that an I/O exception of some sort has occurred
import java.nio.file.Files; // This class consists exclusively of static methods that operate on files, directories, or other types of files
import java.nio.file.Paths; // An object that may be used to locate a file in a file system. It will typically represent a system dependent file path
import java.util.Scanner; // A class for reading data from the input

/** The main class of the program */
public class Database {
	
	/** Main method */
	static public void main(String[] args) {
		
		createTheRequiredFiles();
		
		Scanner input = new Scanner(System.in);
		
		while(true) {
			
			System.out.println("1. Browse data" );
			System.out.println("2. Add movie / series to the database\r" );
			System.out.print("\n Your choice: " );
			
			switch(input.nextLine()) {
				case "1": {
					while(true) {
						System.out.println("\n1. Movie database" );
						System.out.println("2. Serials database\r" );
						System.out.print("\n Your choice: " );
						
						switch(input.nextLine()) {
							case "1": {
								//////////////////
								break;
							}
							case "2": {
								//////////////////
								break;
							}
							default: {
								System.err.println("There is no such option in the menu");
							}
						}
					}
				}
				case "2": {
					while(true) {
						System.out.println("\n1. Add movie..." );
						System.out.println("2. Add series...\r" );
						System.out.print("\n Your choice: " );
						
						switch(input.nextLine()) {
							case "1": {
								Movie movie = new Movie();
								movie.addToTheDatabase();
								
								break;
							}
							case "2": {
								//////////////////
								break;
							}
							default: {
								System.err.println("There is no such option in the menu");
							}
						}
					}
				}
				default: {
					System.err.println("There is no such option in the menu");
				}
			}
		}
	}
	
	/** This method produces the required files */
	static final void createTheRequiredFiles() {
		try {
			Files.createFile(Paths.get("movies.ser"));
			Files.createFile(Paths.get("series.ser"));
		} catch(IOException e) {}
	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1