Question: Create an application that creates a ShoppingBag object and reads an input file. For each item the file contains the number purchased and the cost

Create an application that creates a ShoppingBag object and reads an input file. For each

item the file contains the number purchased and the cost of the item. Read until end of file. Place each item into the shopping bag object and when you are done; provide a summary of the current status of the Shopping Bag. Use a tax rate of 7%. Send all output to an output file.

You also need to provide exception handling. You may not assume that every line in the input file contains the expected data types. You are expecting an int followed by a double but you may not get this with every input line that is read. (There may be input mismatch exceptions.)

If an input line contains an input mismatch exception, then catch and handle the exception. Handle the exception by skipping that line of input and counting the error. The incorrect line of input should not crash your program. At the end of the program output the number of errors found.

Example:

There were 5 lines of input skipped due to input mismatch exception.

So far i got.

shoppingbag.java

import java.text.DecimalFormat;

public class ShoppingBag implements Retail {

private int itemsinbag; private double tax; private double totalcost; private double itemcost; DecimalFormat df = new DecimalFormat("$##.##"); public ShoppingBag(double tax) { this.tax = tax; } public void buy(int itemsinbag, double itemcost) { this.itemcost += itemcost; double costofitem = itemsinbag * itemcost; this.itemsinbag += itemsinbag; this.totalcost += costofitem; } @Override public double RetailCost() { return this.totalcost; }

@Override public double AverageCost() { return TotalCost() / TotalItems(); }

@Override public double TotalCost() { return ((getTax() * RetailCost() ) + RetailCost()); }

@Override public int TotalItems() { return itemsinbag; } public double getIndividualCost() { return this.itemcost; } public double getTax() { return tax; } @Override public String toString() { return "items :" + TotalItems() + "retail cost : " + df.format(RetailCost()) + "average cost : " + df.format(AverageCost()) + "Total cost (including tax (7%)) : " + TotalCost(); } }

retail.java

public interface Retail { double RetailCost(); double AverageCost(); double TotalCost(); int TotalItems(); }

driver.java

import java.util.Scanner;

/** * * @author kevin */ public class driver { final static double TAX_RATE = .07; public static void main(String args[]) { Scanner kbd = new Scanner(System.in); // needed for scanner class ShoppingBag target = new ShoppingBag(TAX_RATE); // ShoppingBag class I created target.buy(5, 5.00); System.out.println(target.getIndividualCost()); } }

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 Databases Questions!