PHP项目数字缅甸缅元与eMMK

wen PHP项目 1

本文目录导读:

PHP项目数字缅甸缅元与eMMK

  1. 项目结构
  2. 主配置文件 (config.php)
  3. 货币基础类 (src/MMK.php)
  4. eMMK类 (src/eMMK.php)
  5. 货币转换器 (src/CurrencyConverter.php)
  6. 主入口文件 (index.php)
  7. 测试文件 (tests/CurrencyConverterTest.php)
  8. 使用方法
  9. 主要功能

我来帮您创建一个PHP项目,用于处理缅甸缅元(MMK)和电子缅甸缅元(eMMK)的数字转换和计算。

项目结构

mmk-emmk-converter/
├── index.php
├── src/
│   ├── CurrencyConverter.php
│   ├── MMK.php
│   └── eMMK.php
├── tests/
│   └── CurrencyConverterTest.php
└── config.php

主配置文件 (config.php)

<?php
// config.php
return [
    'mmk_to_emmk_rate' => 1, // 1 MMK = 1 eMMK (假设等值)
    'decimal_places' => 2,
    'currency_symbol' => 'Ks',
    'emmk_symbol' => 'eKs',
];

货币基础类 (src/MMK.php)

<?php
// src/MMK.php
namespace App;
class MMK {
    private float $amount;
    private int $decimalPlaces;
    public function __construct(float $amount = 0.0, int $decimalPlaces = 2) {
        $this->decimalPlaces = $decimalPlaces;
        $this->amount = round($amount, $decimalPlaces);
    }
    public function getAmount(): float {
        return $this->amount;
    }
    public function format(bool $includeSymbol = true): string {
        $formatted = number_format($this->amount, $this->decimalPlaces);
        return $includeSymbol ? "Ks {$formatted}" : $formatted;
    }
    public function add(MMK $other): MMK {
        return new MMK($this->amount + $other->amount);
    }
    public function subtract(MMK $other): MMK {
        return new MMK($this->amount - $other->amount);
    }
    public function multiply(float $multiplier): MMK {
        return new MMK($this->amount * $multiplier);
    }
    public function divide(float $divisor): MMK {
        if ($divisor == 0) {
            throw new \InvalidArgumentException("Division by zero");
        }
        return new MMK($this->amount / $divisor);
    }
    public function compareTo(MMK $other): int {
        if ($this->amount > $other->amount) return 1;
        if ($this->amount < $other->amount) return -1;
        return 0;
    }
    public function __toString(): string {
        return $this->format();
    }
}

eMMK类 (src/eMMK.php)

<?php
// src/eMMK.php
namespace App;
class eMMK {
    private float $amount;
    private int $decimalPlaces;
    private string $walletAddress;
    public function __construct(float $amount = 0.0, string $walletAddress = '', int $decimalPlaces = 2) {
        $this->decimalPlaces = $decimalPlaces;
        $this->amount = round($amount, $decimalPlaces);
        $this->walletAddress = $walletAddress;
    }
    public function getAmount(): float {
        return $this->amount;
    }
    public function getWalletAddress(): string {
        return $this->walletAddress;
    }
    public function format(bool $includeSymbol = true): string {
        $formatted = number_format($this->amount, $this->decimalPlaces);
        $walletInfo = $this->walletAddress ? " [{$this->walletAddress}]" : '';
        return $includeSymbol ? "eKs {$formatted}{$walletInfo}" : $formatted;
    }
    public function transfer(eMMK $recipient, float $amount): bool {
        if ($this->amount < $amount) {
            throw new \RuntimeException("Insufficient eMMK balance");
        }
        $this->amount -= $amount;
        $recipient->amount += $amount;
        return true;
    }
    public function verifyTransaction(string $txHash): bool {
        // 模拟交易验证
        return !empty($txHash) && strlen($txHash) > 0;
    }
    public function __toString(): string {
        return $this->format();
    }
}

货币转换器 (src/CurrencyConverter.php)

<?php
// src/CurrencyConverter.php
namespace App;
class CurrencyConverter {
    private float $exchangeRate;
    private int $decimalPlaces;
    public function __construct(float $exchangeRate = 1.0, int $decimalPlaces = 2) {
        $this->exchangeRate = $exchangeRate;
        $this->decimalPlaces = $decimalPlaces;
    }
    public function mmkToEmmk(MMK $mmk): eMMK {
        $convertedAmount = $mmk->getAmount() * $this->exchangeRate;
        return new eMMK(round($convertedAmount, $this->decimalPlaces));
    }
    public function emmkToMmk(eMMK $emmk): MMK {
        if ($this->exchangeRate == 0) {
            throw new \InvalidArgumentException("Exchange rate cannot be zero");
        }
        $convertedAmount = $emmk->getAmount() / $this->exchangeRate;
        return new MMK(round($convertedAmount, $this->decimalPlaces));
    }
    public function batchConvert(array $amounts, string $direction): array {
        $results = [];
        foreach ($amounts as $amount) {
            if ($direction === 'mmk_to_emmk') {
                $mmk = new MMK($amount);
                $results[] = $this->mmkToEmmk($mmk);
            } elseif ($direction === 'emmk_to_mmk') {
                $emmk = new eMMK($amount);
                $results[] = $this->emmkToMmk($emmk);
            }
        }
        return $results;
    }
    public function updateExchangeRate(float $newRate): void {
        if ($newRate <= 0) {
            throw new \InvalidArgumentException("Exchange rate must be positive");
        }
        $this->exchangeRate = $newRate;
    }
}

