Question: I am trying to get this code to read a CSV file instead of a normal txt file. I believe I implemented the code to
I am trying to get this code to read a CSV file instead of a normal txt file. I believe I implemented the code to read the CSV file wrong. Any help would be great, as well as the code being fixed.
import java.ioBufferedReader;
import java.ioFile;
import java.ioFileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;
Item class
public class HW
Arrays to store data
public int itemCode;
public String itemName;
public float unitPrice;
Item constructor
HW
String data new String;
Reading file
data this.readFiledata;
Getting data
itemCode new intdatalength;
itemName new Stringdatalength;
unitPrice new floatdatalength;
forint i ;i data.length;i
String dataDivided dataisplit;
itemCodei Integer.parseIntdataDivided;
itemNamei dataDivided;
unitPricei Float.parseFloatdataDivided;
Reading files from the source provided
List records new ArrayList;
try BufferedReader br new BufferedReadernew FileReaderbookcsv
String line;
while line brreadLine null
String values line.splitCOMMADELIMITER;
records.addArraysasListvalues;
GetName function
public String getNameint code
return itemNamecode;
GetPrice function
public double getPriceint code
return unitPricecode ;
Driver's Class
public class OnlineSale
public static void mainString args
creating scanner class object
Scanner sc new ScannerSystemin;
Variables to store data
String data new String;
int count ;
int code;
double totalDataCode ;
double sales ;
System.out.printlnBeginning a new sale YN;
String ans scnext;
whileanstoLowerCaseLocaleROOTequalsy
if anstoLowerCaseLocaleROOTequalsy
do
HW n new HW;
System.out.printEnter product code: ;
String value scnext;
Checking whether input is correct or not
whilevaluecharAt valuecharAt
System.out.printlnInvalid product code";
System.out.printEnter product code: ;
value scnext;
code Integer.parseIntvalue;
if code
continue;
System.out.println item name: ngetNamecode;
String itemName ngetNamecode;
double price ngetPricecode;
System.out.printEnter quantity: ;
value scnext;
whilevaluecharAt valuecharAt
System.out.printlnInvalid quantity :;
System.out.printEnter quantity code: ;
value scnext;
int quantity Integer.parseIntvalue;
double total quantity price;
System.out.printlnItem Total : $ total;
totalDataCode totalDataCode total;
datacount quantity itemName $ String.formatf total;
count;
while code ;
System.out.println;
System.out.printlnItem List :;
for String datum : data
ifdatumnull
System.out.printlndatum;
System.out.printlnSubTotal $ totalDataCode;
double totalWithTax totalDataCode totalDataCode ;
System.out.printlnTotal with tax String.formatf totalWithTax;
System.out.printTendered Amount : $ ;
double amountPay Double.parseDoublescnext;
while true
if totalWithTax amountPay
System.out.printlnEnter tendered amount again.";
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
