amount_card.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. namespace mcard;
  4. class amount_card extends card
  5. {
  6. public function usable(): bool
  7. {
  8. $state = $this->state();
  9. if ($state === STATE_OVER) {
  10. return false;
  11. }
  12. elseif ($state === STATE_USING)
  13. {
  14. $left = intval($this->left_amount() * 100);
  15. if ($left <= 0) {
  16. return false;
  17. }
  18. else {
  19. return true;
  20. }
  21. }
  22. else
  23. {
  24. return true;
  25. }
  26. }
  27. public function deduct(int $cent)
  28. {
  29. $left_cent = intval($this->left_amount() * 100 + 0.5);
  30. if ($left_cent > $cent)
  31. {
  32. $amount = $cent;
  33. $useup = false;
  34. }
  35. else
  36. {
  37. $amount = $left_cent;
  38. $useup = true;
  39. }
  40. return [$amount, $useup];
  41. }
  42. public function calc(int $cent)
  43. {
  44. $left_cent = intval($this->left_amount() * 100 + 0.5);
  45. if ($left_cent > $cent)
  46. {
  47. $amount = $cent;
  48. $useup = false;
  49. }
  50. else
  51. {
  52. $amount = $left_cent;
  53. $useup = true;
  54. }
  55. $price = round($amount * (1 - $this->discount()) / 100, 2);
  56. return [$amount, $useup,$price];
  57. }
  58. }