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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| pragma solidity ^0.5.0;
contract TiaoSao{ string public name; uint public productCount = 0; mapping (uint => Product) public products; struct Product{ uint id; string name; string description; uint price; address payable owner; bool purchased; } event ProductCreated( uint id, string name, string description, uint price, address payable owner, bool purchased );
event ProductPurchased( uint id, string name, string description, uint price, address payable owner, bool purchased ); constructor() public { name= "-----HITSZ Flea Market-----"; } function createProduct(string memory _name , string memory _description,uint _price) public { if(bytes(_name).length <= 0) revert("Product's name's length should be positive!"); if(bytes(_description).length <= 0) revert("Product's description's length should be positive!"); if(_price <= 0) revert("Product's price should be positive!"); productCount++; products[productCount] = Product(productCount,_name,_description,_price,msg.sender,false); emit ProductCreated(productCount,_name,_description,_price,msg.sender,false); } function purchaseProduct(uint _id) public payable { Product memory _product = products[_id]; address payable _seller = _product.owner; if(_product.id < 0 || _product.id > productCount) revert("Product's id should bewteen 1~productCount!"); if(msg.value < _product.price) revert("Fee not enough!"); if(_product.purchased) revert("This Product has been purchased!"); if(_seller == msg.sender) revert("You can't buy your own products!"); _product.owner = msg.sender; _product.purchased = true; products[_id]= _product; address(_seller).transfer(msg.value); emit ProductPurchased(productCount,_product.name,_product.description,_product.price,msg.sender,true); } function getAllProductBrief() view public returns(string memory){ string memory res = "-----HITSZ Flea Market"; for(uint i=1;i<=productCount;i++){ res=string(abi.encodePacked (res,"-----NO",uint2str(i),":","[Name]:",products[i].name,", [Price]:",uint2str(products[i].price),", [State]:",boolstr(products[i].purchased))); } return res; } function getAllProductDes() view public returns(string memory){ string memory res = "-----HITSZ Flea Market"; for(uint i=1;i<=productCount;i++){ res=string(abi.encodePacked (res,"-----NO",uint2str(i),":","[Name]:",products[i].name,", Description]:",products[i].description,",[Price]:",uint2str(products[i].price),", [State]:",boolstr(products[i].purchased))); } return res; } function getUnpurchasedProduct() view public returns(string memory){ string memory res = "---*Unpurchased Products:*"; uint i =1; uint j=1; while(j<=productCount){ if(!products[j].purchased){ res=string(abi.encodePacked (res,"-----NO",uint2str(j),":","[Name]:",products[j].name,", [Price]:",uint2str(products[j].price) )); i++; } j++; } if(i == 1) return "No Unpurchased Products"; return res; } function getPurchasedProduct()view public returns(string memory){ string memory res = "---*Purchased Products:*"; uint i =1; uint j=1; while(j<=productCount){ if(products[j].purchased){ res=string(abi.encodePacked (res,"-----NO",uint2str(j),":","[Name]:",products[j].name,", [Price]:",uint2str(products[j].price) )); i++; } j++; } if(i == 1) return "No Purchased Products"; return res; } function undercarriage(uint _id) public returns (string memory){ if(products[_id].owner == msg.sender) { for(uint i = _id;i <productCount;i++){ products[i]=products[i+1]; } productCount--; return "Product has been undercarriaged"; } else return "You are not the owner!"; } function uint2str(uint _i) internal pure returns (string memory _uintAsString) { if (_i == 0) { return "0"; } uint j = _i; uint len; while (j != 0) { len++; j /= 10; } bytes memory bstr = new bytes(len); uint k = len; while (_i != 0) { k = k-1; uint8 temp = (48 + uint8(_i - _i / 10 * 10)); bytes1 b1 = bytes1(temp); bstr[k] = b1; _i /= 10; } return string(bstr); } function boolstr(bool _i) internal pure returns (string memory){ if (_i == false) return "Unpurchased"; else return "Purchased"; } function char(bytes1 b) internal pure returns (bytes1 c) { if (uint8(b) < 10) return bytes1(uint8(b) + 0x30); else return bytes1(uint8(b) + 0x57); }
}
|