728x90
/*
Using everything we learned, make a Dictionary class with the following methods:
add: 단어를 추가함.
get: 단어의 정의를 리턴함.
delete: 단어를 삭제함.
update: 단어를 업데이트 함.
showAll: 사전 단어를 모두 보여줌.
count: 사전 단어들의 총 갯수를 리턴함.
upsert 단어를 업데이트 함. 존재하지 않을시. 이를 추가함. (update + insert = upsert)
exists: 해당 단어가 사전에 존재하는지 여부를 알려줌.
bulkAdd: 다음과 같은 방식으로. 여러개의 단어를 한번에 추가할 수 있게 해줌. [{"term":"김치", "definition":"대박이네~"}, {"term":"아파트", "definition":"비싸네~"}]
bulkDelete: 다음과 같은 방식으로. 여러개의 단어를 한번에 삭제할 수 있게 해줌. ["김치", "아파트"]
Requirements:
Use class
Use typedefs
Use List
Use Map
*/
import 'dart:developer';
typedef Word = Map<String, String>;
class Dictionary {
List<Word> words = [];
void add(Word word) {
words.add(word);
}
String? get(String key) {
try {
dynamic targetWord = words.firstWhere(
(Word? element) => element?.keys.first == key,
);
return targetWord.values.first;
} on StateError {
return null;
}
}
void delete(String key) {
words.removeWhere((Word element) => element.keys.first == key);
}
void update(Word word) {
var newWords = words
.map((Word element) =>
element.keys.first == word.keys.first ? word : element)
.toList();
words = newWords;
}
List<Word> showAll() {
return words;
}
int count() {
return words.length;
}
void upsert(Word word) {
//만약 이미 있다면
if (exists(word.keys.first)) {
update(word);
} else {
//없다면
add(word);
}
}
bool exists(String key) {
return get(key) != null ? true : false;
}
void bulkAdd(List<Word> newWords) {
newWords.forEach((element) => upsert(element));
}
void bulkDelete(List<String> newWords) {
newWords.forEach((element) => delete(element));
}
Dictionary(List<Word> this.words);
}
void main() {
var dictionary = Dictionary([
{'apple': '사과'}
]);
print(dictionary.showAll());
dictionary.add({'banana': '바나나'});
print('add');
print(dictionary.showAll());
print(dictionary.get('apple'));
dictionary.delete('apple');
print('delete');
print(dictionary.showAll());
dictionary.update({'banana': '나나바'});
print('update');
print(dictionary.showAll());
print('count');
print(dictionary.count());
print(dictionary.exists('a'));
dictionary.upsert({'banana': '바나나'});
dictionary.upsert({'apple': '사과'});
print('upsert');
print(dictionary.showAll());
dictionary.exists('apple');
print(dictionary.showAll());
dictionary.bulkAdd([
{'apple': '몽몽'},
{'clock': '시계'}
]);
print('bulkAdd');
print(dictionary.showAll());
dictionary.bulkDelete(['apple', 'banana']);
print('bulkDelete');
print(dictionary.showAll());
}
https://stackoverflow.com/questions/52354195/list-firstwhere-bad-state-no-element
개선할 점.
get 메소드에서 firstWhere을 사용할때, 딕셔너리에 없어서 null인 경우 orElese 속성을 넣었더니, List<Word> 타입으로 넣었기 때문에 안돌아간다는 에러가 나왔다. 그래서 에러처리로 넣어줬는데, 메소드를 적절하게 사용해서 해결하고 싶다.
https://api.flutter.dev/flutter/dart-core/Iterable/firstWhere.html
https://dart.dev/language/operators 다트 연산자, !!는 없다. !는 bool 타입만 사용할수있는 연산자이다.
과제 링크 https://replit.com/@Grapefruitgreen/flutter-1-eunsun#main.dart
'공부 > flutter' 카테고리의 다른 글
[flutter] 4.0~4.4 stateful widgets (0) | 2024.04.17 |
---|---|
[flutter]3.5~ (0) | 2024.04.17 |
[flutter] flutter 강의 3 (0) | 2024.04.05 |
[flutter] 기초강의 2 (0) | 2024.04.03 |
[flutter] 기초강의 1 (1) | 2024.04.03 |