From 295ff641e0fddfc11bf13537e6242503a20b2e42 Mon Sep 17 00:00:00 2001 From: dev one Date: Sat, 29 Jul 2023 23:45:10 +0300 Subject: [PATCH 1/2] Mjd submitted the project --- .gitignore | 3 + CHANGELOG.md | 3 + README.md | 85 +------ analysis_options.yaml | 30 +++ bin/data.dart | 51 ++++ bin/project1.dart | 527 ++++++++++++++++++++++++++++++++++++++++++ pubspec.lock | 381 ++++++++++++++++++++++++++++++ pubspec.yaml | 15 ++ 8 files changed, 1012 insertions(+), 83 deletions(-) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 analysis_options.yaml create mode 100644 bin/data.dart create mode 100644 bin/project1.dart create mode 100644 pubspec.lock create mode 100644 pubspec.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a85790 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..effe43c --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/README.md b/README.md index a28fd1a..3816eca 100644 --- a/README.md +++ b/README.md @@ -1,83 +1,2 @@ -# Project #1 - Library Project using Dart - -Congratulations on reaching the project phase! This project will assess your ability to develop a "Library Project using Dart" with a Command Line Interface (CLI) to manage a library's book inventory and user interactions. Below are the key requirements and evaluation criteria: - - -## Description: - -The "Library Project using Dart" is an exciting software application designed to efficiently manage a library's book inventory and streamline user interactions through a Command Line Interface (CLI). This capstone project provides you, as a student, with an excellent opportunity to apply your knowledge and skills in Flutter Development, showcasing your proficiency in Dart programming. - -The primary goal of the project is to develop a user-friendly and efficient CLI that caters to both library staff and patrons. With this CLI, users can effortlessly query books, view the number of available copies, add new books to the library, delete books from the inventory, make book purchases, and receive detailed invoices after each purchase. Additionally, the project demands an essential feature - updating the number of book copies after a purchase is made to maintain accurate inventory records. - -To begin, you will create a new Dart project, ensuring that the project structure is appropriately organized for a clean and manageable codebase. As you progress, it is crucial to write code that is clear, well-organized, and scalable, adhering to standard coding conventions to enhance code readability. Demonstrating your comprehensive understanding of programming concepts and techniques is essential for a successful outcome. - -Your CLI should allow users to search for books by title, author, or category, providing a straightforward and accessible way to retrieve information about the library's collection. Implementing a method to add new books to the library with the appropriate updates to the book count will further enhance the system's usability. - -Managing the library's inventory also involves implementing a method to delete books, ensuring smooth handling of cases where a book to be deleted does not exist. Facilitating book purchases through the CLI is another critical aspect, requiring you to decrease the number of book copies according to the books purchased and generate an informative invoice for the user. - -As an extra credit opportunity, you can implement features such as displaying all book categories in the library, returning a list of books belonging to a selected category. Additionally, providing a method to count the number of purchased books will further showcase your skills. - -The project must be written entirely in the Dart language and fully submitted through GitHub using version control (Git). Remember to create descriptive commits to showcase your progress and follow the provided guidelines for academic integrity. - - - -## Minimum Requirements: - -1. **Project Structure**: Create a new Dart project and organize its structure appropriately. Set up the necessary folders, files, and ensure a clean and organized project layout. - -2. **Clean Code**: Write clear, well-organized, and scalable code. Use meaningful variable and function names, following standard coding conventions to improve code readability. Well-structured and easily maintainable code is essential. - -3. **Applying Concepts**: Demonstrate a comprehensive understanding of programming concepts and techniques. Implement the CLI features with efficiency and correctness. - -4. **Query Books**: Develop a simple CLI user interface that allows users to query books and view the number of available copies. Users should be able to search for books by title, author, or category. - -5. **Add New Book**: Provide a method to add a new book to the library and update the number of book copies accordingly. Ensure proper error handling for invalid inputs. - -6. **Delete Book**: Implement a method to delete a book from the library and update the book count. Handle cases where the book to be deleted does not exist. - -7. **Purchase**: Implement the purchase process, allowing users to buy books from the library. Ensure that the number of book copies is decreased according to the books purchased. - -8. **Invoice**: Display a detailed invoice after the purchase, showing the books purchased, their prices, and the total cost. - -9. **Editing Capability**: Add the ability to modify book data, such as title, author, and price. Users should be able to update book information easily. - -## Ideas for Extra Credit: - -- **Display all Book Categories**: Provide a method to display a list of all book categories in the library. When a category is selected, return a list of all books belonging to that category. -- **Reporting:** Provide a method to present a report that contains the number of all purchased books from the library, and calculate their amount. -- - -## Delivery Requirements: - -- **Language**: The project should be written in Dart language. - -- **GitHub Submission**: - - Create a Fork from the exam’s GitHub repository. - - Create a new branch with your name, i.e., Nawaf-Alshawan. - - Commit frequently with descriptive messages to show your progress. - - Finally, create a Pull Request to the exam’s original repository containing your solution. - -- **README.md File**: Include a README.md file with instructions on how to run and test the project. This file should provide a clear guide for users to understand how to interact with the CLI. - -- **Screenshots**: Include relevant screenshots of the app to showcase its appearance and functionalities. Visuals can help users understand the app's design and layout. - -## Schedule & Deadlines: - -- Exam Start Date: 27/7/2023 - 3:00 PM -- Exam Submission Deadline: 30/7/2023 – 11:59 AM - -## Evaluation Criteria: - -The project will be evaluated based on the following criteria: - -- **Completion**: The extent to which the project meets all the minimum requirements, including the extra credit ideas if implemented. - -- **Quality**: The overall quality of the project, including the coding style, organization, and ease of maintenance. Well-structured and readable code will be favorably evaluated. - -- **Use of Programming Concepts**: How well you utilize programming concepts such as classes, abstracts, functions, and other relevant techniques. - -## Resources - -- [Dart Tutorials](https://dart.dev/tutorials) - -Good luck with the project, and feel free to ask any additional questions if needed! +A sample command-line application with an entrypoint in `bin/`, library code +in `lib/`, and example unit test in `test/`. diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..dee8927 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/bin/data.dart b/bin/data.dart new file mode 100644 index 0000000..81c4b1a --- /dev/null +++ b/bin/data.dart @@ -0,0 +1,51 @@ +final List myData = [ + { + "title": "Unlocking Android", + "description" : "A Developer's Guide provides concise, hands-on instruction for the Android operating system and development tools.", + "authors": ["W. Frank Ableson", "Charlie Collins", "Robi Sen"], + "categories": ["Open Source", "Mobile"], + "copies" : 1, + "price" : 120 + + }, + { + "title": "Android in Action, Second Edition", + "description" : "comprehensive tutorial for Android developers", + "authors": ["Gojko Adzic"], + "categories": ["Software Engineering"], + "copies" : 10, + "price" : 85 + }, + { + "title": "Specification by Example", + "description" : "author Gojko Adzic distills interviews with successful teams worldwide, sharing how they specify, develop, and deliver software, without defects", + "authors": ["W. Frank Ableson", "Charlie Collins", "Robi Sen"], + "categories": ["Open Source", "Mobile"], + "copies" : 1, + "price" : 100 + }, + { + "title": "Flex 3 in Action", + "description" : "easy-to-follow, hands-on Flex tutorial.", + "authors": ["Tariq Ahmed with Jon Hirschi", "Faisal Abid"], + "categories": ["Internet"], + "copies" : 1, + "price" : 150 + }, + { + "title": "Collective Intelligence in Action", + "description" : "hands-on guidebook for implementing collective intelligence concepts using Java.", + "authors": ["Satnam Alag"], + "categories": ["Internet"], + "copies" : 1, + "price" : 56 + }, + { + "title": "Zero to One", + "description" : "Note on Start Ups, or How to Build the Future ", + "authors": ["Blake MasterSouqs Peter Thiel"], + "categories": ["Internet"], + "copies" : 12, + "price" : 60 + } +]; \ No newline at end of file diff --git a/bin/project1.dart b/bin/project1.dart new file mode 100644 index 0000000..fdec23e --- /dev/null +++ b/bin/project1.dart @@ -0,0 +1,527 @@ +import 'data.dart'; +import 'dart:io'; + +void main(List args) { + try{ + //Check who want to do a process, Library admin or Customer? + print("For Admin enter 'A or a', for Customr enter 'C or c', and 'E or e' to exit"); + String choice = stdin.readLineSync()!.toLowerCase(); + //when Admine want to make an action + if(choice == 'a'){ + admin(); + } + else if(choice == 'c'){ + user(); + } + else{ + exit(0); + } + + }catch(error){ + print("ERROR, somthing went wrong!"); + } + + +} + +//Books class +class Books{ + late String? title; + late List? authors; + late List? categories; + late int? price; + late String? descrip; //Book description + late int? copies; + static int bookCount = bookInStore();// The number of the books + static int pruchaseNumber = 0; //number of paid books + static List? invoice; + + //Class Book constractor + Books({ + this.title , + this.authors, + this.categories, + this.price, + this.descrip, + this.copies + }); + + //Method to display an information about any book + displayData() { + print("Title:$title, Authours: $authors, \nCategories: $categories, Copies: $copies Price: $price\nDescription: $descrip"); + } +} + + + + + +//Functions: + +//Function for searching for a specific book, it will take the way to search from the user & info about title, author, or category +searchBy(String wayToSearch, String info){ + List allBooks = []; //List to store all books realted to Author, Title, or Category + int? copy = 0; //To check for any existing copies + + if(wayToSearch == 'title'){ // check if the user will search by book title + for(int i =0 ; i < myData.length ; i++){ //enter a loop for searching im myData + if((myData[i]['title']).toString().toLowerCase() == info.toLowerCase()){ //Ignore the case in searching to return accurate info + allBooks.add(myData[i]['title']);// if there is any matching store the book in a list + copy = myData[i]['copies']; //and get the number of copies + } + } + return ("There are $copy copies of Book named '$info'"); //Info msg + } + + else{// if user search by either Author or Categories + for (int i =0 ; i< myData.length ; i++) { + List temp = myData[i][wayToSearch]; //Store each list insid Category or Author list as a temprary list + if((temp.toString().toLowerCase()).contains(info.toLowerCase())){//Check if its containe the searched info + allBooks.add(myData[i]['title']); + } + } + + if(allBooks.isEmpty){ + return ("Sorry, there is no found book :( ");// If there are no book found, the list will be empty + }else{ + return ("Books with similer $wayToSearch are : $allBooks");//Otherwise show an information about the book + } + + } + +} + + +// Adding new books function: +addNewBook(){ + //Take the new book info from the admin user + print("Book Title: "); + String newTitle = stdin.readLineSync()!; + + print("Give a brife description about the book:"); + String descrip = stdin.readLineSync()!; + + print("How many copeis you want to add? "); + int copyNum = int.parse(stdin.readLineSync()!); + + print("How much does it cost? "); + int price = int.parse(stdin.readLineSync()!); + + print("Number of Authors: "); + int numContrib = int.parse(stdin.readLineSync()!); + + print("Enter AUTHORS name: 'Hit enter after each name' "); + List contrib = addItemLoop(numContrib); //Enter names depending on the number owner want + + print("Number of Related Categories: "); + int numCategory = int.parse(stdin.readLineSync()!); + + print("Enter related CATEGORIES: 'Hit enter after each name' "); + List typs = addItemLoop(numCategory); //Enter names depending on the number owner want + + //then store all collected information as a map + final newBook = { + "title": newTitle, + "authors": contrib, + "categories": typs, + 'price': price, + 'description': descrip, + "copies": copyNum + }; + + myData.add(newBook);// add the new info to myData + Books.bookCount ++; + displayItem();// then display all books with new ones +} + + +//A function to add any new items as a list 'its for adding multiple Authors or Categories +addItemLoop(int numberOfItems){ + List list = []; //Empty list to store info + int counter = 0;// loop counter + while(counter < numberOfItems){ //loop until the counter == to number of items + String named = stdin.readLineSync()!; // Read Author/Category name + list.add(named); //Add name to the list + counter++;//increas counter + } + return list; //Return the list +} + +//A function to display all books in the store +displayItem() { + List listItem = []; // Create an empty list with type BOOKS + for (var item in myData) { //loop for retrieving info from myData file + + //Add a new object form book class each time, until finishing all stored books in data file + listItem.add(Books( + title: item["title"], + authors: item["authors"], + categories: item["categories"], + descrip: item["description"], + price: item['price'], + copies: item["copies"])); + } + + for (var item in listItem) { //Go in another loop to display all stored data + print("____________"); + item.displayData(); //call method from Books class + } +} + +//Delete or Update on the books function +modifyBook(){ + print("\nFor deleting a book enter 'D or d', for update book information enter 'U or u':"); + String selection = stdin.readLineSync()!.toLowerCase(); + String choice; + + //Determaine the choice of the user to print sutiable msg + if(selection == 'd'){ + choice = "Deleted"; + } + else{ + choice = "Updated"; + } + //If the user choose a selection print a msg + if(choice.isNotEmpty){ + // Ask the user to enter the needed title + print("\nPleas enter book title to be $choice: "); + String title = stdin.readLineSync()!.toLowerCase(); + + int exist = 0;//for a future msg + + //Search for matched title in myData file + for(int i = 0 ; i < myData.length ; i++){ + if(myData[i]["title"].toString().toLowerCase() == title){ + if(selection == 'd'){ + //if the user want to delete a book -> remove it from the list + myData.remove(myData[i]); + Books.bookCount --; //Decreas the number of books and print a confirmation msg + print("\n\tBook with title '$title' removed successfully "); + displayItem(); // then display all books to show the changes + } + else if(selection == 'u'){ + //if the user want to update a book -> call update function + updateBook(i); + displayItem(); // then display all books to show the changes + } + exist++; + } + } + + + if(exist == 0){ //check if there is any changes by checking the number of counter + print("\nSorry there is no book with title '$title' in the library :("); + }else{ + print("\n\tModification succeeded"); // print confirmation msg + } + + } + else{ + print("\n\tSorry there are no entered choice!!");// Show alert msg if there is no selection + } + +} + +//Updating function +updateBook(int index){ //take the direct index of the item in myData list + //Ask the user for the needed part to be update by displaying a menu + print("\nWhat do you wand to modify? \n"); + print("\t1- Title"); + print("\t2- Authors"); + print("\t3- Categories"); + print("\t4- Description"); + print("\t5- Price"); + print("\t6- Number of copies"); + print("Enter yor choice:"); + int choice = int.parse(stdin.readLineSync()!); + + //Go in a case depending on the choice + switch(choice){ + case 1: + print("Enter new title"); + String newTitle = stdin.readLineSync()!; + //change the title by assigning the new one dirctly + myData[index]['title'] = newTitle; + break; + + case 2: + print("Enter the number of Authors"); + int authors = int.parse(stdin.readLineSync()!);// Enter the number of authors + print("Enter AUTHORS name: 'Hit enter after each name' "); + List contrib = addItemLoop(authors); //Enter names depending on the number owner want + myData[index]['authors'] = contrib;//Assigning the authors insted of registerd one + break; + + case 3: + print("Enter the number of categories"); + int categ = int.parse(stdin.readLineSync()!);// Enter the number of categories + print("Enter CATEGORIES name: 'Hit enter after each name' "); + List type = addItemLoop(categ); //Enter names depending on the number owner want + myData[index]['categories'] = type; //Assigning the category insted of registerd one + break; + + case 4: + print("Enter new description"); + String descrip = stdin.readLineSync()!; + myData[index]['description'] = descrip; //Assigning the new description + break; + + case 5: + print("Enter new price"); + int price = int.parse(stdin.readLineSync()!); + myData[index]['price'] = price;//Assigning the new price + break; + + case 6: + print("Enter new numbers of copies"); + int copy = int.parse(stdin.readLineSync()!); + myData[index]['copies'] = copy; //Assigning the new number of copies + break; + + default: + print("Wrong choice!!"); + } + +} + +//Buying a book function +purchase(){ + List myBooks = []; //Create an empty book to store chosen books + bool flag = false; + //Show the avilable books + print("Avilable Books in the store:"); + displayItem(); + + print("\nHow many books you want to buy?"); + int bookToBuy = int.parse(stdin.readLineSync()!); //Ask for the number of needed books + + List index = []; //store the index of each needed book to facilitate future operations + int counter = 0; + while(counter < bookToBuy){ //loop untile counter become less than number of books + print("\nEnter the ${counter+1} book title: ");//ask the user to enter the title of each needed book + String title = stdin.readLineSync()!; + + for(int i = 0 ; i < myData.length ; i++){ + if(myData[i]["title"] == title){///find the match title from myData + index.add(i);//store there indexes + myBooks.add(title); //add the book need to buy + } + } + counter++; + } + + //check if the user are sure about buying the books + print("To confirm buying chosen books enter 'C or c', else 'N or n'"); + String confirmation = stdin.readLineSync()!.toLowerCase(); + + if(confirmation == 'c'){ + //when order confirmed start reducing the number of copyies + for(int x = 0; x < index.length; x++){ + if( myData[index[x]]['copies'] > 0){//At first check if there are enough copies + myData[index[x]]['copies']--; + Books.pruchaseNumber++;//Increase the pruches process + flag = true; + + }else{ + print("Sorry, no enough copies to sale for ${myData[index[x]]['title']}"); //Show msg if no found copies + } + } + + if(flag){ + print("\nPurchase completed"); //Show confirmation msg + invoice(myBooks);// Preduce an invoice for the user + } + + } + else{ + return; //Return if user not confirm + } +} + +//Function to count all existing books number +bookInStore(){ + num allBooks = 0; + for(int i = 0; i < myData.length; i++){ + allBooks += myData[i]['copies'];//Summ all copies number + } + return allBooks; // return the number of copies +} + +//Creat an invoice function +invoice(List myInvoice){ + //Print the structure of the invoice + print("\n\tYOUR INVOICE"); + print("BOOKS \t\t\tPRICE\n"); + + num totalPrice = 0;//to store the summtion of all prices + List price = [];// to store each price + + for(int i = 0 ; i < myInvoice.length; i++){ + for(int j = 0 ; j < myData.length; j++){ + String bookName = myInvoice[i];//collect all the needed books in a list + int bookPrice = myData[j]['price']; //get each book price + if(bookName == myData[j]['title']){ //if the needed book found + price.add(bookPrice);//take its price and add it to the list + print('$bookName, \t$bookPrice');//show each book info individually + print("___________"); + } + } + } + + //Sum the total price + for (num i in price) { + totalPrice += i; + } + + print("Total payment: $totalPrice");//Show the total +} + + + +admin(){ + while(true){ + //Display available actions + print("\nChoose an action: "); + print("\t1- Add new Book"); + print("\t2- Modifiy on existing Book 'Delete or Update' "); + print("\t3- Number of books in the library"); + print("\t4- Number of Purches"); + print("\t5- Change to user side"); + print("\t6- Exit"); + + //Ask admine to enter his choic + print("\nEnter your choice: "); + int selection = int.parse(stdin.readLineSync()!); + + switch(selection){ + case 1: + addNewBook(); //If admin need to add new book -> Call addNewBook() function + break; + + case 2: + modifyBook(); //If admin/staff want to delete a book -> Call deleteBook() function + break; + + case 3: + //show the number of books by calling bookCount variable from Books class + print("\tThere are ${Books.bookCount} books found in the library\n"); + break; + + case 4: + //Show the number of pruches that done by the user + print("\t\n There are ${Books.pruchaseNumber} pruches done from the library"); + break; + + case 5: + //Switch to user menu + user(); + break; + + case 6: + exit(0);// Otherwise exit form program + + default: + print("\n\tSorry, something went wrong! ");// For unpredictable actions print msg + } + } +} + +user(){ + while(true){ + //Display available actions + print("\nChoose from menu: "); + print("\t1- Search for a Book"); + print("\t2- Buy a Book"); + print("\t3- Display all Books info"); + print("\t4- Display all categories"); + print("\t5- Change to admin side"); + print("\t6- Exit"); + print("\nEnter your choice number: "); + int selection = int.parse(stdin.readLineSync()!); //take the selection + String info;// will be used later + + switch(selection){ + case 1: + //Display all avilable ways to search for a book + print("\nI will search by: "); + print("\t1- Title"); + print("\t2- Author"); + print("\t3- Category"); + print("\t4- Cancel & Exit"); + print("\nChoice: "); + int searchWay = int.parse(stdin.readLineSync()!); + + switch(searchWay){ + case 1: + //Take the book title info + print("\nEnter book title: "); + info = stdin.readLineSync()!; + print(searchBy('title',info)); //call search function + break; + + case 2: + //Take the book authors info + print("\nEnter book author: "); + info = stdin.readLineSync()!; + print(searchBy('authors',info));//call search function + break; + + case 3: + //Take the book categories info + print("\nEnter book category: "); + info = stdin.readLineSync()!; + print(searchBy('categories',info));//call search function + break; + + case 4: + exit(0);//end program + + default: + print("\n\tSorry, something went wrong! "); + } + break; + + case 2: + //Call purchase function to buy a book + purchase(); + break; + + case 3: + //Display all item in library by calling displayItem function + displayItem(); + break; + + case 4: + //Show all avilable category using allCategory function + print(allCategory()); + break; + + case 5: + //Switch to admin/staff menu + admin(); + break; + + case 6: + exit(0); + + default: + print("\n\tSorry, something went wrong! "); + + } + } +} + +//Extra +allCategory(){ + List allType = []; //List to store all categories + + for (int i =0 ; i< myData.length ; i++) { + List temp = myData[i]['categories']; //Store each list insid Category list + + for(int j = 0 ; j < temp.length ; j++){//Another loop for the inner list + allType.add(temp[j]); // Add each element insted of adding a list + } + } + return allType.toSet(); //Return list as a set to remove duplication +} + diff --git a/pubspec.lock b/pubspec.lock new file mode 100644 index 0000000..5a37741 --- /dev/null +++ b/pubspec.lock @@ -0,0 +1,381 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + _fe_analyzer_shared: + dependency: transitive + description: + name: _fe_analyzer_shared + sha256: "0816708f5fbcacca324d811297153fe3c8e047beb5c6752e12292d2974c17045" + url: "https://pub.dev" + source: hosted + version: "62.0.0" + analyzer: + dependency: transitive + description: + name: analyzer + sha256: "21862995c9932cd082f89d72ae5f5e2c110d1a0204ad06e4ebaee8307b76b834" + url: "https://pub.dev" + source: hosted + version: "6.0.0" + args: + dependency: transitive + description: + name: args + sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596 + url: "https://pub.dev" + source: hosted + version: "2.4.2" + async: + dependency: transitive + description: + name: async + sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" + url: "https://pub.dev" + source: hosted + version: "2.11.0" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + collection: + dependency: transitive + description: + name: collection + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a + url: "https://pub.dev" + source: hosted + version: "1.18.0" + convert: + dependency: transitive + description: + name: convert + sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592" + url: "https://pub.dev" + source: hosted + version: "3.1.1" + coverage: + dependency: transitive + description: + name: coverage + sha256: "2fb815080e44a09b85e0f2ca8a820b15053982b2e714b59267719e8a9ff17097" + url: "https://pub.dev" + source: hosted + version: "1.6.3" + crypto: + dependency: transitive + description: + name: crypto + sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab + url: "https://pub.dev" + source: hosted + version: "3.0.3" + dart_internal: + dependency: transitive + description: + name: dart_internal + sha256: "689dccc3d5f62affd339534cca548dce12b3a6b32f0f10861569d3025efc0567" + url: "https://pub.dev" + source: hosted + version: "0.2.9" + file: + dependency: transitive + description: + name: file + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" + url: "https://pub.dev" + source: hosted + version: "7.0.0" + frontend_server_client: + dependency: transitive + description: + name: frontend_server_client + sha256: "408e3ca148b31c20282ad6f37ebfa6f4bdc8fede5b74bc2f08d9d92b55db3612" + url: "https://pub.dev" + source: hosted + version: "3.2.0" + glob: + dependency: transitive + description: + name: glob + sha256: "0e7014b3b7d4dac1ca4d6114f82bf1782ee86745b9b42a92c9289c23d8a0ab63" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + http_multi_server: + dependency: transitive + description: + name: http_multi_server + sha256: "97486f20f9c2f7be8f514851703d0119c3596d14ea63227af6f7a481ef2b2f8b" + url: "https://pub.dev" + source: hosted + version: "3.2.1" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + url: "https://pub.dev" + source: hosted + version: "4.0.2" + io: + dependency: transitive + description: + name: io + sha256: "2ec25704aba361659e10e3e5f5d672068d332fc8ac516421d483a11e5cbd061e" + url: "https://pub.dev" + source: hosted + version: "1.0.4" + js: + dependency: transitive + description: + name: js + sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 + url: "https://pub.dev" + source: hosted + version: "0.6.7" + lints: + dependency: "direct dev" + description: + name: lints + sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + logging: + dependency: transitive + description: + name: logging + sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340" + url: "https://pub.dev" + source: hosted + version: "1.2.0" + matcher: + dependency: transitive + description: + name: matcher + sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + url: "https://pub.dev" + source: hosted + version: "0.12.16" + meta: + dependency: transitive + description: + name: meta + sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + url: "https://pub.dev" + source: hosted + version: "1.9.1" + mime: + dependency: transitive + description: + name: mime + sha256: e4ff8e8564c03f255408decd16e7899da1733852a9110a58fe6d1b817684a63e + url: "https://pub.dev" + source: hosted + version: "1.0.4" + node_preamble: + dependency: transitive + description: + name: node_preamble + sha256: "6e7eac89047ab8a8d26cf16127b5ed26de65209847630400f9aefd7cd5c730db" + url: "https://pub.dev" + source: hosted + version: "2.0.2" + package_config: + dependency: transitive + description: + name: package_config + sha256: "1c5b77ccc91e4823a5af61ee74e6b972db1ef98c2ff5a18d3161c982a55448bd" + url: "https://pub.dev" + source: hosted + version: "2.1.0" + path: + dependency: transitive + description: + name: path + sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + url: "https://pub.dev" + source: hosted + version: "1.8.3" + pool: + dependency: transitive + description: + name: pool + sha256: "20fe868b6314b322ea036ba325e6fc0711a22948856475e2c2b6306e8ab39c2a" + url: "https://pub.dev" + source: hosted + version: "1.5.1" + pub_semver: + dependency: transitive + description: + name: pub_semver + sha256: "40d3ab1bbd474c4c2328c91e3a7df8c6dd629b79ece4c4bd04bee496a224fb0c" + url: "https://pub.dev" + source: hosted + version: "2.1.4" + shelf: + dependency: transitive + description: + name: shelf + sha256: ad29c505aee705f41a4d8963641f91ac4cee3c8fad5947e033390a7bd8180fa4 + url: "https://pub.dev" + source: hosted + version: "1.4.1" + shelf_packages_handler: + dependency: transitive + description: + name: shelf_packages_handler + sha256: "89f967eca29607c933ba9571d838be31d67f53f6e4ee15147d5dc2934fee1b1e" + url: "https://pub.dev" + source: hosted + version: "3.0.2" + shelf_static: + dependency: transitive + description: + name: shelf_static + sha256: a41d3f53c4adf0f57480578c1d61d90342cd617de7fc8077b1304643c2d85c1e + url: "https://pub.dev" + source: hosted + version: "1.1.2" + shelf_web_socket: + dependency: transitive + description: + name: shelf_web_socket + sha256: "9ca081be41c60190ebcb4766b2486a7d50261db7bd0f5d9615f2d653637a84c1" + url: "https://pub.dev" + source: hosted + version: "1.0.4" + source_map_stack_trace: + dependency: transitive + description: + name: source_map_stack_trace + sha256: "84cf769ad83aa6bb61e0aa5a18e53aea683395f196a6f39c4c881fb90ed4f7ae" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + source_maps: + dependency: transitive + description: + name: source_maps + sha256: "708b3f6b97248e5781f493b765c3337db11c5d2c81c3094f10904bfa8004c703" + url: "https://pub.dev" + source: hosted + version: "0.10.12" + source_span: + dependency: transitive + description: + name: source_span + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" + url: "https://pub.dev" + source: hosted + version: "1.10.0" + stack_trace: + dependency: transitive + description: + name: stack_trace + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" + url: "https://pub.dev" + source: hosted + version: "1.11.1" + stream_channel: + dependency: transitive + description: + name: stream_channel + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 + url: "https://pub.dev" + source: hosted + version: "2.1.2" + string_scanner: + dependency: transitive + description: + name: string_scanner + sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" + url: "https://pub.dev" + source: hosted + version: "1.2.0" + term_glyph: + dependency: transitive + description: + name: term_glyph + sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84 + url: "https://pub.dev" + source: hosted + version: "1.2.1" + test: + dependency: "direct dev" + description: + name: test + sha256: "67ec5684c7a19b2aba91d2831f3d305a6fd8e1504629c5818f8d64478abf4f38" + url: "https://pub.dev" + source: hosted + version: "1.24.4" + test_api: + dependency: transitive + description: + name: test_api + sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" + url: "https://pub.dev" + source: hosted + version: "0.6.1" + test_core: + dependency: transitive + description: + name: test_core + sha256: "6b753899253c38ca0523bb0eccff3934ec83d011705dae717c61ecf209e333c9" + url: "https://pub.dev" + source: hosted + version: "0.5.4" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + url: "https://pub.dev" + source: hosted + version: "1.3.2" + vm_service: + dependency: transitive + description: + name: vm_service + sha256: ada49637c27973c183dad90beb6bd781eea4c9f5f955d35da172de0af7bd3440 + url: "https://pub.dev" + source: hosted + version: "11.8.0" + watcher: + dependency: transitive + description: + name: watcher + sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8" + url: "https://pub.dev" + source: hosted + version: "1.1.0" + web_socket_channel: + dependency: transitive + description: + name: web_socket_channel + sha256: d88238e5eac9a42bb43ca4e721edba3c08c6354d4a53063afaa568516217621b + url: "https://pub.dev" + source: hosted + version: "2.4.0" + webkit_inspection_protocol: + dependency: transitive + description: + name: webkit_inspection_protocol + sha256: "67d3a8b6c79e1987d19d848b0892e582dbb0c66c57cc1fef58a177dd2aa2823d" + url: "https://pub.dev" + source: hosted + version: "1.2.0" + yaml: + dependency: transitive + description: + name: yaml + sha256: "75769501ea3489fca56601ff33454fe45507ea3bfb014161abc3b43ae25989d5" + url: "https://pub.dev" + source: hosted + version: "3.1.2" +sdks: + dart: ">=3.0.3 <3.3.0" diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..9d37cee --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,15 @@ +name: project1 +description: A sample command-line application. +version: 1.0.0 +# repository: https://github.com/my_org/my_repo + +environment: + sdk: ^3.0.3 + +# Add regular dependencies here. +dependencies: + # path: ^1.8.0 + +dev_dependencies: + lints: ^2.0.0 + test: ^1.21.0 From 95306cccea02a4f5dfdc71df1e0e6769e41328ca Mon Sep 17 00:00:00 2001 From: dev one Date: Sun, 30 Jul 2023 20:56:32 +0300 Subject: [PATCH 2/2] Update my project files and add a README content --- README.md | 143 +++++++++++- bin/admin_func.dart | 225 ++++++++++++++++++ bin/data.dart | 2 +- bin/main.dart | 27 +++ bin/project1.dart | 527 ------------------------------------------- bin/shared_func.dart | 75 ++++++ bin/user_func.dart | 215 ++++++++++++++++++ 7 files changed, 684 insertions(+), 530 deletions(-) create mode 100644 bin/admin_func.dart create mode 100644 bin/main.dart delete mode 100644 bin/project1.dart create mode 100644 bin/shared_func.dart create mode 100644 bin/user_func.dart diff --git a/README.md b/README.md index 3816eca..ecebf4c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,141 @@ -A sample command-line application with an entrypoint in `bin/`, library code -in `lib/`, and example unit test in `test/`. + +# Library project using Dart + +A simple book store that allow both customr or library staff to do different actions. + +**In my progrme the customr can do the following:** +- Search for a Book +- Buy a Book +- Display details about each book +- Display available books categories +- Exite the program + +**Library staff/admin can do the following actions:** +- Add new Book to the store +- Modifiy on existing Book 'Delete or Update' +- Display the number of books in the library +- Check the number of Purches + +"**Both kind of user can switch to the other one**" + +## Project workflow +At first the program will ask the user if he is Admin/Staff or Customer by entering a specific choice. Also the user can end the program by typing 'E or e'. + +- **Following image show the beginning of the program** + + The beginning of the program + +### Admin/Staff side: ### + +- **When the user enter the program as an Admin/Staff the following menu will displayed:** +
Admin choices + + + +- **If the Admin/Staff need to add a new book, the following instructions will appear, which ask the user to enter the new book specifications** +
Screenshot 2023-07-30 180220 + + +- **Then the new book will appear among the list of stored books, as the following:** +
![After adding a new book](https://github.com/Mjd-Gh/Project-dart-1/assets/101333803/2dfe03dd-f44d-497e-9de7-4d07ddd7c198) + + +- **If the Admin need to modify on the books, admin can do either deleting a book or updating an existing book information** +
Modification choices + + +- **For deleting option, admine need to enter the title of the need to be deleted book as the following:** +
delete a book process + + +- **For updating book information the following fields are required to be entered by the admin:** +
update book info + + +- **Then Admin need to choose the book section to be updated from the menu**
+ - **Edit the title** +
Edit title + + - **Authors** +
Edit authors + + - **Categories** +
Edit category + + - **Description** +
Edit description + + - **Price** +
edit price + + - **Number of copies** +
edit number of copies + + - ***The Book named 'Collective Intelligence in Action' after updating each part:*** +
book after update + +- **Admin can view the number of all books in the store** +
Number of the books in the store + +- **Admin can check the number of purches from the library** +
Number of purches + + +### Customer side: ### +- **When the user enter the program as a Customer the following menu will displayed:** +
Customer menu + +- **If the customer need to search for a book, customer need to determine the search way from the following:** +
search way + + - **Search by the Title** +
search by title + + - **Search by the Author** +
search by author + + - **Search by the Category** +
search by category + + - **If there are no matched books the following messages will appear:** +
No match title +
No match author +
No match category + +- **When the customer need to buy a book the program will display the available books and ask the user 'How many books you want to buy?** +
how many books user want + + - **Then the user have to enter books names** +
needed books names + - **After that, a confirmation message will appear to the user to confirm the purchase** +
user confirmation + + - **Then the program will display an invoice with all purchases information and total cost** +
invoice info + +- **Customer can view all books in the store** +
Display all books in the store + +- **Customer can check all available categories** +
all categories + +### Common functions ## +- **Switch to othrt user type** + - Admin can enter customer side and vice versa +
switch user + +- **Exit from the program** +
Exit choice + + +### Parts to be noticed ### +- **The number of books and copies will changed after the following processes:** + - Adding new books with a specific number of copies "Done by Admin" + - Deleting a books with its whole copies "Done by Admin" + - Buying a books from the library "Done by Customer" + +- **If there is no existing book or the user enter a wrong option alerting message will appear** + - For example: + - No found book with a specific searched title, author, or category + - No enough copies to sale + - Wrong chosen option \ No newline at end of file diff --git a/bin/admin_func.dart b/bin/admin_func.dart new file mode 100644 index 0000000..25c298e --- /dev/null +++ b/bin/admin_func.dart @@ -0,0 +1,225 @@ +import 'dart:io'; +import 'data.dart'; +import 'shared_func.dart'; +import 'user_func.dart'; + + +admin(){ + while(true){ + //Display available actions + print("\nChoose an action: "); + print("\t1- Add new Book"); + print("\t2- Modifiy on existing Book 'Delete or Update' "); + print("\t3- Number of books in the library"); + print("\t4- Number of Purches"); + print("\t5- Change to user side"); + print("\t6- Exit"); + + //Ask admine to enter his choic + print("\nEnter your choice: "); + int selection = int.parse(stdin.readLineSync()!); + + switch(selection){ + case 1: + addNewBook(); //If admin need to add new book -> Call addNewBook() function + break; + + case 2: + modifyBook(); //If admin/staff want to delete a book -> Call deleteBook() function + break; + + case 3: + //show the number of books by calling bookCount variable from Books class + print("\tThere are ${bookInStore()} books found in the library\n"); + break; + + case 4: + //Show the number of pruches that done by the user + print("\t\n There are ${Books.pruchaseNumber} pruches done from the library"); + break; + + case 5: + //Switch to user menu + user(); + break; + + case 6: + exit(0);// Otherwise exit form program + + default: + print("\n\tSorry, something went wrong! ");// For unpredictable actions print msg + } + } +} + +// Adding new books function: +addNewBook(){ + //Take the new book info from the admin user + print("\nBook Title: "); + String newTitle = stdin.readLineSync()!; + + print("\nGive a brife description about the book:"); + String descrip = stdin.readLineSync()!; + + print("\nHow many copeis you want to add? "); + int copyNum = int.parse(stdin.readLineSync()!); + + print("\nHow much does it cost? "); + int price = int.parse(stdin.readLineSync()!); + + print("\nNumber of Authors: "); + int numContrib = int.parse(stdin.readLineSync()!); + + print("\nEnter AUTHORS name: 'Hit enter after each name' "); + List contrib = addItemLoop(numContrib); //Enter names depending on the number owner want + + print("\nNumber of Related Categories: "); + int numCategory = int.parse(stdin.readLineSync()!); + + print("\nEnter related CATEGORIES: 'Hit enter after each name' "); + List typs = addItemLoop(numCategory); //Enter names depending on the number owner want + + //then store all collected information as a map + final newBook = { + "title": newTitle, + "authors": contrib, + "categories": typs, + 'price': price, + 'description': descrip, + "copies": copyNum + }; + + myData.add(newBook);// add the new info to myData + // Books.bookCount ++; + displayItem();// then display all books with new ones +} + + +//A function to add any new items as a list 'its for adding multiple Authors or Categories +addItemLoop(int numberOfItems){ + List list = []; //Empty list to store info + int counter = 0;// loop counter + while(counter < numberOfItems){ //loop until the counter == to number of items + String named = stdin.readLineSync()!; // Read Author/Category name + list.add(named); //Add name to the list + counter++;//increas counter + } + return list; //Return the list +} + + +//Delete or Update on the books function +modifyBook(){ + print("\nFor deleting a book enter 'D or d', for update book information enter 'U or u':"); + String selection = stdin.readLineSync()!.toLowerCase(); + String choice; + + //Determaine the choice of the user to print sutiable msg + if(selection == 'd'){ + choice = "Deleted"; + } + else{ + choice = "Updated"; + } + //If the user choose a selection print a msg + if(choice.isNotEmpty){ + // Ask the user to enter the needed title + print("\nPleas enter book title to be $choice: "); + String title = stdin.readLineSync()!.toLowerCase(); + + int exist = 0;//for a future msg + + //Search for matched title in myData file + for(int i = 0 ; i < myData.length ; i++){ + if(myData[i]["title"].toString().toLowerCase() == title){ + if(selection == 'd'){ + //if the user want to delete a book -> remove it from the list + myData.remove(myData[i]); + // Books.bookCount -= myData[i]['copies']; //Decreas the number of books and print a confirmation msg + print("\n\tBook with title '$title' removed successfully "); + displayItem(); // then display all books to show the changes + } + else if(selection == 'u'){ + //if the user want to update a book -> call update function + updateBook(i); + displayItem(); // then display all books to show the changes + } + exist++; + } + } + + + if(exist == 0){ //check if there is any changes by checking the number of counter + print("\nSorry there is no book with title '$title' in the library :("); + }else{ + print("\n\tModification succeeded"); // print confirmation msg + } + + } + else{ + print("\n\tSorry there are no entered choice!!");// Show alert msg if there is no selection + } + +} + +//Updating function +updateBook(int index){ //take the direct index of the item in myData list + //Ask the user for the needed part to be update by displaying a menu + print("\nWhat do you want to modify? \n"); + print("\t1- Title"); + print("\t2- Authors"); + print("\t3- Categories"); + print("\t4- Description"); + print("\t5- Price"); + print("\t6- Number of copies"); + print("Enter yor choice:"); + int choice = int.parse(stdin.readLineSync()!); + + //Go in a case depending on the choice + switch(choice){ + case 1: + print("Enter new title"); + String newTitle = stdin.readLineSync()!; + //change the title by assigning the new one dirctly + myData[index]['title'] = newTitle; + break; + + case 2: + print("Enter the number of Authors"); + int authors = int.parse(stdin.readLineSync()!);// Enter the number of authors + print("Enter AUTHORS name: 'Hit enter after each name' "); + List contrib = addItemLoop(authors); //Enter names depending on the number owner want + myData[index]['authors'] = contrib;//Assigning the authors insted of registerd one + break; + + case 3: + print("Enter the number of categories"); + int categ = int.parse(stdin.readLineSync()!);// Enter the number of categories + print("Enter CATEGORIES name: 'Hit enter after each name' "); + List type = addItemLoop(categ); //Enter names depending on the number owner want + myData[index]['categories'] = type; //Assigning the category insted of registerd one + break; + + case 4: + print("Enter new description"); + String descrip = stdin.readLineSync()!; + myData[index]['description'] = descrip; //Assigning the new description + break; + + case 5: + print("Enter new price"); + int price = int.parse(stdin.readLineSync()!); + myData[index]['price'] = price;//Assigning the new price + break; + + case 6: + print("Enter new numbers of copies"); + int copy = int.parse(stdin.readLineSync()!); + myData[index]['copies'] = copy; //Assigning the new number of copies + break; + + default: + print("Wrong choice!!"); + } + +} diff --git a/bin/data.dart b/bin/data.dart index 81c4b1a..417f55d 100644 --- a/bin/data.dart +++ b/bin/data.dart @@ -37,7 +37,7 @@ final List myData = [ "description" : "hands-on guidebook for implementing collective intelligence concepts using Java.", "authors": ["Satnam Alag"], "categories": ["Internet"], - "copies" : 1, + "copies" : 3, "price" : 56 }, { diff --git a/bin/main.dart b/bin/main.dart new file mode 100644 index 0000000..68bcce0 --- /dev/null +++ b/bin/main.dart @@ -0,0 +1,27 @@ +import 'admin_func.dart'; +import 'user_func.dart'; +import 'dart:io'; + + +void main(List args) { + try{ + //Check who want to do a process, Library admin or Customer? + print("\nFor Admin enter 'A or a', for Customr enter 'C or c', and 'E or e' to exit"); + String choice = stdin.readLineSync()!.toLowerCase(); + //when Admine want to make an action + if(choice == 'a'){ + admin(); + } + else if(choice == 'c'){ + user(); + } + else{ + exit(0); + } + + }catch(error){ + print("ERROR, somthing went wrong!"); + } + + +} \ No newline at end of file diff --git a/bin/project1.dart b/bin/project1.dart deleted file mode 100644 index fdec23e..0000000 --- a/bin/project1.dart +++ /dev/null @@ -1,527 +0,0 @@ -import 'data.dart'; -import 'dart:io'; - -void main(List args) { - try{ - //Check who want to do a process, Library admin or Customer? - print("For Admin enter 'A or a', for Customr enter 'C or c', and 'E or e' to exit"); - String choice = stdin.readLineSync()!.toLowerCase(); - //when Admine want to make an action - if(choice == 'a'){ - admin(); - } - else if(choice == 'c'){ - user(); - } - else{ - exit(0); - } - - }catch(error){ - print("ERROR, somthing went wrong!"); - } - - -} - -//Books class -class Books{ - late String? title; - late List? authors; - late List? categories; - late int? price; - late String? descrip; //Book description - late int? copies; - static int bookCount = bookInStore();// The number of the books - static int pruchaseNumber = 0; //number of paid books - static List? invoice; - - //Class Book constractor - Books({ - this.title , - this.authors, - this.categories, - this.price, - this.descrip, - this.copies - }); - - //Method to display an information about any book - displayData() { - print("Title:$title, Authours: $authors, \nCategories: $categories, Copies: $copies Price: $price\nDescription: $descrip"); - } -} - - - - - -//Functions: - -//Function for searching for a specific book, it will take the way to search from the user & info about title, author, or category -searchBy(String wayToSearch, String info){ - List allBooks = []; //List to store all books realted to Author, Title, or Category - int? copy = 0; //To check for any existing copies - - if(wayToSearch == 'title'){ // check if the user will search by book title - for(int i =0 ; i < myData.length ; i++){ //enter a loop for searching im myData - if((myData[i]['title']).toString().toLowerCase() == info.toLowerCase()){ //Ignore the case in searching to return accurate info - allBooks.add(myData[i]['title']);// if there is any matching store the book in a list - copy = myData[i]['copies']; //and get the number of copies - } - } - return ("There are $copy copies of Book named '$info'"); //Info msg - } - - else{// if user search by either Author or Categories - for (int i =0 ; i< myData.length ; i++) { - List temp = myData[i][wayToSearch]; //Store each list insid Category or Author list as a temprary list - if((temp.toString().toLowerCase()).contains(info.toLowerCase())){//Check if its containe the searched info - allBooks.add(myData[i]['title']); - } - } - - if(allBooks.isEmpty){ - return ("Sorry, there is no found book :( ");// If there are no book found, the list will be empty - }else{ - return ("Books with similer $wayToSearch are : $allBooks");//Otherwise show an information about the book - } - - } - -} - - -// Adding new books function: -addNewBook(){ - //Take the new book info from the admin user - print("Book Title: "); - String newTitle = stdin.readLineSync()!; - - print("Give a brife description about the book:"); - String descrip = stdin.readLineSync()!; - - print("How many copeis you want to add? "); - int copyNum = int.parse(stdin.readLineSync()!); - - print("How much does it cost? "); - int price = int.parse(stdin.readLineSync()!); - - print("Number of Authors: "); - int numContrib = int.parse(stdin.readLineSync()!); - - print("Enter AUTHORS name: 'Hit enter after each name' "); - List contrib = addItemLoop(numContrib); //Enter names depending on the number owner want - - print("Number of Related Categories: "); - int numCategory = int.parse(stdin.readLineSync()!); - - print("Enter related CATEGORIES: 'Hit enter after each name' "); - List typs = addItemLoop(numCategory); //Enter names depending on the number owner want - - //then store all collected information as a map - final newBook = { - "title": newTitle, - "authors": contrib, - "categories": typs, - 'price': price, - 'description': descrip, - "copies": copyNum - }; - - myData.add(newBook);// add the new info to myData - Books.bookCount ++; - displayItem();// then display all books with new ones -} - - -//A function to add any new items as a list 'its for adding multiple Authors or Categories -addItemLoop(int numberOfItems){ - List list = []; //Empty list to store info - int counter = 0;// loop counter - while(counter < numberOfItems){ //loop until the counter == to number of items - String named = stdin.readLineSync()!; // Read Author/Category name - list.add(named); //Add name to the list - counter++;//increas counter - } - return list; //Return the list -} - -//A function to display all books in the store -displayItem() { - List listItem = []; // Create an empty list with type BOOKS - for (var item in myData) { //loop for retrieving info from myData file - - //Add a new object form book class each time, until finishing all stored books in data file - listItem.add(Books( - title: item["title"], - authors: item["authors"], - categories: item["categories"], - descrip: item["description"], - price: item['price'], - copies: item["copies"])); - } - - for (var item in listItem) { //Go in another loop to display all stored data - print("____________"); - item.displayData(); //call method from Books class - } -} - -//Delete or Update on the books function -modifyBook(){ - print("\nFor deleting a book enter 'D or d', for update book information enter 'U or u':"); - String selection = stdin.readLineSync()!.toLowerCase(); - String choice; - - //Determaine the choice of the user to print sutiable msg - if(selection == 'd'){ - choice = "Deleted"; - } - else{ - choice = "Updated"; - } - //If the user choose a selection print a msg - if(choice.isNotEmpty){ - // Ask the user to enter the needed title - print("\nPleas enter book title to be $choice: "); - String title = stdin.readLineSync()!.toLowerCase(); - - int exist = 0;//for a future msg - - //Search for matched title in myData file - for(int i = 0 ; i < myData.length ; i++){ - if(myData[i]["title"].toString().toLowerCase() == title){ - if(selection == 'd'){ - //if the user want to delete a book -> remove it from the list - myData.remove(myData[i]); - Books.bookCount --; //Decreas the number of books and print a confirmation msg - print("\n\tBook with title '$title' removed successfully "); - displayItem(); // then display all books to show the changes - } - else if(selection == 'u'){ - //if the user want to update a book -> call update function - updateBook(i); - displayItem(); // then display all books to show the changes - } - exist++; - } - } - - - if(exist == 0){ //check if there is any changes by checking the number of counter - print("\nSorry there is no book with title '$title' in the library :("); - }else{ - print("\n\tModification succeeded"); // print confirmation msg - } - - } - else{ - print("\n\tSorry there are no entered choice!!");// Show alert msg if there is no selection - } - -} - -//Updating function -updateBook(int index){ //take the direct index of the item in myData list - //Ask the user for the needed part to be update by displaying a menu - print("\nWhat do you wand to modify? \n"); - print("\t1- Title"); - print("\t2- Authors"); - print("\t3- Categories"); - print("\t4- Description"); - print("\t5- Price"); - print("\t6- Number of copies"); - print("Enter yor choice:"); - int choice = int.parse(stdin.readLineSync()!); - - //Go in a case depending on the choice - switch(choice){ - case 1: - print("Enter new title"); - String newTitle = stdin.readLineSync()!; - //change the title by assigning the new one dirctly - myData[index]['title'] = newTitle; - break; - - case 2: - print("Enter the number of Authors"); - int authors = int.parse(stdin.readLineSync()!);// Enter the number of authors - print("Enter AUTHORS name: 'Hit enter after each name' "); - List contrib = addItemLoop(authors); //Enter names depending on the number owner want - myData[index]['authors'] = contrib;//Assigning the authors insted of registerd one - break; - - case 3: - print("Enter the number of categories"); - int categ = int.parse(stdin.readLineSync()!);// Enter the number of categories - print("Enter CATEGORIES name: 'Hit enter after each name' "); - List type = addItemLoop(categ); //Enter names depending on the number owner want - myData[index]['categories'] = type; //Assigning the category insted of registerd one - break; - - case 4: - print("Enter new description"); - String descrip = stdin.readLineSync()!; - myData[index]['description'] = descrip; //Assigning the new description - break; - - case 5: - print("Enter new price"); - int price = int.parse(stdin.readLineSync()!); - myData[index]['price'] = price;//Assigning the new price - break; - - case 6: - print("Enter new numbers of copies"); - int copy = int.parse(stdin.readLineSync()!); - myData[index]['copies'] = copy; //Assigning the new number of copies - break; - - default: - print("Wrong choice!!"); - } - -} - -//Buying a book function -purchase(){ - List myBooks = []; //Create an empty book to store chosen books - bool flag = false; - //Show the avilable books - print("Avilable Books in the store:"); - displayItem(); - - print("\nHow many books you want to buy?"); - int bookToBuy = int.parse(stdin.readLineSync()!); //Ask for the number of needed books - - List index = []; //store the index of each needed book to facilitate future operations - int counter = 0; - while(counter < bookToBuy){ //loop untile counter become less than number of books - print("\nEnter the ${counter+1} book title: ");//ask the user to enter the title of each needed book - String title = stdin.readLineSync()!; - - for(int i = 0 ; i < myData.length ; i++){ - if(myData[i]["title"] == title){///find the match title from myData - index.add(i);//store there indexes - myBooks.add(title); //add the book need to buy - } - } - counter++; - } - - //check if the user are sure about buying the books - print("To confirm buying chosen books enter 'C or c', else 'N or n'"); - String confirmation = stdin.readLineSync()!.toLowerCase(); - - if(confirmation == 'c'){ - //when order confirmed start reducing the number of copyies - for(int x = 0; x < index.length; x++){ - if( myData[index[x]]['copies'] > 0){//At first check if there are enough copies - myData[index[x]]['copies']--; - Books.pruchaseNumber++;//Increase the pruches process - flag = true; - - }else{ - print("Sorry, no enough copies to sale for ${myData[index[x]]['title']}"); //Show msg if no found copies - } - } - - if(flag){ - print("\nPurchase completed"); //Show confirmation msg - invoice(myBooks);// Preduce an invoice for the user - } - - } - else{ - return; //Return if user not confirm - } -} - -//Function to count all existing books number -bookInStore(){ - num allBooks = 0; - for(int i = 0; i < myData.length; i++){ - allBooks += myData[i]['copies'];//Summ all copies number - } - return allBooks; // return the number of copies -} - -//Creat an invoice function -invoice(List myInvoice){ - //Print the structure of the invoice - print("\n\tYOUR INVOICE"); - print("BOOKS \t\t\tPRICE\n"); - - num totalPrice = 0;//to store the summtion of all prices - List price = [];// to store each price - - for(int i = 0 ; i < myInvoice.length; i++){ - for(int j = 0 ; j < myData.length; j++){ - String bookName = myInvoice[i];//collect all the needed books in a list - int bookPrice = myData[j]['price']; //get each book price - if(bookName == myData[j]['title']){ //if the needed book found - price.add(bookPrice);//take its price and add it to the list - print('$bookName, \t$bookPrice');//show each book info individually - print("___________"); - } - } - } - - //Sum the total price - for (num i in price) { - totalPrice += i; - } - - print("Total payment: $totalPrice");//Show the total -} - - - -admin(){ - while(true){ - //Display available actions - print("\nChoose an action: "); - print("\t1- Add new Book"); - print("\t2- Modifiy on existing Book 'Delete or Update' "); - print("\t3- Number of books in the library"); - print("\t4- Number of Purches"); - print("\t5- Change to user side"); - print("\t6- Exit"); - - //Ask admine to enter his choic - print("\nEnter your choice: "); - int selection = int.parse(stdin.readLineSync()!); - - switch(selection){ - case 1: - addNewBook(); //If admin need to add new book -> Call addNewBook() function - break; - - case 2: - modifyBook(); //If admin/staff want to delete a book -> Call deleteBook() function - break; - - case 3: - //show the number of books by calling bookCount variable from Books class - print("\tThere are ${Books.bookCount} books found in the library\n"); - break; - - case 4: - //Show the number of pruches that done by the user - print("\t\n There are ${Books.pruchaseNumber} pruches done from the library"); - break; - - case 5: - //Switch to user menu - user(); - break; - - case 6: - exit(0);// Otherwise exit form program - - default: - print("\n\tSorry, something went wrong! ");// For unpredictable actions print msg - } - } -} - -user(){ - while(true){ - //Display available actions - print("\nChoose from menu: "); - print("\t1- Search for a Book"); - print("\t2- Buy a Book"); - print("\t3- Display all Books info"); - print("\t4- Display all categories"); - print("\t5- Change to admin side"); - print("\t6- Exit"); - print("\nEnter your choice number: "); - int selection = int.parse(stdin.readLineSync()!); //take the selection - String info;// will be used later - - switch(selection){ - case 1: - //Display all avilable ways to search for a book - print("\nI will search by: "); - print("\t1- Title"); - print("\t2- Author"); - print("\t3- Category"); - print("\t4- Cancel & Exit"); - print("\nChoice: "); - int searchWay = int.parse(stdin.readLineSync()!); - - switch(searchWay){ - case 1: - //Take the book title info - print("\nEnter book title: "); - info = stdin.readLineSync()!; - print(searchBy('title',info)); //call search function - break; - - case 2: - //Take the book authors info - print("\nEnter book author: "); - info = stdin.readLineSync()!; - print(searchBy('authors',info));//call search function - break; - - case 3: - //Take the book categories info - print("\nEnter book category: "); - info = stdin.readLineSync()!; - print(searchBy('categories',info));//call search function - break; - - case 4: - exit(0);//end program - - default: - print("\n\tSorry, something went wrong! "); - } - break; - - case 2: - //Call purchase function to buy a book - purchase(); - break; - - case 3: - //Display all item in library by calling displayItem function - displayItem(); - break; - - case 4: - //Show all avilable category using allCategory function - print(allCategory()); - break; - - case 5: - //Switch to admin/staff menu - admin(); - break; - - case 6: - exit(0); - - default: - print("\n\tSorry, something went wrong! "); - - } - } -} - -//Extra -allCategory(){ - List allType = []; //List to store all categories - - for (int i =0 ; i< myData.length ; i++) { - List temp = myData[i]['categories']; //Store each list insid Category list - - for(int j = 0 ; j < temp.length ; j++){//Another loop for the inner list - allType.add(temp[j]); // Add each element insted of adding a list - } - } - return allType.toSet(); //Return list as a set to remove duplication -} - diff --git a/bin/shared_func.dart b/bin/shared_func.dart new file mode 100644 index 0000000..c545cd2 --- /dev/null +++ b/bin/shared_func.dart @@ -0,0 +1,75 @@ +import 'data.dart'; + +//Books class +class Books{ + late String? title; + late List? authors; + late List? categories; + late int? price; + late String? descrip; //Book description + late int? copies; + // static int bookCount = bookInStore();// The number of the books + static int pruchaseNumber = 0; //number of paid books + static List? invoice; + + //Class Book constractor + Books({ + this.title , + this.authors, + this.categories, + this.price, + this.descrip, + this.copies + }); + + //Method to display an information about any book + displayData() { + print("Title:$title, Authours: $authors, \nCategories: $categories, Copies: $copies Price: $price\nDescription: $descrip"); + } +} + +//A function to display all books in the store +displayItem() { + List listItem = []; // Create an empty list with type BOOKS + for (var item in myData) { //loop for retrieving info from myData file + + //Add a new object form book class each time, until finishing all stored books in data file + listItem.add(Books( + title: item["title"], + authors: item["authors"], + categories: item["categories"], + descrip: item["description"], + price: item['price'], + copies: item["copies"])); + } + + for (var item in listItem) { //Go in another loop to display all stored data + print("____________"); + item.displayData(); //call method from Books class + } +} + + +//Function to count all existing books number +bookInStore(){ + num allBooks = 0; + for(int i = 0; i < myData.length; i++){ + allBooks += myData[i]['copies'];//Summ all copies number + } + return allBooks; // return the number of copies +} + + +//Extra +allCategory(){ + List allType = []; //List to store all categories + + for (int i =0 ; i< myData.length ; i++) { + List temp = myData[i]['categories']; //Store each list insid Category list + + for(int j = 0 ; j < temp.length ; j++){//Another loop for the inner list + allType.add(temp[j]); // Add each element insted of adding a list + } + } + return allType.toSet(); //Return list as a set to remove duplication +} diff --git a/bin/user_func.dart b/bin/user_func.dart new file mode 100644 index 0000000..a934233 --- /dev/null +++ b/bin/user_func.dart @@ -0,0 +1,215 @@ +import 'dart:io'; +import 'data.dart'; +import 'admin_func.dart'; +import 'shared_func.dart'; + + +user(){ + while(true){ + //Display available actions + print("\nChoose from menu: "); + print("\t1- Search for a Book"); + print("\t2- Buy a Book"); + print("\t3- Display all Books info"); + print("\t4- Display all categories"); + print("\t5- Change to admin side"); + print("\t6- Exit"); + print("\nEnter your choice number: "); + int selection = int.parse(stdin.readLineSync()!); //take the selection + String info;// will be used later + + switch(selection){ + case 1: + //Display all avilable ways to search for a book + print("\nI will search by: "); + print("\t1- Title"); + print("\t2- Author"); + print("\t3- Category"); + print("\t4- Cancel & Exit"); + print("\nChoice: "); + int searchWay = int.parse(stdin.readLineSync()!); + + switch(searchWay){ + case 1: + //Take the book title info + print("\nEnter book title: "); + info = stdin.readLineSync()!; + print(searchBy('title',info)); //call search function + break; + + case 2: + //Take the book authors info + print("\nEnter book author: "); + info = stdin.readLineSync()!; + print(searchBy('authors',info));//call search function + break; + + case 3: + //Take the book categories info + print("\nEnter book category: "); + info = stdin.readLineSync()!; + print(searchBy('categories',info));//call search function + break; + + case 4: + exit(0);//end program + + default: + print("\n\tSorry, something went wrong! "); + } + break; + + case 2: + //Call purchase function to buy a book + purchase(); + break; + + case 3: + //Display all item in library by calling displayItem function + displayItem(); + break; + + case 4: + //Show all avilable category using allCategory function + print(allCategory()); + break; + + case 5: + //Switch to admin/staff menu + admin(); + break; + + case 6: + exit(0); + + default: + print("\n\tSorry, something went wrong! "); + + } + } +} + +//Function for searching for a specific book, it will take the way to search from the user & info about title, author, or category +searchBy(String wayToSearch, String info){ + List allBooks = []; //List to store all books realted to Author, Title, or Category + int? copy = 0; //To check for any existing copies + + if(wayToSearch == 'title'){ // check if the user will search by book title + for(int i =0 ; i < myData.length ; i++){ //enter a loop for searching im myData + if((myData[i]['title']).toString().toLowerCase() == info.toLowerCase()){ //Ignore the case in searching to return accurate info + allBooks.add(myData[i]['title']);// if there is any matching store the book in a list + copy = myData[i]['copies']; //and get the number of copies + } + } + return ("There are $copy copies of Book named '$info'"); //Info msg + } + + else{// if user search by either Author or Categories + for (int i =0 ; i< myData.length ; i++) { + List temp = myData[i][wayToSearch]; //Store each list insid Category or Author list as a temprary list + if((temp.toString().toLowerCase()).contains(info.toLowerCase())){//Check if its containe the searched info + allBooks.add(myData[i]['title']); + } + } + + if(allBooks.isEmpty){ + return ("Sorry, there is no found book :( ");// If there are no book found, the list will be empty + }else{ + return ("Books with similer $wayToSearch are : $allBooks");//Otherwise show an information about the book + } + + } + +} + + + + + +//Buying a book function +purchase(){ + List myBooks = []; //Create an empty book to store chosen books + bool flag = false; + //Show the avilable books + print("Avilable Books in the store:"); + displayItem(); + + print("\nHow many books you want to buy?"); + int bookToBuy = int.parse(stdin.readLineSync()!); //Ask for the number of needed books + + List index = []; //store the index of each needed book to facilitate future operations + int counter = 0; + while(counter < bookToBuy){ //loop untile counter become less than number of books + print("\nEnter the ${counter+1} book title: ");//ask the user to enter the title of each needed book + String title = stdin.readLineSync()!.toLowerCase(); + + for(int i = 0 ; i < myData.length ; i++){ + if(myData[i]["title"].toString().toLowerCase() == title){///find the match title from myData + index.add(i);//store there indexes + myBooks.add(title); //add the book need to buy + } + } + counter++; + } + + //check if the user are sure about buying the books + print("To confirm buying chosen books enter 'C or c', else 'N or n'"); + String confirmation = stdin.readLineSync()!.toLowerCase(); + + if(confirmation == 'c'){ + //when order confirmed start reducing the number of copyies + for(int x = 0; x < index.length; x++){ + if( myData[index[x]]['copies'] > 0){//At first check if there are enough copies + myData[index[x]]['copies']--; + Books.pruchaseNumber++;//Increase the pruches process + flag = true; + + }else{ + print("Sorry, no enough copies to sale for ${myData[index[x]]['title']}"); //Show msg if no found copies + } + } + + if(flag){ + print("\nPurchase completed"); //Show confirmation msg + invoice(myBooks);// Preduce an invoice for the user + }else{ + print("\nNo found book"); + } + + } + else{ + return; //Return if user not confirm + } +} + + +//Creat an invoice function +invoice(List myInvoice){ + //Print the structure of the invoice + print("\n\tYOUR INVOICE"); + print("BOOKS \t\t\tPRICE\n"); + + num totalPrice = 0;//to store the summtion of all prices + List price = [];// to store each price + + for(int i = 0 ; i < myInvoice.length; i++){ + for(int j = 0 ; j < myData.length; j++){ + String bookName = myInvoice[i].toString().toLowerCase();//collect all the needed books in a list + int bookPrice = myData[j]['price']; //get each book price + if(bookName == (myData[j]['title']).toString().toLowerCase()){ //if the needed book found + price.add(bookPrice);//take its price and add it to the list + print('$bookName, \t$bookPrice');//show each book info individually + print("___________"); + } + } + } + + //Sum the total price + for (num i in price) { + totalPrice += i; + } + + print("Total payment: $totalPrice");//Show the total +} + +