요구사항
요구사항 관리
[제너릭] :
[PairObject]
package com.java.class08;
public class PairObject {
Object key;
Object value;
//------------------------
public PairObject() {
this(null,null);//초기화를 시켜줬다.
}
public PairObject(Object key, Object value) {
this.key = key;
this.value = value;
}
//-------------------------
public Object getKey() {
return key;
}
public void setKey(Object key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
[Pair]
package com.java.class08;
public class Pair<K> {
K key;
Object value;
public Pair() {
this(null,null);
}
public Pair(K key, Object value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
[Animal]
package com.java.class08;
public class Animal {
private String kind;
private String sound;
public Animal(String kind, String sound) {
this.kind = kind;
this.sound = sound;
}
public void bark() {
System.out.println(kind+" "+sound);
}
}
[Dog]
package com.java.class08;
public class Dog extends Animal {
public Dog(String kind,String sound) {
super(kind,sound);
}
}
[GenericSimulator]
package com.java.class08;
import java.util.HashMap;
import java.util.Map;
public class GenericSimulator {
public static void main(String[] args) {
PairObject pair = new PairObject("key1","home");
String key = (String) pair.getKey();//캐스팅한다.부모의 모습을 하고있기 때문에 자식모습으로 하려면 항상 캐스팅 필요
String value = (String) pair.getValue();
Object objk = "key2";
System.out.println(key);
System.out.println(value);
System.out.println(objk);
PairObject pair2 = new PairObject(1,2);
PairObject pair3 = new PairObject(1.1,2.1);
System.out.println(pair2.getKey());
System.out.println(pair2.getValue());
System.out.println(pair3.getKey());
System.out.println(pair3.getValue());
double dbkey = (double)pair3.getKey();
double dbvalue = (double)pair3.getValue();
System.out.println(dbkey);
System.out.println(dbvalue);
PairObject pairAnimal = new PairObject("Dog",new Animal("puppy","wullwull"));
String strKey = (String)pairAnimal.getKey();
Animal aniVal = (Animal)pairAnimal.getValue();
System.out.println(strKey);
aniVal.bark();
PairObject pairAnimal2 = new PairObject("Dog2",new Animal("puppy","bowwow"));
strKey = (String)pairAnimal2.getKey();
aniVal = (Animal)pairAnimal2.getValue();
System.out.println(strKey);
aniVal.bark();
Pair<String> pairG = new Pair<>("키3","창고");
String genKey = pairG.getKey(); //컴파일러가 처리
String strVal = (String) pairG.getValue(); //JVM이 처리
System.out.println(genKey);
System.out.println(strVal);
Map<String,String> map = new HashMap<>();
map.put("열쇠1", "집");
map.put("열쇠2", "창고");
map.put("열쇠3", "차3333");
System.out.println(map.get("열쇠1"));
System.out.println(map.get("열쇠2"));
System.out.println(map.get("열쇠3"));
map.put("열쇠3", "차1111");
System.out.println(map.get("열쇠3"));
HashMap<Integer,String> map1 = new HashMap<>();//new에서 타입 파라미터 생략가능
map1.put(1,"사과"); //값 추가
map1.put(2,"바나나");
map1.put(3,"포도");
System.out.println(map1.get(1));
}
}
[결과]
key1
home key2 1 2 1.1 2.1 1.1 2.1 Dog puppy wullwull Dog2 puppy bowwow 키3 창고 집 창고 차3333 차1111 사과 |
[클래스다이어그램]
[Map]
package Map;
public interface Map<K,V> {
public V put(K key,V value);
public V get(K key);
public String toString();
public V remove(K key);
}
[HashMap]
package Map;
import java.util.ArrayList;
public class HashMap<K, V> implements Map<K, V>{
//요구사항
//기능요구사항 : 수집, 저장, 처리, 분석
//비기능 요구사항 : 성능, 인터페이스, 데이터, 테스트, 보안, 품질
//제약사항
private ArrayList<Pair<K,V>> list = null; //Pair을 넣는다는건 객체로 가져오겠다는 뜻
public HashMap() {
list = new ArrayList<>();
}
@Override
public V put(K key, V value) {
//기능적인것 기존의 값을 내놓아야한다.
//비기능적인것 : 기존값
/*
* Pair<K,V> pair = null;
* V rValue = null;
* for(int i=0;i<list.size();i++){
* pair = list.get(i);
* if(pair.getKey() == key){
* rValue = pair.getValue();
* pair.setValue(value);
* return rValue;
* }
* }
*
* list.add(new Pair<K,V>(key,value));
* return rValue;
* */
Pair<K,V> pair = new Pair<K,V>(key,value); //list.add(new Pair<K,V>(key,value));
for(int i=0;i<list.size();i++) {
Pair<K,V> pair1 = list.get(i);
if(pair1.getKey().equals(key)) {
list.set(i, pair);
return pair.getValue();
}
}
list.add(pair);
return null;
}
@Override
public V get(K key) {
V value = null;
Pair<K,V> pair;
for(int i=0;i<list.size();i++) {
pair = list.get(i);
if(pair.getKey().equals(key)) {
return pair.getValue(); // vlaue = pair.getValue();
}
}
//new Pair(key, new Object());
return value;
}
// @Override
// public String toString() {
// String result = "{";
// Pair<K, V> pair;
// for (int i=0;i<list.size();i++) {
// pair = list.get(i);
// result += pair.getKey()+"="+pair.getValue()+", ";
// }
// if (!list.isEmpty()) {
// result = result.substring(0, result.length() - 2); // remove the last ", "
// }
// result +="}";
// return result;
// }
@Override
public String toString() {
return list.toString();
}
@Override
public V remove(K key) {
Pair<K, V> pair;
for(int i=0; i<list.size();i++) {
pair = list.get(i);
if(pair.getKey().equals(key)) {
list.remove(i);
return pair.getValue();
}
}
return null;
}
private class Pair<K,V>{
private K key;
private V value;
Pair(K key,V value){
this.key = key;
this.value = value;
}
K getKey() {
return this.key;
}
V getValue() {
return this.value;
}
void setKey(K key) {
this.key = key;
}
void setValue(V value) {
this.value = value;
}
@Override
public String toString() {
return this.key+" - "+this.value;
}
}
}
[MapSimulator]
package Map;
public class MapSimulator {
public static void main(String[] args) {
HashMap<String,String> hashmap = new HashMap<>();
hashmap.put("key1","home");
hashmap.put("key2", "Car");
System.out.println(hashmap.get("key1"));
System.out.println(hashmap.toString());
hashmap.put("key1","house");
System.out.println(hashmap.get("key1"));
System.out.println(hashmap.toString());
hashmap.remove("key1");
System.out.println(hashmap.toString());
}
}
[결과]
home
{key1=home, key2=Car} house {key1=house, key2=Car} {key2=Car} |
2023.05.11 (0) | 2023.05.11 |
---|---|
2023.05.10 (0) | 2023.05.11 |
2023.05.08 [다차원배열&override] (0) | 2023.05.08 |
2023.05.04 (0) | 2023.05.04 |
2023.05.03 (0) | 2023.05.03 |