Build two classes, called Inventory and Sales. The Sales class should have the f
ID: 3531848 • Letter: B
Question
Build two classes, called Inventory and Sales. The Sales class should have the following attributes:
an integer value called productCode
an integer value called quantitySold
There should be get and set methods for each value, as well as a constructor that takes a String and builds a Sales object. This constructor will search the String for a comma, take the beginning of the string through the Integer.parseInt method to create the product code, and take the end of the string through the Integer.parseInt method to get the quantity sold.
The Inventory class should have the following attributes:
an integer value called productCode
a double value called Price
an integer value called quantityOnHand
Again, design get and set methods for each value, as well as a constructor that takes a String with three parts separated by commas: an int for the product code, a double for the price, and an int for the quantity on hand. Also create a method called postSale that takes in an int for the quantity sold and subtracts the quantity from the quantity on hand.
Write a main program that reads the files Sorted.txt containing data for Sales, and Inventory.txt that contains data for Inventory objects. Each line from a file should create a new Sales or Inventory object. Produce a report on the console showing each Inventory object and its new quantity on hand. If any sales items are found that don't have a corresponding inventory item, print an error message that says "Product code xxxx not found". Be sure to handle the case where two or more sales are made for the same product code.
Here's the code I have so far:
import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] arg) throws IOException {
String str;
Scanner fileScan, strScan;
fileScan = new Scanner (new File("SoldSorted.txt"));
while (fileScan.hasNext())
{
str = fileScan.nextLine ();
System.out.println ();
strScan = new Scanner (str);
strScan.useDelimiter(",");
while (strScan.hasNext())
System.out.println (" " + strScan.next());
System.out.println();
}
}
}
public class Sales {
int productCode, quantitySold;
public Sales(String productSold) {
productCode = Integer.parseInt(productSold);
quantitySold = Integer.parseInt(productSold);
}
public void setProduct(int product) {
productCode = product;
}
public void setSold(int sold) {
quantitySold = sold;
}
public int getProduct() {
return productCode;
}
public int getSold() {
return quantitySold;
}
}
public class Inventory {
int productCode;
double price;
int quantityOnHand;
public Inventory(int code, double cost, int inStock) {
productCode = code;
price = cost;
quantityOnHand = inStock;
}
public void setCode(int code) {
productCode = code;
}
public void setPrice(double price) {
this.price = price;
}
public void setInStock(int inStock) {
quantityOnHand = inStock;
}
public int getCode() {
return productCode;
}
public int getPrice() {
return price;
}
public int getInStock() {
return quantityOnHand;
}
public int postSale(int sold) {
quantityOnHand = quantityOnHand - sold;
return quantityOnHand;
}
}
I think my issue is with the main method as I can read the file fine but I cannot figure out how to use the data to create new Sales and Inventory objects.Any help is appreciated, thanks.
Explanation / Answer
//modified version of your code,i have added everything of your requirenment
import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] arg) throws IOException {
String line;
FileReader file=new FileReader("Sorted.txt");
Sales s[]=new Sales[100];
int i=0;
BufferedReader f = new BufferedReader(file);
while((line=f.readLine())!=null)
{
s[i]=new Sales(line);
i++;
}
f.close();
file.close();
file=new FileReader("Inventory.txt");
Inventory iv[]=new Inventory[100];
f = new BufferedReader(file);
int j=0;
while((line=f.readLine())!=null)
{
iv[j]=new Inventory(line);
j++;
}
System.out.println("Inventory Record:--");
for(int k=0;k<j;k++)
{
System.out.print(iv[k].getCode()+" ");
System.out.print(iv[k].getPrice()+" ");
System.out.println(iv[k].getInStock());
}
System.out.println("Sales Record:--");
boolean found=false;
for(int k=0;k<i;k++)
{
found=false;
for(int l=0;l<j;l++)
if((iv[l].productCode==s[k].productCode))
found=true;
if(!found)
System.out.println("Product code "+s[k].productCode+" not found");
else
{
System.out.print(s[k].getProduct()+" ");
System.out.println(s[k].getSold());
}
}
}
}
class Sales {
int productCode, quantitySold;
public Sales(String productSold) {
String data[];
data=productSold.split(",");
setProduct(Integer.parseInt(data[0]));
setSold(Integer.parseInt(data[1]));
}
public void setProduct(int product) {
productCode = product;
}
public void setSold(int sold) {
quantitySold = sold;
}
public int getProduct() {
return productCode;
}
public int getSold() {
return quantitySold;
}
}
class Inventory {
int productCode;
double price;
int quantityOnHand;
public Inventory(String InventoryRecord) {
String Data[];
Data=InventoryRecord.split(",");
setCode(Integer.parseInt(Data[0]));
setPrice(Double.parseDouble(Data[1]));
setInStock(Integer.parseInt(Data[2]));
}
public void setCode(int code) {
productCode = code;
}
public void setPrice(double price) {
this.price = price;
}
public void setInStock(int inStock) {
quantityOnHand = inStock;
}
public int getCode() {
return productCode;
}
public double getPrice() {
return price;
}
public int getInStock() {
return quantityOnHand;
}
public int postSale(int sold) {
quantityOnHand = quantityOnHand - sold;
return quantityOnHand;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.