Skip to content

Instantly share code, notes, and snippets.

Created October 25, 2011 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1310933 to your computer and use it in GitHub Desktop.
Save anonymous/1310933 to your computer and use it in GitHub Desktop.
TDDBC Nagano 0.1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace autovendor
{
public class autovendor
{
//商品の初期化
public autovendor()
{
this.init();
}
class drink //商品のインナークラス
{
int id;
string drinkname;
int stock;
int price;
public drink(int ID, string drinkname, int stock, int price){
this.id = ID;
this.drinkname = drinkname;
this.stock = stock;
this.price = price;
}
public int getid(){
return this.id;
}
public int getstock()
{
return this.stock;
}
public void setstock(int stockvol)
{
this.stock = stockvol;
}
public int getprice()
{
return this.price;
}
}
drink[] item = new drink[5];
//商品の初期化
public void init()
{
drink cola = new drink(1, "コーラ", 5, 120);
item[0] = cola;
}
//入金金額
int totalcash = 0;
//売上金額
int salesaccount = 0;
//お金投入
public int insertmoney(int num)
{
//1円単位は例外
if ((num % 10) != 0)
{
throw new Exception();
}
//通貨単位以外の入力は例外
int[] usejpy= new int[]{10, 50, 100, 500, 1000};
if (Array.IndexOf(usejpy, num) < 0 )
{
throw new Exception();
}
//合計金額を返す
this.totalcash += num;
return totalcash;
}
//商品在庫を確認
public int getstock(int id)
{
return item[id-1].getstock();
}
//購入可能な商品ID配列を返す
public int[] displaybuyable()
{
int[] result = new int[5];
int j=0;
for (int i = 0; i < this.item.Length; i++)
{
if ((this.item[i].getprice() <= this.totalcash) && (this.item[i].getstock() > 0)){
result[j]= this.item[i].getid();
j++;
}
}
return result;
}
//指定ID商品を購入する
public bool buy(int id)
{
if ((this.totalcash > item[id-1].getprice()) && (item[id-1].getstock() > 0)){
this.totalcash = this.totalcash - item[id - 1].getprice();
this.item[id - 1].setstock(item[id - 1].getstock() - 1);
this.salesaccount += this.item[id - 1].getprice();
return true;
} else {
return false;
}
}
//総売上を返す
public int getsalesaccount()
{
return this.salesaccount;
}
//自販機内のコイン枚数を返す
public int stockedcoin(int num)
{
return 0;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using autovendor;
namespace autovendortest
{
[TestFixture]
public class autovendortest
{
[Test]
public void _50円投入()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(50, av.insertmoney(50));
}
[Test]
public void _100円投入()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(100, av.insertmoney(100));
}
[Test]
public void _1000円投入()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(1000, av.insertmoney(1000));
}
[ExpectedException(typeof(Exception))]
[Test]
public void _600円投入()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(600, av.insertmoney(600));
}
[ExpectedException(typeof(Exception))]
[Test]
public void _5円投入()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(5, av.insertmoney(5));
}
[ExpectedException(typeof(Exception))]
[Test]
public void _10000円投入()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(10000, av.insertmoney(10000));
}
[Test]
public void _100円入れて次に50円投入()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(100, av.insertmoney(100));
Assert.AreEqual(150, av.insertmoney(50));
}
[Test]
public void _コーラ本数確認()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(5, av.getstock(1));
}
[Test]
public void _100円入れたらコーラ買えない()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(100, av.insertmoney(100));
Assert.AreEqual(false, av.buy(1));
}
[Test]
public void _150円入れたらコーラ買える()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(100, av.insertmoney(100));
Assert.AreEqual(150, av.insertmoney(50));
Assert.AreEqual(true, av.buy(1));
}
/*
[Test]
public void _購入可能商品を表示100円投入ない()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(100, av.insertmoney(100));
Assert.AreEqual(null, av.displaybuyable());
}
[Test]
public void _購入可能商品を表示150円投入ある()
{
int[] test= new int[0];
test[0]=1;
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(100, av.insertmoney(100));
Assert.AreEqual(150, av.insertmoney(50));
Assert.AreEqual(test, av.displaybuyable());
}
*/
[Test]
public void _コーラ商品1つ購入後在庫が1つ減る()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(100, av.insertmoney(100));
Assert.AreEqual(200, av.insertmoney(100));
Assert.AreEqual(5, av.getstock(1));
Assert.AreEqual(true, av.buy(1));
Assert.AreEqual(4, av.getstock(1));
}
[Test]
public void _コーラ商品1つ購入後在庫が1つ減り売上金額が120円増加()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(100, av.insertmoney(100));
Assert.AreEqual(200, av.insertmoney(100));
Assert.AreEqual(5, av.getstock(1));
Assert.AreEqual(true, av.buy(1));
Assert.AreEqual(4, av.getstock(1));
Assert.AreEqual(120, av.getsalesaccount());
}
/*
[Test]
public void _自販機内の紙幣・硬貨の内訳を確認()
{
autovendor.autovendor av = new autovendor.autovendor();
Assert.AreEqual(10, av.stockedcoin(10));
Assert.AreEqual(10, av.stockedcoin(50));
Assert.AreEqual(10, av.stockedcoin(100));
Assert.AreEqual(10, av.stockedcoin(500));
Assert.AreEqual(5, av.stockedcoin(1000));
}
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment