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

Using Java.. I need a program that will first load customer and movie informatio

ID: 3663105 • Letter: U

Question

Using Java.. I need a program that will first load customer and movie information from text files and keep the information in its memory (suggestion to use ArrayList class for database structure, but you can use any type of list/array)

Then the program will enable a video store employee to complete the following tasks:

1- Add a movie to the collection of movies (database) available for rent.

2- Find information about a movie (name, author, genre, year, and available status) by giving just movie name.

3. Genre includes: drama, comedy, action, and horror

4. Search for movies in the database with a specific genre (type) and/or from a particular year on.

5. Remove a movie from database by giving name, author, genre, and year as long as the movie is available.

6- Add a customer (name, id, checking out movie, and accumulate number of movies that customer has checked out) to the collection of customers (database) that can rent movies.

7. Find information about a customer.

8- Remove a customer from the database as long as that customer doesn’t check out any movie

9- Rent a movie.

10- Return a movie.

11- Print all the movies in the database with sort choice by employee (by name, by author, by year, by genre)

12- Print all the customers in the database with sort choice by employee (by name, by id, by number of rented movies)

Assumptions

Customer' names are unique and consist of a first name and last name (no middle initial).

Movies' titles are unique.

A customer can only rent one movie and immediately pays for it.

A customer must return all movies in order to check out more

The store keeps only one copy of each movie title.

When the program is started if there is no database file, there are no customers or movies in the system.

3 Required Classes

You must implement and use the following classes:

Customer

This class represents a customer. A customer is identified by a name (firstname and lastname), id, name movies that the customercurrently has rented, and accumulated number of movies that customer rented.

Movie

This class represents a movie. A customer is identified by a name, author, genre, and year.

VideoStore

Methods (all are public methods)

o   addMovie: Adds a movie to the collection of movies. The collection of movies is kept sorted by title. The method prototype is given below:

boolean addMovie(String title, String author, int year, String genre)

The year parameter represents the year the movie was released. The genre parameter refers to the film's genre (e.g., Action, Horror, Romance, etc.). This method will return true if the add operation was completed successfully and false if the movie is already part of the database.

o   findMovie: Returns a String with information about the movie or null if the movie is not part of the database.

String findMovie(String title)

Throw exception NotFoundMovieException if not able to find the movie

If the movie is not currently rented, the format of the String to return is:

Title: "title", Author: “author”, Year: year, Genre: genre

where title, year, and genre correspond to the movie's title, year and genre. If the movie is currently rented then the format is:

Title: "title", Author: “author”, Year: year, Genre: genre (RENTED)

o   findMovies: Returns a String with those movies that have the specified genre (e.g., Horror, Action, Romance, etc.) and that were released on or after the specified year.

String findMovies(String genre, int year)

If genre is null then the genre is ignored in the search process. If year is -1 the year value is ignored in the search process. The method will return null if no movies satisfy the search criteria. The format of the String to be returned is a sequence of movies each on a line by itself.

o   addCustomer: Adds to the database the customer whose name is specified in the parameters.

boolean addCustomer(String firstName, String lastName)

