Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Help: (continued) (continued) Implementing a Book Store using Multithreading In this you will implement a simulation in Java. This simulation will have multiple producers

Java Help:

image text in transcribed

(continued)image text in transcribed

image text in transcribed

(continued)

image text in transcribed

Implementing a Book Store using Multithreading In this you will implement a simulation in Java. This simulation will have multiple producers (book sellers) and consumers (book buyers). Each producer and consumer will run on a separate thread, and there can be multiple producer and consumer threads active in the application at any one time. You should create 5.java files in total, as detailed below: Book.java, Bookstore.java, Buyer.java, Seller.java and Main.java. Part 1: Book.java Create a class Book. This class should have private fields for the following details: storecode, price and genre. When the constructor of this class is called, it should create a new instance of the Book class that has randomly generated values for these details. Storecodes should be stored as strings and have a format like the following 11-A-3218". Prices can be rounded to the nearest euro and the generated values should be between 10 and 200. Genres should be stored as strings and should be chosen randomly from one of a finite list of options (hint: create a static final array listing the genres, then select one of these at random for each new Book object created) It may be helpful to override the toString() method, so that the details of a Book may be easily printed to the console as per the code sample below: // create a new Book object with a randomly chosen storecode, genre and price Book b = new Book(); // prints e.g. "horror book with storecode 12-D-6341 worth 112" System.out.println(b.toString(); Part 2: Book Store.java Create a class BookStore. This class should have a private field that specifies its capacity (i.e. the maximum number of books the store can hold at once). Try using a capacity of 5 or 10 books. It should also have a private field that holds a list of books (using e.g. an ArrayList). BookStore should have a method isEmpty() that returns a boolean indicating whether the store is currently empty, and a method is Full() that returns a boolean indicating whether the store is currently full. BookStore should also have an add Book() method to add a new Book to the list, and a takeBook() method to remove the first (oldest) Book in the list and return it to the caller. You may assume that a Buyer will always buy the first available Book in the ArrayList. Part 3: Buyer.java This class will represent customers who wish to buy books from the BookStore. Each instance of Buyer will run as a separate thread, and so the Buyer class should implement the Runnable interface. Each Buyer instance should be assigned a unique ID when it first tries to interact with the BookStore (i.e. when its run() method is called). This unique ID should be stored in a private integer field. Static thread safe fields (e.g. instances of Atomicinteger) should be used to count the total number of buyers created, and the total number of purchases successfully completed. The Buyer constructor should accept an instance of BookStore as an argument, and then store the reference to this object in a private field. The Buyer run() method should attempt to buy a Book from the store. It should first check that there is at least one Book available to buy in the store (by calling the isEmpty() method of the BookStore instance). If there are no books available, the Buyer should wait until a book becomes available before proceeding. The Buyer should then proceed to take a book from the BookStore using the takeBook() method. Once a Buyer has successfully completed a purchase, its run() method can terminate (i.e. the thread has finished its work). Some of the operations in the run() method will need to be made be made thread safe to prevent race conditions (e.g. two buyers trying to buy the same Book). Your answer should use the wait() and notify()otifyAll() methods. A message should be printed to the console each time a new Buyer reaches the store (directly after its run() method is called), each time a Buyer waits to buy a Book, and each time a Buyer successfully buys a Book from the BookStore (see example program output on the last page). Part 4: Seller.java This class will represent customers who wish to sell books to the store. Each instance of Seller will run as a separate thread, and so the Seller class should implement the Runnable interface. Each Seller instance should be assigned a unique ID when it first tries to interact with the BookStore (i.e. when its run() method is called), Static thread safe fields (e.g. instances of Atomicinteger) should be used to count the total number of sellers created, and the total number of sales successfully completed. The Seller constructor should accept an instance of BookStore as an argument, and then store the reference to this object in a private field. The Seller run() method should attempt to buy a Book from the store. It should first check that the BookStore has space available for another Book (by calling the isFull() method of the BookStore instance). If there is no space available, the Seller should wait until a space becomes available before proceeding. The Seller should then proceed to add its Book instance to the BookStore. Once a Seller has successfully completed a sale, its run() method can terminate (i.e. the thread has finished its work). Some of the operations in the run() method should be made thread safe to prevent race conditions (e.g. two sellers trying to sell books at the same time and causing the BookStore capacity to be exceeded). Your answer should use the wait() and notify()otifyAll() methods. A message should be printed to the console each time a new Seller reaches the store (directly after its run() method is called), each time a Seller waits to sell a Book, and each time a Seller successfully sells a Book to the BookStore (see example program output on the last page). Part 5: Main.java This file will contain the main() method that will run your book store simulation. It should create a single instance of the BookStore class; this instance should be passed into the constructors of any Buyer or Seller objects that are created. Your main method should simulate a number of days (e.g. 30) at the book store (hint: use e.g. a while loop and repeat the same operations for each day). A message should print to the console announcing the number of books available at the start of each day (see example program output on the last page) Each day a random number of Buyers (between 0 and e.g. 3) and Sellers (between 0 and e.g. 3) can arrive at the store. For each new Buyer/Seller a new thread must be created and started. You should also add a small delay at the end of each day (loop iteration) to slow down the execution of the program Bonus harder features (not essential): Try limiting the purchasing power of Buyers, i.e. give them a random amount of money to spend. If there are no Books within a Buyer's budget, they would have to wait until an affordable Book shows up on the sales floor Try implementing randomly generated preferences for each Buyer that is created e.g. a preference for a certain genre of Book, or for Books registered after a certain year. Buyers would then choose to buy only a Book that matches their preference (and perhaps wait until an appropriate Book shows up in the BookStore) Adda markup for the store so that the sale price to Buyers is e.g. 20% higher than the price paid to Sellers Sample (truncated) program output: Day 1 beginning. There are 0 books in the store today. A new buyer #1 just appeared. Buyer #1 wants to buy a book, but the store is empty A new seller #1 just appeared with a horror book with storecode 11-G-7212 worth 39 to sell A new seller #3 just appeared with a thriller book with storecode 18-G-2743 worth 140 to sell Seller #1 sold their horror book with storecode 11-G-7212 worth 39 to the store. This is sale number 1. A new seller #2 just appeared with a drama book with storecode 18-G-2469 worth 14 to sell Buyer #1 bought a horror book with storecode 11-G-7212 worth 39 from the store. This is purchase number 1. Seller #3 sold their thriller book with storecode 18-G-2743 worth 140 to the store. This is sale number 2. Seller #2 sold their drama book with storecode 18-G-2469 worth 14 to the store. This is sale number 3 Day 2 beginning. There are 2 books in the store today. A new seller #4 just appeared with a thriller book with storecode 18-G-5011 worth 108 to sell A new seller #5 just appeared with a thriller book with storecode 15-G-8043 worth 119 to sell. Seller #4 sold their thriller book with storecode 18-G-5011 worth 108 to the store. This is sale number 4. Seller #5 sold their thriller book with storecode 15-G-8043 worth 119 to the store. This is sale number 5. Day 3 beginning. There are 4 books in the store today. A new seller #6 just appeared with a fantasy book with storecode 18-G-756 worth 198 to sell. A new seller #7 just appeared with a thriller book with storecode 16-G-2453 worth 185 to sell. Seller #6 sold their fantasy book with storecode 18-G-756 worth 198 to the store. This is sale number 6. Seller #7 is trying to sell a book, but the store is full. Day 4 beginning. There are 5 books in the store today. A new buyer #2 just appeared. A new seller #8 just appeared with a fantasy book with storecode 10-G-5572 worth 114 to sell. Buyer #2 bought a thriller book with storecode 18-G-2743 worth 140 from the store. This is purchase number 2. Seller #7 sold their thriller book with storecode 16-G-2453 worth 185 to the store This is sale number 7. Seller #8 is trying to sell a book, but the store is full. Day 5 beginning. There are 5 books in the store today. Implementing a Book Store using Multithreading In this you will implement a simulation in Java. This simulation will have multiple producers (book sellers) and consumers (book buyers). Each producer and consumer will run on a separate thread, and there can be multiple producer and consumer threads active in the application at any one time. You should create 5.java files in total, as detailed below: Book.java, Bookstore.java, Buyer.java, Seller.java and Main.java. Part 1: Book.java Create a class Book. This class should have private fields for the following details: storecode, price and genre. When the constructor of this class is called, it should create a new instance of the Book class that has randomly generated values for these details. Storecodes should be stored as strings and have a format like the following 11-A-3218". Prices can be rounded to the nearest euro and the generated values should be between 10 and 200. Genres should be stored as strings and should be chosen randomly from one of a finite list of options (hint: create a static final array listing the genres, then select one of these at random for each new Book object created) It may be helpful to override the toString() method, so that the details of a Book may be easily printed to the console as per the code sample below: // create a new Book object with a randomly chosen storecode, genre and price Book b = new Book(); // prints e.g. "horror book with storecode 12-D-6341 worth 112" System.out.println(b.toString(); Part 2: Book Store.java Create a class BookStore. This class should have a private field that specifies its capacity (i.e. the maximum number of books the store can hold at once). Try using a capacity of 5 or 10 books. It should also have a private field that holds a list of books (using e.g. an ArrayList). BookStore should have a method isEmpty() that returns a boolean indicating whether the store is currently empty, and a method is Full() that returns a boolean indicating whether the store is currently full. BookStore should also have an add Book() method to add a new Book to the list, and a takeBook() method to remove the first (oldest) Book in the list and return it to the caller. You may assume that a Buyer will always buy the first available Book in the ArrayList. Part 3: Buyer.java This class will represent customers who wish to buy books from the BookStore. Each instance of Buyer will run as a separate thread, and so the Buyer class should implement the Runnable interface. Each Buyer instance should be assigned a unique ID when it first tries to interact with the BookStore (i.e. when its run() method is called). This unique ID should be stored in a private integer field. Static thread safe fields (e.g. instances of Atomicinteger) should be used to count the total number of buyers created, and the total number of purchases successfully completed. The Buyer constructor should accept an instance of BookStore as an argument, and then store the reference to this object in a private field. The Buyer run() method should attempt to buy a Book from the store. It should first check that there is at least one Book available to buy in the store (by calling the isEmpty() method of the BookStore instance). If there are no books available, the Buyer should wait until a book becomes available before proceeding. The Buyer should then proceed to take a book from the BookStore using the takeBook() method. Once a Buyer has successfully completed a purchase, its run() method can terminate (i.e. the thread has finished its work). Some of the operations in the run() method will need to be made be made thread safe to prevent race conditions (e.g. two buyers trying to buy the same Book). Your answer should use the wait() and notify()otifyAll() methods. A message should be printed to the console each time a new Buyer reaches the store (directly after its run() method is called), each time a Buyer waits to buy a Book, and each time a Buyer successfully buys a Book from the BookStore (see example program output on the last page). Part 4: Seller.java This class will represent customers who wish to sell books to the store. Each instance of Seller will run as a separate thread, and so the Seller class should implement the Runnable interface. Each Seller instance should be assigned a unique ID when it first tries to interact with the BookStore (i.e. when its run() method is called), Static thread safe fields (e.g. instances of Atomicinteger) should be used to count the total number of sellers created, and the total number of sales successfully completed. The Seller constructor should accept an instance of BookStore as an argument, and then store the reference to this object in a private field. The Seller run() method should attempt to buy a Book from the store. It should first check that the BookStore has space available for another Book (by calling the isFull() method of the BookStore instance). If there is no space available, the Seller should wait until a space becomes available before proceeding. The Seller should then proceed to add its Book instance to the BookStore. Once a Seller has successfully completed a sale, its run() method can terminate (i.e. the thread has finished its work). Some of the operations in the run() method should be made thread safe to prevent race conditions (e.g. two sellers trying to sell books at the same time and causing the BookStore capacity to be exceeded). Your answer should use the wait() and notify()otifyAll() methods. A message should be printed to the console each time a new Seller reaches the store (directly after its run() method is called), each time a Seller waits to sell a Book, and each time a Seller successfully sells a Book to the BookStore (see example program output on the last page). Part 5: Main.java This file will contain the main() method that will run your book store simulation. It should create a single instance of the BookStore class; this instance should be passed into the constructors of any Buyer or Seller objects that are created. Your main method should simulate a number of days (e.g. 30) at the book store (hint: use e.g. a while loop and repeat the same operations for each day). A message should print to the console announcing the number of books available at the start of each day (see example program output on the last page) Each day a random number of Buyers (between 0 and e.g. 3) and Sellers (between 0 and e.g. 3) can arrive at the store. For each new Buyer/Seller a new thread must be created and started. You should also add a small delay at the end of each day (loop iteration) to slow down the execution of the program Bonus harder features (not essential): Try limiting the purchasing power of Buyers, i.e. give them a random amount of money to spend. If there are no Books within a Buyer's budget, they would have to wait until an affordable Book shows up on the sales floor Try implementing randomly generated preferences for each Buyer that is created e.g. a preference for a certain genre of Book, or for Books registered after a certain year. Buyers would then choose to buy only a Book that matches their preference (and perhaps wait until an appropriate Book shows up in the BookStore) Adda markup for the store so that the sale price to Buyers is e.g. 20% higher than the price paid to Sellers Sample (truncated) program output: Day 1 beginning. There are 0 books in the store today. A new buyer #1 just appeared. Buyer #1 wants to buy a book, but the store is empty A new seller #1 just appeared with a horror book with storecode 11-G-7212 worth 39 to sell A new seller #3 just appeared with a thriller book with storecode 18-G-2743 worth 140 to sell Seller #1 sold their horror book with storecode 11-G-7212 worth 39 to the store. This is sale number 1. A new seller #2 just appeared with a drama book with storecode 18-G-2469 worth 14 to sell Buyer #1 bought a horror book with storecode 11-G-7212 worth 39 from the store. This is purchase number 1. Seller #3 sold their thriller book with storecode 18-G-2743 worth 140 to the store. This is sale number 2. Seller #2 sold their drama book with storecode 18-G-2469 worth 14 to the store. This is sale number 3 Day 2 beginning. There are 2 books in the store today. A new seller #4 just appeared with a thriller book with storecode 18-G-5011 worth 108 to sell A new seller #5 just appeared with a thriller book with storecode 15-G-8043 worth 119 to sell. Seller #4 sold their thriller book with storecode 18-G-5011 worth 108 to the store. This is sale number 4. Seller #5 sold their thriller book with storecode 15-G-8043 worth 119 to the store. This is sale number 5. Day 3 beginning. There are 4 books in the store today. A new seller #6 just appeared with a fantasy book with storecode 18-G-756 worth 198 to sell. A new seller #7 just appeared with a thriller book with storecode 16-G-2453 worth 185 to sell. Seller #6 sold their fantasy book with storecode 18-G-756 worth 198 to the store. This is sale number 6. Seller #7 is trying to sell a book, but the store is full. Day 4 beginning. There are 5 books in the store today. A new buyer #2 just appeared. A new seller #8 just appeared with a fantasy book with storecode 10-G-5572 worth 114 to sell. Buyer #2 bought a thriller book with storecode 18-G-2743 worth 140 from the store. This is purchase number 2. Seller #7 sold their thriller book with storecode 16-G-2453 worth 185 to the store This is sale number 7. Seller #8 is trying to sell a book, but the store is full. Day 5 beginning. There are 5 books in the store today

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 1 Lnai 9851

Authors: Paolo Frasconi ,Niels Landwehr ,Giuseppe Manco ,Jilles Vreeken

1st Edition

3319461273, 978-3319461274

More Books

Students also viewed these Databases questions