Question: JAVA: Cannot get this to run, not really sure what is causing all the errors or what I am doing wrong, running in eclipse: package

JAVA: Cannot get this to run, not really sure what is causing all the errors or what I am doing wrong, running in eclipse:

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

}

}

I also am trying to use this text file with the program, it has the book list and prices in it

JAVA: Cannot get this to run, not really sure what is causing

age E.. X - D *ShoppingCartjava BookPrices.txt E 1 Did It Your Way, 11.95 ISS238 NAL src 2 The History of Scotland, 14.50 3 Learn Calculus in One Day, 29.95 4 Feel the Stress, 18.560 5 Great Poems, 12.95 shoppingCart 6 Europe on a Shoestring, 10.95 ShoppingCar The Life of Mozart, 14.50 BookPrices.t 8 Effective Java, 47.42 JRE System Library 9Head First Java, 24.50 10 Learn Java in One Day, 2.99 11 Java a Beginner's Guide, 14.75 12 Starting Out with Java, 138.16 13 Introduction to Java, 132.03 14 Java How to Program, 151.5!5 15 Java Software Solutions, 119.75 16 Java Foundations, 140.88 17 Android Boot Camp, 62.88 18 Core Java, 38.27 19 Java How to Program, 59.95

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!