主入口文件 (index.php)

<?php
// index.php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/src/MMK.php';
require_once __DIR__ . '/src/eMMK.php';
require_once __DIR__ . '/src/CurrencyConverter.php';
use App\MMK;
use App\eMMK;
use App\CurrencyConverter;
// 初始化
$config = require 'config.php';
$converter = new CurrencyConverter($config['mmk_to_emmk_rate']);
// 测试功能
echo "<h1>缅甸货币转换系统</h1>";
// 1. MMK 操作
echo "<h2>MMK 操作</h2>";
$amount1 = new MMK(1000.50);
$amount2 = new MMK(500.25);
echo "金额1: " . $amount1->format() . "<br>";
echo "金额2: " . $amount2->format() . "<br>";
echo "相加: " . $amount1->add($amount2)->format() . "<br>";
echo "相减: " . $amount1->subtract($amount2)->format() . "<br>";
echo "乘以2: " . $amount1->multiply(2)->format() . "<br>";
echo "除以2: " . $amount1->divide(2)->format() . "<br>";
// 2. eMMK 操作
echo "<h2>eMMK 操作</h2>";
$wallet1 = new eMMK(5000.00, '0x123abc...');
$wallet2 = new eMMK(2000.00, '0x456def...');
echo "钱包1: " . $wallet1->format() . "<br>";
echo "钱包2: " . $wallet2->format() . "<br>";
// 转账
try {
    $wallet1->transfer($wallet2, 1000.00);
    echo "转账后钱包1: " . $wallet1->format() . "<br>";
    echo "转账后钱包2: " . $wallet2->format() . "<br>";
} catch (\RuntimeException $e) {
    echo "转账失败: " . $e->getMessage() . "<br>";
}
// 3. 货币转换
echo "<h2>货币转换</h2>";
$mmkAmount = new MMK(10000);
$emmkAmount = $converter->mmkToEmmk($mmkAmount);
echo "MMK 10000 = " . $emmkAmount->format() . "<br>";
$emmkInput = new eMMK(5000);
$mmkResult = $converter->emmkToMmk($emmkInput);
echo "eMMK 5000 = " . $mmkResult->format() . "<br>";
// 4. 批量转换
echo "<h2>批量转换</h2>";
$amounts = [1000, 2000, 5000, 10000];
$convertedAmounts = $converter->batchConvert($amounts, 'mmk_to_emmk');
echo "批量转换结果:<br>";
foreach ($convertedAmounts as $result) {
    echo $result->format() . "<br>";
}
// 5. 比较操作
echo "<h2>比较操作</h2>";
$a = new MMK(1000);
$b = new MMK(2000);
$c = new MMK(1000);
echo "1000 与 2000 比较: " . $a->compareTo($b) . "<br>";
echo "1000 与 1000 比较: " . $a->compareTo($c) . "<br>";
echo "2000 与 1000 比较: " . $b->compareTo($a) . "<br>";

测试文件 (tests/CurrencyConverterTest.php)

<?php
// tests/CurrencyConverterTest.php
namespace Tests;
use PHPUnit\Framework\TestCase;
use App\MMK;
use App\eMMK;
use App\CurrencyConverter;
class CurrencyConverterTest extends TestCase {
    private CurrencyConverter $converter;
    protected function setUp(): void {
        $this->converter = new CurrencyConverter(1.0); // 1:1 汇率
    }
    public function testMmkToEmmk() {
        $mmk = new MMK(1000);
        $emmk = $this->converter->mmkToEmmk($mmk);
        $this->assertEquals(1000.00, $emmk->getAmount());
    }
    public function testEmmkToMmk() {
        $emmk = new eMMK(500);
        $mmk = $this->converter->emmkToMmk($emmk);
        $this->assertEquals(500.00, $mmk->getAmount());
    }
    public function testInvalidExchangeRate() {
        $this->expectException(\InvalidArgumentException::class);
        new CurrencyConverter(0);
    }
    public function testNegativeAmount() {
        $this->expectException(\InvalidArgumentException::class);
        new MMK(-100);
    }
}

使用方法

  1. 安装: 将文件复制到您的PHP项目目录
  2. 运行: 通过浏览器访问 index.php
  3. 测试: 使用 phpunit tests/CurrencyConverterTest.php 运行测试

主要功能

  • ✅ 支持MMK和eMMK的基本运算(加、减、乘、除)
  • ✅ 货币格式化和比较
  • ✅ eMMK钱包地址管理
  • ✅ 货币转换(MMK ↔ eMMK)
  • ✅ 批量转换功能
  • ✅ 错误处理和验证

这个项目提供了一个完整的缅甸货币处理系统,可以根据实际需求进一步扩展功能。

抱歉,评论功能暂时关闭!