Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity >=0.5.0 <0.5.12;
- pragma experimental ABIEncoderV2;
- contract Goods{
- //Поля контракта
- //Стоимость товара
- uint24 cost;
- //Количество товара
- uint24 num_of_Goods;
- //Название товара
- string name;
- //Конструктор - вызывается при создании контракта
- constructor (uint24 en_cost, uint24 en_num, string memory en_name) public{
- cost = en_cost;
- num_of_Goods = en_num;
- name = en_name;
- }
- function getCost() public view returns(uint24){
- return cost;
- }
- function getNumOfGoods() public view returns(uint24){
- return num_of_Goods;
- }
- function getName() public view returns(string memory){
- return name;
- }
- function addNum(uint24 add_num){
- num_of_Goods += add_num;
- }
- }
- //Делаем контракт для создания корзины пользователя
- contract Cart{
- //Храним адрес владельца корзины
- address private owner;
- //Список товаров.
- address[] private list_of_goods;
- //Товар - его количество
- mapping(address => uint24) count_of_goods;
- uint32 sum_of_cart = 0;
- //Создаем конструктор, который вытаскивает
- //Отправителя транзакции и делает его
- //Владельцем корзины
- constructor () public {
- owner = msg.sender;
- }
- //Проверка хозяина
- function getOwner() public view returns(address){
- return owner;
- }
- //Добавляем в словарь(map) нужное количество товаров
- //Обращение по адресу товара в сети
- function addGoods(address Goods_addr, uint24 count) public {
- count_of_goods[Goods_addr] += count;
- //Вытащить цену на лекарство
- //И рассчитать новую сумму за корзину
- //Создаем объект Goods по его адресу
- Goods tmp_good = Goods(Goods_addr);
- sum_of_cart += count * tmp_good.getCost();
- }
- function getCartCost() public view returns(uint32){
- return sum_of_cart;
- }
- function checkGoods(address Goods_addr) public view returns(uint24){
- return count_of_goods[Goods_addr];
- }
- }
- contract Bill{
- //Кому чек выписываем
- address private buyer;
- //Временное поле - баланс клиента
- uint32 tempory_balance;
- constructor () public{
- buyer = msg.sender;
- }
- //Для демонстрации
- function setBalance(uint32 tmp_balace) public {
- tempory_balance = tmp_balace;
- }
- function make_a_bill(address addr_cart) public returns(string memory) {
- Cart tmp_cart = Cart(addr_cart);
- if (tempory_balance - tmp_cart.getCartCost() >= 0){
- } else {
- }
- }
- }
- /*Интерфейс работы с аптекой*/
- contract Apteka{
- //Храним адреса контрактов с товаром
- address[] private ListOfGoods;
- uint24 budget = 0;
- address owner;
- constructor(uint24 start_capital) public{
- //Мы храним адреса контрактов типа "Товар"
- //ListOfGoods.push(address(new Goods(100, 10, "Fenotropil")));
- owner = msg.sender;
- budget = start_capital;
- }
- //добавление товара
- funcrion addGood(uint24 cost_of_good, uint24 num_of_good, string memory name_of_good) public{
- require(owner == msg.sender);
- uint24 is_exists = -1;
- for (uint24 i = 0; i < ListOfGoods.length; i++){
- if (Goods(ListOfGoods[i]).getName() == name_of_good){
- is_exists = i;
- }
- }
- if (is_exists == -1){
- ListOfGoods.push(address(new Goods(cost_of_good, num_of_good, name_of_good)));
- }
- else{
- ListOfGoods[i]
- }
- }
- function getProdutcs() public view returns(address[] memory){
- return ListOfGoods;
- }
- //Получаем информацию о товаре по адресу
- function getGoodsInfo(address _prod) public view returns(string memory, uint24){
- //Создаем временный контракт по адресу оригинала
- Goods tmp = Goods(_prod);
- return (tmp.getName(), tmp.getCost());
- }
- function getAllInfo() public view returns(string[] memory, uint24[] memory){
- string[] memory tmp_str = new string[](ListOfGoods.length);
- uint24[] memory tmp_price = new uint24[](ListOfGoods.length);
- for (uint24 i = 0; i < ListOfGoods.length; i++){
- tmp_str[i] = Goods(ListOfGoods[i]).getName();
- tmp_price[i] = Goods(ListOfGoods[i]).getCost();
- }
- return (tmp_str, tmp_price);
- }
- }
Add Comment
Please, Sign In to add comment