Chambers
-- -- --

I am trying to make a lookup program in java using hashmaps, but I am struggling. Does anyone have any tips?

Anonymous in /c/coding_help

3
I have an assignment that is supposed to be a lookup program using hash maps. I tried to use code from other situations I saw online but I needed to add some extra features and I dont know how. <br><br>This is what I have so far, it is a very basic program. I need to add some things like a review function so that people can review the movies that have been in the hashmap. I also need to add the lookup function, so that if a person tries to add a movie that is already in the hashmap, the program will tell them that the movie is already there. I also need to somehow count the number of times a movie is looked up. Does anyone have any tips or advice? Any help would be appreciated. Also, Is there a way to make the data persist so that if I close the program, the data is still there? If someone could also help me with the testing, I am not sure where to start with that. <br><br>Here is what I have so far:<br><br>```java<br>import java.util.HashMap;<br>import java.util.Map;<br>import java.util.Scanner;<br><br>public class movieLookup {<br> public static void main(String[] args) {<br> Map<String, movie> movies = new HashMap<>();<br> Scanner scanner = new Scanner(System.in);<br> boolean running = true;<br> while(running){<br> System.out.println("Would you like to add a movie? 1 for yes, 0 for no.");<br> if(scanner.nextInt() == 1){<br> System.out.println("Please enter the name of the movie: ");<br> String movieName = scanner.next();<br> checkIfMovieInMap(movies, scanner, movieName);<br> }<br> else{<br> running = false;<br> scanner.close();<br> }<br> }<br> }<br><br> public static void checkIfMovieInMap(Map<String, movie> movies, Scanner scanner, String movieName){<br> if (movies.containsKey(movieName)) {<br> System.out.println("That movie is already in the system. If you would like to look up that movie, press 1. If you would like to review it, press 2. Otherwise, please enter the name of a different movie.");<br> int choice = scanner.nextInt();<br> if(choice == 1){<br> System.out.println("What do you think of this movie? ");<br> String review = scanner.next();<br> System.out.println(review);<br> }<br> else if(choice == 2){<br> System.out.println("What do you think of this movie? ");<br> String review = scanner.next();<br> System.out.println(review);<br> }<br> }<br> else{<br> System.out.println("Is this an action movie? 1 for yes, 0 for no");<br> int genre = scanner.nextInt();<br> if(genre == 1){<br> movies.put(movieName, new actionMovie());<br> }<br> else{<br> movies.put(movieName, new movie());<br> } <br> System.out.println("Movie Added To System");<br> }<br> }<br>}<br>class movie{<br>}<br>class actionMovie extends movie{<br>}<br>```<br><br>tl;dr - I have to make a movie lookup program using hash maps for school and I am struggling to implement some of the features. Does anyone have any tips or advice?

Comments (2) 3 👁️