<aside> ❓ 1. 다음 코드를 실행시 얻을 수 있는 결과를 참고하여 UserData 클래스를 제작하고 toString를 새로 재정의하여 만드시오.
main함수내용
실행결과
</aside>
ex01.dart
class UserData {
String id;
DateTime birth;
String email;
DateTime lastLoginDate;
String name;
String phoneNumber;
UserData({
required this.id,
required this.birth,
required this.email,
required this.lastLoginDate,
required this.name,
required this.phoneNumber
});
@override
String toString() {
return 'id: $id\\nname: $name\\nbirth: $birth\\nphoneNumber: $phoneNumber\\nemail: $email\\nlastLoginDate: $lastLoginDate';
}
}
void main() {
UserData userData = UserData(
id: 'id',
birth: DateTime.now(),
email: '[email protected]',
lastLoginDate: DateTime.now(),
name: '스나이퍼',
phoneNumber: '01023456789');
print(userData);
}
<aside> ❓ 2. 다음의 URL에 네트워크 요청을 보내고, 얻은 데이터를 Class를 통해 생성할 수 있도록 만드시오.
https://sniperfactory.com/sfac/http_json_data
이 때 제작하는 Class 명은 자유입니다.
받아온 네트워크 데이터를 Class에 적용시키고 플러터를 사용하여 다음의 화면을 제작합니다.
</aside>
Course.dart
class Course {
String image;
String name;
String description;
int price;
Course(this.image, this.name, this.description, this.price);
Course.fromMap(Map<String, dynamic> map)
: image = map['image'],
name = map['name'],
description = map['description'],
price = map['price'];
}
ex02.dart
import 'package:dio/dio.dart';
import 'package:first_app/homework/week5/day23/Course.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var dio = Dio();
return MaterialApp(
home: Scaffold(
body: Center(
child: FutureBuilder(
future: dio.get('<https://sniperfactory.com/sfac/http_json_data>'),
builder: (context, snapshot) {
Course course = Course.fromMap(snapshot.data!.data['item']);
if (snapshot.connectionState == ConnectionState.done) {
return Container(
width: 250,
height: 400,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Image.network(course.image,
fit: BoxFit.fill,),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(course.name),
Divider(),
Text(course.description),
Padding(
padding: const EdgeInsets.symmetric(vertical: 5.0),
child: ElevatedButton(
onPressed: (){},
child: Text('${course.price}원 결제하고 등록'),
style: ElevatedButton.styleFrom(
minimumSize: Size(double.infinity, 35)
),
),
)
],
),
),
],
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: const [
BoxShadow(
color: Colors.black12,
blurRadius: 2,
spreadRadius: 1,
)
]
),
clipBehavior: Clip.antiAlias,
);
} else {
return CircularProgressIndicator();
}
},
)
),
),
);
}
}
<aside> ❓ 3. 아래 main함수가 실행될 수 있도록 Dart 프로젝트를 구성하고, 지하철과 사람을 표현하는 클래스를 직접 구성하시오.
class Subway {
//...
}
class Human {
//...
}
void main() {
// 인스턴스 생성
var subway = Subway(line:'2호선', current: '홍대입구역', passengers:[]);
var human1 = Human(name: '사람1', money: 5000, curSubway: null);
var human2 = Human(name: '사람2', money: 3450, curSubway: null);
var human3 = Human(name: '사람3', money: 450, curSubway: null);
// 사람 탑승
print(human1.getOn(subway));
print(human2.getOn(subway));
print(human3.getOn(subway));
// 현재 지하철 탑승
print(subway.toString());
// 각 사람들의 현재 상태를 출력
print(human1.toString());
print(human2.toString());
print(human3.toString());
}
반드시 각 클래스는 생성자를 포함하도록 하세요.
메인함수(void main)는 일체 수정할 필요가 없습니다.
각 클래스는 toString의 메서드를 이용하여 아래의 실행결과와 일치하게 나올 수 있도록 합니다.
실행결과(출력결과)는 다음과 같습니다.
[2호선] 지하철이 생성되었습니다.
사람1이(가) 성공적으로 탑승하였습니다. (남은 돈 : 3500)
사람2이(가) 성공적으로 탑승하였습니다. (남은 돈 : 1950)
사람3이(가) 탑승에 실패하였습니다 (잔액부족)
Subway([2호선 | 홍대입구] 현재 지하철 탑승인원 2명)
Human(human:사람1, money: 3500, curSubway: Subway([2호선 | 홍대입구] 현재 지하철 탑승인원 2명))
Human(human:사람2, money: 1950, curSubway: Subway([2호선 | 홍대입구] 현재 지하철 탑승인원 2명))
Human(human:사람3, money: 450, curSubway: null)
</aside>
ex03.dart
class Subway {
String line;
String current;
List passengers;
Subway({required this.line, required this.current, required this.passengers}) {
print('[$line] 지하철이 생성되었습니다.');
}
@override
String toString() => 'Subway([$line | $current] 현재 지하철 탑승인원 ${passengers.length}명)';
}
class Human {
String name;
int money;
Subway? curSubway;
Human({required this.name, required this.money, this.curSubway});
getOn (Subway subway) {
if (money >= 1500) {
money -= 1500;
curSubway = subway;
subway.passengers.add(name);
return '$name이(가) 성공적으로 탑승하였습니다.';
} else {
return '$name이(가) 탑승에 실패하였습니다. (잔액부족)';
}
}
@override
String toString() => 'Human(human: $name, money: $money, curSubway: $curSubway)';
}
void main() {
// 인스턴스 생성
var subway = Subway(line:'2호선', current: '홍대입구역', passengers:[]);
var human1 = Human(name: '사람1', money: 5000, curSubway: null);
var human2 = Human(name: '사람2', money: 3450, curSubway: null);
var human3 = Human(name: '사람3', money: 450, curSubway: null);
// 사람 탑승
print(human1.getOn(subway));
print(human2.getOn(subway));
print(human3.getOn(subway));
// 현재 지하철 탑승
print(subway.toString());
// 각 사람들의 현재 상태를 출력
print(human1.toString());
print(human2.toString());
print(human3.toString());
}