How to write this code (without using linked lists)? This project is based on th
ID: 3821293 • Letter: H
Question
How to write this code (without using linked lists)?
This project is based on the Video Store. The Video Store will do the following tasks:
Rent a video; that is, check out a video.
Return, or check in, a video.
Create a list of videos owned by the store.
Show the details of a particular video.
Print a list of all the videos in the store.
Check whether a particular video is in the store.
Maintain a customer database.
Print a list of all the videos rented by each customer.
The video store has two major components: videos and customers.
The common things associated with a video are as follows:
Title of the movie
Names of the stars
Name of the producer
Name of the director
Name of the production company
Number of copies in the store
The class videoType will be used to implement a video.
The customer object stores information about a customer, such as the first name, last name, account number, and a list of videos rented by the customer.
The class customerType will be used to implement a customer.
The basic operations on an object of type customerType are as follows:
Print the name, the account number, and the list of rented videos.
Set the name and the account number.
Rent a video; that is, add the rented video to the list.
Return a video; that is, delete the rented video from the list.
Show the account number.
The video store will maintain various lists:
A list of all the videos in the store.
A list of all the store’s customers.
Lists of the videos currently rented by each customer
Use the function createVideoList to read the videos data from the videos input file and create a list of videos.
Use another function, createCustomerList, to read the customers data from customers input file and create a list of customers.
Use the function displayMenu to inform the user what to do.
Explanation / Answer
The java program to perform the above functionality is :
VideoStore:
VideoType:
CustomerType:
customers.csv:
videos.csv:
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class VideoStore { private static int choice; private static final String VIDEO_FILE_NAME = "videos.csv"; private static final String CUSTOMER_FILE_NAME = "customers.csv"; private static List<VideoType> videos; private static List<CustomerType> customers; public static void main(String args[]) { CustomerType customerType = null; Scanner scanner = new Scanner(System.in); String firstName = null; String lastName = null; int accNum = 0; String movieTitle = null; videos = createVideoList(); customers = createCustomerList(); choice = displayMenu(); switch(choice) { case 1: System.out.println("Please enter first name"); firstName = scanner.nextLine(); if (customers != null) { for (CustomerType customer : customers) { if (customer.getFirstName().equals(firstName)) { customerType = customer; break; } } if (customerType == null) { System.out.println("Please enter last name"); lastName = scanner.nextLine(); System.out.println("Please enter account number"); accNum = scanner.nextInt(); } } if (lastName != null && accNum != 0) { System.out.println("Adding new customer"); customerType = new CustomerType(firstName, lastName, accNum); addCustomerToFile(customerType); } scanner.nextLine(); System.out.println("Please enter the name of video you wish to rent."); movieTitle = scanner.nextLine(); boolean videoFound = false; for (VideoType video : videos) { if (video.getMovieTitle().equals(movieTitle)) { videoFound = true; if (customerType.rentAVideo(video)) { updateCustomerAndVideoFileAfterRent(customerType, video); } } } if (!videoFound) { System.out.println("This video is not owned by the store."); System.out.println("Please select one of the following: "); for (VideoType video : videos) { System.out.println(video.getMovieTitle()); } } displayMenu(); case 2: System.out.println("Please enter the name of video you wish to return."); movieTitle = scanner.nextLine(); System.out.println("Please enter first name"); firstName = scanner.nextLine(); for (CustomerType customer : customers) { if (customer.getFirstName().equals(firstName)) { customerType = customer; break; } } for (VideoType video : videos) { if (video.getMovieTitle().equals(movieTitle)) { if (customerType.returnAVideo(video)) { updateCustomerAndVideoFileAfterReturn(customerType, video); } } } displayMenu(); case 3: createVideoList(); System.out.println("List of videos created from file."); displayMenu(); case 4: System.out.println("Please enter the name of video."); String videoName = scanner.nextLine(); for (VideoType video : videos) { if (video.getMovieTitle().equals(videoName)) { System.out.println(video); } } displayMenu(); case 5: for (VideoType video : videos) { System.out.println(video); System.out.println(""); } displayMenu(); case 6: System.out.println("Please enter the name of video."); String videoNameForAvailability = scanner.nextLine(); videoFound = false; for (VideoType video : videos) { if (video.getMovieTitle().equals(videoNameForAvailability)) { System.out.println("The video is available!"); videoFound = true; } } if (!videoFound) { System.out.println("Sorry, not available"); } displayMenu(); case 7: createCustomerList(); System.out.println("List of customers created from file."); displayMenu(); case 8: System.out.println("Please enter first name"); firstName = scanner.nextLine(); System.out.println("Videos rented by " + firstName + "are: "); for (CustomerType customer : customers) { if (customer.getFirstName().equals(firstName)) { for (VideoType video : customer.getMoviesRented()) { System.out.println(video.getMovieTitle()); } } } displayMenu(); case 9: break; default: System.out.println("Invalid, try again"); displayMenu(); } } private static void updateCustomerAndVideoFileAfterRent(CustomerType customerType, VideoType videoType) { FileWriter writer; BufferedReader bufferedReader; File customerFile = new File(CUSTOMER_FILE_NAME); File videoFile = new File(VIDEO_FILE_NAME); File tempCustomerFile = new File(CUSTOMER_FILE_NAME + ".tmp"); File tempVideoFile = new File(VIDEO_FILE_NAME + ".tmp"); String line = ""; String comma = ","; try { writer = new FileWriter(tempCustomerFile, false); bufferedReader = new BufferedReader(new FileReader(customerFile)); while ((line = bufferedReader.readLine()) != null) { String[] lineArray = line.split(comma); if (lineArray[0].equals(customerType.getFirstName())) { if (lineArray[3].equals(" ")) { line = line.replace(lineArray[3], videoType.getMovieTitle()); } else { line = line.replace(lineArray[3], (lineArray[3] + ";" + videoType.getMovieTitle())); } } writer.append(line); writer.append(" "); } writer.flush(); writer.close(); bufferedReader.close(); writer = new FileWriter(tempVideoFile, false); bufferedReader = new BufferedReader(new FileReader(videoFile)); while ((line = bufferedReader.readLine()) != null) { String[] lineArray = line.split(comma); if (lineArray[0].equals(videoType.getMovieTitle())) { line = line.replace(lineArray[5], String.valueOf(videoType.getNumberOfCopiesAvailable())); } writer.append(line); writer.append(" "); } writer.flush(); writer.close(); bufferedReader.close(); if (!customerFile.delete()) { System.out.println("Unable to delete " + CUSTOMER_FILE_NAME); } if (!tempCustomerFile.renameTo(customerFile)) { System.out.println("Unable to rename " + CUSTOMER_FILE_NAME); } if (!videoFile.delete()) { System.out.println("Unable to delete " + VIDEO_FILE_NAME); } if (!tempVideoFile.renameTo(videoFile)) { System.out.println("Unable to rename " + VIDEO_FILE_NAME); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static void updateCustomerAndVideoFileAfterReturn(CustomerType customerType, VideoType videoType) { FileWriter writer; BufferedReader bufferedReader; File customerFile = new File(CUSTOMER_FILE_NAME); File videoFile = new File(VIDEO_FILE_NAME); File tempCustomerFile = new File(CUSTOMER_FILE_NAME + ".tmp"); File tempVideoFile = new File(VIDEO_FILE_NAME + ".tmp"); String line = ""; String comma = ","; try { writer = new FileWriter(tempCustomerFile, false); bufferedReader = new BufferedReader(new FileReader(customerFile)); while ((line = bufferedReader.readLine()) != null) { String[] lineArray = line.split(comma); if (lineArray[0].equals(customerType.getFirstName())) { line = line.replace((videoType.getMovieTitle()), ""); line = line.replace(";;", ";"); } writer.append(line); writer.append(" "); } writer.flush(); writer.close(); bufferedReader.close(); writer = new FileWriter(tempVideoFile, false); bufferedReader = new BufferedReader(new FileReader(videoFile)); while ((line = bufferedReader.readLine()) != null) { String[] lineArray = line.split(comma); if (lineArray[0].equals(videoType.getMovieTitle())) { line = line.replace(lineArray[5], String.valueOf(videoType.getNumberOfCopiesAvailable())); } writer.append(line); writer.append(" "); } writer.flush(); writer.close(); bufferedReader.close(); if (!customerFile.delete()) { System.out.println("Unable to delete " + CUSTOMER_FILE_NAME); } if (!tempCustomerFile.renameTo(customerFile)) { System.out.println("Unable to rename " + CUSTOMER_FILE_NAME); } if (!videoFile.delete()) { System.out.println("Unable to delete " + VIDEO_FILE_NAME); } if (!tempVideoFile.renameTo(videoFile)) { System.out.println("Unable to rename " + VIDEO_FILE_NAME); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static void addCustomerToFile(CustomerType customerType) { FileWriter writer; try { writer = new FileWriter(CUSTOMER_FILE_NAME, true); writer.append(" "); writer.append(customerType.getFirstName()).append(",") .append(customerType.getLastName()).append(",") .append(String.valueOf(customerType.getAccountNumber())).append(","); writer.append(" "); writer.flush(); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static int displayMenu() { System.out.println(" Please choose from among the following options:"); System.out.println("1. Rent a video."); System.out.println("2. Return a video."); System.out.println("3. Create a list of all videos available."); System.out.println("4. See the details of a video."); System.out.println("5. See all videos available."); System.out.println("6. Check if a video is available."); System.out.println("7. Create a list of customers."); System.out.println("8. See all videos rented by a customer."); System.out.println("Enter 9 to exit the application."); Scanner scanner = new Scanner(System.in); return scanner.nextInt(); } private static List<VideoType> createVideoList() { String line = ""; String comma = ","; String semicolon = ";"; List<VideoType> videos = new ArrayList<>(); try { BufferedReader br = new BufferedReader(new FileReader(VIDEO_FILE_NAME)); while ((line = br.readLine()) != null) { String[] lineArray = line.split(comma); VideoType video = new VideoType(); for (int i = 0; i < lineArray.length; i++) { if (lineArray[i].contains(semicolon)) { String[] actors = lineArray[i].split(semicolon); List<String> actorList = new ArrayList<>(); for (String actor : actors) { actorList.add(actor); } video.setActors(actorList); } video.setMovieTitle(lineArray[0]); video.setProducer(lineArray[2]); video.setDirector(lineArray[3]); video.setProductionCompany(lineArray[4]); video.setNumberOfCopiesAvailable(new Integer(lineArray[5])); } videos.add(video); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return videos; } private static List<CustomerType> createCustomerList() { videos = createVideoList(); String line = ""; String comma = ","; String semicolon = ";"; List<CustomerType> customers = new ArrayList<>(); try { BufferedReader br = new BufferedReader(new FileReader(CUSTOMER_FILE_NAME)); while ((line = br.readLine()) != null) { String[] lineArray = line.split(comma); CustomerType customerType = new CustomerType(); for (int i = 0; i < lineArray.length; i++) { if (lineArray[i].contains(semicolon)) { String[] moviesRented = lineArray[i].split(semicolon); List<VideoType> movieList = new ArrayList<>(); for (String movie : moviesRented) { for (VideoType video : videos) { if (video.getMovieTitle().equals(movie)) { movieList.add(video); } } } customerType.setMoviesRented(movieList); } customerType.setFirstName(lineArray[0]); customerType.setLastName(lineArray[1]); customerType.setAccountNumber(Integer.parseInt(lineArray[2])); } customers.add(customerType); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return customers; } } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.