Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have already created the classes I am just very stuck on how to search the arr

ID: 641810 • Letter: I

Question

I have already created the classes I am just very stuck on how to search the array list so the "customer " can look for and rent items.

This is my text file and this is what I have so far...

AMERICAN SNIPER,2014,11111,10,STANDARD
THE GUARDIAN,2006,11112,10,STANDARD
THE SANTA CLAUSE,1994,11113,10,STANDARD
SPIDERMAN,2002,11114,5,BLURAY
HERCULES,1992,11115,5,BLURAY
PLANET OF THE APES,2014,11116,5,BLURAY
POKEMON,1995,11222,8,WII
EVOLVE,2015,12223,8,XBOX
SAINTS ROW,2006,12323,8,PS

public static void main(String[] args) {

       ArrayList<RentalItem> rentals = new ArrayList<RentalItem>();

       String filename;
       String data;
       Scanner kb = new Scanner(System.in);

       System.out.print("Enter File Name: ");
       filename = kb.nextLine();

       try {
           File inFile = new File(filename);
           Scanner inputFile = new Scanner(inFile);
           while (inputFile.hasNext()) {
               data = inputFile.nextLine();
               System.out.println(data);

               StringTokenizer tokens = new StringTokenizer(data, ";");
               String title = tokens.nextToken();
               int year = Integer.parseInt(tokens.nextToken());
               int upc = Integer.parseInt(tokens.nextToken());
               int stock = Integer.parseInt(tokens.nextToken());
               String platform = tokens.nextToken();
               String movietype = tokens.nextToken();
           }
          
          
           inputFile.close();
       } catch (FileNotFoundException fileError) {
           System.out.println(filename + " cannot be found!");

       } catch (NoSuchElementException dataError) {
           System.out.println("Error reading file!");

       }

   }

}

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;

//class RentalItem
class RentalItem{
  
   private int year;
   private int fee;
   private int stock;
   private String platform;
   private String title;
  
   public int getYear() {
       return year;
   }
   public void setYear(int year) {
       this.year = year;
   }
   public int getUpc() {
       return fee;
   }
   public void setUpc(int upc) {
       this.fee = upc;
   }
   public int getStock() {
       return stock;
   }
   public void setStock(int stock) {
       this.stock = stock;
   }
   public String getPlatform() {
       return platform;
   }
   public void setPlatform(String platform) {
       this.platform = platform;
   }
  
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
   @Override
   public String toString() {
      
       return this.title+","+this.year+","+this.fee+","+this.stock+","+this.platform;
   }
}


//Application for the rental movie
public class Client {
   //private static Scanner sc = new Scanner(System.in);
   private static ArrayList<RentalItem> rentalMovieList = new ArrayList<RentalItem>();
  
  
   //load the movies from the file
   private static void loadMovies(){
       Scanner sc = new Scanner(System.in);
       BufferedReader br=null;
          String filename;
           System.out.print("Enter File Name: ");
           filename = sc.next();//to read the file name

           try {
                     
               br = new BufferedReader(new FileReader("D:\sample.txt"));//accessing the file
               String sCurrentLine;
               while ((sCurrentLine = br.readLine()) != null) {
                              
                   RentalItem rentalItem=new RentalItem();//create an object of RentalMovie
                   StringTokenizer tokens = new StringTokenizer(sCurrentLine, ",");
                   String title = tokens.nextToken();
                   int year = Integer.parseInt(tokens.nextToken());
                   int fee = Integer.parseInt(tokens.nextToken());
                   int stock = Integer.parseInt(tokens.nextToken());
                   String platform = tokens.nextToken();
                   rentalItem.setPlatform(platform);
                   rentalItem.setTitle(title);
                   rentalItem.setUpc(fee);
                   rentalItem.setYear(year);
                   rentalItem.setStock(stock);
                   rentalMovieList.add(rentalItem);//adding the object to the arraylist
                 
               }
            
            
              // inputFile.close();
           } catch (FileNotFoundException fileError) {
               System.out.println(filename + " cannot be found!");

           } catch (NoSuchElementException dataError) {
               System.out.println("Error reading file!");

           } catch (NumberFormatException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }finally {
               try {
                   if (br != null)br.close();
                  
               } catch (IOException ex) {
                   ex.printStackTrace();
               }
           }
   }
  
   //function to rent a movie
   private static void rentMovie(){
       Scanner sc = new Scanner(System.in);
       while (true) {
           System.out.println(" Which movie are you interested in?");
           String title = sc.nextLine();
           boolean found=false;
           for (RentalItem rental : rentalMovieList) {
               if (rental.getTitle().equalsIgnoreCase(title)) {
                   if(rental.getStock()>0){
                       System.out.println(rental.getTitle()+" found.");
                       System.out.println("Rent for this movie is "+rental.getUpc());
                       found=true;
                       rental.setStock(rental.getStock()-1); //updating the object
                      
                   }else{
                       System.out.println("This movie is out of stock.Please try for another.");
                   }
                  
                   break;
               }
           }
           if(!found){
               System.out.println("This movie is not found.Please try for another.");
           }
           System.out.println("Want to rent another movie? (y/n): ");
           // to check whether user wants to continue
           if (!yes()) {
               System.out.println("Good bye!!");
               break;
           }
       }
   }
  
   //function to return the rented movie
   private static void returnMovie(){
       Scanner sc = new Scanner(System.in);
       while (true) {
           System.out.println(" Which movie are you returning?");
           String title = sc.nextLine();
           boolean found=false;
           for (RentalItem rental : rentalMovieList) {
               if (rental.getTitle().equalsIgnoreCase(title)) {
                       System.out.println(rental.getTitle()+" has been returned successfully.");
                       found=true;
                       rental.setStock(rental.getStock()+1); //updating the object
                        break;
               }
           }
           if(!found){
               System.out.println("This movie is not found.Please try for another.");
           }
           System.out.println("Want to return another movie? (y/n): ");
           // to check whether user wants to continue
           if (!yes()) {
               System.out.println("Good bye!!");
               break;
           }
       }
   }

   public static void main(String[] args) {

       System.out.println("Welcome to the Movie Rental Application.");
       loadMovies();
       System.out.println(" There are " + rentalMovieList.size()
               + " movies in the database.");
       Scanner sc = new Scanner(System.in);
       // multiple choice program
       int choice = 0;
       do {
           // options for multiple choice
           System.out.println(" What do you want to do?");
           System.out.println("1. Return movie");
           System.out.println("2. Rent movie");
            System.out.println("3. Exit");
           System.out.println("Enter your choice");

           choice = sc.nextInt();

           switch (choice) {
           case 1:
               // calling the returnMovie() function
               returnMovie();
               System.out.println("Movies status"+rentalMovieList);
               break;

           case 2:
               // calling the rentMovie() function
               rentMovie();
               System.out.println("Movies status"+rentalMovieList);
               break;

           case 3:
               //Exit the app
               System.out.println("Good Bye!");
               break;

           default:
               // loop will break when user enters wrong choice
               System.err.println("Please enter correct choice");
               break;
           }

       } while (choice != 4);
      

   }

   // to check whether user wants to continue or not
   private static boolean yes() {
       Scanner sc = new Scanner(System.in);
       String str = null;
       while (true) {
           str = sc.next();
           if (str.equalsIgnoreCase("Y")) {
               return true;
           } else if (str.equalsIgnoreCase("N")) {
               break;
           } else {
               System.out.print("Invalid input. Try again. Enter Y or N: ");
           }
       }
       return false;
   }
         

       
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote