change test framework Test::Spec from Test::More
implement put_in money to the vending machine
Merge branch 'put_in_money'

* put_in_money:
  implement put_in money to the vending machine
temporary implement VendingMachine#products, and #buyables
[from now] 2012/07/13 20:38:28

diff --git a/lib/Sample/VendingMachine.pm b/lib/Sample/VendingMachine.pm
index 271acc6..0ebb069 100644
--- a/lib/Sample/VendingMachine.pm
+++ b/lib/Sample/VendingMachine.pm
@@ -40,6 +40,14 @@ sub back {
     $self->{total} = 0;
 }

+sub products {
+    return ('コーラ');
+}
+
+sub buyables {
+    return ();
+}
+
 1;
 __END__
create Slot class for manage juice in VendingMachine
[from now] 2012/07/13 21:31:42

diff --git a/lib/Sample/VendingMachine.pm b/lib/Sample/VendingMachine.pm
index 0ebb069..443ba0b 100644
--- a/lib/Sample/VendingMachine.pm
+++ b/lib/Sample/VendingMachine.pm
@@ -5,11 +5,18 @@ use feature 'switch';

 our $VERSION = '0.01';

+use Sample::Slot;
+
 sub new {
     my $class = shift;
     my $self = {
         total => 0,
         change => 0,
+        slots => [
+            Sample::Slot->new('コーラ', price => 120, stock => 5),
+            Sample::Slot->new('爽健美茶', price => 150, stock => 10),
+            Sample::Slot->new('六甲のおいしい水', price => 100, stock => 5),
+        ],
     };
     return bless $self, $class;
 }
@@ -41,11 +48,14 @@ sub back {
 }

 sub products {
-    return ('コーラ');
+    my $self = shift;
+    return map { $_->name } @{$self->{slots}};
 }

 sub buyables {
-    return ();
+    my $self = shift;
+    return map { $_->name }
+        grep { $_->buyable_by($self->total) } @{$self->{slots}};
 }

 1;
diff --git a/t/02_manage_juice.t b/t/02_manage_juice.t
index a0e06e8..c3f32d0 100644
--- a/t/02_manage_juice.t
+++ b/t/02_manage_juice.t
@@ -9,12 +9,19 @@ describe "自販機" => sub {
     context "何もしていない状態" => sub {
         it "格納されているジュースのリストが得られる" => sub {
             my @products = $vm->products;
-            is_deeply(\@products, ['コーラ']);
+            is_deeply(\@products, ['コーラ', '爽健美茶', '六甲のおいしい水']);
         };
         it "購入可能な商品はない" => sub {
             ok(not $vm->buyables);
         };
     };
+    context "100円を投入した状態" => sub {
+        before each => sub { $vm->put_in(100); };
+        it "六甲のおいしい水が購入可能"  => sub {
+            my @buyables = $vm->buyables;
+            is_deeply(\@buyables, ['六甲のおいしい水']);
+        };
+    };
 };

 runtests unless caller;
[from now] 2012/07/13 21:44:32

diff --git a/lib/Sample/VendingMachine.pm b/lib/Sample/VendingMachine.pm
index 443ba0b..55e00a4 100644
--- a/lib/Sample/VendingMachine.pm
+++ b/lib/Sample/VendingMachine.pm
@@ -12,6 +12,7 @@ sub new {
     my $self = {
         total => 0,
         change => 0,
+        sales => 0,
         slots => [
             Sample::Slot->new('コーラ', price => 120, stock => 5),
             Sample::Slot->new('爽健美茶', price => 150, stock => 10),
@@ -29,6 +30,10 @@ sub change {
     return $_[0]->{change};
 }

+sub sales {
+    return $_[0]->{sales};
+}
+
 sub put_in {
     my ($self, $money) = @_;
     given($money) {
@@ -58,6 +63,16 @@ sub buyables {
         grep { $_->buyable_by($self->total) } @{$self->{slots}};
 }

+sub buy {
+    my ($self, $name) = @_;
+    my ($slot) = grep { $_->name eq $name } @{$self->{slots}};
+    if (defined($slot) && $slot->buyable_by($self->total)) {
+        $slot->buy_by($self->total);
+        $self->{total} -= $slot->price;
+        $self->{sales} += $slot->price;
+    }
+}
+
 1;
 __END__

diff --git a/t/02_manage_juice.t b/t/02_manage_juice.t
index c3f32d0..c5d0e9b 100644
--- a/t/02_manage_juice.t
+++ b/t/02_manage_juice.t
@@ -21,6 +21,15 @@ describe "自販機" => sub {
             my @buyables = $vm->buyables;
             is_deeply(\@buyables, ['六甲のおいしい水']);
         };
+        context "購入ボタンを押した"  => sub {
+            before each => sub { $vm->buy('六甲のおいしい水'); };
+            it "合計額はゼロに" => sub {
+                is($vm->total, 0);
+            };
+            it "売上が100円" => sub {
+                is($vm->sales, 100);
+            };
+        };
     };
 };
[from now] 2012/07/13 21:47:14

diff --git a/t/02_manage_juice.t b/t/02_manage_juice.t
index c5d0e9b..b9b15d4 100644
--- a/t/02_manage_juice.t
+++ b/t/02_manage_juice.t
@@ -30,6 +30,13 @@ describe "自販機" => sub {
                 is($vm->sales, 100);
             };
         };
+        context "買えない商品のボタンを押した" => sub {
+            before each => sub { $vm->buy('コーラ'); };
+            it "何も起こらない" => sub {
+                is($vm->total, 100);
+                is($vm->sales, 0);
+            };
+        };
     };
 };
[from now] 2012/07/13 21:54:27

diff --git a/lib/Sample/VendingMachine.pm b/lib/Sample/VendingMachine.pm
index 55e00a4..20ee6af 100644
--- a/lib/Sample/VendingMachine.pm
+++ b/lib/Sample/VendingMachine.pm
@@ -70,6 +70,7 @@ sub buy {
         $slot->buy_by($self->total);
         $self->{total} -= $slot->price;
         $self->{sales} += $slot->price;
+        $self->back unless $self->buyables;
     }
 }

diff --git a/t/02_manage_juice.t b/t/02_manage_juice.t
index b9b15d4..5d89cf0 100644
--- a/t/02_manage_juice.t
+++ b/t/02_manage_juice.t
@@ -38,6 +38,30 @@ describe "自販機" => sub {
             };
         };
     };
+    context "150円を投入した状態" => sub {
+        before each => sub { $vm->put_in(100); $vm->put_in(50); };
+        it "全ての商品が買える" => sub {
+            my @buyables = $vm->buyables;
+            is_deeply(\@buyables, ['コーラ', '爽健美茶', '六甲のおいしい水']);
+        };
+        context "コーラ(120円)を買った" => sub {
+            before each => sub { $vm->buy('コーラ'); };
+            it "釣り銭30円が勝手に戻る" => sub {
+                is($vm->total, 0);
+                is($vm->change, 30);
+            };
+        };
+    };
+    context "500円を投入した状態" => sub {
+        before each => sub { $vm->put_in(500) };
+        context "コーラ(120円)を買った" => sub {
+            before each => sub { $vm->buy('コーラ'); };
+            it "380円がまだ合計額として残る" => sub {
+                is($vm->total, 380);
+                is($vm->change, 0);
+            };
+        };
+    };
 };

 runtests unless caller;
implement purchase feature

- buy juice
- display buyable juice
- back changes
Merge branch 'manage_juice'

* manage_juice:
  implement purchase feature
  [from now] 2012/07/13 21:54:27
  [from now] 2012/07/13 21:47:14
  [from now] 2012/07/13 21:44:32
  [from now] 2012/07/13 21:31:42
  create Slot class for manage juice in VendingMachine
  temporary implement VendingMachine#products, and #buyables [from now] 2012/07/13 20:38:28