daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Nov 19th, 2018 64 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. pragma solidity ^0.4.25;
  2.  
  3.  
  4.  
  5. contract car_insurance{
  6.        
  7.     struct car
  8.     {
  9.         uint license;
  10.         uint total_fee;
  11.         bool is_applied;
  12.         uint voted_yes;
  13.         uint voted_no;
  14.     }
  15.     uint public IndexSize; //total # of cumstomers
  16.    
  17.    
  18.    
  19.     uint fee;
  20.     uint compensation;
  21.     uint collected_coins=0;
  22.     //car[] public cars;
  23.     mapping (address=>car) public cars;
  24.    
  25.     constructor() public //start
  26.     {
  27.        
  28.         fee = 100; //100 eth
  29.         compensation = 1000;
  30.         IndexSize=1;
  31.     }
  32.  
  33.     function addCar(address _customer, uint _license) public returns (bool)
  34.     {//input information by users
  35.  
  36.      //store new info into data
  37.         if(cars[_customer].license ==0){ //check if it is existing in the data
  38.             cars[_customer].license = _license;
  39.             cars[_customer].total_fee = fee;
  40.             cars[_customer].is_applied = false;
  41.             cars[_customer].voted_yes=0;
  42.             cars[_customer].voted_no=0;
  43.  
  44.            
  45.              if (!address(this).send(1)) throw; // pay 1 as fee & store 1 into the contract
  46.              
  47.              collected_coins +=1;
  48.              IndexSize++; //increase total # of cumstomers
  49.         }
  50.         else{
  51.             cars[_customer].total_fee +=1; //total fee ++ in case if it exists in the data
  52.         }
  53.     }
  54.    
  55.    
  56.     //apply compensation on user's side -> input car's info
  57.     function apply_compensation(uint _license) public payable
  58.     {
  59.         if(cars[msg.sender].license!=0) // checking if it's existing
  60.         {
  61.             //if it exists then,
  62.             cars[msg.sender].is_applied = true; // set applied status as true
  63.             //wait for the vote -> we have to find a way to force users to use "vote(_car , bool) function"
  64.             while(cars[msg.sender].voted_yes+cars[msg.sender].voted_no < IndexSize)
  65.             {
  66.                
  67.             }
  68.            
  69.             if(cars[msg.sender].voted_yes>cars[msg.sender].voted_no)//if majority wins, then compensate
  70.             {
  71.                 compensate(msg.sender);
  72.             }
  73.            
  74.         }
  75.        
  76.     }
  77.    
  78.     function vote(address _car, bool _vote) public //vote for target car.
  79.     {
  80.         if(cars[msg.sender].license!=0)//checking if the sender has right to vote - check if he is customer or not.
  81.         {
  82.             if(cars[_car].is_applied == true)//check if the target car has applied for the compensation
  83.             {
  84.                 if(_vote == true){// if vote for yes
  85.                     cars[_car].voted_yes++;
  86.                 }
  87.                 else{// if vote for no
  88.                     cars[_car].voted_no++;
  89.                 }
  90.             }
  91.            
  92.         }
  93.     }
  94.        
  95.    
  96.    
  97.     function compensate(address _payee) payable
  98.     {
  99.         if( cars[_payee].license!=0)
  100.         {
  101.                 _payee.send(compensation);
  102.                 reset_application(_payee);
  103.         }
  104.        
  105.     }
  106.    
  107.     function reset_application (address _payee) private //reset the number of votes on the application
  108.     {
  109.         cars[_payee].voted_yes=0;
  110.         cars[_payee].voted_no=0;
  111.         cars[_payee].is_applied = false;
  112.     }
  113.    
  114.     function pay(uint _amount) public payable
  115.     {
  116.         if( cars[msg.sender].license!=0)// check if the caller is in the list
  117.         {
  118.             if (!address(this).send(_amount)) throw; //pay x amount of the fee
  119.             cars[msg.sender].total_fee -= _amount;
  120.            
  121.         }
  122.            
  123.        
  124.        
  125.        
  126.     }
  127.    
  128.    
  129.     function set_fee(uint _fee) private
  130.     {
  131.         fee = _fee;
  132.     }
  133.     function set_compenstation (uint _compensation) private
  134.     {
  135.        compensation = _compensation;
  136.     }
  137.    
  138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top