Hi since I can't ask questions from previous answers I need to know why my code won't compile and run here it is: import
Posted: Sun May 15, 2022 10:07 am
Hi since I can't ask questions from previous answers I need to
know why my code won't compile and run here it is:
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;
public class ShoppingCart extends JFrame
{
private static JPanel listPanel;
private static JPanel shoppingcartPanel;
private static JPanel buttonsPanel;
private static JList<String> listItems;
private static JButton addButton;
private static JButton removeButton;
private static JButton clearButton;
private static JButton checkOutButton;
private static String[] listArray;
private static List cartItems = new List();
final double salesTax = 0.06;
public ShoppingCart() throws FileNotFoundException
{
setTitle("Shopping Cart System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 3));
setLocationRelativeTo(null);
buildListPanel();
buildButtonPanel();
buildCartPanel();
add(listPanel);
add(buttonsPanel);
add(shoppingcartPanel);
pack();
setVisible(true);
}
private void buildButtonPanel()
{
buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(4, 1));
addButton = new JButton("Add To Cart");
addButton.addActionListener(new AddButtonListener());
removeButton = new JButton("Remove From Cart");
removeButton.addActionListener(new RemoveButtonListener());
clearButton = new JButton("Clear Cart");
clearButton.addActionListener(new clearButtonListener());
checkOutButton = new JButton("Check Out");
checkOutButton.addActionListener(new
CheckoutButtonListener());
buttonsPanel.add(addButton);
buttonsPanel.add(removeButton);
buttonsPanel.add(clearButton);
buttonsPanel.add(checkOutButton);
}
public class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
String value = (String) listItems.getSelectedValue();
cartItems.add(value);
}
}
public class RemoveButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str = cartItems.getSelectedItem();
cartItems.remove(str);
}
}
public class clearButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
cartItems.removeAll();
}
}
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);
}
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 < cartItems.getItemCount(); i++)
{
if (title.equals(cartItems.getItem(i)))
totalCost += costofItem;
}
}
double tax = salesTax * totalCost;
DecimalFormat myFormatter = new DecimalFormat("###.##");
JOptionPane.showMessageDialog(null, "Total Cost is:" +
myFormatter.format(tax + totalCost));
}
}
private void buildListPanel() throws FileNotFoundException
{
listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
listPanel.setBorder(BorderFactory.createEtchedBorder());
JLabel label = new JLabel("Select A Book Title");
label.setFont(new Font("Times New Roman", Font.BOLD, 18));
String line;
String[] tempArray=new String[100];
int index = 0;
File file = new File("BookPrices.txt");
Scanner fileReader = new Scanner(file);
while (fileReader.hasNext())
{
line = fileReader.nextLine();
String[] titles = line.split(",");
tempArray[index] = titles[0];
index++;
}
listArray=new String[index];
for(int i=0;i<index;i++)
{
listArray=tempArray;
}
listItems = new JList<String>(listArray);
listPanel.add(label, BorderLayout.NORTH);
listPanel.add(listItems, BorderLayout.CENTER);
}
public static void main(String[] args) throws
FileNotFoundException
{
new ShoppingCart();
}
}
if you get it to run please tell me which IDE you use and also
the steps you took. Thank you
know why my code won't compile and run here it is:
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;
public class ShoppingCart extends JFrame
{
private static JPanel listPanel;
private static JPanel shoppingcartPanel;
private static JPanel buttonsPanel;
private static JList<String> listItems;
private static JButton addButton;
private static JButton removeButton;
private static JButton clearButton;
private static JButton checkOutButton;
private static String[] listArray;
private static List cartItems = new List();
final double salesTax = 0.06;
public ShoppingCart() throws FileNotFoundException
{
setTitle("Shopping Cart System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 3));
setLocationRelativeTo(null);
buildListPanel();
buildButtonPanel();
buildCartPanel();
add(listPanel);
add(buttonsPanel);
add(shoppingcartPanel);
pack();
setVisible(true);
}
private void buildButtonPanel()
{
buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(4, 1));
addButton = new JButton("Add To Cart");
addButton.addActionListener(new AddButtonListener());
removeButton = new JButton("Remove From Cart");
removeButton.addActionListener(new RemoveButtonListener());
clearButton = new JButton("Clear Cart");
clearButton.addActionListener(new clearButtonListener());
checkOutButton = new JButton("Check Out");
checkOutButton.addActionListener(new
CheckoutButtonListener());
buttonsPanel.add(addButton);
buttonsPanel.add(removeButton);
buttonsPanel.add(clearButton);
buttonsPanel.add(checkOutButton);
}
public class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
String value = (String) listItems.getSelectedValue();
cartItems.add(value);
}
}
public class RemoveButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str = cartItems.getSelectedItem();
cartItems.remove(str);
}
}
public class clearButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
cartItems.removeAll();
}
}
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);
}
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 < cartItems.getItemCount(); i++)
{
if (title.equals(cartItems.getItem(i)))
totalCost += costofItem;
}
}
double tax = salesTax * totalCost;
DecimalFormat myFormatter = new DecimalFormat("###.##");
JOptionPane.showMessageDialog(null, "Total Cost is:" +
myFormatter.format(tax + totalCost));
}
}
private void buildListPanel() throws FileNotFoundException
{
listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
listPanel.setBorder(BorderFactory.createEtchedBorder());
JLabel label = new JLabel("Select A Book Title");
label.setFont(new Font("Times New Roman", Font.BOLD, 18));
String line;
String[] tempArray=new String[100];
int index = 0;
File file = new File("BookPrices.txt");
Scanner fileReader = new Scanner(file);
while (fileReader.hasNext())
{
line = fileReader.nextLine();
String[] titles = line.split(",");
tempArray[index] = titles[0];
index++;
}
listArray=new String[index];
for(int i=0;i<index;i++)
{
listArray=tempArray;
}
listItems = new JList<String>(listArray);
listPanel.add(label, BorderLayout.NORTH);
listPanel.add(listItems, BorderLayout.CENTER);
}
public static void main(String[] args) throws
FileNotFoundException
{
new ShoppingCart();
}
}
if you get it to run please tell me which IDE you use and also
the steps you took. Thank you