What can do you if someone tries to add one more shopper to a full list? Include
ID: 665078 • Letter: W
Question
What can do you if someone tries to add one more shopper to a full list? Include code in addShopper to handle this. You should also have a way to change the value of canAdd when the waiting list becomes full.
After you have made your edits to Shopper.java and Product.java, copy/paste the following code into a file and save as Tester.java, in the same folder as the other two. You can run the Tester program in order to verify that your Shopper and Product classes are working as you intended. Code for this class may not compile until Shopper and Product classes are complete.
============================================================
import java.util.Random;
public class Shopper {
// YOUR CODE: instance variables
String name;
final int ID=10;
static int shopperID;
// YOUR CODE: The constructor
Shopper(String name)
{
this.name=name;
Random rn = new Random();
this.shopperID = rn.nextInt(99) + 10;
}
// YOUR CODE: getName method
public String getName() {
return name;
}
// YOUR CODE: getID method
public int getID() {
return shopperID;
}
// YOUR CODE: setName method
public void setName(String name) {
this.name = name;
}
// YOUR CODE: toString method
public String toString()
{
return "Name : "+getName()+", ID : "+getID();
}
public static void main(String[] args){
Shopper firstShopper = new Shopper("Jane Doe");
Shopper secondShopper = new Shopper("John Doe");
Shopper thirdShopper = new Shopper("Rob Doe");
System.out.printf("The 1st Shopper's name is %s. ", firstShopper.getName());
System.out.printf("The 2nd Shopper's ID is %d. ", secondShopper.getID());
System.out.print("And the 3rd Shopper... ");
System.out.println(thirdShopper);
System.out.println();
System.out.printf("The 2nd Shopper's name was %s... ", secondShopper.getName());
secondShopper.setName("William Shakespeare");
System.out.printf("...but it is now %s. ", secondShopper.getName());
}
}
================================
import java.util.Random;
public class Product {
// YOUR CODE: instance variables
String name;
final int ITEM_ID=10;
static int ID;
String productDescription;
// YOUR CODE: the constructor
public Product(String name, String productDescription) {
this.name = name;
this.productDescription = productDescription;
Random rn = new Random();
this.ID = rn.nextInt(99) + 10;
}
// YOUR CODE: getName method
public String getName() {
return name;
}
// YOUR CODE: getID method
public int getID() {
return ID;
}
// YOUR CODE: getProductDescription method
public String getProductDescription() {
return productDescription;
}
// YOUR CODE: setName method
public void setName(String name) {
this.name = name;
}
// YOUR CODE: setProductDescription method
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
// YOUR CODE: toString method
public String toString()
{
return "Product Name: "+getName()+", ID:"+ID+", Description: "+getProductDescription();
}
public static void main(String[] args){
Product firstProduct = new Product("Microwave Oven", "Cooks your food");
Product secondProduct = new Product("Toaster", "Toasts your bread");
Product thirdProduct = new Product("Bookcase", "Holds your books");
System.out.printf("The 1st product's name is %s. ", firstProduct.getName());
System.out.printf("The 2nd product's ID is %d. ", secondProduct.getID());
System.out.print("And the 3rd product... ");
System.out.println(thirdProduct);
System.out.println();
System.out.printf("The 2nd product's name was %s... ", secondProduct.getName());
secondProduct.setName("Blender");
System.out.printf("...but it is now %s. ", secondProduct.getName());
}
}
==============================
===============================================
Output is like:
=====================================
Put Shopper.java and Product.java into a folder of their own, dedicated to this homework assignment. Modify the Shopper class so that it has a variable products of type Product I] (not just Product) a variable canAdd of type boolean. It should be initialized to true in the constructor. . a method addProduct (Product newProduct) that updates the products array. Limit the amount of products a shopper may take. .a boolean method canAdd () that returns the value of the variable canAdd. What can do you if someone tries to add one more to a full product load Include code in addProduct to handle this. You should also have a way to change the value of canAdd when the product load becomes full. Let us assume our store is a very high-demand store. We have only the most amazing products, and we have to special order them. The products are so rare and valuable, in fact, that each product has a waiting list of shoppers who want one. Modify the Product class so that it has: a variable waitingList of type Shopper [] (not just Shopper) a variable canAdd of type boolean. It should be initialized to true in the constructor. a method addShopper (Shopper newshopper) that updates the shoppers array. Limit the amount of shoppers a product may have. .a boolean method canAdd () that returns the value of the variable canAdd.Explanation / Answer
import java.util.Random;
class Shopper {
// YOUR CODE: instance variables
String name;
final int ID=10;
static int shopperID;
int noOfProducts;
int shoppingBag;
// YOUR CODE: The constructor
Shopper(String name,int products)
{
this.name=name;
Random rn = new Random();
this.shopperID = rn.nextInt(99) + 10;
this.noOfProducts = products;
shoppingBag = 0;
}
public boolean canAdd(){
return shoppingBag < noOfProducts;
}
public void addProduct(){
shoppingBag++;
}
// YOUR CODE: getName method
public String getName() {
return name;
}
// YOUR CODE: getID method
public int getID() {
return shopperID;
}
// YOUR CODE: setName method
public void setName(String name) {
this.name = name;
}
// YOUR CODE: toString method
public String toString()
{
return "Name : "+getName()+", ID : "+getID();
}
}
class Product {
// YOUR CODE: instance variables
String name;
final int ITEM_ID=10;
static int ID;
String productDescription;
int totalQuantity;
// YOUR CODE: the constructor
public Product(String name, String productDescription,int quantity) {
this.name = name;
this.productDescription = productDescription;
Random rn = new Random();
this.ID = rn.nextInt(99) + 10;
totalQuantity = quantity;
}
public boolean canAdd(){
return totalQuantity > 0;
}
public void addShopper(){
totalQuantity--;
}
// YOUR CODE: getName method
public String getName() {
return name;
}
// YOUR CODE: getID method
public int getID() {
return ID;
}
// YOUR CODE: getProductDescription method
public String getProductDescription() {
return productDescription;
}
// YOUR CODE: setName method
public void setName(String name) {
this.name = name;
}
// YOUR CODE: setProductDescription method
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
// YOUR CODE: toString method
public String toString()
{
return "Product Name: "+getName()+", ID:"+ID+", Description: "+getProductDescription();
}
}
public class Tester {
public static void main(String[] args){
Product[] products = {
new Product("Microwave Oven", "Cooks your food",3),
new Product("Toaster", "Toasts your bread",2),
new Product("Bookcase", "Holds your books",1),
new Product("Blender", "Blends stuff",4),
new Product("Television", "Lets you watch TV",3),
new Product("Air Conditioner", "Keeps you cool",5)
};
Shopper[] shoppers = {
new Shopper("Monica",4),
new Shopper("Erica",1),
new Shopper("Rita",2),
new Shopper("Tina",3),
new Shopper("Sandra",2),
new Shopper("Mary",1),
new Shopper("Jessica",5)
};
System.out.println("*********************************************");
System.out.println("***Testing Shopper class addProduct method***");
System.out.println("********************************************* ");
testShoppersAddingProducts(shoppers, products);
System.out.println("*********************************************");
System.out.println("***Testing Product class addShopper method***");
System.out.println("********************************************* ");
testProductsWaitingLists(products, shoppers);
}
private static void testShoppersAddingProducts(Shopper[] shoppers, Product[] products){
for (int j = 0; j < 15; j++){
Shopper s = shoppers[randomIntInRange(0, shoppers.length-1)];
if (s.canAdd()){
System.out.printf("%s (ID: #%d) has started shopping... ", s.getName(), s.getID());
int productsToGet = randomIntInRange(0, (products.length-1)/2);
for (int i = 0; i <= productsToGet; i++){
Product p = products[i];
if (s.addProduct(p))
System.out.printf("%s has purchased %s (ID: #%d). ", s.getName(), p.getName(), p.getID());
else
break;
}
System.out.printf("%s is finished shopping. ", s.getName());
}
else
System.out.printf("%s (ID: #%d) has a full cart and cannot shop anymore. ", s.getName(), s.getID());
}
}
private static void testProductsWaitingLists(Product[] products, Shopper[] shoppers){
for (int j = 0; j < 15; j++){
Product p = products[randomIntInRange(0, products.length-1)];
if (p.canAdd()){
System.out.printf("Adding to waiting list of %s (ID: #%d)... ", p.getName(), p.getID());
int shoppersToGet = randomIntInRange(0, (shoppers.length-1)/2);
for (int i = 0; i <= shoppersToGet; i++){
Shopper s = shoppers[i];
if (p.addShopper(s))
System.out.printf("%s added shopper to waiting list: %s (ID: #%d). ", p.getName(), s.getName(), s.getID());
else
break;
}
System.out.printf("%s is finished adding shoppers. ", p.getName());
}
else
System.out.printf("%s (ID: #%d) has a full waiting list and cannot add anymore. ", p.getName(), p.getID());
}
}
private static int randomIntInRange(int low, int high) {
int multiplier = high - (low - 1);
return (int)(Math.random() * multiplier) + low;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.