I am writing a code to store an inventory an while writing the first class (Prod
ID: 669188 • Letter: I
Question
I am writing a code to store an inventory an while writing the first class (Product.java) I need to make a different class (ProductTest.java) that tests the variables. I was able to test the String, Integer, and Double variables, but am not sure how to test the ArrayList variable. Here are my classes:
Product.Java
import java.util.ArrayList;
import java.util.List;
public class Product {
private String name; private String inventoryCode; private String type;
private int quantity; private double price;
private ArrayList<Integer> userRatings;
// Private member variables go here - you will need to add them yourself.
/*
* Product constructor
*/
public Product(){
userRatings = new ArrayList<Integer>();
}
/*
* setName
* @param name - new name for the product
*/
public void setName(String name) {
this.name=name;
}
/*
* getName
* @return the name of the product
*/
public String getName() {
return this.name;
}
/*
* setType
* @param type - the type of the product
*/
public void setType(String type) {
this.type=type ;
}
/*
* getType
* @return - the product type
*/
public String getType() {
return this.type;
}
/*
* setPrice
* @param price - the price of the product
*/
public void setPrice(double price) {
this.price=price;
}
/*
* getPrice
* @return the price of the product
*/
public double getPrice() {
return this.price;
}
/*
* setQuantity
* @param quantity - the number of this product in inventory
*/
public void setQuantity(int quantity) {
this.quantity=quantity;
}
/*
* getQuantity
* @return the number of this product in inventory
*/
public int getQuantity() {
return this.quantity;
}
/*
* setInventoryCode
* @param code - the new inventory code for the product
*/
public void setInventoryCode(String code) {
this.inventoryCode=code;
}
/*
* getInventoryCode
* @return the inventory code of the product
*/
public String getInventoryCode() {
return this.inventoryCode;
}
/*
* addUserRating
* NOTE: Each individual rating is stored with the product, so you need to maintain a list
* of user ratings. This method should append a new rating to the end of that list
* @param rating - the new rating to add to this product
*/
public void addUserRating(int rating) {
this.userRatings.add(rating);
}
/*
* getUserRating
* NOTE: See note on addUserRating above. This method should be written to allow you
* to access an individual value from the list of user ratings
* @param index - the index of the rating we want to see
* @return the rating indexed by the value index
*/
public Integer getUserRating(int index) {
return this.userRatings.get(index);
}
/*
* getUserRatingCount
* NOTE: See note on addUserRating above. This method should be written to return
* the total number of ratings this product has associated with it
* @return the number of ratings associated with this product
*/
public int getUserRatingCount() {
return this.userRatings.size();
}
/*
* getAvgUserRating
* NOTE: see note on addUserRating above. This method should be written to compute
* the average user rating on demand from a stored list of ratings.
* @return the average rating for this product as a whole integer value (use integer math)
*/
public int getAvgUserRating() {
int ratingsSum = 0;
for (Integer rating : this.userRatings){
ratingsSum += rating;
}
return ratingsSum/this.userRatings.size();
}
}
ProductTest.java
import java.util.ArrayList;
import java.util.List;
public class ProductTest {
public static void main(String[] args) {
// testing Product's name variable
Product n = new Product();
String name;
n.setName("Batman");
name = n.getName();
System.out.println(name);
//testing Product's type variable
Product t = new Product();
String type;
t.setType("DVD");
type = t.getType();
System.out.println(type);
//testing Product's price variable
Product p = new Product();
double price;
p.setPrice(9.99);
price = p.getPrice();
System.out.println(price);
//testing Product's quantity variable
Product q = new Product();
int quantity;
q.setQuantity(152);
quantity = q.getQuantity();
System.out.println(quantity);
//testing Product's inventory code variable
Product c = new Product();
String inventoryCode;
c.setInventoryCode("C00001");
inventoryCode = c.getInventoryCode();
System.out.println(inventoryCode);
//testing Product's userRating code variable
Explanation / Answer
Answer:
The added code is highlighted in bold letters. And and executed program output is added to the solution.
Program code:
import java.util.ArrayList;
import java.util.List;
public class ProductTest
{ public static void main(String[] args)
{
// testing Product's name variable
Product n = new Product();
String name;
n.setName("Batman");
name = n.getName();
System.out.println(name);
// testing Product's type variable
Product t = new Product();
String type;
t.setType("DVD");
type = t.getType();
System.out.println(type);
// testing Product's price variable
Product p = new Product();
double price;
p.setPrice(9.99);
price = p.getPrice();
System.out.println(price);
// testing Product's quantity variable
Product q = new Product();
int quantity;
q.setQuantity(152);
quantity = q.getQuantity();
System.out.println(quantity);
// testing Product's inventory code variable
Product c = new Product();
String inventoryCode;
c.setInventoryCode("C00001");
inventoryCode = c.getInventoryCode();
System.out.println(inventoryCode);
// testing Product's userRating code variable
//create a Product object r
Product r = new Product();
//by using r call the addUserRating method
//by passing the integer values
r.addUserRating(3);
r.addUserRating(4);
r.addUserRating(1);
r.addUserRating(6);
r.addUserRating(5);
//display the values
for(int i =0;i<r.getUserRatingCount();i++)
System.out.println("User "+(i+1)+" rating: "+r.getUserRating(i));
//calculate the average of the user rating and display the value
System.out.println("Average of the user's rating is: "+r.getAvgUserRating());
}
}
import java.util.ArrayList;
import java.util.List;
public class Product
{
private String name;
private String inventoryCode;
private String type;
private int quantity;
private double price;
private ArrayList<Integer> userRatings;
// Private member variables go here - you will need to add them yourself.
/*
* Product constructor
*/
public Product()
{
userRatings = new ArrayList<Integer>();
}
/*
* setName
*
* @param name - new name for the product
*/
public void setName(String name)
{
this.name = name;
}
/*
* getName
*
* @return the name of the product
*/
public String getName()
{
return this.name;
}
/*
* setType
*
* @param type - the type of the product
*/
public void setType(String type)
{
this.type = type;
}
/*
* getType
*
* @return - the product type
*/
public String getType()
{
return this.type;
}
/*
* setPrice
*
* @param price - the price of the product
*/
public void setPrice(double price)
{
this.price = price;
}
/*
* getPrice
*
* @return the price of the product
*/
public double getPrice()
{
return this.price;
}
/*
* setQuantity
*
* @param quantity - the number of this product in inventory
*/
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
/*
* getQuantity
*
* @return the number of this product in inventory
*/
public int getQuantity()
{
return this.quantity;
}
/*
* setInventoryCode
*
* @param code - the new inventory code for the product
*/
public void setInventoryCode(String code)
{
this.inventoryCode = code;
}
/*
* getInventoryCode
*
* @return the inventory code of the product
*/
public String getInventoryCode()
{
return this.inventoryCode;
}
/*
* addUserRating NOTE: Each individual rating is stored with the product,
* so you need to maintain a list of user ratings. This method should
* append a new rating to the end of that list
*
* @param rating - the new rating to add to this product
*/
public void addUserRating(int rating)
{
this.userRatings.add(rating);
}
/*
* getUserRating NOTE: See note on addUserRating above. This method should
* be written to allow you to access an individual value from the list of
* user ratings
*
* @param index - the index of the rating we want to see
*
* @return the rating indexed by the value index
*/
public Integer getUserRating(int index)
{
return this.userRatings.get(index);
}
/*
* getUserRatingCount NOTE: See note on addUserRating above. This method
* should be written to return the total number of ratings this product has
* associated with it
*
* @return the number of ratings associated with this product
*/
public int getUserRatingCount()
{
return this.userRatings.size();
}
/*
* getAvgUserRating NOTE: see note on addUserRating above. This method
* should be written to compute the average user rating on demand from a
* stored list of ratings.
*
* @return the average rating for this product as a whole integer value
* (use integer math)
*/
public int getAvgUserRating()
{
int ratingsSum = 0;
for (Integer rating : this.userRatings)
{
ratingsSum += rating;
}
return ratingsSum / this.userRatings.size();
}
}
Sample Output:
Batman
DVD
9.99
152
C00001
User 1 rating: 3
User 2 rating: 4
User 3 rating: 1
User 4 rating: 6
User 5 rating: 5
Average of the user's rating is: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.