Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3883288 • Letter: C

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 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());
    }
}

Explanation / Answer

public interface Retail {

double RetailCost();

double AverageCost();

double TotalCost();

int TotalItems();

}

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();

}

}

import java.io.File;

import java.util.InputMismatchException;

import java.util.Scanner;

/**

*

* @author kevin

*/

public class driver {

final static double TAX_RATE = .07;

public static void main(String args[]) {

try {

Scanner kbd = new Scanner(new File("inputfile.txt")); // needed for

// scanner

// class

ShoppingBag target = new ShoppingBag(TAX_RATE); // ShoppingBag class

// I created

int countInputMismath = 0;

while (kbd.hasNext()) {

String line = kbd.nextLine();

int quantity = 0;

double costOfItem = 0.0d;

String[] lineArr = line.split(" ");

if (lineArr.length == 2) {

try {

quantity = Integer.parseInt(lineArr[0]);

} catch (Exception e) {

// TODO: handle exception

countInputMismath++;

}

try {

costOfItem = Double.parseDouble(lineArr[1]);

} catch (Exception e) {

// TODO: handle exception

countInputMismath++;

}

target.buy(quantity, costOfItem);

} else {

countInputMismath++;

}

}

System.out.println(target);

System.out

.println("There were "

+ countInputMismath

+ " lines of input skipped due to input mismatch exception.");

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

inputfile.txt

5 211.09
4
3
a
4 2332

3 43
4 dff
as 343
as dd

OUTPUT:

items :16, retail cost : $10512.45, average cost : $703.02, Total cost (including tax (7%)) : 11248.3215

NOTE: The last line contains two mismatches.
There were 8 lines of input skipped due to input mismatch exception.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote