Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Requirements - Part 1 (filenames: ProductPart1, ProductTesterPart1) 1) Choose a

ID: 3547559 • Letter: R

Question


Requirements - Part 1 (filenames: ProductPart1, ProductTesterPart1)

1) Choose a product that lends itself to an inventory (for example, products in your home, school, or workplace: office supplies; music CDs; DVD movies; or software).

2) Create a Product class with instance variables for:

a) item number

b) the name of the product

c) the number of units in stock

d) the price of each unit

3) Create two constructors:

a) A default constructor without parameters that will initialize numeric variable(s)with zeros, and String variable(s) with nulls.

b) Overload the default constructor, and create a constructor with parameters for all four instance variables from #2 above

that can initialize the object with values from the tester.

4) Write getter/accessor and setter/mutator methods for each of the four instance variables.

5) Override the toString() method from the object class that will show a description of each object that includes the variable values.

6) Create a Java main class called ProductTester that creates and initializes six Product objects.

a) Two of the Products should be created using the default constructor

b) The other four should be created using values for the arguments

7) From ProductTester, display the product number, the name of the product, the number of units in stock, and the price of each unit.

Requirements - Part 2 (filenames: ProductPart2, ProductTesterPart2)

At this point, students may either modify the original ProductTester and Product classes orcreate new classes that

will add the new functionality from subsequent parts.

Topic(s): Scanner/keyboard input

1. Modify ProductTester

a) Add a Scanner.

b) Ask the user to input values for the arguments of the two products in 6a from Part

2. Create a method in the Product class that will calculate the value of the each inventory item, using the quantity on hand and price.

3. Display the information in ProductTester for these products as was in Part 1. In addition, include the inventory value for each product by modifying the toString()method in Product.

Requirements - Part 3 (filenames: ProductPart3, ProductTesterPart3)

Topic(s): Arrays of objects, for loops

1. Modify ProductTester to handle multiple products using an array of Products.

2. Ask the user to enter the number of products they wish to add. Accept a positive integer for the number of products, and handle the value of zero.

3. Using a for loop, ask the user to input the values for the items.

4. Use another for loop to display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product.

Requirements - Part 4 (filenames: ProductPart4, ProductTesterPart4)

Topic(s): Modifying programs, adding methods, user interface to test new methods

1. Create two new methods in the Product class, one that will allow the user to add to the number of units in stock, and one that will allow the user to deduct from the number of units in stock. Both methods should have a parameter for the number of items to add/deduct.

2. Modify ProductTester again so that the user can modify the items. Ask the user if they would like to add or deduct from the inventory for the item as each item is displayed.

Requirements - Part 5 (filenames: ProductPart5, ProductTesterPart5)

Topic(s): Interfaces - particularly the Comparable Interface for sorting one type of object, the compareTo() method, Arrays.sort()

1. Implement the Comparable Interface to create another method in the Product class. Modify ProductTester to use Arrays.sort() to sort the array items by the name of the product. Redisplay the products.

Requirements - Part 6 (filenames: ProductPart6, ProductTesterPart6)

Topic(s): Adding a subclass, using extends, using super(), overriding methods from a superclass, using the generic Comparable Interface

1. Create a subclass of the Product class that has two additional variables. (For example, a DVD subclass could use movie title and length).

2. In the subclass, override the method to calculate the value of the inventory of a product with the same name as that method previously created for the product class. The subclass method should also add a 5% restock king fee to the value of the inventory of that product.

3. Override the toString() method from the Product class so that all information about new subclass objects can be

printed to the output.

4. Modify ProductTester so that an array of objects of the new subclass can be created from user input. Display the subclass products using a for loop.

5. Implement the Comparable Interface to create another method in the subclass. Override the compareTo() method from the

Comparable Interface so that the DVDs will sort in alphabetic order by DVD title. (Note: This will require a modification in the class header implements statement. See the solution for explanation.)

6. Modify ProductTester to sort the array items by the title of the DVD. Redisplay the products.

Explanation / Answer

ProductPart4 class:

public class ProductPart4 {


public int itemNumber;

public String productName;

public int inStock;

public double price;


public ProductPart4() {

itemNumber = 0;

productName = "";

inStock = 0;

price = 0.0;


}


public ProductPart4(int itemNumber, String productName, int inStock, double price) {


this.itemNumber = itemNumber;

this.productName = productName;

this.inStock = inStock;

this.price = price;

}


public int getItemNumber() {

return itemNumber;

}


public void setItemNumber(int itemNumber) {

this.itemNumber = itemNumber;

}


public String getProductName() {

return productName;

}


public void setProductName(String productName) {

this.productName = productName;

}


public int getInStock() {

return inStock;

}


public void setInStock(int inStock) {

this.inStock = inStock;

}


public double getPrice() {

return price;

}


public void setPrice(double price) {

this.price = price;

}


public String toString() {


String des = itemNumber + ". " + productName + " price is $" + price + " and having " + inStock + " in stock and price for each "

+ productName + " is " + valEachItem(inStock, price);

return des;

}


//return the price for each item

public double valEachItem(int inStock, double price) {


double priceEach = 0.0;

priceEach = price / inStock;

return priceEach;

}


public void addUnit(int numUnits) {

inStock = inStock + numUnits;

}


public void deductUnit(int numUnits) {

if (inStock - numUnits <= 0) {

System.out.println("You can not deduct so many items");

} else {

inStock = inStock - numUnits;

}

}

}


ProductTesterPart4 class:

public class ProductTesterPart4 {


public static void main(String args[]) {

try {

int numOfProducts = 0;

//get an array of Products


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


System.out.println("Enter the number of products");

numOfProducts = Integer.parseInt(br.readLine());

ProductPart4 product[] = new ProductPart4[numOfProducts];


for (int i = 0; i < numOfProducts; i++) {

ProductPart4 prod = new ProductPart4();

product[i] = prod;

System.out.println("Enter the item number for product" + (i + 1));

product[i].itemNumber = Integer.parseInt(br.readLine());

System.out.println("Enter the product name for product" + (i + 1));

product[i].productName = br.readLine().toString();

System.out.println("Enter the in stock for product" + (i + 1));

product[i].inStock = Integer.parseInt(br.readLine());

System.out.println("Enter the price for product" + (i + 1));

product[i].price = Double.parseDouble(br.readLine());

}

for (int i = 0; i < numOfProducts; i++) {

System.out.println(product[i].toString());

System.out.println("Like to add/deduct from inventory. Write add/delete to add/deduct");

String input=br.readLine().toString();

if(input.equals("add")){

System.out.println("How many items?");

int items = Integer.parseInt(br.readLine());

product[i].addUnit(items);

}

else if(input.equals("delete")){

System.out.println("How many items?");

int items = Integer.parseInt(br.readLine());

product[i].deductUnit(items);

}

System.out.println(product[i].toString());

}

} catch (IOException ex) {

Logger.getLogger(ProductTesterPart4.class.getName()).log(Level.SEVERE, null, ex);

}



}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote