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

Write an object-oriented Java program that implements a product list using a 2-D

ID: 3710041 • Letter: W

Question

Write an object-oriented Java program that implements a product list using a 2-D arraylist and has four methods to it.

gatherfileData: this method is to read data from a file you create yourself that holds the student names and grades. This method should return a 2-D array with the data from this file

dataUpdate: this method should edit the data. Design a program to make dynamic update based on user input. Example being if the user wants to update to item number 7, the program should do this. Product name has “” around them, write code to remove all the quotes from product names. If user wants to update quantity or product name, your code should handle that dynamically. This method should accept a 2D array as a parameter and return a 2D array with the modified data.

dataSave: this method is to write data from the 2D array to a file. This method should accept a 2D array as a parameter and return void.

Main: this method should be used to call all other methods.

example for the file:

Explanation / Answer

Prouduct.java

public class Product {

int SKUNumber;

String productName;

double productPrice;

int quantity;

public int getSKUNumber() {

return SKUNumber;

}

public void setSKUNumber(int sKUNumber) {

SKUNumber = sKUNumber;

}

public String getProductName() {

return productName;

}

public void setProductName(String productName) {

this.productName = productName;

}

public double getProductPrice() {

return productPrice;

}

public void setProductPrice(double productPrice) {

this.productPrice = productPrice;

}

public int getQuantity() {

return quantity;

}

public void setQuantity(int quantity) {

this.quantity = quantity;

}

@Override

public String toString() {

return "Product [SKUNumber=" + SKUNumber + ", productName="

+ productName + ", productPrice=" + productPrice

+ ", quantity=" + quantity + "]";

}

public Product(int sKUNumber, String productName, double productPrice,

int quantity) {

SKUNumber = sKUNumber;

this.productName = productName;

this.productPrice = productPrice;

this.quantity = quantity;

}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Demo {

static Product[] products=new Product[10];

static Scanner scanner=new Scanner(System.in);;

public static void main(String[] args) throws IOException {

BufferedReader reader=new BufferedReader(new FileReader(new File("D:\file.txt")));

String line=null;

int count=0;

while((line=reader.readLine())!=null)

{

//to split the string if it has atleast two whitespaces

String[] words=line.split("\s{2,}");

int SKUNumber=0;

String productName=null;

double productPrice=0.0;

int quantity=0;

for(int i=0;i<words.length;i++)

{

//to remove double quotes

String string = words[1].replaceAll("^"|"$", "");

//printing the string without double quotes

System.out.println(string);

SKUNumber=Integer.parseInt(words[0]);

productName=string;

productPrice=Double.parseDouble(words[2]);

quantity=Integer.parseInt(words[3]);

  

}

Product product=new Product(SKUNumber, productName, productPrice, quantity);

products[count]=product;

count++;

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

}

//to print all products after reading the file

for(Product product:products)

{

if(product!=null)

System.out.println(product);

}

//to update the data

System.out.println("enter the item no");

int itemno=scanner.nextInt();

dataupdate(itemno);

dataSave(products);

}

private static void dataSave(Product[] products2) throws FileNotFoundException {

File Fileright = new File("D:\f.txt");

PrintWriter pw = new PrintWriter(Fileright);

for (Product product:products) {

if(product!=null)

{

pw.println(product);

}

}

pw.close();

}

private static void dataupdate(int itemno) {

for(Product product:products)

{

if(product.SKUNumber==itemno)

{

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

  

String productname=scanner.next();

  

System.out.println("enter the product quantity to change");

  

int quantity=scanner.nextInt();

  

product.productName=productname;

product.quantity=quantity;

  

System.out.println("product record successfully updated");

  

}

}

  

  

}

}

output

Eldon Base for stackble storage shelf,platinum

*********************************

1.7 cubic foot compact "cube""office refrigetaros

*********************************

Cardinal slant-D Ring Binder,heavy gauge vinyl

*********************************

Product [SKUNumber=1, productName=Eldon Base for stackble storage shelf,platinum, productPrice=38.94, quantity=6]

Product [SKUNumber=2, productName=1.7 cubic foot compact "cube""office refrigetaros, productPrice=208.16, quantity=49]

Product [SKUNumber=3, productName=Cardinal slant-D Ring Binder,heavy gauge vinyl, productPrice=8.69, quantity=27]

enter the item no

2

enter the product name to change

jeorge

enter the product quantity to change

5

product record successfully updated

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