9
This commit is contained in:
13
lib/13/go/raw_book/src/data/author.dart
Normal file
13
lib/13/go/raw_book/src/data/author.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'book.dart';
|
||||
|
||||
class Author {
|
||||
final int id;
|
||||
final String name;
|
||||
final books = <Book>[];
|
||||
|
||||
Author(this.id, this.name);
|
||||
}
|
||||
15
lib/13/go/raw_book/src/data/book.dart
Normal file
15
lib/13/go/raw_book/src/data/book.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'author.dart';
|
||||
|
||||
class Book {
|
||||
final int id;
|
||||
final String title;
|
||||
final Author author;
|
||||
final bool isPopular;
|
||||
final bool isNew;
|
||||
|
||||
Book(this.id, this.title, this.isPopular, this.isNew, this.author);
|
||||
}
|
||||
61
lib/13/go/raw_book/src/data/library.dart
Normal file
61
lib/13/go/raw_book/src/data/library.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'author.dart';
|
||||
import 'book.dart';
|
||||
|
||||
final libraryInstance = Library()
|
||||
..addBook(
|
||||
title: 'Left Hand of Darkness',
|
||||
authorName: 'Ursula K. Le Guin',
|
||||
isPopular: true,
|
||||
isNew: true)
|
||||
..addBook(
|
||||
title: 'Too Like the Lightning',
|
||||
authorName: 'Ada Palmer',
|
||||
isPopular: false,
|
||||
isNew: true)
|
||||
..addBook(
|
||||
title: 'Kindred',
|
||||
authorName: 'Octavia E. Butler',
|
||||
isPopular: true,
|
||||
isNew: false)
|
||||
..addBook(
|
||||
title: 'The Lathe of Heaven',
|
||||
authorName: 'Ursula K. Le Guin',
|
||||
isPopular: false,
|
||||
isNew: false);
|
||||
|
||||
class Library {
|
||||
final List<Book> allBooks = [];
|
||||
final List<Author> allAuthors = [];
|
||||
|
||||
void addBook({
|
||||
required String title,
|
||||
required String authorName,
|
||||
required bool isPopular,
|
||||
required bool isNew,
|
||||
}) {
|
||||
var author = allAuthors.firstWhere(
|
||||
(author) => author.name == authorName,
|
||||
orElse: () {
|
||||
final value = Author(allAuthors.length, authorName);
|
||||
allAuthors.add(value);
|
||||
return value;
|
||||
},
|
||||
);
|
||||
var book = Book(allBooks.length, title, isPopular, isNew, author);
|
||||
|
||||
author.books.add(book);
|
||||
allBooks.add(book);
|
||||
}
|
||||
|
||||
List<Book> get popularBooks => [
|
||||
...allBooks.where((book) => book.isPopular),
|
||||
];
|
||||
|
||||
List<Book> get newBooks => [
|
||||
...allBooks.where((book) => book.isNew),
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user