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

At this point, students should create new classes ProductPart4 and ProductTester

ID: 3755102 • Letter: A

Question

At this point, students should create new classes ProductPart4 and ProductTesterPart4 that will add onto the functionality of Part 3 of the project. (copy and paste part 3 code into new part 4 classes) Topic(s): Modifying programs, adding methods, using parameters in a method, user interface to test new methods

1. Create two new methods in the ProductPart4 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 ProductTesterPart4 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.

Here is the code:

package javaInventory;

import java.util.Scanner; //imports the Scanner class for keyboard input

public class ProductTester4 {

public static void main(String[] args) {

//this section is the same as ProductTester 3, which allows the user to create products

//************************************************************************************

Scanner s = new Scanner(System.in); //create a Scanner object for keyboard input

System.out.println("Hello User, let's add products to the inventory");

System.out.println("Enter the number of products you would like to add.");

System.out.println("Enter 0 (zero) if you do not wish to add products.");

int numproducts = s.nextInt(); //assume that the user enters a valid integer value

if(numproducts==0){

System.out.println(" Thank you, no products were entered");

}

else{

ProductPart4[] p = new ProductPart4[numproducts];

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

System.out.println(" Entering item #" + i);

System.out.println(" Please enter the item number");

int id = s.nextInt();

System.out.println("Please enter the product name");

String name = s.next();

System.out.println("Please enter the number in stock for this product");

int qty = s.nextInt();

System.out.println("Please enter the price for this product");

double price = s.nextDouble();

p[i] = new ProductPart4(id, name, qty, price);

}

//each item entered above is displayed one at a time and then

//the following loop allows the user to add a quantity to a product

//OR deduct a quantity from a prodct

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

System.out.println("*****************************************");

System.out.println(p[i]); //displays the product

System.out.println(" Would you like to modify this item?");

System.out.println("Enter 1 to add inventory, 2 to deduct, or 3 for neither");

int ans = s.nextInt();

int qty = 0;

if(ans==1){ //add to the product's quantity in stock

System.out.println(" How many would you like to add?");

qty = s.nextInt();

p[i].addToInventory(qty);

System.out.println(" New information for " + p[i].getName());

System.out.println(p[i]); //display the product again

}

else if(ans==2){ //deduct from the product's quantity in stock

System.out.println("How many would you like to deduct?");

qty = s.nextInt();

p[i].deductFromInventory(qty);

System.out.println(" New information for " + p[i].getName());

System.out.println(p[i]); //display the product again

}

//no need to handle ans==3, which would do nothing

}

//Display the array again

System.out.println(" Final inventory list:");

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

System.out.println(p[i]);

}

}

}

}

Explanation / Answer


You have not given your ProductPart3.java code. All you need to do it copy(rename) that file as ProductPart4.java and the class to ProductPart4 and then include the following 2 methods inside of that class. Since I do not know the class member variable names inside the Product class, I have assumed you have a field named quantity in there. Let me know if it is not that, I'll help change the methods below to use correct variable name. Post a comment if you need any help, I will respond.
Run your test program to check if it works as intended. Please do rate if it helped. thank you


public class ProductPart4{

////...................other variables and methods from Part3 will remain as is

public void addToInventory(int qty) {
this.quantity = this.quantity + qty;
}
public void deductFromInventory(int qty) {
this.quantity = this.quantity - qty;
}
}