The Way to Programming
The Way to Programming
Here is the code, just dont copy paste and submit your project. Try to understand how i implemented, if possible use your own approach and take this code as reference.
MovieStore.java
import java.util.ArrayList; public class MovieStore { // Add the missing implementation to this class /* You need a dynamic structure to keep movies in store */ /* ArrayList is a dynamic array which you can add or remove item easily */ private ArrayList<Movie> store = new ArrayList<Movie>(); public String storeCity = ""; public MovieStore(String city){ storeCity = city; } public static String printOpeningHours(){ return "Open daily from 9am to 5pm."; } public void addMovie(Movie m){ store.add(m); } public String printMovieStoreCity(){ return storeCity; } public String borrowMovie(String movieName) { // Check whether we have the movie Movie temp = null; // We set movie object to null if we cant find movie it will be still null after loop for (int i = 0; i < store.size(); i++) { temp = store.get(i); if (temp.getTitle().equals(movieName)){ break; // we found the movie quit loop and assign movie object to temp. } } String returnMessage = ""; if (temp == null){ // That means we dont have movie in the store returnMessage = ": Sorry, " + movieName + " is not in our catalog. "; } if (temp != null){ // yay we have the movie // Lets check if it is already borrowed. if (temp.isBorrowed() == true){ returnMessage = ": Sorry, " + movieName + "is already borrowed."; }else{ // Available for borrow and lets borrow it temp.borrowMovie(); returnMessage = ": You successfully borrowed " + movieName; } } return returnMessage; } public String printAvailableMovies() { // Create a string for output. String movieList = ""; for (int i = 0; i < store.size(); i++) { Movie temp = store.get(i); if (!temp.isBorrowed()){ movieList = movieList + temp.getTitle() + "\n"; } } return movieList; } public String returnMovie(String movieName) { // Loop through store array and when you find movie, call it's returnMovie fuction for (int i = 0; i < store.size(); i++) { Movie temp = store.get(i); if (temp.getTitle().equals(movieName)){ temp.returnMovie(); break; } } return " You successfully returned " + movieName; } }
Movie.java
public class Movie { String title; boolean borrowed; /*Constructor */ public Movie(String MovieTitle){ title = MovieTitle; } /* Marks the Movie as rented */ public void borrowMovie () { borrowed = true; } /* Marks the Movie as not rented */ public void returnMovie () { borrowed = false; } /* Return true if the Movie is rented, false otherwise */ public boolean isBorrowed() { return borrowed; } /* Return the title of the Movie */ public String getTitle() { return title; } }
Sign in to your account