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

You must have these three classes and they must work as it is described in these

ID: 3706274 • Letter: Y

Question

You must have these three classes and they must work as it is described in these specifications. You are welcome to add methods to any class that you may find useful.  You must add comments to all your classes, using the javadoc style commenting modeled in class.  You may add other classes if you consider them necessary.  

The third class ProgTwo has a main method which creates and manipulates Product objects and stores/retrieves the Products from an array structure.

Your program must be developed following the guidelines previously discussed in class. These guidelines include appropriate variable names, use of comments to describe the purpose of the program and the purpose of each method, and proper use of indentation of all program statements.

JAVA code incorporating everything below.

ProgTwo.java technical specifications: ProgTwa should manage a array of product objects. Initial data about products is available in a text file named product.txt. This should be read into the array of products. The product.bxt file is delimited by commas. After the array is createda loop is entered. The loop should display a menu which has the following options obtain the number of the type of transaction from the user, and process the transaction. The loop should continue to execute until the "8" Exit option is selected from the menu. When the exit option is selected, the program should write out to a text file information about all the products in the array in the same format as the input file. The name of the output text file should be productUpdate.txt. Prompt the user to enter the data required for a new product and add the product into the array of products Have the user enter the product ID. If the product exists prompt the user to confirm that they want the product to be deleted. If so, delete the product by moving all of the products below it in the array up one compartment and subtract one from count. Display an informative message to the user regarding the action taken Have the user enter the product ID and the new price of the product. Change the price of the product in the array if it exists, otherwise display an informative message to the user that the product does not exist Have the user enter the product ID and quantity of the product purchased. Adjust the quantity to reflect the purchase of products. Display a message for the user if the product does not exist in the array. Have the user enter the product ID and quantity of the product to be sold. Adjust the quantity to reflect the sale of products. Display a message for the user if the product does not exist in the array. 1. Add Product 2. Delete Product 3. Change Price 4. Purchase Product Units 5. Sell Product Units 6. Display information Have the user enter the ID number of the product to be about an individual ilayed. Display the information if the product exists product otherwise display an information message. 7. Display information List all the products in inventory by using the toString) about all the products method 8. Exit This will end the program execution.

Explanation / Answer

import java.io.*;

import java.util.*;

// Class Product definition

class Product

{

// Instance variables to store data

String productId;

String productName;

int quantity;

double price;

// Default constructor

public Product()

{

productId = productName = null;

quantity = 0;

price = 0.0;

}// End of default constructor

// Method to set id

public void setProductId(String productId)

{

this.productId = productId;

}// End of method

// Method to set name

public void setProductName(String productName)

{

this.productName = productName;

}// End of method

// Method to set quantity

public void setQuantity(int quantity)

{

this.quantity = quantity;

}// End of method

// Method to set price

public void setPrice(double price)

{

this.price = price;

}// End of method

// Method to return id

public String getProductId()

{

return productId;

}// End of method

// Method to return name

public String getProductName()

{

return productName;

}// End of method

// Method to return quantity

public int getQuantity()

{

return quantity;

}// End of method

// Method to return price

public double getPrice()

{

return price;

}// End of method

// Method to override toString() to return product information

public String toString()

{

return " Product ID: " + this.getProductId() + " Product Name: " + this.getProductName() +

" Quantity in hand: " + this.getQuantity() + " Price: " + this.getPrice();

}// End of method

}// End of class

// Class ProductOperations definition

class ProductOperations

{

// Declares an array of objects of class Product

Product products[];

// Scanner class object created to accept data from console

Scanner keyboard = new Scanner(System.in);

// Counter for number of records

int counter;

// Method to read file contents and store it in Product class array of objects

void readFile(String fileName)

{

// Initializes the record counter to zero

counter = 0;

// Try block begins

try

{

// Scanner class object created to read file

Scanner fileRead = new Scanner(new File(fileName));

// Count number of records

// Loops till end of the file

while(fileRead.hasNext())

{

// Increase the record counter by one

counter++;

// Move to next record

fileRead.nextLine();

}// End of while loop

// Dynamically allocates memory to the array of object of size counter

products = new Product[counter];

// Loops till record counter

for(int x = 0; x < counter; x++)

// Dynamically allocates memory to each object

products[x] = new Product();

// Close the file

fileRead.close();

// Reopen the file for reading

fileRead = new Scanner(new File(fileName));

// Reset the counter to zero

counter = 0;

// To store the data in array of objects

// Loops till end of the file

while(fileRead.hasNext())

{

// Reads a record

String data = fileRead.nextLine();

// Splits the data by comma (,) delimiter

// Get rid of white spaces around items before and after comma

String dataSplit[] = data.split("\s*,\s");

// Stores the data using setter method of the class Product at counter position of array of object

products[counter].setProductId(dataSplit[0]);

products[counter].setProductName(dataSplit[1]);

products[counter].setQuantity(Integer.parseInt(dataSplit[2]));

products[counter].setPrice(Double.parseDouble(dataSplit[3]));

// Increase the record counter by one

counter++;

}// End of while loop

// Close the file

fileRead.close();

}// End of try block

// Catch block to handle FileNotFoundException exception

catch(FileNotFoundException fe)

{

// Displays the error message

fe.printStackTrace();

}// End of catch block

}// End of method

/* Method to increase the array of object size

* Receives the old array of objects as parameter

* Returns the new array of objects with increased size

*/

Product [] increaseSize(Product p[])

{

// Creates a temporary array of objects with increasing the size by one  

Product temp[] = new Product[products.length + 1];

// Loops till end of the record

for(int x = 0; x < counter; x++)

// Stores each object of the old array object in new array of object

temp[x] = p[x];

// Returns the new array of object with increased size

return temp;

}// End of method

// Method to search a product id and return its index position if found

// Otherwise returns -1

int searchID(String id)

{

// Initializes the index position to -1 for not found

int index = -1;

// Loops till end of the record

for(int x = 0; x < counter; x++)

{

// Checks the current product id with parameter product id

if(products[x].getProductId().equals(id))

{

// If found set the current counter value as the found index position

index = x;

// Come out of the loop

break;

}// End of if condition

}// End of for loop

// Returns the index position

return index;

}// End of method

// Method to add a product

void addProduct()

{

// Calls the method to increase the array of object size

products = increaseSize(products);

// Allocates memory to new object

products[counter] = new Product();

// Accepts data from the user

// Using the setter method adds the data to instance variables

System.out.print(" Enter the Product ID: ");

products[counter].setProductId(keyboard.next());

System.out.print(" Enter the Product Name: ");

products[counter].setProductName(keyboard.next());

System.out.print(" Enter the Product Quantity: ");

products[counter].setQuantity(keyboard.nextInt());

System.out.print(" Enter the Product Price: ");

products[counter].setPrice(keyboard.nextDouble());

// Increase the record counter by one

counter++;

}// End of method

// Method to delete a product

void deleteProduct()

{

// Accepts the user id from the user

System.out.print(" Enter the Product ID to delete: ");

String id = keyboard.next();

// Calls the method to search the id

// Stores the return index position

int position = searchID(id);

// Checks if the index position is not -1 then record found

if(position != -1)

{

// Loops from found index position to number of records minus one times

for(int x = position; x < counter - 1; x++)

// Stores the next index position object in previous index position

products[x] = products[x+1];

// Decrease the record counter by one

counter--;

// Displays the success message

System.out.println(" Product deleted successfully.");

}// End of if condition

// Otherwise, display error message record not found

else

System.out.println("No such Product ID " + id + " found.");

}// End of method

// Method to update the price

void changePrice()

{

// Accepts the user id from the user

System.out.print(" Enter the Product ID to change price: ");

String id = keyboard.next();

// Calls the method to search the id

// Stores the return index position

int position = searchID(id);

// Checks if the index position is not -1 then record found

if(position != -1)

{

// Accepts the price from the user

System.out.print(" Enter the new price of the Product: " + products[position].getProductName());

double pr = keyboard.nextDouble();

// Update the price using setter method

products[position].setPrice(pr);

}// End of if condition

// Otherwise, display error message record not found

else

System.out.println("No such Product ID " + id + " found.");

}// End of method

// Method to update the quantity for purchase

void purchaseProductUnits()

{

// Accepts the user id from the user

System.out.print(" Enter the Product ID to purchase: ");

String id = keyboard.next();

// Calls the method to search the id

// Stores the return index position

int position = searchID(id);

// Checks if the index position is not -1 then record found

if(position != -1)

{

// Accepts the quantity from the user

System.out.print(" Enter the quantity purchased of the Product: " + products[position].getProductName());

int quantity = keyboard.nextInt();

quantity += products[position].getQuantity();

// Update the quantity using setter method

products[position].setQuantity(quantity);

}// End of if condition

// Otherwise, display error message record not found

else

System.out.println("No such Product ID " + id + " found.");

}// End of method

// Method to update the quantity for sell

void sellProductUnits()

{

// Accepts the user id from the user

System.out.print(" Enter the Product ID to sell: ");

String id = keyboard.next();

// Calls the method to search the id

// Stores the return index position

int position = searchID(id);

// Checks if the index position is not -1 then record found

if(position != -1)

{

// Accepts the quantity from the user

System.out.print(" Enter the quantity sold of the Product: " + products[position].getProductName());

int quantity = keyboard.nextInt();

quantity = products[position].getQuantity() - quantity;

// Update the quantity using setter method

products[position].setQuantity(quantity);

}// End of condition

// Otherwise, display error message record not found

else

System.out.println("No such Product ID " + id + " found.");

}// End of method

// Method to display a particular producrt information

void displayIndividualProduct()

{

// Accepts the user id from the user

System.out.print(" Enter the Product ID to display information: ");

String id = keyboard.next();

// Calls the method to search the id

// Stores the return index position

int position = searchID(id);

// Checks if the index position is not -1 then record found

if(position != -1)

System.out.print(products[position]);

// Otherwise, display error message record not found

else

System.out.println("No such Product ID " + id + " found.");

}// End of method

// Method to display all product information

void displayAllProductInformation()

{

// Loops till end of the record

for(int x = 0; x < counter; x++)

// Display each record information

System.out.println(products[x]);

}// End of method

/* Method to display menu.

* Accept user choice.

* Returns usr choice

*/

int mainMenu()

{

// To store user choice

int choice;

// Displays menu

System.out.println(" 1 - Add Product.");

System.out.println(" 2 - Delete Product.");

System.out.println(" 3 - Change Price.");

System.out.println(" 4 - Purchase Products Units.");

System.out.println(" 5 - Sell Product Units.");

System.out.println(" 6 - Display information about an individual product.");

System.out.println(" 7 - Display information about all the products.");

System.out.println(" 8 - Exit.");

// Accepts user choice

System.out.print(" Enter hour choice: ");

choice = keyboard.nextInt();

// Returns user choice

return choice;

}// End of method

}// End of class

// Deriver class ProgTwo definition

public class ProgTwo

{

// main method definition

public static void main(String[] args)

{

// Creates an object of the class ProductOperations

ProductOperations po = new ProductOperations();

// Calls the method to read the file

po.readFile("product.txt");

// Loops till use choice is not 8

do

{

// Calls the method to display menu.

// Based on the return user choice calls the method

switch(po.mainMenu())

{

case 1:

po.addProduct();

break;

case 2:

po.deleteProduct();

break;

case 3:

po.changePrice();

break;

case 4:

po.purchaseProductUnits();

break;

case 5:

po.sellProductUnits();

break;

case 6:

po.displayIndividualProduct();

break;

case 7:

po.displayAllProductInformation();

break;

case 8:

System.exit(0);

default:

System.out.println(" Invalid choice!");

}// End of switch - case

}while(true); // End of do - while loop

}// End of main method

}// End of class

Sample Output:


1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 1

Enter the Product ID: 116

Enter the Product Name: Pepsi

Enter the Product Quantity: 56

Enter the Product Price: 89.12

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 6

Enter the Product ID to display information: 116

Product ID: 116
Product Name: Pepsi
Quantity in hand: 56
Price: 89.12
1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 2

Enter the Product ID to delete: 120
No such Product ID 120 found.

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 7

Product ID: 111
Product Name: Lux
Quantity in hand: 10
Price: 33.25

Product ID: 112
Product Name: Dove
Quantity in hand: 15
Price: 12.56

Product ID: 113
Product Name: Cinthol
Quantity in hand: 50
Price: 40.55

Product ID: 114
Product Name: Rexona
Quantity in hand: 25
Price: 23.41

Product ID: 115
Product Name: Liril
Quantity in hand: 33
Price: 39.61

Product ID: 116
Product Name: Pepsi
Quantity in hand: 56
Price: 89.12

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 2

Enter the Product ID to delete: 114

Product deleted successfully.

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 7

Product ID: 111
Product Name: Lux
Quantity in hand: 10
Price: 33.25

Product ID: 112
Product Name: Dove
Quantity in hand: 15
Price: 12.56

Product ID: 113
Product Name: Cinthol
Quantity in hand: 50
Price: 40.55

Product ID: 115
Product Name: Liril
Quantity in hand: 33
Price: 39.61

Product ID: 116
Product Name: Pepsi
Quantity in hand: 56
Price: 89.12

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 3

Enter the Product ID to change price: 123
No such Product ID 123 found.

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 3

Enter the Product ID to change price: 113

Enter the new price of the Product: Cinthol33.25

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 6

Enter the Product ID to display information: 113

Product ID: 113
Product Name: Cinthol
Quantity in hand: 50
Price: 33.25
1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 4

Enter the Product ID to purchase: 111

Enter the quantity purchased of the Product: Lux20

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 7

Product ID: 111
Product Name: Lux
Quantity in hand: 30
Price: 33.25

Product ID: 112
Product Name: Dove
Quantity in hand: 15
Price: 12.56

Product ID: 113
Product Name: Cinthol
Quantity in hand: 50
Price: 33.25

Product ID: 115
Product Name: Liril
Quantity in hand: 33
Price: 39.61

Product ID: 116
Product Name: Pepsi
Quantity in hand: 56
Price: 89.12

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 5

Enter the Product ID to sell: 115

Enter the quantity sold of the Product: Liril10

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 6

Enter the Product ID to display information: 115

Product ID: 115
Product Name: Liril
Quantity in hand: 23
Price: 39.61
1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 10

Invalid choice!

1 - Add Product.
2 - Delete Product.
3 - Change Price.
4 - Purchase Products Units.
5 - Sell Product Units.
6 - Display information about an individual product.
7 - Display information about all the products.
8 - Exit.
Enter hour choice: 8

product.txt file contents

111, Lux, 10, 33.25
112, Dove, 15, 12.56
113, Cinthol, 50, 40.55
114, Rexona, 25, 23.41
115, Liril, 33, 39.61

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