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

Sample text file (Excerpt): (Note format: item number, name, category, price, an

ID: 3741540 • Letter: S

Question









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

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 I 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 chooses1 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. Part4 Thank Customer. Finally, the program thanks the customer for visiting the online store .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. Java code that accesses the text file must be enclosed in a try-catch block. Minimally, your program must handle the FileNotFoundException. .

Explanation / Answer

//item.txt

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

//Item.java

package com.src;

public class Item {

private int itemNumber;

private String itemName;

private String itemCategory;

private Double itemPrice;

private int itemQuantity=1;

public Item(int itemNumber, String itemName, String itemCategory, Double itemPrice, int itemQuantity) {

super();

this.itemNumber = itemNumber;

this.itemName = itemName;

this.itemCategory = itemCategory;

this.itemPrice = itemPrice;

this.itemQuantity = itemQuantity;

}

public int getItemNumber() {

return itemNumber;

}

public String getItemName() {

return itemName;

}

public String getItemCategory() {

return itemCategory;

}

public Double getItemPrice() {

return itemPrice;

}

public int getItemQuantity() {

return itemQuantity;

}

public void setItemNumber(int itemNumber) {

this.itemNumber = itemNumber;

}

public void setItemName(String itemName) {

this.itemName = itemName;

}

public void setItemCategory(String itemCategory) {

this.itemCategory = itemCategory;

}

public void setItemPrice(Double itemPrice) {

this.itemPrice = itemPrice;

}

public void setItemQuantity(int itemQuantity) {

this.itemQuantity = itemQuantity;

}

@Override

public String toString() {

return "Item [itemNumber=" + itemNumber + ", itemName=" + itemName + ", itemCategory=" + itemCategory

+ ", itemPrice=" + itemPrice + ", itemQuantity=" + itemQuantity + "]";

}

}

//Driver.java

package com.src;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.net.Inet4Address;

import java.util.ArrayList;

import java.util.Scanner;

public class Driver {

public static ArrayList<Item> cart=null;

public static ArrayList<Item> inventory = new ArrayList<>();

public static ArrayList<Item> CreateInventory() throws FileNotFoundException

{

ArrayList<Item> inventory = new ArrayList<>();

File file = new File("C:\Users\surya\eclipse-workspace\Chegg2018\items.txt"); // add local path of your item file

BufferedReader br = new BufferedReader(new FileReader(file));

String st;

int itemNumber = 0,quantity=0;

Double price=0.0;

String name="", category="";

int i=1;

try {

while ((st = br.readLine()) != null) {

st = st.trim();

switch(i)

{

case 1: itemNumber = Integer.parseInt(st);

i++;

break;

case 2: name = st;

i++;

break;

case 3: category = st;

i++;

break;

case 4: price = Double.parseDouble(st);

i++;

break;

case 5: quantity = Integer.parseInt(st);

Item it = new Item(itemNumber, name, category, price, quantity);

inventory.add(it);

i=1;

break;

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return inventory;

}

//Search item in inventory

public static int SearchItem(int ItemNumber){

for(int i=0;i<inventory.size();i++)

{

Item it = inventory.get(i);

if(it.getItemNumber() == ItemNumber){

return i;

}

}

return -1;

}

//Display Cart

public static void DisplayCart()

{

for(int i=0;i<cart.size();i++)

{

Item it = cart.get(i);

System.out.println("Number: " + it.getItemNumber());

System.out.println("Name: " + it.getItemName());

System.out.println("Category: " + it.getItemCategory());

System.out.println("Price: " + it.getItemPrice());

System.out.println("Quantity: "+ it.getItemQuantity());

System.out.println("");

}

}

public static void AddItem(int category){

/*System.out.println("choose a category");

System.out.println("1) Books");

System.out.println("2) Home & Gifts");

System.out.println("3) Movies & TV");

System.out.println("4) Music");*/

Scanner sc = new Scanner(System.in);

String categories[] = {"Books","Home & Gifts", "Movies & TV", "Music"};

for(int i=0;i<inventory.size();i++)

{

Item it = inventory.get(i);

if(it.getItemCategory().equals(categories[category])){

System.out.println(it.getItemNumber() + " " + it.getItemName() + " $" + it.getItemPrice());

}

}

System.out.println("Enter a number of item you wish to add.");

System.out.print("Your Choice? ");

int ItemNumber = sc.nextInt();

int itemIndex = SearchItem(ItemNumber);

while(true)

{

if( itemIndex != -1) {

Item item = inventory.get(itemIndex);

boolean found = false;

for(int i=0;cart!= null && i < cart.size();i++)

{

Item cartItem = cart.get(i);

if(cartItem.getItemNumber() == ItemNumber) {

found = true;

cartItem.setItemQuantity(cartItem.getItemQuantity() + 1 );

}

}

if(!found) {

//int itemNumber, String itemName, String itemCategory, Double itemPrice, int itemQuantity

Item itemToAdd = new Item(ItemNumber,item.getItemName(),item.getItemCategory(),item.getItemPrice(),1);

cart.add(itemToAdd);

}

System.out.println("Your choice " + item.getItemNumber() + " " + item.getItemName() + " has been added to shopping cart");

break;

}

else {

System.out.println("Item #"+ ItemNumber + " was not found in the inventory.");

System.out.println("Enter a number of item you wish to add.");

System.out.print("Your Choice? ");

ItemNumber = sc.nextInt();

itemIndex = SearchItem(ItemNumber);

}

}

DisplayCart();

}

public static void RemoveItem()

{

Scanner sc = new Scanner(System.in);

DisplayCart();

System.out.print("Plese enter Item number to remove: ");

int ItemNumber = sc.nextInt();

boolean found = false;

Item it = null ;

for(int i=0;i<cart.size();i++)

{

it = cart.get(i);

if(it.getItemNumber() == ItemNumber){

found = true;

}

}

if(found) {

if(it.getItemQuantity() > 1)

it.setItemQuantity(it.getItemQuantity() - 1);

else

cart.remove(it);

}

DisplayCart();

}

public static void checkOut()

{

System.out.println("Summary Of Order:");

DisplayCart();

}

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

System.out.println("Creating inventory....");

inventory = CreateInventory();

Scanner sc = new Scanner(System.in);

int choice;

System.out.println("/*********************************************************************/");

System.out.println("Wnlcome to Barnes and Noble Online!");

System.out.println("Choose an option.");

System.out.println("1) Create 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? ");

choice = sc.nextInt();

System.out.println("/*********************************************************************/");

while(choice != 4)

{

switch (choice) {

case 1:

System.out.println("/*********************************************************************/");

cart = new ArrayList<>();

System.out.println("Shopping cart created!");

System.out.println("/*********************************************************************/");

break;

case 2:

System.out.println("/*********************************************************************/");

if(cart == null)

System.out.println("Please create a shopping cart!");

else

{

int categoryOption = 0;

System.out.println("choose a category");

System.out.println("1) Books");

System.out.println("2) Home & Gifts");

System.out.println("3) Movies & TV");

System.out.println("4) Music");

categoryOption = sc.nextInt();

switch(categoryOption) {

case 0:

break;

case 1:

AddItem(1);

break;

case 2:

AddItem(2);

break;

case 3:

AddItem(3);

break;

case 4:

AddItem(4);

break;

default:

System.out.println(categoryOption + " is not a valid choice! Please Enter a number from 1 to 4.");

System.out.println("Enter 0 to return Main menu");

}

}

System.out.println("/*********************************************************************/");

break;

case 3:

RemoveItem();

break;

case 4:

checkOut();

System.out.println("Thanks!! Visit Again");

break;

default:

System.out.println("Please enter a valid option 1 to 4.");

break;

}

System.out.println("/*********************************************************************/");

System.out.println("Wnlcome to Barnes and Noble Online!");

System.out.println("Choose an option.");

System.out.println("1) Create 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? ");

choice = sc.nextInt();

}

}

}

//output:

Creating inventory....
/*********************************************************************/
Wnlcome to Barnes and 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!
/*********************************************************************/
/*********************************************************************/
Wnlcome to Barnes and 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!
/*********************************************************************/
/*********************************************************************/
Wnlcome to Barnes and 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
1
800 Doctor Who TARDIS Bookends Set of 2 $51.95
801 WonderFlex Light, Black $18.95
Enter a number of item you wish to add.
Your Choice? 888
Item #888 was not found in the inventory.
Enter a number of item you wish to add.
Your Choice? 800
/*********************************************************************/
/*********************************************************************/
Wnlcome to Barnes and 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

Due to time constraint I am not able to do checkout payment functionlity Please add this part again . As your question very big for 2 hour.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote