You may use an ArrayList or an Array (or some other List/File types) for this as
ID: 3887282 • Letter: Y
Question
You may use an ArrayList or an Array (or some other List/File types) for this assignment.
Please follow the detail requirements below (with the required steps #s in bold):
#1. Create something in java to store thousands of users (or customers) objects. You can use Array/ArrayList/etc for those objects. For this assignment, you can use a simple (dummy) users/customers with a unique ID and some random customer name plus other fields of your choice.
#2. Create 1,000 user/customer objects for the new customers. Place them into the list you declared in step #1. Write a Runnable task to accomplish this process of creating those
#3. In the meantime, continually remove the user/customer objects from your list. Write a Runnable task with a loop of 1,000 removal steps.
#4. (Optional) you may also have a processing thread if you choose to process the objects further between enter and exit. But you do not have to do #4 as this is optional.
#5. Make sure that the processing are concurrent/parallel in steps 2 and 3. /* If you implement the optional step 4 , it has to run in parallel with steps 2&3. */
Print the total # of user/customer(s) left in your list after steps 1~4. You do not need to print each object details; only print the total # left in your list.
Please submit screen captures to show that you have done at least 2 run(s) to see different #s at the end of each run before you implement next requirement in #6.
#6. Implement additional control to make sure the removal process is NOT ahead of the user/customer object creation process AND make sure that the creation process has to pause when the list has reach capacity. The creation process continues when one or more objects are removed to allow new objects. Then create more objects to flood the list capacity you got from step #1.
Submit another screen capture to show the total # of users/customers left after you added the additional thread control.
#7. Make sure you also add comments in your java codes to document your solutions.
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.september;
import java.util.ArrayList;
import java.util.Random;
/**
*
* @author Sam
*/
public class ConsumerProducer {
static class Customer {
int id;
String name;
double billedAmount;
public Customer(int id, String name, double billedAmount) {
this.id = id;
this.name = name;
this.billedAmount = billedAmount;
}
@Override
public String toString() {
return "Customer{" + "id=" + id + ", name=" + name + ", billedAmount=" + billedAmount + '}';
}
}
static ArrayList<Customer> customers = new ArrayList<>(1000);
static int capacity = 5;
static class Producer implements Runnable {
Random r = new Random(System.currentTimeMillis());
void produceRandomCustomer() {
int id = r.nextInt(1000) + 10000;
String name = "Customer: " + id;
double billedAmount = r.nextDouble()*10000;
Customer newCustomer = new Customer(id, name, billedAmount);
customers.add(newCustomer);
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
if (customers.size() >= capacity) //part 6
i--;
else
produceRandomCustomer();
System.out.print(""); // do somthing
}
}
}
static class Consumer implements Runnable {
int consumeCustomer() {
try {
customers.remove(0);
return 1;
} catch (IndexOutOfBoundsException e) { //you must handle this
return 0;
}
}
@Override
public void run() {
for (int i = 0; i < 1000; ) {
i += consumeCustomer();
System.out.println("Customer left: " + customers.size());
}
}
}
public static void main(String[] args) {
Producer producer = new Producer();
Consumer consumer = new Consumer();
Thread t1 = new Thread(producer);
Thread t2= new Thread(consumer);
t1.start();
t2.start();
}
}
Let me know if tou face any trouble
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.