1. Problem: Create a Java application that implements an online store complete w
ID: 3741463 • Letter: 1
Question
1. Problem: Create a Java application that implements an online store complete with shopping cart and
check out procedure. To clarify the problem, I have divided it into 4 parts.
Part 1: Create Store Inventory. The program begins by creating an online store inventory from a list
of items stored in a text file. A try-catch block must be used to handle checked exceptions.
Part 2: Create/Add to/Remove from Shopping Cart. After creating the store inventory, the program
welcomes the customer, then provides him/her with a menu of options for creating a shopping cart,
adding items to or removing items from the shopping cart, or checking out. Creating a shopping cart
entails creating an empty shopping cart. Adding an item to the shopping cart entails searching the
store inventory first by category then by item to locate an item to add. (If the customer chooses to
add an item that is already in the cart, simply increase the quantity by one.) Removing an item from
the shopping cart entails displaying the current contents of the shopping cart, then prompting the
customer to enter the number of the item to remove. The shopping cart must be displayed after each
addition or removal. All customer input must be validated. try-catch blocks must be used to
handle unchecked exceptions. The program continues to display the main menu until the customer
decides to check out.
Part 3: Check Out. Once the customer selects check out, the program displays a summary of the
customer’s order. Next, the program asks the customer how s/he wishes to pay for the order. If the
customer chooses 1 to charge to a credit card on file, the program simply displays the payment
summary. If the customer chooses 2 to charge to a new credit card, the program prompts the
customer to enter payment information (card holder full name, followed by card type, number, and
expiration date), then displays the credit payment summary. Both payment summaries must be
formatted as shown in the sample runs. The customer’s choice of payment option must be validated.
With respect to the credit card information, you need only check that the card number consists of 16
digits. try-catch blocks must be used to handle unchecked exceptions.
Part 4: Thank Customer. Finally, the program thanks the customer for visiting the online store.
Implementation Requirements and Guidelines:
o All items must be stored in and read from a text file. An example of recommended formatting
for the text file appears at the end of this document.
o Java code that accesses the text file must be enclosed in a try-catch block. Minimally, your
program must handle the FileNotFoundException.
o You must use a minimum of two classes to implement this program: a “driver” containing main and
an Item class. Important Note: Each Java class must be in a separate *.java file.
o The store inventory and the shopping cart must each be implemented as an ArrayList of
Item objects.
o An Item object must have 5 attributes: item number (int), item name (String), item
category (String), item price (double), and item quantity (int; initialize to 1).
o Required methods for the Item class include:
o A five-argument constructor that initializes the five instance variables (specified above).
o set and get methods for each of the five instance variables.
o A toString method to generate nicely formatted output.
o The store inventory must contain at least 15 items from 3 or more categories with 3 or more items per
category.
o As usual, your program output should strongly resemble the sample runs at the end of this document.
Pay particular attention to the handling of invalid input.
Sample Runs:
Sample run #1 (Customer correctly creates a shopping cart, adds items to it, then checks out.):
run:
Creating inventory ...
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 1
/************************************************************************************************/
Shopping cart created!
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 2
/************************************************************************************************/
Choose a category.
1) Books
2) Home & Gifts
3) Movies & TV
4) Music
Your choice? 3
600 Bones: Season 12 $19.99
601 Logan $17.99
602 The Polar Express $ 9.99
Enter the number of the item you wish to add.
Your choice? 600
Your choice "600 Bones: Season 12" has been added to the shopping cart.
Number: 600
Name: Bones: Season 12
Category: Movies & TV
Price: $ 19.99
Quantity: 1
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 2
/************************************************************************************************/
Choose a category.
1) Books
2) Home & Gifts
3) Movies & TV
4) Music
Your choice? 2
800 Doctor Who TARDIS Bookends Set of 2 $51.95
801 Wallace Plaid Mini Umbrella $14.95
802 WonderFlex Light, Black $18.95
Enter the number of the item you wish to add.
Your choice? 801
Your choice "801 Wallace Plaid Mini Umbrella" has been added to the shopping cart.
Number: 600
Name: Bones: Season 12
Category: Movies & TV
Price: $ 19.99
Quantity: 1
Number: 801
Name: Wallace Plaid Mini Umbrella
Category: Home & Gifts
Price: $ 14.95
Quantity: 1
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 3
/************************************************************************************************/
Displaying the current contents of the shopping cart ...
Number: 600
Name: Bones: Season 12
Category: Movies & TV
Price: $ 19.99
Quantity: 1
Number: 801
Name: Wallace Plaid Mini Umbrella
Category: Home & Gifts
Price: $ 14.95
Quantity: 1
Enter the number of the item you wish to remove.
Your choice? 801
Item #801 has been removed from the shopping cart.
Displaying the current contents of the shopping cart ...
Number: 600
Name: Bones: Season 12
Category: Movies & TV
Price: $ 19.99
Quantity: 1
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 2
/************************************************************************************************/
Choose a category.
1) Books
2) Home & Gifts
3) Movies & TV
4) Music
Your choice? 1
100 Doctor Who: Deep Time $ 8.47
101 Doctor Who: Heart of Stone $ 9.99
102 The Hitchhiker's Guide to the Galaxy $ 7.99
Enter the number of the item you wish to add.
Your choice? 100
Your choice "100 Doctor Who: Deep Time" has been added to the shopping cart.
Number: 600
Name: Bones: Season 12
Category: Movies & TV
Price: $ 19.99
Quantity: 1
Number: 100
Name: Doctor Who: Deep Time
Category: Books
Price: $ 8.47
Quantity: 1
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 2
/************************************************************************************************/
Choose a category.
1) Books
2) Home & Gifts
3) Movies & TV
4) Music
Your choice? 1
100 Doctor Who: Deep Time $ 8.47
101 Doctor Who: Heart of Stone $ 9.99
102 The Hitchhiker's Guide to the Galaxy $ 7.99
Enter the number of the item you wish to add.
Your choice? 100
Your choice "100 Doctor Who: Deep Time" has been added to the shopping cart.
Number: 600
Name: Bones: Season 12
Category: Movies & TV
Price: $ 19.99
Quantity: 1
Number: 100
Name: Doctor Who: Deep Time
Category: Books
Price: $ 8.47
Quantity: 2
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 4
/************************************************************************************************/
ORDER SUMMARY:
Number: 600
Name: Bones: Season 12
Category: Movies & TV
Price: $ 19.99
Quantity: 1
Number: 100
Name: Doctor Who: Deep Time
Category: Books
Price: $ 8.47
Quantity: 2
Order Total: $ 39.15 (6.0% tax included)
/************************************************************************************************/
How do you wish to pay for your order?
(Enter 1 to charge to credit card on file or 2 to charge to new credit card.): 2
Please enter your payment information:
Card holder name: Jane Wymark
Credit card type (e.g., MasterCard): Visa
Credit card number (e.g., 5201345098756420): 4321009854761189
Expiration date (e.g., 10/2016): 08/2021
/************************************************************************************************/
Credit payment summary:
Customer name: Jane Wymark
Payment amount: $ 39.15
Card type: Visa
Card number: ************1189
Exp date: 08/2021
/************************************************************************************************/
Thanks for shopping at Barnes & Noble Online.
Please come back soon!
/************************************************************************************************/
BUILD SUCCESSFUL (total time: 5 minutes 29 seconds)
Sample run #2 (Customer makes a variety of invalid choices while shopping but finally checks out.):
run:
Creating inventory ...
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 2
/************************************************************************************************/
Please create a shopping cart!
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 1
/************************************************************************************************/
Shopping cart created!
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 2
/************************************************************************************************/
Choose a category.
1) Books
2) Home & Gifts
3) Movies & TV
4) Music
Your choice? 5
5 is not a valid choice! Please enter a number from 1 to 4.
Enter 0 to return to the main menu.
Your choice? 4
500 Acoustic Christmas $31.34
501 Hamilton (Original Broadway Cast Recording) $22.39
502 Les Miserables (Original Broadway Cast) $30.16
Enter the number of the item you wish to add.
Your choice? 503
Item #503 was not found in the inventory.
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 2
/************************************************************************************************/
Choose a category.
1) Books
2) Home & Gifts
3) Movies & TV
4) Music
Your choice? 4
500 Acoustic Christmas $31.34
501 Hamilton (Original Broadway Cast Recording) $22.39
502 Les Miserables (Original Broadway Cast) $30.16
Enter the number of the item you wish to add.
Your choice? 502
Your choice "502 Les Miserables (Original Broadway Cast)" has been added to the shopping cart.
Number: 502
Name: Les Miserables (Original Broadway Cast)
Category: Music
Price: $ 30.16
Quantity: 1
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? 2
/************************************************************************************************/
Choose a category.
1) Books
2) Home & Gifts
3) Movies & TV
4) Music
Your choice? 1
100 Doctor Who: Deep Time $ 8.47
101 Doctor Who: Heart of Stone $ 9.99
102 The Hitchhiker's Guide to the Galaxy $ 7.99
Enter the number of the item you wish to add.
Your choice? 101
Your choice "101 Doctor Who: Heart of Stone" has been added to the shopping cart.
Number: 502
Name: Les Miserables (Original Broadway Cast)
Category: Music
Price: $ 30.16
Quantity: 1
Number: 101
Name: Doctor Who: Heart of Stone
Category: Books
Price: $ 9.99
Quantity: 1
/************************************************************************************************/
Welcome to Barnes & Noble Online!
Choose an option.
1) Create empty shopping cart
2) Add item to shopping cart
3) Remove item from shopping cart
4) Check out
Your choice? four
For input string: "four" is not a valid format for an integer. Please re-enter your choice.
Your choice? 4
/************************************************************************************************/
ORDER SUMMARY:
Number: 502
Name: Les Miserables (Original Broadway Cast)
Category: Music
Price: $ 30.16
Quantity: 1
Number: 101
Name: Doctor Who: Heart of Stone
Category: Books
Price: $ 9.99
Quantity: 1
Order Total: $ 42.56 (6.0% tax included)
/************************************************************************************************/
How do you wish to pay for your order?
(Enter 1 to charge to credit card on file or 2 to charge to new credit card.): 3
3 is not a valid payment option.
Please enter 1 to charge to card on file or 2 to charge to new card.
How do you wish to pay for your order?
(Enter 1 to charge to credit card on file or 2 to charge to new credit card.): 1
Payment summary:
Payment amount: $ 42.56 charged to card on file
/************************************************************************************************/
Thanks for shopping at Barnes & Noble Online.
Please come back soon!
/************************************************************************************************/
BUILD SUCCESSFUL (total time: 8 minutes 28 seconds)
Sample text file (Excerpt):
(Note format: item number, name, category, price, and quantity listed on separate lines for each attribute with no blank
line(s) between items.)
100
Doctor Who: Deep Time
Books
8.47
1
101
Doctor Who: Heart of Stone
Books
9.99
1
800
Doctor Who TARDIS Bookends Set of 2
Home & Gifts
51.95
1
801
WonderFlex Light, Black
Home & Gifts
18.95
1
500
Acoustic Christmas
Music
31.34
1
501
Hamilton (Original Broadway Cast Recording)
Music
22.39
1
Explanation / Answer
package January;
// Class Item definition
public class Item
{
// Instance variables to store data
String itemNumber;
String name;
String category;
double price;
int quantity;
// Default constructor
Item()
{
itemNumber = name = category = null;
price = quantity = 0;
}// End of constructor
// Parameterized constructor
Item(String itemNumber, String name, String category, double price, int quantity)
{
this.itemNumber = itemNumber;
this.name = name;
this.category = category;
this.price = price;
this.quantity = quantity;
}// End of constructor
// Method to set item number
void setItemNumber(String num)
{
itemNumber = num;
}// End of method
// Method to set name
void setName(String na)
{
name = na;
}// End of method
// Method to set category
void setCategory(String ca)
{
category = ca;
}// End of method
// Method to set price
void setPrice(double pr)
{
price = pr;
}// End of method
// Method to set quantity
void setQuantity(int quan)
{
quantity = quan;
}// End of method
// Method to return item number
String getItemNumber()
{
return itemNumber;
}// End of method
// Method to return name
String getName()
{
return name;
}// End of method
// Method to return category
String getCategory()
{
return category;
}// End of method
// Method to return price
double getPrice()
{
return price;
}// End of method
// Method to return quantity
int getQuantity()
{
return quantity;
}// End of method
// Method to override toString() to display item information
public String toString()
{
return " Number: " + getItemNumber() + " Name: " + getName() +
" Category: " + getCategory() + " Price: $" + getPrice()
+ " Quantity: " + getQuantity();
}// End of method
}// End of class
-----------------------------------------------------------------------------------------------------------
package January;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
// Class ShopingCart definition
public class ShopingCart
{
// Creates an array of objects for Item class
Item item[] = new Item[50];
// Scanner class object created
Scanner sc = new Scanner(System.in);
// // Method to display welcome menu, accept user choice and return it
int welcomeMenu()
{
// To store user choice
int choice;
// Displays menu
System.out.println(" /*************************************************************************");
System.out.println("Welcome to Barners & Nobel Online!");
System.out.println("Choose an option.");
System.out.println("1) Create an empty shopping cart");
System.out.println("2) Add Item to shopping cart");
System.out.println("3) Remove item from shopping cart");
System.out.println("4) Check out");
System.out.print(" Your Choice? ");
// Accepts user choice
choice = sc.nextInt();
System.out.println("/*************************************************************************");
// Returns user choice
return choice;
}// End of method
// Method to display category menu, accept user choice and return it
int categoryMenu()
{
// To store user choice
int choice;
// Displays menu
System.out.print(" Choice a category: ");
System.out.print(" 1) Books");
System.out.print(" 2) Home & Gifts");
System.out.print(" 3) Movie & TV");
System.out.print(" 4) Music");
System.out.print(" Your Choice? ");
// Accepts user choice
choice = sc.nextInt();
// Returns user choice
return choice;
}// End of method
// Method to read file contents and store it in item object
// Returns number of records
int readFile()
{
// Counter for number of records
int counter = 0;
// try block begins
try
{
// Scanner class object created to open file for reading
// Using anonymous object of class FileInputStream
Scanner inFile = new Scanner (new FileInputStream("shopingCart.txt"));
// Loops till data available
while(inFile.hasNextLine())
{
// Initializes object
item[counter] = new Item();
// Reads data from file and calls the appropriate setter method to set data
item[counter].setItemNumber(inFile.nextLine());
System.out.println("No: " + item[counter].getItemNumber());
item[counter].setName("Name: " + inFile.nextLine());
System.out.println(item[counter].getName());
item[counter].setCategory(inFile.nextLine());
System.out.println("Cate: " + item[counter].getCategory());
item[counter].setPrice(Double.parseDouble(inFile.nextLine()));
System.out.println("Price: " + item[counter].getPrice());
item[counter].setQuantity(Integer.parseInt(inFile.nextLine()));
System.out.println("Qty: " + item[counter].getQuantity());
// Increase record counter by one
counter++;
}// End of while
// Close the file
inFile.close();
}// End of try block
// Catch block to hand File not found exception
catch(FileNotFoundException fnfe)
{
// Displays error message
System.out.println("File shopingCart.txt was not found!");
}// End of catch block
// Returns number of records
return counter;
}// End of method
// main method definition
public static void main(String[] args)
{
// Creates an object of ShopingCart
ShopingCart sCart = new ShopingCart();
// Creates an array list object for item
ArrayList<Item> cart = new ArrayList<Item>();
int create = 0, countItem = 0;
// Loops till user choice is not 4
do
{
// Calls the display welcome menu
// Checks the return value and calls the appropriate method
switch(sCart.welcomeMenu())
{
case 1:
// Sets the create value to one
create = 1;
// Calls the method to read file
countItem = sCart.readFile();
System.out.println("Shopping cart created!");
break;
case 2:
// Checks if create is not 1 i.e., not selected option 1
if(create != 1)
// Displays error message
System.out.println("Please create a shopping cart!");
// Otherwise option one selected
else
{
// Creates a string array to store item number match found
String no[] = new String[20];
int f = 0, found = 0;
// To store the item number entered by the user
String num;
int c, numC = 0;
// Calls the method to display category menu and calls the appropriate method
switch(sCart.categoryMenu())
{
case 1:
// Loops till number of records
for(c = 0; c < countItem; c++)
{
// Compares the current category with the category option selected
if(sCart.item[c].getCategory().trim().equalsIgnoreCase("Books"))
{
// Displays the category selected item information
System.out.println(sCart.item[c].getItemNumber() +
" " + sCart.item[c].getName() + " $"+ sCart.item[c].getPrice());
// Stores the item number in the string array
no[numC] = sCart.item[c].getItemNumber();
// Increase the found category number counter
numC++;
}// End of if condition
}// End of for loop
// Accept the item number to add to cart
System.out.print(" Enter number of items you wish to add.");
System.out.print(" Your choice? ");
num = sCart.sc.next();
// Loops till number of selected item number found
for(int d = 0; d < numC; d++)
{
// Compares current item number from the selected list with the
// item number entered by the user
if(no[d].trim().equals(num))
{
// Set the found to one and come out of the loop
found = 1;
break;
}// End of if condition
}// End of for loop
// Checks if found is zero display error message item number not found
if(found == 0)
System.out.println("Itrem #" + num + " not found in the inventory");
// Otherwise, item number found
else
{
// Loops till number of records
// Check for the duplicate record
for(c = 0; c < countItem; c++)
{
// Checks if the current item number in the item list with the item number entered by the user
if(sCart.item[c].getItemNumber().trim().equalsIgnoreCase(num))
{
System.out.println("Your choice " " + num + " " +
sCart.item[c].getCategory() + "" has been added to the shopping cart.");
// Loops till number of records in the cart
for(int e = 0; e < cart.size(); e++)
{
// Checks if the item number in the cart is equals to the item number entered by the user
if(cart.get(e).getItemNumber().trim().equals(num))
{
// Increase the quantity of the existing item by one
sCart.item[e].setQuantity(sCart.item[e].getQuantity()+1);
// Set the f to one
f = 1;
}// End of inner if condition
}// End of for loop
// Checks if f is zero item number not found
if(f == 0)
// Add the item to the cart
cart.add(sCart.item[c]);
}// End of outer if condition
}// End of outer for loop
}// End of else
break;
case 2:
// Loops till number of records
for(c = 0; c < countItem; c++)
{
// Compares the current category with the category option selected
if(sCart.item[c].getCategory().trim().equalsIgnoreCase("Home & Gifts"))
{
// Displays the category selected item information
System.out.println(sCart.item[c].getItemNumber() +
" " + sCart.item[c].getName() + " $"+ sCart.item[c].getPrice());
// Stores the item number in the string array
no[numC] = sCart.item[c].getItemNumber();
// Increase the found category number counter
numC++;
}// End of if condition
}// End of for loop
// Accept the item number to add to cart
System.out.print(" Enter number of items you wish to add.");
System.out.print(" Your choice? ");
num = sCart.sc.next();
// Loops till number of selected item number found
for(int d = 0; d < numC; d++)
{
// Compares current item number from the selected list with the
// item number entered by the user
if(no[d].trim().equals(num))
{
// Set the found to one and come out of the loop
found = 1;
break;
}// End of if condition
}// End of for loop
// Checks if found is zero display error message item number not found
if(found == 0)
System.out.println("Itrem #" + num + " not found in the inventory");
// Otherwise, item number found
else
{
// Loops till number of records
// Check for the duplicate record
for(c = 0; c < countItem; c++)
{
// Checks if the current item number in the item list with the item number entered by the user
if(sCart.item[c].getItemNumber().trim().equalsIgnoreCase(num))
{
System.out.println("Your choice " " + num + " " +
sCart.item[c].getCategory() + "" has been added to the shopping cart");
// Loops till number of records in the cart
for(int e = 0; e < cart.size(); e++)
{
// Checks if the item number in the cart is equals to the item number entered by the user
if(cart.get(e).getItemNumber().trim().equals(num))
{
// Increase the quantity of the existing item by one
sCart.item[e].setQuantity(sCart.item[e].getQuantity()+1);
// Set the f to one
}// End of inner if condition
}// End of inner for loop
// Checks if found is zero display error message item number not found
if(f == 0)
// Add the item to the cart
cart.add(sCart.item[c]);
}// End of for loop
}// End of outer if condition
}// End of outer for loop
break;
case 3:
// Loops till number of records
for(c = 0; c < countItem; c++)
{
// Compares the current category with the category option selected
if(sCart.item[c].getCategory().trim().equalsIgnoreCase("Movies & TV"))
{
// Displays the category selected item information
System.out.println(sCart.item[c].getItemNumber() +
" " + sCart.item[c].getName() + " $"+ sCart.item[c].getPrice());
// Stores the item number in the string array
no[numC] = sCart.item[c].getItemNumber();
// Increase the found category number counter
numC++;
}// End of if condition
}// End of for loop
// Accept the item number to add to cart
System.out.print(" Enter number of items you wish to add.");
System.out.print(" Your choice? ");
num = sCart.sc.next();
// Loops till number of selected item number found
for(int d = 0; d < numC; d++)
{
// Compares current item number from the selected list with the
// item number entered by the user
if(no[d].trim().equals(num))
{
// Set the found to one and come out of the loop
found = 1;
break;
}// End of if condition
}// End of for loop
// Checks if found is zero display error message item number not found
if(found == 0)
System.out.println("Itrem #" + num + " not found in the inventory");
// Otherwise, item number found
else
{
// Loops till number of records
// Check for the duplicate record
for(c = 0; c < countItem; c++)
{
// Checks if the current item number in the item list with the item number entered by the user
if(sCart.item[c].getItemNumber().trim().equalsIgnoreCase(num))
{
System.out.println("Your choice " " + num + " " +
sCart.item[c].getCategory() + "" has been added to the shopping cart");
// Loops till number of records in the cart
for(int e = 0; e < cart.size(); e++)
{
// Checks if the item number in the cart is equals to the item number entered by the user
if(cart.get(e).getItemNumber().trim().equals(num))
{
// Increase the quantity of the existing item by one
sCart.item[e].setQuantity(sCart.item[e].getQuantity()+1);
// Set the f to one
}// End of inner if condition
}// End of inner for loop
// Checks if found is zero display error message item number not found
if(f == 0)
// Add the item to the cart
cart.add(sCart.item[c]);
}// End of outer if condition
}// End of outer for loop
}// End of for loop
break;
case 4:
// Loops till number of records
for(c = 0; c < countItem; c++)
{
// Compares the current category with the category option selected
if(sCart.item[c].getCategory().trim().equalsIgnoreCase("Music"))
{
// Displays the category selected item information
System.out.println(sCart.item[c].getItemNumber() +
" " + sCart.item[c].getName() + " $"+ sCart.item[c].getPrice());
// Stores the item number in the string array
no[numC] = sCart.item[c].getItemNumber();
// Increase the found category number counter
numC++;
}// End of if condition
}// End of for loop
// Accept the item number to add to cart
System.out.print(" Enter number of items you wish to add.");
System.out.print(" Your choice? ");
num = sCart.sc.next();
// Loops till number of selected item number found
for(int d = 0; d < numC; d++)
{
// Compares current item number from the selected list with the
// item number entered by the user
if(no[d].trim().equals(num))
{
// Set the found to one and come out of the loop
found = 1;
break;
}// End of if condition
}// End of for loop
// Checks if found is zero display error message item number not found
if(found == 0)
System.out.println("Itrem #" + num + " not found in the inventory");
// Otherwise, item number found
else
{
// Loops till number of records
// Check for the duplicate record
for(c = 0; c < countItem; c++)
{
// // Checks if the current item number in the item list with the item number entered by the user
if(sCart.item[c].getItemNumber().trim().equalsIgnoreCase(num))
{
System.out.println("Your choice " " + num + " " +
sCart.item[c].getCategory() + "" has been added to the shopping cart");
// Loops till number of records in the cart
for(int e = 0; e < cart.size(); e++)
{
// Checks if the item number in the cart is equals to the item number entered by the user
if(cart.get(e).getItemNumber().trim().equals(num))
{
// Increase the quantity of the existing item by one
sCart.item[e].setQuantity(sCart.item[e].getQuantity()+1);
// Set the f to one
}// End of if condition
}// End of inner for loop
// Checks if found is zero display error message item number not found
if(f == 0)
// Add the item to the cart
cart.add(sCart.item[c]);
}// End of outer if condition
}// End of outer for loop
}// End of else
break;
}
}
break;
case 3:
int f = 0;
// Checks if create is not 1 i.e., not selected option 1
if(create != 1)
// Displays error message
System.out.println("Please create a shopping cart!");
// Otherwise option one selected
else
{
// Loops till number of record in the cart and display it
for(int c = 0; c < cart.size(); c++)
System.out.println(cart.get(c));
// Accept the item number to remove
System.out.print(" Enter the Item Number: ");
String no = sCart.sc.next();
// Loops till number of record in the cart and display it
for(int c = 0; c < cart.size(); c++)
{
// Compares the current item number in the cart with the item number entered by the user
if(cart.get(c).getItemNumber().trim().equals(no))
{
// Set the f value to one for found
f = 1;
// Remove the item from the cart
cart.remove(c);
}// End of if condition
}// End of for loop
// Checks if the f value is zero display error message not found
if(f == 0)
System.out.println("Item not found.");
}// End of else
break;
case 4:
// To store the total
double total = 0;
// To store payment option
int opt;
System.out.println("ORDER SUMMARY:");
// Loops till number of record in the cart and display it
for(int c = 0; c < cart.size(); c++)
{
// Displays the information
System.out.println(cart.get(c));
// Calculates the total amount
total += cart.get(c).getQuantity() * cart.get(c).getPrice();
}// End of for loop
// Calculates the tax
total += total * 0.06;
// Displays the amount
System.out.printf("Order Total: $ %.2f (6.0 tax included)", total);
System.out.println(" /****************************************************/");
// Loops till valid payment option
do
{
// Displays payment menu
System.out.println("How do you wish to pay for your order?");
System.out.println("(Enter 1 to charge to credit card on file or 2 to charge to new credit card.): ");
// Accepts user choice
opt = sCart.sc.nextInt();
// Checks if choice is one
if(opt == 1)
{
System.out.println("Payment summary: ");
System.out.printf("Payment amount: $ %.2f charged to card on file", total );
break;
}// End of if condition
// Checks if choice is two
else if(opt == 2)
{
System.out.println("Payment summary: ");
System.out.printf("Payment amount: $ %.2f charged to new credit card", total );
break;
}// End of else if
// Otherwise, invalid option
else
System.out.println(opt + " is not a valid payment option.");
}while(true);// End of do while condition
System.out.println(" /************************************************************************************************/");
System.out.println(" Thanks for shopping at Barnes & Noble Online.");
System.out.println("Please come back soon!");
System.out.println("/************************************************************************************************/");
break;
}// End of outer switch case
}while(true); // End of do while loop
}// End of main method
}// End of class
Sample Output:
/*************************************************************************
Welcome to Barners & Nobel Online!
Choose an option.
1) Create an empty shopping cart
2) Add Item to shopping cart
3) Remove item from shopping cart
4) Check out
Your Choice? 2
/*************************************************************************
Please create a shopping cart!
/*************************************************************************
Welcome to Barners & Nobel Online!
Choose an option.
1) Create an empty shopping cart
2) Add Item to shopping cart
3) Remove item from shopping cart
4) Check out
Your Choice? 1
/*************************************************************************
Shopping cart created!
/*************************************************************************
Welcome to Barners & Nobel Online!
Choose an option.
1) Create an empty shopping cart
2) Add Item to shopping cart
3) Remove item from shopping cart
4) Check out
Your Choice? 2
/*************************************************************************
Choice a category:
1) Books
2) Home & Gifts
3) Movie & TV
4) Music
Your Choice? 1
100 Doctor Who: Deep Time $8.47
101 Doctor Who: Heart of Stone $9.99
Enter number of items you wish to add.
Your choice? 100
Your choice " 100 Books " has been added to the shopping cart.
/*************************************************************************
Welcome to Barners & Nobel Online!
Choose an option.
1) Create an empty shopping cart
2) Add Item to shopping cart
3) Remove item from shopping cart
4) Check out
Your Choice? 2
/*************************************************************************
Choice a category:
1) Books
2) Home & Gifts
3) Movie & TV
4) Music
Your Choice? 1
100 Doctor Who: Deep Time $8.47
101 Doctor Who: Heart of Stone $9.99
Enter number of items you wish to add.
Your choice? 100
Your choice " 100 Books " has been added to the shopping cart.
/*************************************************************************
Welcome to Barners & Nobel Online!
Choose an option.
1) Create an empty shopping cart
2) Add Item to shopping cart
3) Remove item from shopping cart
4) Check out
Your Choice? 2
/*************************************************************************
Choice a category:
1) Books
2) Home & Gifts
3) Movie & TV
4) Music
Your Choice? 2
800 Doctor Who TARDIS Bookends Set of 2 $51.95
801 WonderFlex Light, Black $18.95
Enter number of items you wish to add.
Your choice? 800
Your choice " 800 Home & Gifts " has been added to the shopping cart
/*************************************************************************
Welcome to Barners & Nobel Online!
Choose an option.
1) Create an empty shopping cart
2) Add Item to shopping cart
3) Remove item from shopping cart
4) Check out
Your Choice? 4
/*************************************************************************
ORDER SUMMARY:
Number: 100
Name: Doctor Who: Deep Time
Category: Books
Price: $8.47
Quantity: 2
Number: 800
Name: Doctor Who TARDIS Bookends Set of 2
Category: Home & Gifts
Price: $51.95
Quantity: 1
Order Total: $ 73.02 (6.0 tax included)
/****************************************************/
How do you wish to pay for your order?
(Enter 1 to charge to credit card on file or 2 to charge to new credit card.):
3
3 is not a valid payment option.
How do you wish to pay for your order?
(Enter 1 to charge to credit card on file or 2 to charge to new credit card.):
1
Payment summary:
Payment amount: $ 73.02 charged to card on file
/************************************************************************************************/
Thanks for shopping at Barnes & Noble Online.
Please come back soon!
shopingCart.txt file contents
100
Doctor Who: Deep Time
Books
8.47
1
101
Doctor Who: Heart of Stone
Books
9.99
1
800
Doctor Who TARDIS Bookends Set of 2
Home & Gifts
51.95
1
801
WonderFlex Light, Black
Home & Gifts
18.95
1
500
Acoustic Christmas
Music
31.34
1
501
Hamilton (Original Broadway Cast Recording)
Music
22.39
1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.