2008-12-14 ================================================================================ ■例題 レストランの支払い つぎの対応を行う。 ・消費税を加えた定価を返す。 ・定価や個数を間違えたら、それ以降の処理は行わない。 □amount.php --- add($r_detail); $order->add($c_detail); // 支払額を算出し、表示する print "支払額: {$order->getAmount()}
"; } catch (Exception $e) { print $e->getMessage() . "
"; print $e->getTraceAsString() . "
"; } print "

商品数: " . Product::getCount() . "

"; print "

1.2 消費税率、例外対応

"; ?> --- □Order.class.php --- details as $detail) { $totalAmount += $detail->getAmount(); } return $totalAmount; } /** * 指定された明細をこの伝票に追加します。 * @param $detail 追加する明細 */ public function add($detail) { $this->details[] = $detail; } } ?> --- □Detail.class.php --- setProduct($product); $this->setQuantity($quantity); } /** * 商品の定価に個数を乗じた金額を取得します。 * @return 金額 */ public function getAmount() { return $this->quantity * $this->product->getPrice(); } /** * 商品を設定します。 * @param $product 商品 */ public function setProduct($product) { $this->product = $product; } /** * 個数を設定します。 * @exception 個数が正しくない * @param $quantity 個数 */ public function setQuantity($quantity) { if (!is_numeric($quantity) || $quantity < 0) { throw new Exception("個数が正しくありません。({$quantity})"); } $this->quantity = $quantity; } } ?> --- □Product.class.php --- setPrice($price); self::$count++; } catch (Exception $e) { throw $e; } } public static function getCount() { return self::$count; } /** * 消費税を加えた定価を取得します。 * @return 消費税を加えた定価。端数は切捨て。 */ public function getPrice() { return (int) ($this->price * (self::TAX + self::SCALE) / self::SCALE); } /** * この商品の税を加えない定価を設定します。 * @exception 定価が正しくない * @param $price 税抜きの定価 */ public function setPrice($price) { if (!is_numeric($price) || $price < 0) { throw new Exception("定価が正しくありません。({$price})"); } $this->price = $price; } } ?> ---