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

· Create a Java GUI application that works like a shopping cart system for an on

ID: 3717271 • Letter: #

Question

·         Create a Java GUI application that works like a shopping cart system for an online book store.

·         You will be provided a file named BookPrice.txt. This file contains the names and prices of various books, formatted as follows:

I Did It Your Way, 11.95

The History of Scotland, 14.50

Learn Calculus in One Day, 29.95

Feel the Stress, 18.50

·         Each line in the file contains the name of a book, followed by a comma, followed by the book’s retail price. When your application begins execution, it should read the contents of the file and store the titles in a ListView.

·         The user should be able to select a book from the list and add it to the Shopping Cart.

·         The Shopping Cart is another ListView.

·         The application should have buttons or menu items that allow the user to Add Selected Items, Remove Selected Items, and Check Out.

·         When the Check Out button is selected, the application should calculate and display the subtotal of all books in the shopping cart, the sales tax of 7%, and the total. It should also create a receipt file, which should be a text file that includes the books purchased, the quantities of each book purchased, their prices, and total price both before and after taxes. Here is an example of what the receipt file could look like:

(File Name would be Receipt-02-41-2018.14.52.56.txt)

Receipt: 02-41-2018 14:52:56

Title                                         Price

----------------------------------- ---------

The Life of Mozart                 $ 14.50

Java How to Program           $151.55

Feel the Stress                         $ 18.50

Core Java                                $ 38.27

Sub Total                                 $222.82

Tax                                          $ 15.60

                                                ---------

Grand Total                             $238.42

·         To exit the application, click the x in the upper right corner.

Explanation / Answer

I have tried to conplete it as much as possible. But because of less time i was not able to implement the receipt part. Please post a different question for adding the receipt part...

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;

//create class ShoppintCart that extends the frame class

public class ShoppingCart extends JFrame {

private static JPanel listPanel;

private static JPanel shoppingcartPanel;

private static JPanel buttonsPanel;

//You need to define which type of JList you are using

//This was causing nchecked or unsafe operations.

private static JList<String> listItems;

private static JButton addButton;

private static JButton removeButton;

private static JButton clearButton;

private static JButton checkOutButton;

//Only declare listArray here, as right now you dont know the number of books in text file

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

// sprinkle some 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 < cartItems.getItemCount(); 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<index;i++){

listArray[i]=tempArray[i];

}

// add titles of book

listItems = new JList<String>(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();

}

}