Java: Using Enum for Linked List sorting question

13 hours ago 3
ARTICLE AD BOX

having a little difficulty with something here. I'm working on a project for a college class that involves reading a text file and using the values in that text file to populate two doubly linked lists. The nodes need to contain a String name, String genre, release Date, Date value of when the movie was received by the theater, and an enum value of if the movie status is "released" or "received". These nodes then have to be sorted into one of the two doubly linked lists based on if the enum value is "released" or "received".

The sorting of nodes into the proper linked list is where I'm getting stuck and would love some advice on what direction to take here.

Here's my (currently unfinished) method for reading the text file:

package movies; import java.io.*; import movies.Movies.Movie; import movies.Movies.Status; public class movieLists { public static void main(String[] args) throws Exception { Linked_List<Movie> releasedMovies = new Linked_List<Movie>(); Linked_List<Movie> receivedMovies = new Linked_List<Movie>(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the Path : "); String path = br.readLine(); FileReader fr = new FileReader(path); BufferedReader movieReader = new BufferedReader(fr); String line = null; Movie movie = new Movie(); while ((line = movieReader.readLine()) != null) { movie = new Movie(); } } movieReader.close(); } }

This is my linked list class:

package movies; import java.util.NoSuchElementException; public class Linked_List<T> implements Iterable<T> { private class MovieNode { T Movie; MovieNode next; MovieNode prev; MovieNode(T Movie) { this.Movie = Movie;} MovieNode(T Movies, MovieNode next, MovieNode prev) { this.Movie = Movies; this.next = next; this.prev = prev; } } private MovieNode head; private MovieNode tail; private int numOfItems; public Linked_List() {} public Linked_List(Linked_List<T> other) { numOfItems = other.numOfItems; if (numOfItems != 0) { head = tail = new MovieNode(other.head.Movie); MovieNode x = other.head.next; while (x != null) { tail.next = new MovieNode(x.Movie, null, tail); tail = tail.next; x = x.next; } } } public int size() {return numOfItems;} public boolean isEmpty() {return size() == 0;} public T getFirst() { if (isEmpty()) {throw new NoSuchElementException("Accessing empty list");} return head.Movie; } public T getLast() { if (isEmpty()) {throw new NoSuchElementException("Accessing empty list");} return tail.Movie; } public void addFirst(T item) { if (numOfItems++ == 0) {head = tail = new MovieNode(item);} else { head.prev = new MovieNode(item); head.prev.next = head; head = head.prev; } } public void addLast(T item) { if (numOfItems++ == 0) { head = tail = new MovieNode(item); } else { tail.next = new MovieNode(item); tail.next.prev = tail; tail = tail.next; } } public T removeFirst() { if (isEmpty()) { throw new NoSuchElementException("Accessing empty list"); } T deleted = head.Movie; if (numOfItems-- == 1) { head = tail = null; } else { head = head.next; head.prev = null; } return deleted; } public T removeLast() { if (isEmpty()) { throw new NoSuchElementException("Accessing empty list"); } T deleted = tail.Movie; if (numOfItems-- == 1) { head = tail = null; } else { tail = tail.prev; tail.next = null; } return deleted; } public boolean contains(T target) { MovieNode p = head; while (p != null) { if (p.Movie.equals(target)) { return true; } p = p.next; } return false; } public void print() { MovieNode current = head; while(current != null) { System.out.print(current.toString() + " "); current = current.next; } System.out.println(); } public Iterator<T> iterator() { return new MovieIterator(); } private class MovieIterator implements Iterator<T> { private MovieNode nextNode = head; private MovieNode prevNode = null; @Override public boolean hasNext() {return nextNode != null;} @Override public boolean hasPrevious() {return prevNode != null;} @Override public T next() { if (!hasNext()) { throw new NoSuchElementException("Accessing null reference"); } prevNode = nextNode; nextNode = nextNode.next; return prevNode.Movie; } @Override public T previous() { if (!hasPrevious()) { throw new NoSuchElementException("Accessing null reference"); } nextNode = prevNode; prevNode = prevNode.prev; return nextNode.Movie; } @Override public void setNext(T item) { if (!hasNext()) { throw new NoSuchElementException("Accessing null reference"); } nextNode.Movie = item; next(); } @Override public void setPrevious(T item) { if (!hasPrevious()) { throw new NoSuchElementException("Accessing null reference"); } prevNode.Movie = item; previous(); } @Override public T removeNext() { if (!hasNext()) { throw new NoSuchElementException("Accessing null reference"); } T toBeRemoved = nextNode.Movie; if (numOfItems == 1) { head = tail = prevNode = nextNode = null; } else if (prevNode == null) { removeFirst(); nextNode = head; } else if (nextNode.next == null) { removeLast(); nextNode = null; } else { prevNode.next = nextNode.next; nextNode = nextNode.next; nextNode.prev = prevNode; numOfItems--; } return toBeRemoved; } @Override public T removePrevious() { if (!hasPrevious()) { throw new NoSuchElementException("Accessing null reference"); } previous(); return removeNext(); } @Override public void add(T item) { if (!hasPrevious()) { addFirst(item); prevNode = head; nextNode = head.next; } else if (!hasNext()) { addLast(item); prevNode = tail; nextNode = null; } else { MovieNode newNode = new MovieNode(item, nextNode, prevNode); newNode.prev.next = newNode; newNode.next.prev = newNode; prevNode = newNode; numOfItems++; } } } }

And this is the class for getting and setting data to pass to the node constructor:

package movies; import java.util.Date; public class Movies { enum Status {RELEASED, RECEIVED}; public class Movie{ private String name; private String genre; private Date receiveDate; private Date releaseDate; private Enum<Status> status; public Movie() {} public Movie(String n, String g, Date rec, Date rel, Enum<Status> s) { name = n; genre = g; receiveDate = rec; releaseDate = rel; set_Status(s); } public String get_name() { return name; } public String get_genre() { return genre; } public Date get_receive_date() { return receiveDate; } public Date get_release_date() { return releaseDate; } public Enum<Status> get_Status() { return status; } public void set_name(String name) { this.name = name; } public void set_genre(String genre) { this.genre = genre; } public void set_receive_date(Date receiveDate) { this.receiveDate = receiveDate; } public void set_release_date(Date releaseDate) { this.releaseDate = releaseDate; } public void set_Status(Enum<Status> status) { this.status = status; } } }

Any advice would be greatly appreciated, as I find I'm really struggling with this.

Edit: Forgot to add that the information in the text file I was provided for testing is formatted like this:

Edge Path,Comedy,11/03/2024,02/20/2025,received

Read Entire Article