In: Computer Science
The GroceryItem class must maintain the name of the item and its price (a double).
You must read the grocery data from file, and store each line's information in a single GroceryItem object. In other words, there should be one GroceryItem object created for each line in the file. Implement all standard and necessary getters and setters, as well as constructors. Also, implement a calculateWithTax method that takes a double parameter, and returns the price with the tax added.
The input file, input.txt is attached to this assignment. You
can assume each item has only one name without space (i.e., beef,
pork, chicken, salad are all acceptable, but roast beef, roast
chicken, honey ham, kale salad are not.)
Store all the objects of type GroceryItem in an ArrayList, and then in the main file, write a method that takes the ArrayList as a parameter, and prints out all the items with their pre- and post-tax prices.
The file I/O should take place in the GroceryItemDemo class, NOT the GroceryItem class.
**input.txt is as follows**
Tomatoes 1.99
Fudge 2.99
Bananas 1.99
Popcorn 0.99
Jerky 3.99
Milk 1.99
Two separate class files are required. We are using Netbeans with javaFX. I have input.txt in the correct place Im just confused on how to execute this.
Thanks for the question, here are the two classes you will be needing.
Please do update the inputFileName when you run the program in your system. Comments are included so that you can follow the code.
Let me know for any questions/doubts or incase you need any help.
thanks a lot !
=========================================================
public class GroceryItem {
private String
name;
private double
price;
public GroceryItem(String name,
double price) {
this.name = name;
this.price = price;
}
public String getName() {
return
name;
}
public double getPrice()
{
return
price;
}
public void setName(String
name) {
this.name = name;
}
public void
setPrice(double price) {
this.price = price;
}
@Override
public String toString()
{
return
"Name: " + getName() + ", Price
$" + String.format("%.2f",
getPrice());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class GroceryItemDemo { private static final double TAX = 5; // assuming the tax is 5% you may change it private static double calculateWithTax(double price) { return price * TAX / 100.0 + price; // calculate tax, add tax to the price and return it } public static void main(String[] args) { // update the input file name based on the location of the file in your system String inputFileName = "D:\\input.txt"; ArrayList<GroceryItem> groceryItems = new ArrayList<>(); try { Scanner fileReader = new Scanner(new File(inputFileName)); String line; // read each line of the file once line at a time while (fileReader.hasNextLine()) { line = fileReader.nextLine(); String data[] = line.split("\\s+"); // split the line by space character String itemName = data[0]; double itemPrice = Double.parseDouble(data[1]); // create a groceryitem object using the values GroceryItem anItem = new GroceryItem(itemName, itemPrice); // store the object in the array list groceryItems.add(anItem); } fileReader.close(); } catch (FileNotFoundException e) { System.out.println("Error: Unable to open/read file: " + inputFileName); System.exit(1); } // print each item with the pre and post tax amount for (GroceryItem item : groceryItems) { System.out.println("Item Name: " + item.getName() + ", Pre-Tax Price: $" + String.format("%.2f", item.getPrice()) + ", Post-Tax Price: $" + String.format("%.2f", calculateWithTax(item.getPrice()))); } } }
/////////////////////////////////////////////////////////////////////////////////////////////