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
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.