• SIS Lab
  • >
  • Blog
  • >
  • TDDBootCamp名古屋の1日目の課題にリトライ!

TDDBootCamp名古屋の1日目の課題にリトライ!

更新日:2020.07.16 作成日:2010.07.13

set(),get(),dump()の実装。うーん、上手くかけない><..

package file;
import static org.junit.Assert.*;
import org.junit.Test;

public class FileStoreTest {
    @Test
    public void testdump() throws Exception {
        FileStore fs = new FileStore();
        fs.set("hoge","piyo");
        assertEquals("hoge:piyo", fs.dump());
        fs.set("foo", "bar");
        assertEquals("hoge:piyo,foo:bar", fs.dump());
        fs.set("bar", "baz");
        assertEquals("hoge:piyo,foo:bar,bar:baz", fs.dump());
    }
    
    @Test
    public void testget() throws Exception {
        FileStore fs = new FileStore();
        fs.set("hoge", "piyo");
        assertEquals("piyo", fs.get("hoge"));
        fs.set("foo", "bar");
        assertEquals("keyが空文字です。", fs.get(""));
    }
    
    @Test
    public void 空文字をkeyにしてsetする() throws Exception {
        FileStore fs = new FileStore();
        fs.set("hoge","piyo");
        fs.set("", "piyo");
        assertEquals("hoge:piyo", fs.dump());
    }
    
    @Test
    public void 空文字をsetする() throws Exception {
        FileStore fs = new FileStore();
        fs.set("hoge","");
        fs.set("hoge","piyo");
        assertEquals("hoge:piyo", fs.dump());
    }
    
    @Test
    public void すでに登録されているkeyがある場合上書きされて最後にdumpされる() throws Exception {
        FileStore fs = new FileStore();
        fs.set("hoge","piyo1");
        fs.set("hoge","piyo2");
        assertEquals("hoge:piyo2", fs.dump());
    }
}
package file;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


public class FileStore {
    List<Map<String,String>> lists = new LinkedList<Map<String,String>>();
    
    public FileStore() {
    }

    public String dump() {
        StringBuilder sb = new StringBuilder();
        for(Iterator<Map<String,String>> i = lists.iterator(); i.hasNext();){
            makeString(sb, i);
        }
        return sb.toString();
    }

    private void makeString(StringBuilder sb, Iterator<Map<String, String>> i) {
        Map<String,String> set =  i.next();
        String key ="";
        for(Iterator<String> j= set.keySet().iterator(); j.hasNext();){
            key = j.next();
        }
        sb.append(key);
        sb.append(":");
        sb.append(set.get(key));
        if(i.hasNext())
            sb.append(",");
    }

    public void set(String key, String value) {
        if(key.equals("")) return;
        if(value.equals("")) return;
        for(Iterator<Map<String,String>> i=lists.iterator();i.hasNext();){
            Map<String,String> set = i.next();
            if(set.containsKey(key)){
                i.remove();
            }
        }
        Map<String,String> map = new HashMap<String, String>();
        map.put(key, value);
        lists.add(map);
    }

    public String get(String key) {
        if(key.equals("")) return "keyが空文字です。";
        for(Iterator<Map<String,String>> i=lists.iterator();i.hasNext();){
            Map<String,String> set = i.next();
            if(set.containsKey(key)){
                return set.get(key);
            }
        }
        return null;
    }
}

Related contents