Other field associated with a new customer must be initialized correctly. A unique customer’s id must be generated automatically (and sequentially with the highest existing customer's id). The rented movie field will be set to null, and the number of rented movies will be zero. The method will return true if the add operation was successful and false if the customer is already part of the database.

o   findCustomer: Returns a String with information about a customer or null if the customer is not part of the database.

String findCustomer(String firstName, String lastName)

Note that we can pass empy string (or null string) for either field, and the function should return all possible customers which has the same name as specified by other parameter. For example:

            findCustomer(“Tim”, null)

            should return all customers have first name as “Tim”

The format of the String to return is one of the following. If the customer has no rented movies the result is:

Name: name, ID: id, No movies rented,#.

If the customer has rented movies the result is:

Name: name, ID: id, Movies (rented): MovieName,#.

o   removeCustomer: Removes the specified customer from the database.

void removeCustomer(String firstName, String lastName)

Throw exception NotFoundCustomerException if not able to find the customer

o   checkoutMovie: A reference to the specified movie is added to the array of movies associated with the specified customer.

void checkoutMovie(String firstName, String lastName, String title)

The following error conditions can arise as a result of this operation. These conditions must be tested in the following order. Once an error condition has been recognized, the method will return that condition and ignore the rest

Throw exception NotFoundCustomerException if not able to find the customer

Throw exception NotFoundMovieException if not able to find the movie

Throw exception InvalidMovieRentException if movie is current being rented

o   checkinMovie: Removes the reference to the specified movie from the array of movies associated with the customer.

void checkinMovie(String firstName, String lastName, String title)

The following error conditions can arise as a result of this operation. These conditions must be tested in the following order. Once an error condition has been recognized, the method will return that condition and ignore the rest.

Throw exception NotFoundCustomerException if not able to find the customer

Throw exception NotFoundMovieException if not able to find the movie

o   printAllMovies: Returns a String with all the movies in the database.

String allMovies(int criteria)

When doing printall movies, I would expect to be able teo see the list sorted by name, author, year, or genre

The format of the output string is similar as method findMovies above. A value of null will be returned if there are no movies in the database.

o   printAllCustomers: Returns a String will all the customers in the database.

String allCustomers(int criteria)

When doing printall customers, I would expect to be able to see the list can be sorted by firstname, lastname, id, or the number of rented movies.

The format of the String to be returned is a sequence of customers which is the similar as findCustomer above each on a line by itself. We refer to this list as the CustomerList. A value of null will be returned if there are no customers in the database.

Explanation / Answer

TextIO has the ability to write data to files and to read data from files. TextIO includes output functions TextIO.put, TextIO.putln, and TextIO.putf. Ordinarily, these functions work exactly likeSystem.out.print, System.out.println, and System.out.prinf and are interchangeable with them. However, they can also be used to output text to files and to other destinations.

When you write output using TextIO.put, TextIO.putln, or TextIO.putf, the output is sent to the current output destination. By default, the current output destination is standard output. However,TextIO has subroutines that can be used to change the current output destination. To write to a file named "result.txt", for example, you would use the statement:

After this statement is executed, any output from TextIO output statements will be sent to the file named "result.txt" instead of to standard output. The file will be created if it does not already exist. Note that if a file with the same name already exists, its previous contents will be erased without any warning!

When you call TextIO.writeFile, TextIO remembers the file and automatically sends any output from TextIO.put or other output functions to that file. If you want to go back to writing to standard output, you can call

Here is a simple program that asks the user some questions and outputs the user's responses to a file named "profile.txt." As an example, it uses TextIO for output to standard output as well as to the file, but System.out could also have been used for the output to stadard output.

In many cases, you want to let the user select the file that will be used for output. You could ask the user to type in the file name, but that is error-prone, and users are more familiar with selecting a file from a file dialog box. The statement

will open a typical graphical-user-interface file selection dialog where the user can specify the output file. This also has the advantage of alerting the user if they are about to replace an existing file. It is possible for the user to cancel the dialog box without selecting a file. TextIO.writeUserSelectedFile is a function that returns a boolean value. The return value is true if the user selected a file, and is false if the user canceled the dialog box. Your program can check the return value if it needs to know whether it is actually going to write to a file or not.

TextIO can also read from files, as an alternative to reading from standard input. You can specify an input source for TextIO's various "get" functions. The default input source is standard input. You can use the statement TextIO.readFile("data.txt") to read from a file named "data.txt" instead, or you can let the user select the input file with a GUI-style dialog box by sayingTextIO.readUserSelectedFile(). After you have done this, any input will come from the file instead of being typed by the user. You can go back to reading the user's input withTextIO.readStandardInput().

When your program is reading from standard input, the user gets a chance to correct any errors in the input. This is not possible when the program is reading from a file. If illegal data is found when a program tries to read from a file, an error occurs that will crash the program. (Later, we will see that it is possible to "catch" such errors and recover from them.) Errors can also occur, though more rarely, when writing to files.

TextIO makes it easy to get input from the user. However, since it is not a standard class, you have to remember to make TextIO.java available to any program that uses it. Another option for input is the Scanner class. One advantage of using Scanner is that it's a standard part of Java and so is always there when you want it.

First, you should add the following line to your program at the beginning of the source code file, before the "public class...":

Then include the following statement at the beginning of your main() routine:

This creates a variable named stdin of type Scanner. (You can use a different name for the variable if you want; "stdin" stands for "standard input.") You can then use stdin in your program to access a variety of subroutines for reading user input. For example, the function stdin.nextInt() reads one value of type int from the user and returns it. It is almost the same as TextIO.getInt()except for two things: If the value entered by the user is not a legal int, then stdin.nextInt() will crash rather than prompt the user to re-enter the value. And the integer entered by the user must be followed by a blank space or by an end-of-line, whereas TextIO.getInt() will stop reading at any character that is not a digit.

There are corresponding methods for reading other types of data, including stdin.nextDouble(), stdin.nextLong(), and stdin.nextBoolean(). (stdin.nextBoolean() will only accept "true" or "false" as input.) These subroutines can read more than one value from a line, so they are more similar to the "get" versions of TextIO subroutines rather than the "getln" versions. The methodstdin.nextLine() is equivalent to TextIO.getln(), and stdin.next(), like TextIO.getWord(), returns a string of non-blank characters.

As a simple example, here is a version of the sample program Interest2.java that uses Scanner instead of TextIO for user input:

Note the inclusion of the two lines given above to import Scanner and create stdin. Also note the substitution of stdin.nextDouble() for TextIO.getlnDouble(). (In fact, stdin.nextDouble() is really equivalent to TextIO.getDouble() rather than to the "getln" version, but this will not affect the behavior of the program as long as the user types just one number on each line of input.)

I will continue to use TextIO for input for the time being, but I will give a few more examples of using Scanner in the on-line solutions to the end-of-chapter exercises. There will be more detailed coverage of Scanner later in the book.

class FileStreamsReadnWrite {

       public static void main(String[] args) {

              try {

                     File stockInputFile = new File("C://stock/stockIn.txt");

                     File StockOutputFile = new File("C://stock/StockOut.txt");

                     /*

                     * Constructor of FileInputStream throws FileNotFoundException if

                     * the argument File does not exist.

                     */

                     FileInputStream fis = new FileInputStream(stockInputFile);

                     FileOutputStream fos = new FileOutputStream(StockOutputFile);

                     int count;

                     while ((count = fis.read()) != -1) {

                           fos.write(count);

                     }

                     fis.close();

                     fos.close();

              } catch (FileNotFoundException e) {

                     System.err.println("FileStreamsReadnWrite: " + e);

              } catch (IOException e) {

                     System.err.println("FileStreamsReadnWrite: " + e);

              }

       }

}



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