Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Tried to put the BookPrices file in the JRE System library and it wont let me. I was told that is what I am doing

Tried to put the BookPrices file in the JRE System library and it wont let me. I was told that is what I am doing wrong but it wont let me fix it.

image text in transcribed

I keep getting these errors:

How do I fix it??

Exception in thread "main" java.io.FileNotFoundException: BookPrices.txt (The system cannot find the file specified)

at java.base/java.io.FileInputStream.open0(Native Method)

at java.base/java.io.FileInputStream.open(Unknown Source)

at java.base/java.io.FileInputStream.(Unknown Source)

at java.base/java.util.Scanner.(Unknown Source)

at shoppingCart.ShoppingCart.buildListPanel(ShoppingCart.java:281)

at shoppingCart.ShoppingCart.(ShoppingCart.java:84)

at shoppingCart.ShoppingCart.main(ShoppingCart.java:323)

package shoppingCart;

/* Add your required multi-line comment here

*

*/

//Necessary classes from java package

import java.awt.BorderLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.List;

import java.io.File;

import java.io.FileNotFoundException;

import java.text.DecimalFormat;

import java.util.Scanner;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollBar;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

//for the date

import java.text.SimpleDateFormat;

//class ShoppingCart that extends the frame class

public class ShoppingCart extends JFrame

{

private static JPanel listPanel;

private static JPanel shoppingcartPanel;

private static JPanel buttonsPanel;

//Defining the type of JList being used

private static JList listItems;

private static JButton addButton;

private static JButton removeButton;

private static JButton clearButton;

private static JButton checkOutButton;

//Declare listArray

private static String[] listArray;

private static List cartItems = new List();

// value for salestax

final double salesTax = 0.06;

// Constructor of class Shopping Cart

public ShoppingCart() throws FileNotFoundException {

// set the title

setTitle("Shopping Cart System");

// set the frame exit close button

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// set frame layout as grid layout 1row 3columns

setLayout(new GridLayout(1, 3));

// set frame position center

setLocationRelativeTo(null);

// call buildListPanel for list, button, & cart

buildListPanel();

buildButtonPanel();

buildCartPanel();

//components

add(listPanel);

add(buttonsPanel);

add(shoppingcartPanel);

pack();

// summon the frame

setVisible(true);

}

// method to add add,remove,clear and checkout buttons

private void buildButtonPanel() {

buttonsPanel = new JPanel();

// set layout to GridLayout

buttonsPanel.setLayout(new GridLayout(4, 1));

addButton = new JButton("Add To Cart");

// add action listener

addButton.addActionListener(new AddButtonListener());

removeButton = new JButton("Remove From Cart");

// add action listener to the removeButton

removeButton.addActionListener(new RemoveButtonListener());

clearButton = new JButton("Clear Cart");

// add action listener to clear button

clearButton.addActionListener(new clearButtonListener());

checkOutButton = new JButton("Check Out");

// add action listener to checkout button

checkOutButton.addActionListener(new CheckoutButtonListener());

// a dash of more buttons to buttonPanel

buttonsPanel.add(addButton);

buttonsPanel.add(removeButton);

buttonsPanel.add(clearButton);

buttonsPanel.add(checkOutButton);

}

// method implements add button action Listener

public class AddButtonListener implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

// sprinkle more items from list items

String value = (String) listItems.getSelectedValue();

cartItems.add(value);

}

}

// method implements remove button action listener

public class RemoveButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

// remove items from list items

String str = cartItems.getSelectedItem();

cartItems.remove(str);

}

}

// method removes all items added to the cart list

public class clearButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

cartItems.removeAll();

}

}

// method sprinkles more Label and List Components

private void buildCartPanel() {

shoppingcartPanel = new JPanel();

shoppingcartPanel.setLayout(new BorderLayout());

shoppingcartPanel.setBorder(BorderFactory.createEtchedBorder());

JLabel cartLbl = new JLabel("Cart");

cartLbl.setFont(new Font("Times New Roman", Font.BOLD, 18));

shoppingcartPanel.add(cartLbl, BorderLayout.NORTH);

shoppingcartPanel.add(cartItems, BorderLayout.CENTER);

}

// subtotal all book titles plus sales tax

public class CheckoutButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

String line;

double totalCost = 0;

double costofItem = 0;

File file = new File("BookPrices.txt");

Scanner fileReader = null;

try {

fileReader = new Scanner(file);

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

while (fileReader.hasNext()) {

line = fileReader.nextLine();

String[] cost = line.split(",");

String title = cost[0];

costofItem = Double.parseDouble(cost[1]);

for (int i = 0; i

if (title.equals(cartItems.getItem(i)))

totalCost += costofItem;

}

}

// calculate tax amount for total cost

double tax = salesTax * totalCost;

DecimalFormat myFormatter = new DecimalFormat("###.##");

// display the total cost in message box

JOptionPane.showMessageDialog(null, "Total Cost is:" + myFormatter.format(tax + totalCost));

}

}

// method creates the list panel with one list

private void buildListPanel() throws FileNotFoundException {

listPanel = new JPanel();

listPanel.setLayout(new BorderLayout());

listPanel.setBorder(BorderFactory.createEtchedBorder());

// set label text

JLabel label = new JLabel("Select A Book Title");

// set bold font

label.setFont(new Font("Times New Roman", Font.BOLD, 18));

String line;

String[] tempArray=new String[100];

int index = 0;

// read book titles from txt file

File file = new File("BookPrices.txt");

Scanner fileReader = new Scanner(file);

// read file title

while (fileReader.hasNext()) {

line = fileReader.nextLine();

String[] titles = line.split(",");

tempArray[index] = titles[0];

index++;

}

//Initialize listArray to be of length=index

//because now index represents number of lines in a file

listArray=new String[index];

//Add titles to listArray

for(int i=0;i

listArray[i]=tempArray[i];

}

// add titles of book

listItems = new JList(listArray);

// set list panel north side

listPanel.add(label, BorderLayout.NORTH);

// set list panel north with list items

listPanel.add(listItems, BorderLayout.CENTER);

}

// method for program execution

public static void main(String[] args) throws FileNotFoundException {

new ShoppingCart();

}

}

shoppingCart ShoppingCart.javaj JRE System Library [jre-9.0.4]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

d. How were you expected to contribute to family life?

Answered: 1 week ago