-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inventory.sol
65 lines (55 loc) · 2.48 KB
/
Inventory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0;
contract Inventory{
//Items Information
struct Items{
uint256 ItemID;
string ItemName;
uint256 ItemQuantity;
uint256 ItemPrice;
}
Items[] public ItemsInInventory; //Inventory`s Array
//Function to add Items in Inventory Array.
//Enter Item ID, Item Name, Item Quantity, Item Price as arguments.
function addItem(uint _itemID, string memory _itemName, uint256 _itemQuantity, uint256 _itemPrice) public{
ItemsInInventory.push(Items(_itemID, _itemName,_itemQuantity,_itemPrice));
}
//Function to sell Items from Inventory Array.
//Enter Item ID, Item Quantity as arguments.
function sellItem(uint _itemID, uint256 _itemQuantity) public{
for (uint256 i=0;i<ItemsInInventory.length;i++){
if (ItemsInInventory[i].ItemID==_itemID){
uint256 itemQuantityAlreadyInStock = ItemsInInventory[i].ItemQuantity;
ItemsInInventory[i].ItemQuantity=itemQuantityAlreadyInStock-_itemQuantity;
}
}
}
//Function to edit information of Items in Inventory Array.
//Enter Item ID, Item Name, Item Quantity, Item Price as arguments.
function editItem(uint _itemID, string memory _itemName, uint256 _itemQuantity, uint256 _itemPrice) public {
for (uint256 j=0;j<ItemsInInventory.length;j++){
if (ItemsInInventory[j].ItemID==_itemID){
ItemsInInventory[j].ItemName = _itemName;
ItemsInInventory[j].ItemQuantity = _itemQuantity;
ItemsInInventory[j].ItemPrice = _itemPrice;
}
}
}
//Function to display information of Items in Inventory Array.
//Enter Item ID as arguments.
function displayItem(uint256 _itemID) public view returns (uint256,string memory,uint256,uint256){
for (uint256 k=0;k<ItemsInInventory.length;k++){
if (ItemsInInventory[k].ItemID==_itemID){
return (ItemsInInventory[k].ItemID, ItemsInInventory[k].ItemName, ItemsInInventory[k].ItemQuantity, ItemsInInventory[k].ItemPrice);
}
}
}
//Function to display Items who are out of stock.
function OutOfStockItems() public view returns (uint256, string memory){
for (uint256 l=0;l<ItemsInInventory.length;l++){
if (ItemsInInventory[l].ItemQuantity==0){
return (ItemsInInventory[l].ItemID, ItemsInInventory[l].ItemName);
}
}
}
}