stored data infix data Task II: Write a program to keep track of a hardware stor
ID: 3692737 • Letter: S
Question
stored data
infix data
Task II: Write a program to keep track of a hardware store inventory in order to become familiar with the vector type and discover how to sort records using recursive functions.
Write a program to keep track of a hardware store inventory. The store sells various items. For each item in the store, the following information is kept: item ID, item name, number of pieces ordered, number of pieces currently in the store, number of pieces sold, manufacturers price for the item, and the stores selling price. At the end of each week, the store manager would like to see a report in the following form:
The total inventory is the total selling value of all of the items currently in the store. The total number of items is the sum of the number of pieces of all of the items in the store. Your program must be menu driven, giving the user various choices, such as checking whether an item is in the store, selling an item, and printing the report. After inputting the data, sort it according to the items names. Also, after an item is sold, update the appropriate counts. Initially, the number of pieces (of an item) in the store is the same as the number of pieces ordered, and the number of pieces of an item sold is zero. Input to the program is a le consisting of data in the following form:
itemID itemName pOrdered manufPrice sellingPrice
Use seven parallel vectors to store the information. The program must contain at least the following functions: one to input data into the vectors, one to display the menu, one to sell an item, and one to print the report for the manager.
Explanation / Answer
InventoryDemo.java
import java.io.*;
import java.util.*;
public class InventoryDemo
{
static Scanner console = new Scanner(System.in);
static final int N = 4;
public static void main(String[] args) throws FileNotFoundException
{
int answer;
String item;
Scanner inFile = new Scanner(new FileReader("data.txt"));
ArrayList<Inventory> inventory = new ArrayList<Inventory>(N);
fillReport(inventory, inFile);
do
{
System.out.println("Welcome to the Friendly Hardware Store!");
System.out.println("Choose among the following options.");
System.out.println("1: To see if an item is in the store.");
System.out.println("2: To buy an item.");
System.out.println("3: To check the price of an item.");
System.out.println("4: To print the inventory.");
System.out.println("9: To end the program.");
answer = console.nextInt();
switch(answer)
{
case 1:
checkItem(inventory);
break;
case 2:
buyItem(inventory);
break;
case 3:
checkPrice(inventory);
break;
case 4:
printReport(inventory);
break;
}
}while (answer != 9);
inFile.close();
}
//FILL ARRAY
public static void fillReport(ArrayList<Inventory> list, Scanner inFile)
{
int index;
int itemID, pOrdered, pInStore, pSold;
double manufPrice, sellingPrice;
String itemName;
for (index = 0; index < N; index++)
{
itemID = inFile.nextInt();
itemName = inFile.next() + " " + inFile.next();
pOrdered = inFile.nextInt();
pInStore = pOrdered;
pSold = 0;
manufPrice = inFile.nextDouble();
sellingPrice = inFile.nextDouble();
list.add(index, new Inventory(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice));
}
}
//CHECK ITEM
public static void checkItem(ArrayList<Inventory> list)
{
String y;
int index = 0;
System.out.println("Enter the name of the item: ");
y = new String(console.next() + " " + console.next());
for (index = 0; index < N; index++)
{
if (list.get(index).getItemName().compareTo(y) == 0 && list.get(index).getPInStore() > 0)
System.out.println(y + " is in the store");
else if (list.get(index).getPInStore() < 1)
System.out.println(y + " is not in the store");
}
}
//BUY ITEM
public static void buyItem(ArrayList<Inventory> list)
{
String y;
int index = 0, number = 0;
double amountDue = 0;
System.out.println("Enter the name of the item: ");
y = new String(console.next() + " " + console.next());
for (index = 0; index < N; index++)
{
if (list.get(index).getItemName().compareTo(y) == 0)
{
System.out.println("Enter the number of pieces: ");
number = console.nextInt();
amountDue = number * list.get(index).getSellingPrice();
System.out.printf("Amount Due = $%.2f%n", amountDue);
list.get(index).setPInStore(list.get(index).getPInStore() - number);
list.get(index).setPSold(list.get(index).getPSold() + number);
}
}
}
//CHECK PRICE
public static void checkPrice(ArrayList<Inventory> list)
{
String y;
int index = 0;
System.out.println("Enter the name of the item: ");
y = new String(console.next() + " " + console.next());
for (index = 0; index < N; index++)
{
if (list.get(index).getItemName().compareTo(y) == 0)
{
System.out.println("The price of " + y + " is " + list.get(index).getSellingPrice());
}
}
}
//PRINT REPORT
public static void printReport(ArrayList<Inventory> list)
{
int index;
int sum = 0;
double sum2 = 0;
System.out.printf("%-15s %-15s %-15s %-15s %-15s %-15s %-15s", "itemID", "itemName", "pOrdered", "pInStore", "pSold", "manufPrice", "sellingPrice");
System.out.println();
for (index = 0; index < N; index++)
{System.out.printf("%-15s %-15s %-15s %-15s %-15s", list.get(index).getItemID(), list.get(index).getItemName(), list.get(index).getPOrdered(), list.get(index).getPInStore(), list.get(index).getPSold());
System.out.printf("%.2f ", list.get(index).getManufPrice());
System.out.printf("%.2f %n", list.get(index).getSellingPrice());
}
for (index = 0; index < N; index++)
sum = sum + (list.get(index).getPInStore());
for (index = 0; index < N; index++)
sum2 = sum2 + ((list.get(index).getPInStore())*(list.get(index).getSellingPrice()));
System.out.println();
System.out.printf("Total Inventory: $%.2f%n", sum2);
System.out.println("Total number of items in the store: " + sum);
System.out.println();
}
}
Inventory.java
public class Inventory {
private int itemID, pOrdered, pInStore, pSold;
private double manufPrice, sellingPrice;
private String itemName;
//Default Constructor
public Inventory(){
itemID = 0;
itemName = " ";
pOrdered = 0;
pInStore = 0;
pSold = 0;
manufPrice = 0;
sellingPrice = 0;
}
//Constructor with parameters
public Inventory (int itemID, String itemName, int pOrdered, int pInStore, int pSold, double manufPrice, double sellingPrice){
this.itemID = itemID;
this.itemName = itemName;
this.pOrdered = pOrdered;
this.pInStore = pInStore;
this.pSold = pSold;
this.manufPrice = manufPrice;
this.sellingPrice = sellingPrice;
}
//itemID
public void setItemID(int itemID){
this.itemID = itemID;
}
public int getItemID(){
return itemID;
}
//itemName
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemName() {
return itemName;
}
//pOrdered
public void setPOrdered(int pOrdered){
this.pOrdered = pOrdered;
}
public int getPOrdered(){
return pOrdered;
}
//pInStore
public void setPInStore(int pInStore) {
this.pInStore = pInStore;
}
public int getPInStore() {
return pInStore;
}
//pSold
public void setPSold(int pSold){
this.pSold = pSold;
}
public int getPSold(){
return pSold;
}
//manufPrice
public void setManufPrice(double manufPrice) {
this.manufPrice = manufPrice;
}
public double getManufPrice() {
return manufPrice;
}
//sellingPrice
public void setSellingPrice(double sellingPrice) {
this.sellingPrice = sellingPrice;
}
public double getSellingPrice() {
return sellingPrice;
}
}
data.txt
1111
Cooking Range
50 450.00 850.00
2222
Circular Saw
150 45.00 125.00
3333
Dish Washer
20 250.50 550.50
4444
Micro Wave
75 150.00 400.00
sample output
Welcome to the Friendly Hardware Store!
Choose among the following options.
1: To see if an item is in the store.
2: To buy an item.
3: To check the price of an item.
4: To print the inventory.
9: To end the program.
1
Enter the name of the item:
Dish Washer
Dish Washer is in the store
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.