読者です 読者をやめる 読者になる 読者になる

hayateasdf's blog

C#,javascript,pythonなどいろいろ。広く浅く

インターフェイス 練習

test.csv

cola, 10, COOL
tea, 20, HOT
water, 30, COOL
main.cpp

// 自販機クラス
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
enum TEMPERATURE{
	DRINK_COLD,
	DRINK_HOT,
};

class IDrink
{
public:
	virtual TEMPERATURE GetType() = 0;
	virtual std::string GetName() = 0;
	virtual unsigned int GetMoney() = 0;
};

class Drink : public IDrink{
public:
	Drink(std::string name, unsigned int money, TEMPERATURE type)
	: name_(name)
	, money_(money)
	, type_(type){}
	TEMPERATURE GetType(){
		return type_;
	}
	std::string GetName(){
		return name_;
	}
	unsigned int GetMoney(){
		return money_;
	}
private:
	std::string name_;
	unsigned int money_;
	TEMPERATURE type_;
};
class Vending
{
public:
	Vending(std::vector<IDrink*> drink) : drink_(drink){}
	void Show(){
		for (size_t i = 0; i < drink_.size(); ++i)
		{
			printf("this is %s\n", drink_[i]->GetName().c_str());
			if(drink_[i]->GetType() == DRINK_HOT){
				printf("HOT DRINK!\n");
			}else if(drink_[i]->GetType() == DRINK_COLD){
				printf("COLD_DRINK!\n");
			}
			printf("Money is %dyen\n", drink_[i]->GetMoney());

			printf("\n");	
		}
	}
private:
	std::vector<IDrink*> drink_;
};
class ReadCSV{
public:
	ReadCSV(const char* file): file_(file)
	{
		if(file_.fail()){
			std::cout<<"failed."<<std::endl;
			return;
		}
		if(!Read()) return;
		Show();
	}
	bool Read(){
		std::string temp = "";
		int pos = 0;
		while(std::getline(file_, temp)){
			if((pos = temp.find("//") != temp.npos)) continue;
			std::vector<std::string> inner;

			while((pos = temp.find(",")) != temp.npos){
				inner.push_back(temp.substr(0, pos));
				// tempの中身は","の2文字を飛ばす
				temp = temp.substr(pos + 2);
			}
			inner.push_back(Trim(temp));
			values_.push_back(inner);
		}
		return true;
	}
	std::string Trim(std::string line){
		std::string temp;
		for(size_t i = 0; i < line.length(); ++i){
			if(line[i] != ' '){
				temp += line[i];
			}
		}
		return temp;
	}
	void Show(){
		for(size_t i = 0; i< values_.size(); ++i){
			for (size_t j = 0; j < values_[i].size(); ++j)
			{
				std::cout<<values_[i][j]<<" ";
			}
			std::cout<<std::endl;
		}
	}
	std::vector<std::vector<std::string> > GetData(){
		return values_;
	}
private:
	std::ifstream file_;
	std::vector<std::vector<std::string> > values_;
};
int main()
{
	ReadCSV read("test.csv");
	std::vector<std::vector<std::string> > data = read.GetData();

	std::vector<Drink> drink;
	for (size_t i = 0; i < data.size(); ++i){
		if(data[i].size() != 3) {
			printf("error %d", i);
			continue;
		}
		TEMPERATURE type = DRINK_COLD;
		if(data[i][2] == "HOT") {
			type = DRINK_HOT;
		}
		unsigned int money = atoi(data[i][1].c_str());
		std::string name = data[i][0];
		drink.push_back(Drink(name, money, type));
	}

	std::vector<IDrink*> pDrink(drink.size());
	for (size_t i = 0; i < drink.size(); ++i){
		pDrink[i] = &drink[i];
	}

	Vending v(pDrink);
	v.Show();

	return 0;
}

これは・・・

なんかすごいぐらいvector生成しまくってるw
これ絶対いろいろ間違ってる気がする。
メモ帳代わりに書き込んであるだけなので、もっと勉強してから直します。(たぶん)

http://jibun.atmarkit.co.jp/lskill01/rensai/imajava02/imajava01.html
この記事を読むのがめんどくさくなって適当に作りました。

http://scitex.hateblo.jp/entry/2013/02/23/231640
参考URL