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

Your task is to write a simple inventory control system for a small specialty st

ID: 3548844 • Letter: Y

Question

Your task is to write a simple inventory control system for a small specialty store, which acquires and sells various items of different types. Your program will process an input file containing various types of transactions and should produce reports as required.

Each transaction or activity will appear as a separate line in the input.txt file. The end of the input is marked by a line containing an asterisk in column one; no other activity lines will be so marked. Activity lines begin with a lower-case keyword identifying the action to be performed. The names of the items in the inventory are case sensitive, and each is represented by a unique item id consisting of no more than ten non-blank characters. All fields in the activity lines are separated by blanks. The activity lines will have one of the following formats:

new item-id item-cost item-selling-price

This line adds a new item (not previously carried in the store) to the potential inventory. The item-cost and item-selling-price are given as normal dollar amounts, without the dollar sign. That is, they will contain one or more decimal digits, a decimal point, and two more decimal digits. Note that this activity line doesn't actually result in a change in the inventory, but is used in anticipation of adding units of the new item to the store's offerings. item-cost is what the store owner pays for each unit of the item, and item-selling-price is the price for which the owner sells the item. item-cost and item-selling-price will never be larger than 100.00. Since item ids are unique, a transaction line of this type containing an item id already in inventory should simply be ignored.

delete item-id

If an item isn't selling well, the store owner can remove it from the inventory by including this line in the program input. All units of item-id in the inventory are written off as a loss.

buy item-id quantity

When the store owner buys some units (at the unit-cost previously indicated) of an item-id to offer for sale, she'll indicate that with one of these lines in the program input. quantity indicates the number of units she purchased. The quantity she purchases will never be larger than 5000 at a time, but the total number of units in the inventory may be as large as 10,000.

sell item-id quantity

When one or more units of an item are sold, that fact is recorded by placing one of these lines in the input. quantity indicates the number of units sold (at the item-selling-price previously indicated). Obviously, the quantity sold cannot exceed the number of items in stock.

report

This line in the input requests a report. This is the only input line for which output is expected. Your program will display columns, with suitable headings, showing item-id, the buying price, the selling price, the number of units in the inventory, and the value

of the units in the inventory (that is, the product of the number of units in the inventory and the buying price). Following the last item, the total value of all units in the inventory should be displayed. Finally, a line should appear showing the total profit since the last report was issued. Profit is defined as total sales, less the cost of the items sold, less the cost of items written off (by the delete activity). The sample output shown illustrates the desired format for the report.

Any illegal transactions (ie, buying units of an item not in inventory, selling more units than the number in stock, etc.) should cause error messages to be included in an errorlog.txt output file, so that it can be examined by the store owner for possible adjustments

Your task is to write a simple inventory control system for a small specialty store, which acquires and sells various items of different types. Your program will process an input file containing various types of transactions and should produce reports as required. Each transaction or activity will appear as a separate line in the input.txt file. The end of the input is marked by a line containing an asterisk in column one; no other activity lines will be so marked. Activity lines begin with a lower-case keyword identifying the action to be performed. The names of the items in the inventory are case sensitive, and each is represented by a unique item id consisting of no more than ten non-blank characters. All fields in the activity lines are separated by blanks. The activity lines will have one of the following formats: new item-id item-cost item-selling-price This line adds a new item (not previously carried in the store) to the potential inventory. The item-cost and item-selling-price are given as normal dollar amounts, without the dollar sign. That is, they will contain one or more decimal digits, a decimal point, and two more decimal digits. Note that this activity line doesn't actually result in a change in the inventory, but is used in anticipation of adding units of the new item to the store's offerings. item-cost is what the store owner pays for each unit of the item, and item-selling-price is the price for which the owner sells the item. item-cost and item-selling-price will never be larger than 100.00. Since item ids are unique, a transaction line of this type containing an item id already in inventory should simply be ignored. delete item-id If an item isn't selling well, the store owner can remove it from the inventory by including this line in the program input. All units of item-id in the inventory are written off as a loss. buy item-id quantity When the store owner buys some units (at the unit-cost previously indicated) of an item-id to offer for sale, she'll indicate that with one of these lines in the program input. quantity indicates the number of units she purchased. The quantity she purchases will never be larger than 5000 at a time, but the total number of units in the inventory may be as large as 10,000. sell item-id quantity When one or more units of an item are sold, that fact is recorded by placing one of these lines in the input. quantity indicates the number of units sold (at the item-selling-price previously indicated). Obviously, the quantity sold cannot exceed the number of items in stock. report This line in the input requests a report. This is the only input line for which output is expected. Your program will display columns, with suitable headings, showing item-id, the buying price, the selling price, the number of units in the inventory, and the value of the units in the inventory (that is, the product of the number of units in the inventory and the buying price). Following the last item, the total value of all units in the inventory should be displayed. Finally, a line should appear showing the total profit since the last report was issued. Profit is defined as total sales, less the cost of the items sold, less the cost of items written off (by the delete activity). The sample output shown illustrates the desired format for the report. Any illegal transactions (ie, buying units of an item not in inventory, selling more units than the number in stock, etc.) should cause error messages to be included in an errorlog.txt output file, so that it can be examined by the store owner for possible adjustments

Explanation / Answer

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;

import java.text.DecimalFormat;

import java.util.Scanner;


public class Inventory {

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

//To write error messages on a file in given path

PrintStream out = new PrintStream(new FileOutputStream("output.txt"));

System.setErr(out);

//Declaring an array of class Item type

Item itemArray[] = new Item[100];

int i = 0;


double profitTotal = 0;

//Logic to read file contents

//Making a file instance

File file = new File("input.txt");


try {

//Scanner instance

Scanner scanner = new Scanner(file);

//Read till the file has next line it it

while (scanner.hasNextLine()) {

String line = scanner.nextLine();

//If line content is *, then stop reading

if (line.equals("*"))

break;

//Storing the contents of one line separated by space into a String type array

String[] splited = line.split("\s+");


//if the first word of the line is new

if (splited[0].equals("new")) {

boolean flag = false;

//Padding the name for better output indentation.

while(splited[1].length() < 10) splited[1]=splited[1]+" ";

//if name already exist in the array then set flag to true

for (int j = 0; j < itemArray.length; j++) {

if (itemArray[j] != null) {

if (itemArray[j].getName().equals(splited[1])) {

flag = true;

}

}

}

//If flag is false. i.e if the value does not exist in the array, then check if the item cost and selling price has value lesser than 100

//if yes, make an object with the following value. else print the error message in log file and make the object and proceed.

if (!flag) {

if (Double.parseDouble(splited[2])> 100 )

System.err.println("Unit Price for buying should always be less than or equal to 100. Price mentioned by you for "+splited[1]+" exceeds the limit.");

if (Double.parseDouble(splited[3]) > 100)

System.err.println("Unit Price for selling should always be less than or equal to 100. Price mentioned by you for "+splited[1]+" exceeds the limit.");

Item item = new Item("new", splited[1],

Double.parseDouble(splited[2]),

Double.parseDouble(splited[3]));

//Add the object into array

itemArray[i] = item;

i++;

}

}

//if the first word of the line is buy

else if (splited[0].equals("buy")) {

boolean flag = false;

int quantity = 0;

while(splited[1].length() < 10) splited[1]=splited[1]+" ";

for (int j = 0; j < itemArray.length; j++) {

if (itemArray[j] != null) {

//if the item exist in our list

if (itemArray[j].getName().equals(splited[1])) {

flag = true;

//if trying to add more than 5000 quantity then print error message

if (Integer.parseInt(splited[2])> 5000){

System.err.println("At one time, you can only add at maximum 5000 quantity of an item");

}

//taking the value from the getter method of item class

quantity = itemArray[j].getQuantity();

quantity = quantity

+ Integer.parseInt(splited[2]);

// if the total quantity exceeds 10000 of a particular item, then print error message in output file

if (quantity> 10000){

System.err.println("We can have at maximum 10000 quantities of particular item");

}

//set the quantity value to modified value

itemArray[j].setQuantity(quantity);

}


}


}//if the item does not exist in the array and you are trying to buy it, print error message

if (!flag)

System.err.println("You are trying to buy "+splited[1]+". We do not have any such item in our shop.");


//if the first word of the line is sell

} else if (splited[0].equals("sell")) {

while(splited[1].length() < 10) splited[1]=splited[1]+" ";

for (int j = 0; j < itemArray.length; j++) {

if (itemArray[j] != null) {

//if the item exist

if (itemArray[j].getName().equals(splited[1])) {

int quantity = itemArray[j].getQuantity();

//and you are trying to sell a quantity which is greater than the quantity in stock, then print error message and come out of the loop

if (Integer.parseInt(splited[2]) > quantity){

System.err.println("You are trying to sell more number of items than we have for "+itemArray[j].getName()+" ");

break;


}


quantity = quantity

- Integer.parseInt(splited[2]);

//set the quantity to modified value

itemArray[j].setQuantity(quantity);

//multiply the number of items to difference of selling price minus buying price.

profitTotal += (double) Integer

.parseInt(splited[2])

* (itemArray[j].getitemSellingPrice() - itemArray[j]

.getitemCost());

}

}

}

}


//if the first word of the line is delete

else if (splited[0].equals("delete")) {

boolean flag = false;

while(splited[1].length() < 10) splited[1]=splited[1]+" ";

for (int j = 0; j < itemArray.length; j++) {

if (itemArray[j] != null) {


if (itemArray[j].getName().equals(splited[1])) {

flag= true;

//if the item exist

//subtract loss from the existing profit value. Value of loss will be number of units remaining * price of each unit

profitTotal = (profitTotal - (itemArray[j].getitemCost()* (double)itemArray[j].getQuantity()));

//remove the entry from the array

itemArray[j] = null;


}

}

}

//if item does not exist, print error message

if (!flag)

System.err.println("You are trying to delete item id "+splited[1]+". It does not exist in our Shop");

}

//if the first word of line is report

else if (splited[0].equals("report")) {

//Using decimal formatter to format the numbers

DecimalFormat decimal = new DecimalFormat("####0.00");

double totalValue = 0;

System.out

.println(" INVENTORY REPORT ");

System.out

.println("Item Name Buy At Sell At On Hand Value");

System.out

.println("--------- ------ ------- ------- -----");

for (int j = 0; j < itemArray.length; j++) {

if (itemArray[j] != null) {

//Printing data after formatting using DecimalFormat

System.out

.println(itemArray[j].getName()

+ " "

+ decimal.format(itemArray[j].getitemCost())

+ " "

+ decimal.format(itemArray[j].getitemSellingPrice())

+ " "

+ itemArray[j].getQuantity()

+ " "

+ decimal.format((double) itemArray[j]

.getQuantity() * itemArray[j]

.getitemCost()));

totalValue += (double) itemArray[j].getQuantity()

* itemArray[j].getitemCost();


}

}

System.out.println("------------------------");

System.out

.println("Total value of inventory " + decimal.format(totalValue));

System.out.println("Profit since last report "

+ decimal.format(profitTotal));

profitTotal = 0;

}

}

scanner.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}


class Item {

//Declaring private variables

private String type;

private String name;

private double itemCost;

private double itemSellingPrice;

private int quantity;


//Parameterized constructor

public Item(String type, String name, double itemCost, double itemSellingPrice) {

super();

this.type = type;

this.name = name;

this.itemCost = itemCost;

this.itemSellingPrice = itemSellingPrice;

this.quantity = 0;

}


//getter and setter methods

public int getQuantity() {

return quantity;

}


public void setQuantity(int quantity) {

this.quantity = quantity;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getitemCost() {

return itemCost;

}

public void setitemCost(double itemCost) {

this.itemCost = itemCost;

}

public double getitemSellingPrice() {

return itemSellingPrice;

}

public void setitemSellingPrice(double itemSellingPrice) {

this.itemSellingPrice = itemSellingPrice;

}



}