Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Dart & Flutter",
"request": "launch",
"type": "dart"
}
]
}
37 changes: 37 additions & 0 deletions Book.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Book{

late String bookTitle;
late String bookAuthorName;
late String category;
late int availableCopies;
late int price;

Book(String bookTitle, String bookAuthorName,
String category, int availableCopies, int price){

this.bookTitle = bookTitle;
this.bookAuthorName = bookAuthorName;
this.category = category;
this.availableCopies = availableCopies;
this.price = price;
}

addBook (){

availableCopies ++;
}

removeBook (){

availableCopies--;
}

@override
String toString() {

String bookAsString = "Book title - $bookTitle Author- $bookAuthorName Price- $price";

return bookAsString;
}

}
266 changes: 266 additions & 0 deletions main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
import 'dart:io';
import 'Book.dart';

List<Book> library = [];

main (){
showMainMenu();

}

showMainMenu(){

starsLine();
print("1. Query a book");
print("2. Add a book");
print("3. Delete a book");
print("4. Update a book");
print("5. Make a purchase");
starsLine();
print("Please select an option:");

int? choice = int.parse(stdin.readLineSync()!);

switch(choice){
case 1:
findABookMenu();
break;
case 2:
addBook();
break;
case 3:
deleteBook();
break;
case 4:
updateBook();
break;
case 5:
purchaseBooks();
break;
default: {

invaildOption();
}
break;
}
}

findABookMenu() {

starsLine();
print("How do you want to find the book:");
print("1. Using it's title.");
print("2. Using the author name.");
print("3. Using the category of the book.");
starsLine();

print("Select an option:");
int optionFind = int.parse(stdin.readLineSync()!);


print("Please enter the search term:");
String? searchTerm = stdin.readLineSync();
if (searchTerm == null || searchTerm.isEmpty) {
invaildOption();
return;
}

Book? book;
switch(optionFind){
case 1:
book = findBook(searchTerm, "title");
print(book.toString());
break;
case 2:
book = findBook(searchTerm, "author");
print(book.toString());
break;
case 3:
book = findBook(searchTerm, "category");
print(book.toString());
break;
default: {
invaildOption();
}
break;
}

starsLine();
print("To look for another book press [Y], otherwise return to main menu:");

if(stdin.readLineSync() == "Y"){

findABookMenu();
} else{

showMainMenu();
}


}

Book? findBook(String searchTerm, String searchAttribute) {

Book? targetBook = null;
String bookSearchData;

library.forEach((book) {

if (searchAttribute == "title"){
bookSearchData = book.bookTitle;
} else if(searchAttribute == "author"){
bookSearchData = book.bookAuthorName;
} else {
bookSearchData = book.category;
}

if (bookSearchData == searchTerm){
targetBook = book;
}
});

return targetBook;
}

addBook(){

starsLine();
print("Please enter the book title:");
String title = stdin.readLineSync() ?? "";

print("Please enter the book author:");
String author = stdin.readLineSync() ?? "";

print("Category of the book:");
String category = stdin.readLineSync() ?? "";

print("Number of copies to be added:");
int numberOfCopies = int.parse(stdin.readLineSync() ?? "");

print("The price of the book:");
int price = int.parse(stdin.readLineSync() ?? "");

library.add(Book(title, author, category, numberOfCopies, price));

starsLine();
print("Book was added successfully");
starsLine();
showMainMenu();

}

deleteBook(){

starsLine();
print("Enter book title:");
String? bookTitle = stdin.readLineSync();
if (bookTitle == null || bookTitle.isEmpty) {
invaildOption();
return;
}

library.remove(findBook(bookTitle, "title"));
print("Book was removed successfully");
showMainMenu();

}

updateBook(){
starsLine();
print("Enter book title:");
String? bookTitle = stdin.readLineSync();
if (bookTitle == null || bookTitle.isEmpty) {
invaildOption();
return;
}

Book? book = findBook(bookTitle, "title");

print("Which data do you want to update:");
print("1. Title");
print("2. Author");
print("3. Category");
print("4. Number of copies");
print("5. Price");

print("Select an option:");
int optionFind = int.parse(stdin.readLineSync()!);

print("Input new value:");
String? newValue = stdin.readLineSync();
if (newValue == null || newValue.isEmpty) {
invaildOption();
return;
}

switch(optionFind){
case 1:
book!.bookTitle = newValue;
break;
case 2:
book!.bookAuthorName = newValue;
break;
case 3:
book!.category = newValue;
break;
case 4:
book!.availableCopies = int.parse(newValue);
break;
case 5:
book!.price = int.parse(newValue);
break;
}

print("Book was updated successfully");
showMainMenu();

}

purchaseBooks(){

List<Book> books = [];

do {
starsLine();
print("Enter book title:");
String? bookTitle = stdin.readLineSync();
if (bookTitle == null || bookTitle.isEmpty) {
invaildOption();
return;
}

books.add(findBook(bookTitle, "title")!);

starsLine();
print("To look for another book press [Y], otherwise checkout:");

} while(stdin.readLineSync() == "Y");

checkout(books);
}

checkout(List<Book> books){

int totalCost = 0;
books.forEach((book) {

print(book.toString());
totalCost = totalCost + book.price;
book.availableCopies--;
});

starsLine();
print("Total cost: $totalCost");
showMainMenu();
}

starsLine(){

print("*******************************");
}

invaildOption(){

print("Please enter a vaild option!");
showMainMenu();
}