Java Coder
run php code online
1 Answer(s)      4 years and 11 months ago
Posted in : PHP
Is it possible to run PHP Code online?
Advertisement
View Answers

November 28, 2011 at 5:28 PM


class createProjectFiles { private $createMinParts; private $createMaxParts; private $uniqueCarsCount; private $manufacturers; private $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private $parts = array(); private $vehicles = array(); private $vehicleNumbers = array(); private $uniquePartNumbers = array(); private $uniqueCars = array(); private $partTransactions;

public function __construct()
{
    $this->createMinParts = 500;
    $this->createMaxParts = 5000;
    $this->uniqueCarsCount = 50;    
    $this->partTransactions = 10; 

    $this->manufacturers = array
    (
        'Toyota',
        'General Motors',
        'Volkswagen',
        'Hyundai',
        'Ford',
        'Nissan',
        'Honda',
        'PSA',
        'Suzuki',
        'Renault',
        'Fiat',
        'Darnier AG',
        'Chrysler',
        'BMW',
        'Mazda',
        'Mitsubishi',
        'Tata',
        'FAW',
        'Geely',
        'Cherry',
        'Fuji'
    );
}

public function setMinParts( $min )
{
    $this->createMinParts = $min;
}

public function setMaxParts( $max )
{
    $this->createMaxParts = $max;
}

public function setMaxCarCount( $count )
{
    $this->uniqueCarsCount = $count;
}

public function createPartTransactions( $partTransactions )
{
    $this->partTransactions = $partTransactions; 
}

public function generateFiles()
{
    $this->generateStock();
    $this->addSubstitutePartsToStock();
    $this->writeStockFile();
    $this->writeVehicleFile();

    sort( $this->uniquePartNumbers );
    sort( $this->uniqueCars );
    sort( $this->vehicleNumbers );
    sort( $this->vehicles );

    $this->writeTransFile();

}

private function generateVehicleNumber()
{
    if( $this->uniqueCarsCount > 0 )
    {
        // create random car
        $vehicleNumber = rand( 10000 , 99999 );
        $this->uniqueCars[] = $vehicleNumber;
        $this->uniqueCarsCount--;
        return sprintf( "%05s" , $vehicleNumber );
    }
    else
    {
        // pick random existing car
        $vehicleNumber = $this->uniqueCars[ rand( 0 , count( $this->uniqueCars ) -1 ) ];
        return sprintf( "%05s" , $vehicleNumber );
    }
}

private function generatePartNumber()
{
    $randomLetter1 = $this->chars[ rand( 0 , 25 ) ];
    $randomLetter2 = $this->chars[ rand( 0 , 25 ) ];
    $partNumber = rand( 1 , 99999 );

    return $randomLetter1 . $randomLetter2 . sprintf( "%05s" , $partNumber );
}


private function getExistingPartNumber()
{
    $randError = rand( 0 , 5 );

    if( $randError == 3 )
    {
        //bogus part
        return "AA00000";
    }
    else
    {
        return $this->uniquePartNumbers[ rand( 0 , count( $this->uniquePartNumbers ) - 1 ) ]; 
    }   
}

private function getExistingVehicleNumber()
{
    $randError = rand( 0 , 5 );

    if( $randError == 3 )
    {
        //bogus car
        return "00000";
    }
    else
    {
        $vehicleNumber = $this->uniqueCars[ rand( 0 , count( $this->uniqueCars ) -1 ) ];
        return sprintf( "%05s" , $vehicleNumber );
    }   
}

private function generateStock()
{
    for( $i = 0; $i < rand( $this->createMinParts , $this->createMaxParts ); $i++ )
    {
        $partNumber = $this->generatePartNumber();
        $vehicleNumber = $this->generateVehicleNumber();

        $description = "--------dont care--------";
        $quatity = rand( 0 , 99999 );
        $price = rand( 10 , 999999 );
        $reorderL = rand( 0 , 999 );
        $reorderQ = rand( 0 , 99999 );
        $numparts = rand( 0 , 10 );

        if( in_array( $partNumber , $this->uniquePartNumbers ) )
        {
            continue;
        }
        else
        {
            $part = array
            (
                "partNumber" => $partNumber,
                "vehicleNumber" => $vehicleNumber,
                "description" => $description,
                "quatity" => sprintf( "%05s" , $quatity ),
                "price" => sprintf( "%06s" , $price ),
                "reorderL" => sprintf( "%03s" , $reorderL ),
                "reorderQ" => sprintf( "%05s" , $reorderQ ),
                "numSubparts" => sprintf( "%02s" , $numparts ),
                "part1" => '',
                "part2" => '',
                "part3" => '',
                "part4" => '',
                "part5" => '',
                "part6" => '',
                "part7" => '',
                "part8" => '',
                "part9" => '',
                "part10" => ''
            );

            $this->parts[] = $part;

            if( !in_array( $vehicleNumber , $this->vehicleNumbers ) )
            {
                $this->vehicleNumbers[] = $vehicleNumber;
                $this->vehicles[ $vehicleNumber ] = array( $vehicleNumber , "-- vehicle description --" , 
                    sprintf( "%20s" , $this->manufacturers[ rand ( 0 , count( $this->manufacturers ) - 1 ) ] ) );
            }

            $this->uniquePartNumbers[] = $partNumber;
        }
    }
}

private function addSubstitutePartsToStock()
{
    // add substitute parts to the parts from the parts

    for( $t=0; $t < count( $this->parts ); $t++ )
    {
        $thepart = $this->parts[ $t ];
        $numofsubparts = $thepart[ 'numSubparts' ];

        $ignoreDuplicates = array();

        for( $y =0; $y < $numofsubparts; $y++ )
        {
            $rand = rand( 0 , count( $this->parts ) -1 );

            if( in_array( $this->parts[ $rand ][ 'partNumber' ], $ignoreDuplicates ) )
            {
                $y = $y -1;
                continue;
            }
            else
            {
                $index = "part" . ( $y + 1 );
                $this->parts[ $t ][ $index ] = $this->parts[ $rand ][ 'partNumber' ];
                $ignoreDuplicates[] = $this->parts[ $rand ][ "partNumber" ];
            }
        }
    }
}


private function writeStockFile()
{
    // bang out the stock master file

    $fp = fopen( "INAUTO-SMF.DAT" , 'w' );

    if( $fp )
    {
        foreach( $this->parts as $part )
        {
            fwrite( $fp , sprintf( "%-128s" , $part[ 'partNumber' ] . 
                    $part[ 'vehicleNumber' ] . 
                    $part[ 'description' ] . 
                    $part[ 'quatity' ] . 
                    $part[ 'price' ] . 
                    $part[ 'reorderL' ] . 
                    $part[ 'reorderQ' ] . 
                    $part[ 'numSubparts' ] . 
                    $part[ 'part1' ] . 
                    $part[ 'part2' ] . 
                    $part[ 'part3' ] . 
                    $part[ 'part4' ] . 
                    $part[ 'part5' ] . 
                    $part[ 'part6' ] . 
                    $part[ 'part7' ] . 
                    $part[ 'part8' ] . 
                    $part[ 'part9' ] . 
                    $part[ 'part10' ] ) . "\r\n" );
        }

        fclose( $fp );
    }
}


private function writeVehicleFile()
{
    $fp = fopen( "INAUTO-VEHICLE.DAT" , 'w' );

    if( $fp )
    {
        ksort( $this->vehicles );
        foreach( $this->vehicles as $vehicle )
        {
            fwrite( $fp , sprintf( "%-49s" , $vehicle[ 0 ] . $vehicle[ 1 ] . $vehicle[ 2 ] ) . "\r\n" );
        }
        fclose( $fp );
    }
}


private function writeTransFile()
{
    $fp = fopen( "INAUTO-TRANS.DAT" , 'w' );

    if( $fp )
    {
        $partNumberTransactions = $this->createOrderedPartNumberlist();

        for( $partTrans = 0; $partTrans < count( $partNumberTransactions ); $partTrans++ )
        {
            $i = 0;
            $records = array();
            $p = rand( 1 , 5 );

            while( $i < $p )
            {
                $type = rand( 1 , 5 );

                switch( $type )
                {
                    case 1:
                        $records[ "$type" ] = $this->createInsertion( $partNumberTransactions[ $partTrans ] );
                    break;
                    case 2:
                        $records[ "$type" ] = $this->createDeletion( $partNumberTransactions[ $partTrans ] );
                    break;
                    case 3:
                        $records[ "$type" ] = $this->createQuantity( $partNumberTransactions[ $partTrans ] );
                    break;
                    case 4:
                        $records[ "$type" ] = $this->createAddSub( $partNumberTransactions[ $partTrans ] );

                    break;
                    case 5:
                        $records[ "$type" ] = $this->createRemoveSub( $partNumberTransactions[ $partTrans ] );
                    break;
                }

                $i++;
            }

            ksort( $records );

            foreach( $records as $key => $val )
            {
                fwrite( $fp , sprintf( "%-129s" , $val ) . "\r\n" );
            }
        }
        fclose( $fp );
    }
}

private function createOrderedPartNumberlist()
{
    $list = array();

    for( $i = 0; $i < $this->partTransactions; $i++ )
    {
        if( $i % 5 > 0 )
        {
            // choose existing part number
            $list[] = $this->uniquePartNumbers[ rand( 0 , count( $this->uniquePartNumbers ) - 1 ) ]; 
        }
        else
        {
            $list[] = $this->generatePartNumber();
        }
    }

    sort( $list );

    return $list;
}

private function createDeletion( $part )
{
    return "2" . $part;
}

private function createQuantity( $part )
{
    return "3" . $part . sprintf( "%05s" , rand( 0 , 99999 ) );
}

private function createAddSub( $part )
{
    return "4" . $part . $this->getExistingPartNumber();
}

private function createRemoveSub( $part )
{
    return "5" . $part . $this->getExistingPartNumber();
}

private function createInsertion( $part )
{
    $partNumber = $part;
    $vehicleNumber = $this->generateVehicleNumber();

    $description = "--------dont care--------";
    $quatity = rand( 0 , 99999 );
    $price = rand( 10 , 999999 );
    $reorderL = rand( 0 , 999 );
    $reorderQ = rand( 0 , 99999 );
    $numparts = rand( 0 , 10 );

    $part = array
    (
        "partNumber" => $partNumber,
        "vehicleNumber" => $vehicleNumber,
        "description" => $description,
        "quatity" => sprintf( "%05s" , $quatity ),
        "price" => sprintf( "%06s" , $price ),
        "reorderL" => sprintf( "%03s" , $reorderL ),
        "reorderQ" => sprintf( "%05s" , $reorderQ ),
        "numSubparts" => sprintf( "%02s" , $numparts ),
        "part1" => '',
        "part2" => '',
        "part3" => '',
        "part4" => '',
        "part5" => '',
        "part6" => '',
        "part7" => '',
        "part8" => '',
        "part9" => '',
        "part10" => ''
    );

    $ignoreDuplicates = array();

    for( $y =0; $y < $numparts; $y++ )
    {
        $randSubPart = $this->getExistingPartNumber();

        if( in_array( $randSubPart , $ignoreDuplicates ) )
        {
            $y = $y -1;
            continue;
        }
        else
        {
            $index = "part" . ( $y + 1 );
            $part[ $index ] = $randSubPart;
            $ignoreDuplicates[] = $randSubPart;
        }
    }

    return  "1" . 
            $part[ 'partNumber' ] . 
            $part[ 'vehicleNumber' ] . 
            $part[ 'description' ] . 
            $part[ 'quatity' ] . 
            $part[ 'price' ] . 
            $part[ 'reorderL' ] . 
            $part[ 'reorderQ' ] . 
            $part[ 'numSubparts' ] . 
            $part[ 'part1' ] . 
            $part[ 'part2' ] . 
            $part[ 'part3' ] . 
            $part[ 'part4' ] . 
            $part[ 'part5' ] . 
            $part[ 'part6' ] . 
            $part[ 'part7' ] . 
            $part[ 'part8' ] . 
            $part[ 'part9' ] . 
            $part[ 'part10' ];
}

}

