mercredi 4 avril 2018

how to solve this While Loop Algorithem

i am using laravel 5.6 trying to make bitcoin trading webapp so basic is if in market has 1 bitcoin in $1 and i want to buy 2 at $1 so automatically buy 1 bitcoin rest 1 will post for buyer and remove 1 bitcoin from seller in market i am stuck on if market has 5 bitcoin at $1 and 5 bitcoin at $2 and buyer post for 15 bitcoin at $3 than buy 10 at $3 which is 5+5 $1 & $2 because buyer paying $3 and rest 5 bitcoin post for buying at market

here is my controller

$this->validate($request, [
        'bitcoin' => 'required|max:20',
        'price' => 'required|max:10',
    ]);

    $wallet = User::find(Auth::user()->id)->wallet;
    if ($wallet == null) {
        return back()->with('warning', 'Please Make Your Wallet First');
    }
    if ($wallet->amount_balance >= $request->price) {
        $wallet->amount_balance = $wallet->amount_balance - $request->price;
        $wallet->save();
        //Bitcoin Listing for User
        $listing = new BitcoinBuyerListing;
        $listing->bitcoin = $request->bitcoin;
        $listing->price = $request->price;
        $listing->user_id = Auth::user()->id;
        $listing->transaction_id = 'BUY' . strtoupper(str_random(10));
        $listing->save();

        $buyer = BitcoinBuyMarket::where('price', $request->price)->first();
        $seller = BitcoinSellMarket::where('price', '<=', $request->price)->first();


        if ($seller == !null) {
            if ($seller->bitcoin <= $request->bitcoin) {
                $marketplus = $request->bitcoin - $seller->bitcoin;

                if ($buyer == !null) {
                    $buyer->bitcoin = $buyer->bitcoin + $marketplus;
                    $buyer->save();
                }else{
                    if ($marketplus == !0) {
                        $newmarket = new BitcoinBuyMarket;
                        $newmarket->price = $request->price;
                        $newmarket->bitcoin = $marketplus;
                        $newmarket->save();
                    }

                }

                $seller->delete();

            }
            else{
                $seller->bitcoin = $seller->bitcoin - $request->bitcoin;

                if ($seller->bitcoin == 0) {
                    $seller->delete();
                }
                else{
                    $seller->save();
                }
            }


        }

        elseif ($market == !null) {
            $market->bitcoin = $market->bitcoin + $request->bitcoin;
            $market->save();
        } else {
            $newmarket = new BitcoinBuyMarket;
            $newmarket->price = $request->price;
            $newmarket->bitcoin = $request->bitcoin;
            $newmarket->save();
        }
        return back()->with('status', 'Your Listing Added on Market');
    }
    else{
        return back()->with('warning', 'Your Dont have enough balance');
    }

Buy Market

$table->increments('id');
$table->decimal('price',10,2);
$table->decimal('bitcoin', 20,8);
$table->timestamps();

Seller Market

$table->increments('id');
$table->decimal('price',10,2);
$table->decimal('bitcoin', 20,8);
$table->timestamps();

Buyer Listing

$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')
        ->onDelete('cascade')
        ->onUpdate('cascade');
$table->decimal('price',10,2);
$table->decimal('bitcoin', 20,8);
$table->string('transaction_id');
$table->timestamps();

Seller Listing

$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')
       ->onDelete('cascade')
       ->onUpdate('cascade');
$table->decimal('price',10,2);
$table->decimal('bitcoin', 20,8);
$table->string('transaction_id');
$table->timestamps();

While Loop will work but i can't understand how to start first



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire