-
Notifications
You must be signed in to change notification settings - Fork 0
/
shop
68 lines (58 loc) · 1.59 KB
/
shop
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
66
67
68
pragma solidity ^0.5.17;
contract shop
{
address payable owner;
uint iud = 1;
uint count = 0;
mapping (uint => product) Product;
product[] pro;
struct product
{
uint id;
address seller;
string name;
uint price;
uint quantity;
string status;
}
constructor() public
{
owner = msg.sender;
}
function addProduct(string memory _name, uint _price, uint _quantity, string memory _status) public
{
Product[iud] = product(iud, msg.sender, _name, _price, _quantity, _status);
iud++;
}
function getProduct(uint _id) public view returns(uint, address, string memory, uint, uint, string memory)
{
return (pro[_id-1].id, pro[_id-1].seller, pro[_id-1].name, pro[_id-1].price, pro[_id-1].quantity, pro[_id-1].status);
}
function loadingProducts() public
{
pro.length = 0;
for(uint i = 1; i < iud; i++)
{
if(Product[i].id!=0)
{
pro.push(Product[i]);
}
}
}
function removeProduct(uint _id) public
{
delete Product[_id];
delete pro[_id-1];
}
function buyProduct(uint _id, uint _qty) payable external
{
uint cost = Product[_id].price * _qty;
require(cost==msg.value, "Check the payment...!");
owner.transfer(msg.value);
Product[_id].quantity = Product[_id].quantity - _qty;
if(Product[_id].quantity==0)
{
Product[_id].status = "Sold Out";
}
}
}