1
public class Movie {
    private String title, director;
    private float overallRating;

    private enum showingStatus {ComingSoon, Preview, NowShowing};

    public Movie(String title, enum showingStatus, String director)
    {
        this.title = title;
        this.showingStatus = showingStatus;
        this.director = director;
        overallRating = 0;
    }
}

How do I restrict in such a way when someone creates Movie object, they pass in only my defined list of showingStatus?

I also want to define the get and set methods but they are throwing errors pointed out on comments

public void setShowingStatus(enum showingStatus){ this.showingStatus = showingStatus;} // showingStatus cannot be resolved or not a field
public String getShowingStatus() { return showingStatus; } // showingStatus cannot be resolved to a variable
Gavin
  • 2,784
  • 6
  • 41
  • 78
  • you are already doing it mate! http://stackoverflow.com/questions/3054247/how-to-define-properties-for-enum-items – Brij Raj Singh - MSFT Oct 21 '15 at 05:23
  • Hm.. it's throwing a lot of errors on my constructor definition – Gavin Oct 21 '15 at 05:29
  • What are the errors? – Siddhartha Oct 21 '15 at 05:33
  • `Multiple markers at this line` - `Syntax error on token ",", invalid BlockStatements`. - `The member enum showingStatus can only be defined inside a top-level class or interface or in a static context.` - `the type showingStatus is hiding the type Movie.showingStatus` - `Syntax error on token "enum", Type expected.` - `Syntax error on token "enum", bype expected.` – Gavin Oct 21 '15 at 05:34

1 Answers1

4

I think this might be what you're looking for.

public class Movie {
    private String title, director;
    private float overallRating;
    private ShowingStatus showingStatus;

    public enum ShowingStatus {ComingSoon, Preview, NowShowing}

    public Movie(String title, ShowingStatus showingStatus, String director)
    {
        this.title = title;
        this.showingStatus = showingStatus;
        this.director = director;
        overallRating = 0;
    }
}

Edit:

This is the same thing. You can't declare a enum type when passing in variable. It's whatever you made it to be. In this case, I set it as ShowingStatus, so you would say:

public void setShowingStatus(ShowingStatus showingStatus){ this.showingStatus = showingStatus;} // showingStatus cannot be resolved or not a field
public ShowingStatus getShowingStatus() { return showingStatus; } // showingStatus cannot be resolved to a variable

To allow ShowingStatus enums to get used by other classes, create a separate enum file by doing the following:

public enum ShowingStatus {
    ComingSoon, Preview, NowShowing
}

And now, other classes should be able to call ShowingStatus.ComingSoon or any other enum elements within ShowingStatus.

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
  • Yup, thanks this was what I was looking for. Another question how do one create the `Movie` object then? Doing `Movie myMovie = new Movie("myTitle", "ComingSoon", "myDirector");` Does not work – Gavin Oct 21 '15 at 05:43
  • 1
    ```Movie myMovie = new Movie("myTitle", ShowingStatus.ComingSoon, "myDirector");``` – Siddhartha Oct 21 '15 at 05:45
  • Exactly what Siddhartha said. Remember that `Enum` does not equal to `String`. – sparkhee93 Oct 21 '15 at 05:49
  • Yup, I am new in using enum. Now I understand. I have an error when I try to create `Movie` object in another class though. It seems to fail to import the enum. `import static Movie.ShowingStatus;` yields error `The import Movie cannot be resolved` How can I define the enum such that all the classes in the same package(default package) can have access to it? – Gavin Oct 21 '15 at 05:58
  • You can always create `ShowingStatus` outside of `Movie` so that other classes can use it. Like a separate file that other classes can freely call. – sparkhee93 Oct 21 '15 at 06:03
  • Edited my answer in case my last comment didn't make sense. – sparkhee93 Oct 21 '15 at 06:11
  • I created a seperate enum file and try to do an import on my `Application` class using this `import static ShowingStatus.*;` But it's seems to fail. How do I import the enum? It it because I can't import any class or enum in the default package? – Gavin Oct 21 '15 at 06:17
  • I just simply shifted all my classes to my own package for it to work and used `import static myPackage.ShowingStatus.*;` – Gavin Oct 21 '15 at 06:41
  • It's been my experience that if you have the enum file in the same package, you don't even need to declare an `import`. But if it works for you, I don't see why not. – sparkhee93 Oct 21 '15 at 13:36