$creator = new createProjectFiles(); $creator->setMinParts( 500 ); // minimum amount of parts $creator->setMaxParts( 5000 ); // maximum amount of parts $creator->setMaxCarCount( 50 ); // after X cars, no new one will be gnerated and the old car numbers reused $creator->createPartTransactions( 120 ); // number of parts, may contain up to 3 transations per part $creator->generateFiles();










Related Tutorials/Questions & Answers:
run php code online
run php code online  Is it possible to run PHP Code online
PHP find online users
PHP find online users  How to find the online users in PHP
Advertisements
PHP Auto schedule and run php scripts
PHP Auto schedule and run php scripts  Hi... This is vinay.. I am trying to schedule the execution of php scripts and run them... But I dont want... that php which automatically runs the scheduled work.. But I am not getting how
Run a simple EJB code
Run a simple EJB code  I found the code this. However, as I have no idea with EJB, I can't understand how to run it. Can anybody help me by giving steps (by giving snapshots or writing full procedure)how to run it (any platform
Run a simple EJB code
Run a simple EJB code  I found the code this. However, as I have no idea with EJB, I can't understand how to run it. Can anybody help me by giving steps (by giving snapshots or writing full procedure)how to run it (any platform
ONLINE EXAM CODE SPLIT
ONLINE EXAM CODE SPLIT  hi.. im developing online exam for c programming in jsp.. i read the question from database as a string suppose String ques="#include<stdio.h>main(){int i;for(i=0;<100;i++){printf("hai
PHP Training online in India
PHP Training online in India We are providing PHP Training online in India using desktop sharing softwares. PHP Training online is very effective way to learn PHP from our experts. You can take the PHP Training online from your home
Failed to run Online Quiz Application in JSP - JSP-Interview Questions
Failed to run Online Quiz Application in JSP   Hi, I have been following your following link to run the online quiz application, but I failed... your question. Thanks  Hi, I cannot run your online quiz
secure online payment code for jsp
secure online payment code for jsp   how to implements online payment in jsp
online shopping code using jsp
online shopping code using jsp  plz send me the code of online shopping using jsp or jdbc or servlets plz plz help me
calendar in php code - PHP
calendar in php code   Any Idea to create a calendar for school in PHP? I need a simple nd handy code
Pagination in PHP code - PHP
Pagination in PHP code  3.Pagination in PHP codeIs it possible to get the paging in PHP without database call? How will I show the image instead of numbering? any code that can help
How to run PHP Script from the command line ?
How to run PHP Script from the command line ?  Running PHP Script from the command line   Actually it is very simple to run php script... expect. You can also check : http://www.phpzag.com/running-php-script-from
Run this code..plzz - Java Beginners
Run this code..plzz  Hi Friend... ERROR: Exception in thread "main...;Hi friend, I am sending code according to your requirement but you are defined in this example what you want to do please explain and send me full code
Online Java Compiler
testing your code online. The best thing about online compilers...Online Java Compiler provides the Java development environment online, which allows you to write and test Java code from anywhere. All you need
online shopping project report with source code in java
online shopping project report with source code in java  Dear Sir/Mam, i want to a project in java with source code and report project name online shopping. thank you
online voting system source code in java
online voting system source code in java  Please send me source code for online voting system in java. please replay as fast as. Thank you
Java code to run unix command - Java Beginners
Java code to run unix command  Hi, I need a java code to connect to a remote unix server and run a unix command. Please help. Regards, Pratyush
to run html code in web browser - JSP-Servlet
to run html code in web browser  how to run jsp file which calls html file on web browser??  Hi Friend, Try the following code: 1)form.jsp: Enter Name: 2)welcome.html: Add Profile Edit
php download file code
php download file code  PHP code to download the files from the remote server
user authentication code in php - PHP
user authentication code in php  Hi, Can you please send me a user authentication code in php? or just post the guidelines to authenticate a user. Thanks
email search code in php
email search code in php  email search with multiple option in php
php login and logout code
php login and logout code  Hi, Can anyone share there code for creating a user login and logout page in PHP?or any useful tutorial that can help to create a login and logout application in PHP.. Thanks in Advance
cache php code
cache php code  Basically, i'm looking for a code to cache the content of webpage in server side
Applet run with appletviewer but not in browser, any code problem.
Applet run with appletviewer but not in browser, any code problem.  ... fine but not run from the browser as http://localhost:8080/ProgressoApplication... the 52 cards will be show in browser as by "appletviewer" and run as "java applet" I
Mysql-Php code
Mysql-Php code  I want to mysql script for query data from two tables in the mysql database trough php code. Can you help me   Hi Friend, Please visit the following link: Php Mysql Thanks   Hi Friend
java code to insert select at run time....????
java code to insert select at run time....????  java database code to retrieve data at runtime and please give codes for insert delete too.... using jsp and withot jsp   1)application.jsp: <%@ page import="java.sql.
Help on this java code for online library search
Help on this java code for online library search   I have written the following java code for simple library book search. but its having some errors ... please help me on this code. import java.sql.*; import java.awt.
can i know the error in this code... am unable to run this code
can i know the error in this code... am unable to run this code  ... code like < >. The for is improper and not completed. Moreover, you have open and close braces properly. Anyways, we have modified your code. Here
code for timer in java netbeans for online test series countdown timer
code for timer in java netbeans for online test series countdown timer  code for timer in java netbeans for online test series countdown timer
CAN U HELP ME TO CODE IN JSP FOR ONLINE VOTING SYSTEM
CAN U HELP ME TO CODE IN JSP FOR ONLINE VOTING SYSTEM  can u help me to code in jsp for online voting system