public static void LineDelete(Book[] myBooks) { Console.WriteLine("Enter the ISBN of a book you'd like to remove from inventory (book file): "); int choice = int.Parse(Console.ReadLine()); int indexFound = BookUtility.BinarySearch(myBooks, choice); string path2 = "books.txt"; File.WriteAllText(path2, String.Empty); TextWriter tW2 = new StreamWriter(path2, true); tW2.Close(); myBooks.ToString(); using (StreamWriter sW = File.AppendText("books.txt")) for (int i = 0; i < indexFound; i++) { sW.WriteLine(myBooks[i].GetISBN() + "#" + myBooks[i].GetTitle() + "#" + myBooks[i].GetAuthor() + "#" + myBooks[i].GetGenre() + "#" + myBooks[i].GetListeningTime() + "#" + myBooks[i].GetStatus() + "#" + myBooks[i].GetBookCount()); } using (StreamWriter outFile = File.AppendText("books.txt")) for (int j = indexFound + 1; j < GetCount(); j++) { outFile.WriteLine(myBooks[j].GetISBN() + "#" + myBooks[j].GetTitle() + "#" + myBooks[j].GetAuthor() + "#" + myBooks[j].GetGenre() + "#" + myBooks[j].GetListeningTime() + "#" + myBooks[j].GetStatus() + "#" + myBooks[j].GetBookCount()); } Console.WriteLine("Updated book file:\n"); for (int i = 0; i < indexFound; i++) { Console.WriteLine(myBooks[i].GetISBN() + "#" + myBooks[i].GetTitle() + "#" + myBooks[i].GetAuthor() + "#" + myBooks[i].GetGenre() + "#" + myBooks[i].GetListeningTime() + "#" + myBooks[i].GetStatus() + "#" + myBooks[i].GetBookCount()); } for (int j = indexFound + 1; j < GetCount(); j++) { Console.WriteLine(myBooks[j].GetISBN() + "#" + myBooks[j].GetTitle() + "#" + myBooks[j].GetAuthor() + "#" + myBooks[j].GetGenre() + "#" + myBooks[j].GetListeningTime() + "#" + myBooks[j].GetStatus() + "#" + myBooks[j].GetBookCount()); } }
public static void BookReturn(Transactions[] myTransactions, Book[] myBooks) { //return book Console.WriteLine("\nEnter the ISBN of the book to be returned: "); //search user return ISBN in transaction file int tempTranISBN = int.Parse(Console.ReadLine()); Console.WriteLine("\nEnter the customer's email: "); string tempEmail = Console.ReadLine(); //nested for loop to search whole array, checking email with i and ISBN with j for (int i = 0; i < Transactions.GetTranCount(); i++) { for (int j = 0; j < Transactions.GetTranCount(); j++) { //check if user input email equals any email on the array if (tempEmail == myTransactions[i].GetCustomerEmail()) { //if the email is found, check if the user ISBN equals that instance if (tempTranISBN == myTransactions[j].GetTranISBN()) { //if the email found and ISBN found match each other's instances, replace the return date if (i == j) { //find the isbn instance in the transaction file and replace the N/A with the current date (date of return) string newReturnDate = DateTime.Now.ToString("MM/dd/yyyy"); myTransactions[i].SetReturnDate(newReturnDate); //take the ISBN of the book returned and search it in the book array, if found, increase the count of copies (since one was returned) int tempISBN = myTransactions[i].GetTranISBN(); int indexFound = BookUtility.BinarySearch(myBooks, tempISBN); int copiesAdd = myBooks[indexFound].GetBookCount() + 1; myBooks[indexFound].SetBookCount(copiesAdd); if (myBooks[indexFound].GetBookCount() > 0) { string status = "Available"; myBooks[indexFound].SetStatus(status); } } } } } } //clear text file and resend updated info string path5 = "transactions.txt"; File.WriteAllText(path5, String.Empty); TextWriter tW5 = new StreamWriter(path5, true); tW5.Close(); ToFile(myTransactions); }
public static Book[] GetBookData() { BookFile data = new BookFile("books.txt"); Book[] myBooks = data.ReadBookData(); Console.WriteLine("Enter book ISBN (-1 to stop): \n"); int ISBN = int.Parse(Console.ReadLine()); while (ISBN != -1) { int indexFound = BookUtility.BinarySearch(myBooks, ISBN); Console.WriteLine(indexFound); if (indexFound == -1) { int bookCount = 1; Console.Write("Enter the title: \n"); string title = Console.ReadLine(); Console.Write("Enter the author: \n"); string author = Console.ReadLine(); Console.Write("Enter the genre: \n"); string genre = Console.ReadLine(); Console.Write("Enter the listening time: \n"); double listeningTime = double.Parse(Console.ReadLine()); string status = "Available"; Book newBook = new Book(ISBN, title, author, genre, listeningTime, status, bookCount); myBooks[GetCount()] = newBook; IncrCount(); Console.Write("Enter book ISBN (- 1 to stop): \n"); ISBN = int.Parse(Console.ReadLine()); } else { int copies = myBooks[indexFound].GetBookCount() + 1; myBooks[indexFound].SetBookCount(copies); ISBN = -1; } } return(myBooks); }
public static Transactions[] GetTransactionData(Book[] myBooks) { Transactions[] myTransactions = new Transactions[200]; Console.WriteLine("Enter the ISBN of the book to be marked as rented (-1 to stop):\n "); int tranISBN = int.Parse(Console.ReadLine()); while (tranISBN != -1) { int indexFound = BookUtility.BinarySearch(myBooks, tranISBN); if (myBooks[indexFound].GetBookCount() > 0) { int copies1 = myBooks[indexFound].GetBookCount() - 1; myBooks[indexFound].SetBookCount(copies1); if (myBooks[indexFound].GetBookCount() == 0) { string status = "Rented"; myBooks[indexFound].SetStatus(status); } Console.WriteLine("Enter the customer's name:\n"); string customerName = Console.ReadLine(); Console.WriteLine("Enter the customer's email:\n"); string customerEmail = Console.ReadLine(); Random rnd = new Random(); int rentID = rnd.Next(); string rentDate = DateTime.Now.ToString("M/d/yyyy"); string returnDate = "N/A"; Transactions newTransactions = new Transactions(rentID, tranISBN, customerName, customerEmail, rentDate, returnDate); myTransactions[Transactions.GetTranCount()] = newTransactions; Transactions.IncrTranCount(); Console.WriteLine("Enter the ISBN of the book to be rented (-1 to stop):\n "); tranISBN = int.Parse(Console.ReadLine()); } else { Console.WriteLine("\nERROR... There are no available copies of this book\n"); GetTransactionData(myBooks); } } return(myTransactions); }
public static Transactions[] GetTransactionData(Book[] myBooks) { //creates transaction array to input books to be marked as rented Transactions[] myTransactions = new Transactions[200]; //show available books Console.WriteLine("\nBooks available for rent:\n\n"); for (int i = 0; i < Book.GetCount(); i++) { if (myBooks[i].GetStatus() == "Available") { Console.WriteLine(myBooks[i]); } } Console.WriteLine("\nPress enter to continue... "); Console.ReadLine(); //get ISBN from user to identify boook being rented Console.WriteLine("Enter the ISBN of the book to be marked as rented (-1 to stop):\n "); int tranISBN = int.Parse(Console.ReadLine()); while (tranISBN != -1) { //search book array for ISBN int indexFound = BookUtility.BinarySearch(myBooks, tranISBN); //if the number of book copies is greater than 0 (there are still books available under this ISBN) if (myBooks[indexFound].GetBookCount() > 0) { //subract one from the number of book copies (one has been rented) int copies1 = myBooks[indexFound].GetBookCount() - 1; myBooks[indexFound].SetBookCount(copies1); //if the copy count now equals zero, mark the status under the book file as rented ie. there are none left if (myBooks[indexFound].GetBookCount() == 0) { string status = "Rented"; myBooks[indexFound].SetStatus(status); } //get rest of renter's info Console.Clear(); Console.WriteLine("\nEnter the customer's name:\n"); string customerName = Console.ReadLine(); Console.WriteLine("\nEnter the customer's email:\n"); string customerEmail = Console.ReadLine(); //rent ID randomized Random rnd = new Random(); int rentID = rnd.Next(); //rent date set to current date, return date set to N/A (per Dr. Saifee's instruction) string rentDate = DateTime.Now.ToString("MM/dd/yyyy"); string returnDate = "N/A"; Transactions newTransactions = new Transactions(rentID, tranISBN, customerName, customerEmail, rentDate, returnDate); myTransactions[Transactions.GetTranCount()] = newTransactions; Transactions.IncrTranCount(); Console.Clear(); Console.WriteLine("Enter the ISBN of the book to be rented (-1 to stop):\n "); tranISBN = int.Parse(Console.ReadLine()); } //if user tries to rent a book that there are no available copies of, error message is presented else { Console.WriteLine("\nERROR... There are no available copies of this book\n"); GetTransactionData(myBooks); } Console.WriteLine("\nBooks rented. Press enter to continue"); Console.ReadLine(); } return(myTransactions); }
public static void Edit(Book[] myBooks) { // edit function Console.WriteLine("Enter the ISBN of the book you would like to edit: "); int tempISBN = int.Parse(Console.ReadLine()); //searching ISBN int indexFound = BookUtility.BinarySearch(myBooks, tempISBN); Console.WriteLine("Please enter:\n'Title' to edit the title\n'Author' to edit the author\n'Genre' to edit the genre\n'Listening Time' to edit the listening time\n'Copy Count' to chnage the number of available copies"); string answer = Console.ReadLine().ToLower(); if (answer == "title") { Console.WriteLine("\nEnter a new title:"); string newTitle = Console.ReadLine(); myBooks[indexFound].SetTitle(newTitle); } else if (answer == "author") { Console.WriteLine("\nEnter a new author:"); string newAuthor = Console.ReadLine(); myBooks[indexFound].SetAuthor(newAuthor); } else if (answer == "genre") { Console.WriteLine("\nEnter a new genre:"); string newGenre = Console.ReadLine(); myBooks[indexFound].SetGenre(newGenre); } else if (answer == "listening time") { Console.WriteLine("\nEnter a new listening time:"); double newLT = double.Parse(Console.ReadLine()); myBooks[indexFound].SetListeningTime(newLT); } else if (answer == "copy count") { Console.WriteLine("Enter 'add' to add a copy of the book or 'subtract' to subtract a copy"); string copy = Console.ReadLine().ToLower(); if (copy == "add") { int copiesAdd = myBooks[indexFound].GetBookCount() + 1; myBooks[indexFound].SetBookCount(copiesAdd); Console.WriteLine("\nBook count is currently: " + myBooks[indexFound].GetBookCount()); } else if (copy == "subtract") { int copies = myBooks[indexFound].GetBookCount() - 1; myBooks[indexFound].SetBookCount(copies); Console.WriteLine("\nBook count is currently: " + myBooks[indexFound].GetBookCount()); } } // clear and re-send sorted and edited array (update the text file) string path2 = "books.txt"; File.WriteAllText(path2, String.Empty); TextWriter tW2 = new StreamWriter(path2, true); tW2.Close(); ToFile(myBooks); }
//Method gets books from user to be added to book file public static Book[] GetBookData() { //read existing data from book file into array BookFile data = new BookFile("books.txt"); Book[] myBooks = data.ReadBookData(); Console.WriteLine("\nEnter book ISBN (-1 to stop): \n"); int ISBN = int.Parse(Console.ReadLine()); while (ISBN != -1) { //binary search checks to see if the book already exists, if not, it continues to get user info on the book int indexFound = BookUtility.BinarySearch(myBooks, ISBN); if (indexFound == -1) { int bookCount = 1; Console.Write("\nEnter the title: \n"); string title = Console.ReadLine(); Console.Write("\nEnter the author: \n"); string author = Console.ReadLine(); Console.Write("\nEnter the genre: \n"); string genre = Console.ReadLine(); Console.Write("\nEnter the listening time: \n"); double listeningTime = double.Parse(Console.ReadLine()); string status = "Available"; Book newBook = new Book(ISBN, title, author, genre, listeningTime, status, bookCount); myBooks[GetCount()] = newBook; IncrCount(); Console.Clear(); Console.Write("\nEnter book ISBN (- 1 to stop): \n"); ISBN = int.Parse(Console.ReadLine()); } //if the book ISBN already exists, program will increase the number of copies by one else { int copies = myBooks[indexFound].GetBookCount() + 1; myBooks[indexFound].SetBookCount(copies); //exits the GetBookData method ISBN = -1; string status = "Available"; myBooks[indexFound].SetStatus(status); Console.Clear(); Console.WriteLine("This ISBN already exists... a copy has been added"); Console.WriteLine("\nPress enter to continue..."); Console.ReadLine(); } } Console.Clear(); Console.WriteLine("\nBook(s) inputed"); Console.WriteLine("\nPress enter to continue..."); Console.ReadLine(); return(myBooks); }