Question: Can you debug this code below? import java.io . * ; import java.util.Scanner; / / Product class to store Id , name, price, and quantity

Can you debug this code below?
import java.io.*;
import java.util.Scanner;
// Product class to store Id, name, price, and quantity and its getters and
// setters and toString method
class Product {
private int idNum;
private String name;
private double price;
private int quantity;
public Product(int idNum, String name, double price, int quantity){
this.idNum = idNum;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public int getID(){
return idNum;
}
public void setId(int sID){
idNum = sID;
}
public String getName(){
return name;
}
public void setName(String sName){
name = sName;
}
public double getPrice(){
return price;
}
public void setPrice(double sPrice){
price = sPrice;
}
public int getQuantity(){
return quantity;
}
public void setQuantity(int newQuantity){
quantity = newQuantity;
}
public String toString(){
return String.format("ID:"+ idNum +", Name:" + name +", Price:" + price +", Quantity:" + quantity);
}
}
// ProductCollection class with all required features
class ProductCollection {
private Product[] collection;
private int count;
int maximumSize;
public ProductCollection(){
maximumSize =20;
collection = new Product[maximumSize];
count =0;
}
public void addProduct(int prodID, String prodName, double unitPrice, int prodQty){
if (count < maximumSize){
collection[count]= new Product(prodID, prodName, unitPrice, prodQty);
count++;
} else {
increaseSize();
collection[count]= new Product(prodID, prodName, unitPrice, prodQty);
count++;
}
}
public void increaseSize(){
maximumSize = maximumSize *2;
Product[] prdt = new Product[maximumSize];
for (int i =0; i < count; i++){
prdt[i]= collection[i];
}
collection = prdt;
}
public int indexOf(int prodID){
int flag =0, result =0;
for (int i =0; i < count; i++){
if (prodID == collection[i].getID()){
result = i;
flag =1;
}
}
if (flag ==0)
result =-1;
return result;
}
public void changePrice(int prodID, double newPrice){
int ind = indexOf(prodID);
if (ind !=-1)
collection[ind].setPrice(newPrice);
}
public void buyProduct(int prodID, int qty){
int ind = indexOf(prodID);
if (ind !=-1)
collection[ind].setQuantity(collection[ind].getQuantity()+ qty);
else
System.out.println("The product ID is not in the list.");
}
public void sellProduct(int prodID, int qty){
int ind = indexOf(prodID);
if (ind !=-1)
collection[ind].setQuantity(collection[ind].getQuantity()- qty);
else
System.out.println("The product ID is not in the list.");
}
public void deleteProduct(int prodID){
int ind = indexOf(prodID);
if (ind !=-1){
for (int i = ind; i < count -1; i++){
collection[i]= collection[i +1];
}
count--;
} else
System.out.println("The product ID is not in the list.");
}
public void displayProduct(int prodID){
int ind = indexOf(prodID);
if (ind !=-1)
System.out.println("Product Name:" + collection[ind].getName()+", Price:" + collection[ind].getPrice()+", Quantity:" + collection[ind].getQuantity());
else
System.out.println("The product ID is not in the list.");
}
public String createOutputFile(){
try {
File file = new File("productUpdate.txt");
file.createNewFile();
} catch (IOException e){
System.out.println(e);
}
return "productUpdate.txt";
}
public String toString(){
String str = "The products in Collection.
";
for (int i =0; i < count; i++){
str = str + collection[i].toString();
str = str +"
";
}
return str;
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class ProgTwo {
public static void main(String[] args){
ProductCollection collection = new ProductCollection();
try {
BufferedReader reader = new BufferedReader(new FileReader("C:/Users/SHANOOB/Desktop/product.txt"));
String line = reader.readLine();
while (line != null){
String[] arr = line.split(",");
collection.addProduct(Integer.parseInt(arr[0]), arr[1], Double.parseDouble(arr[2]), Integer.parseInt(arr[3]));
line = reader.readLine();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!