need help: import java.util.Random; public class Product { String name; final in
ID: 664823 • Letter: N
Question
need help:
import java.util.Random;
public class Product {
String name;
final int ITEM_ID=10;
static int ID;
String productDescription;
public Product(String name, String productDescription) {
this.name = name;
this.productDescription = productDescription;
Random rn = new Random();
this.ID = rn.nextInt(99) + 10;
}
public String getName() {
return name;
}
// YOUR CODE: getID method
public int getID() {
return ID;
}
public String getProductDescription() {
return productDescription;
}
public void setName(String name) {
this.name = name;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
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());
}
}
----------------------------------------------------------------------------
import java.util.Random;
public class Shopper {
String name;
int ID=10;
int shopperID;
Shopper(String name){
this.name=name;
Random rn = new Random();
this.shopperID = rn.nextInt(99) + 10;
}
public String getName() {
return name;
}
public int getID() {
return shopperID;
}
public void setName(String name) {
this.name = name;
}
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());
}
}
==============================================
public class Tester {
public static void main(String[] args){
Product[] products = {
new Product("Microwave Oven", "Cooks your food"),
new Product("Toaster", "Toasts your bread"),
new Product("Bookcase", "Holds your books"),
new Product("Blender", "Blends stuff"),
new Product("Television", "Lets you watch TV"),
new Product("Air Conditioner", "Keeps you cool")
};
Shopper[] shoppers = {
new Shopper("Monica"),
new Shopper("Erica"),
new Shopper("Rita"),
new Shopper("Tina"),
new Shopper("Sandra"),
new Shopper("Mary"),
new Shopper("Jessica")
};
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;
}
}
=========================================================
question 1:
Modify the Shopper class so that it has:
a variable products of type Product[] (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.
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.
Question2:
Put Shopper.java and Product.java into a folder of their own, dedicated to this homework assignment. Make the following changes to...
...the Shopper class:
A balance attribute (type double), with corresponding getter and setter methods. This will represent the money currently in the shopper's account.
A printProducts method (return type void) that prints each product (from the shopper's product list) on a separate line.
A getTotal method (return type double) that computes and returns the total cost of all the products in the shopper's list.
...the Product class:
A price attribute (type double), with corresponding getter and setter methods. This will represent the product's price.
A printShoppers method (return type void) that prints each shopper (on the product's waiting list) on a separate line.
Write a static class (revisit the notes, if needed, to remind yourself what this means) Store, which declares and initializes the static variables scan (typeScanner), shoppers (type Shopper[]), and products (type Product[]). The Store.java file should go in the same folder as Shopper.java andProduct.java.
It should have static methods:
main(String[] args),
createNewShopper() - return void or boolean. Code for getting information for a new Shopper (name and balance), creating the object, and adding it to the shoppers array. (First check to make sure the shoppers array has space. If not, just print an error message instead of going through the adding process.)
createNewProduct() - return void or boolean. Code for getting information for a new Product (name, description, and price), creating the object, and adding it to the products array. (First check to make sure the products array has space. If not, just print an error message instead of going through the adding process.)
doWaitList(Shopper shopper, Product product) - return void or boolean. Code for mutually adding the shopper to the product's wait listand the product to the shopper's product list. (If you want to be bolder, include code to make sure that it does not complete the process if shopper has a full load or the product has a full wait list -- may add helper methods to Shopper and Product classes for this.)
Finally, your actual main method:
Set up a boolean again and an int option
Set up a do-while loop repeating on again. The loop code should carry out the following steps:
Ask if user wants to add a new shopper, add a new product, or place a shopper on the wait list for a product
Take the input as an integer, 1-3, stored in option. Can use a do-while loop to enforce the range, or just say Math.abs(option-1) % 3 + 1, or give the switch statement a default behavior (e.g., printing an error message if input is not in range).
Use a switch statement with break statements to execute the proper code.
Add a new shopper by invoking createNewShopper()
Add a new product by invoking createNewProduct()
Link a shopper and a product:
Print the list of shoppers and prompt the user to choose one. This will involve code to get a user response and translate it to a particularShopper object, with a reference stored in a variable of type Shopper.
Print the list of products and prompt the user to choose one. This will involve code to get a user response and translate it to a particularProduct object, with a reference stored in a variable of type Product.
Link them by invoking the doWaitList method with the respective objects.
Ask if the user wishes to go again, get a value of true or false from the user, and store that value in again.
For each shopper, print: the object, the shopper's product list, and the total price of the shopper's products.
For each product, print: the object and the product's wait list.
Explanation / Answer
Answer:
Note: I have done Question 1 alone.
Question 1:
public class Shopper {
String name;
int ID=10;
int shopperID;
Product[] product;
boolean canAdd;
int count;
Shopper(String name){
this.name=name;
Random rn = new Random();
this.shopperID = rn.nextInt(99) + 10;
canAdd=true;
count=0;
}
public String getName() {
return name;
}
public int getID() {
return shopperID;
}
public void setName(String name) {
this.name = name;
}
void addProduct(Product newProduct)
{
if(count<10)
{
product[count]=newProduct;
count++;
}
else
canAdd=false;
}
boolean canAdd()
{
reutn canAdd;
}
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());
}
}
public class Product {
String name;
final int ITEM_ID=10;
static int ID;
String productDescription;
Shopper waitingList[];
boolean canAdd;
int shopperCount;
public Product(String name, String productDescription) {
this.name = name;
this.productDescription = productDescription;
Random rn = new Random();
this.ID = rn.nextInt(99) + 10;
canAdd=true;
shoppercount=0;
}
public String getName() {
return name;
}
// YOUR CODE: getID method
public int getID() {
return ID;
}
public String getProductDescription() {
return productDescription;
}
public void setName(String name) {
this.name = name;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
void addShopper(Shopper newShopper)
{
if(shopperCount<4)
{
waitingList[shopperCount]=newShopper;
shopperCount++;
}
else
canAdd=false;
}
boolean canAdd()
{
return canAdd;
}
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());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.