Skip to content

Commit

Permalink
finish q4
Browse files Browse the repository at this point in the history
  • Loading branch information
tkjnz committed Jan 7, 2016
1 parent 7ba1816 commit fd13502
Showing 1 changed file with 122 additions and 1 deletion.
123 changes: 122 additions & 1 deletion question_2.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,66 @@ public function get_balance() {


// Write your code below
class Database {
protected $DB_HOST;
protected $DB_NAME;
protected $DB_USER;
protected $DB_PASS;
private $mysqli;

/**
* database constructor.
*/
public function __construct() {
$this->DB_HOST = 'localhost';
$this->DB_NAME = 'test';
$this->DB_USER = 'test';
$this->DB_PASS = 'test';
$this->mysqli = new \mysqli($this->DB_HOST, $this->DB_USER, $this->DB_PASS, $this->DB_NAME);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}

public function check_username($username){
$username = mysqli_real_escape_string($this->mysqli, $username);
$sql = 'SELECT first_name FROM customer WHERE username = "'.$username.'"';
$result = $this->mysqli->query($sql);
return ($result->num_rows > 0) ? $result : false;
}

/**
* Close database connection
* @return bool
*/
public function close(){
return mysqli_close($this->mysqli);
}


/**
* Generate Random string
* @param int $length maximum string generate
* @param int $min minimum string generate
* @return string
*/
public function randomString($length = 30, $min=8) {
$str = "";
$characters = array_merge(range('a','z'), range('0','9'));
$max = count($characters) - 1;
$randomLength = mt_rand($min, $length);
for ($i = 0; $i < $randomLength; $i++) {
$rand = mt_rand(0, $max);
$str .= $characters[$rand];
}
return $str;
}

}


/**
* Class Bronze_Customer
* @package SoftwareEngineerTest
Expand All @@ -35,7 +95,6 @@ public function deposit($amount){
$this->balance = $this->get_balance() + $amount;
}


/**
* Instantiate the correct object for customer ID
* @return bool
Expand All @@ -49,6 +108,26 @@ public function get_instance(){
throw new \InvalidArgumentException('Invalid customer ID');
}
}

/**
* Generate unique random username
* @return string
*/
public function generate_username(){
$flag = true;
$db = new Database();
$username = '';
while($flag){
$username = 'B'.$db->randomString();
$result = $db->check_username($username);
if (!$result){
//username is unique now
$flag = false;
}
}
$db->close();
return $username;
}
}

/**
Expand Down Expand Up @@ -78,6 +157,27 @@ public function get_instance(){
throw new \InvalidArgumentException('Invalid customer ID');
}
}

/**
* Generate unique random username
* @return string
*/
public function generate_username(){
$flag = true;
$db = new Database();
$username = '';
while($flag){
$username = 'S'.$db->randomString();
$result = $db->check_username($username);
if (!$result){
//username is unique now
$flag = false;
}
}
$db->close();
return $username;
}

}

/**
Expand Down Expand Up @@ -107,4 +207,25 @@ public function get_instance(){
throw new \InvalidArgumentException('Invalid customer ID');
}
}

/**
* Generate unique random username
* @return string
*/
public function generate_username(){
$flag = true;
$db = new Database();
$username = '';
while($flag){
$username = 'G'.$db->randomString();
$result = $db->check_username($username);
if (!$result){
//username is unique now
$flag = false;
}
}
$db->close();
return $username;
}

}

0 comments on commit fd13502

Please sign in to comment.