public static void PrintRent(Transactions[] myTransactions) { for (int i = 0; i < Transactions.GetTranCount(); i++) { Console.WriteLine(myTransactions[i].ToString()); } }
public static void BookReturn(Transactions[] myTransactions) { //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(); for (int i = 1; i < Transactions.GetTranCount(); i++) { int tempFound = TranUtility.BinarySearch(myTransactions, tempTranISBN); if (tempEmail == myTransactions[i].GetCustomerEmail()) { if (i == tempFound) { //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("M/d/yyyy"); myTransactions[tempFound].SetReturnDate(newReturnDate); } } } //clear and resend string path5 = "transactions.txt"; File.WriteAllText(path5, String.Empty); TextWriter tW5 = new StreamWriter(path5, true); tW5.Close(); ToFile(myTransactions); }
public void CustDateSort(Transactions[] myTransactions) { //nested for loop for (int i = 0; i < Transactions.GetTranCount() - 1; i++) { //set min equal to i (first value we will see) int minIndex = i; for (int j = i + 1; j < Transactions.GetTranCount(); j++) { //nested for loop compares each index to the one next to it if (myTransactions[i].GetCustomerName() == myTransactions[j].GetCustomerName()) { DateTime dateObjMin = DateTime.Parse(myTransactions[minIndex].GetRentDate()); DateTime dateObjJ = DateTime.Parse(myTransactions[j].GetRentDate()); //if any value J we see is smaller than min, swap the two if (DateTime.Compare(dateObjMin, dateObjJ) == 1) { minIndex = j; } //swap function changes order of dates for each customer if (minIndex != i) { Swap(i, minIndex); } } } if (minIndex != i) { Swap(myTransactions, minIndex, i); } } }
//used in program.cs to read info from text file into array public Transactions[] ReadTranData() { StreamReader inFile = new StreamReader(fileName); Transactions[] myTransactions = new Transactions[200]; string data = inFile.ReadLine(); Transactions.SetTranCount(0); while (data != null) { string[] temp = data.Split('#'); int tempRentID = int.Parse(temp[0]); int tempTranISBN = int.Parse(temp[1]); string custName = temp[2]; string custEmail = temp[3]; string rentDate = temp[4]; string returnDate = temp[5]; myTransactions[Transactions.GetTranCount()] = new Transactions(tempRentID, tempTranISBN, temp[2], temp[3], temp[4], temp[5]); myTransactions[Transactions.GetTranCount()].ToString(); Transactions.IncrTranCount(); data = inFile.ReadLine(); } inFile.Close(); return(myTransactions); }
//ToFile to write transaction array to text file with # delimeter public static void ToFile(Transactions[] myTransactions) { myTransactions.ToString(); using (StreamWriter sW = File.AppendText("transactions.txt")) for (int i = 0; i < Transactions.GetTranCount(); i++) { sW.WriteLine(myTransactions[i].GetRentID() + "#" + myTransactions[i].GetTranISBN() + "#" + myTransactions[i].GetCustomerName() + "#" + myTransactions[i].GetCustomerEmail() + "#" + myTransactions[i].GetRentDate() + "#" + myTransactions[i].GetReturnDate()); } }
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 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); }
//individual report (gives all rentals by that customer) public static void IndividualReport(Transactions[] myTransactions) { //clear and re-send sorted and edited array (update the text file) string returnPath = "transactions.txt"; File.WriteAllText(returnPath, String.Empty); TextWriter tWReturn = new StreamWriter(returnPath, true); tWReturn.Close(); Transactions.ToFile(myTransactions); Console.WriteLine("\nEnter the customer's email: "); string tempIndvEmail = Console.ReadLine(); Console.WriteLine("\nCustomer rental history: \n"); //simple for loop for (int i = 1; i < Transactions.GetTranCount(); i++) { //checks if user inputed email is equal to any email in the text file, if yes, display if (tempIndvEmail == myTransactions[i].GetCustomerEmail()) { Console.WriteLine(myTransactions[i]); } } Console.WriteLine("\nWould you like to enter this into a text file? ('yes' or 'no')"); string choice = Console.ReadLine(); if (choice.ToLower() == "yes") { Console.WriteLine("\nEnter the name of the text file: "); string tempTxt = Console.ReadLine(); StreamWriter outFile = new StreamWriter(tempTxt); //Streamwrite the info written out above into a text file of the user's choice for (int i = 0; i < Transactions.GetTranCount(); i++) { if (tempIndvEmail == myTransactions[i].GetCustomerEmail()) { outFile.WriteLine(myTransactions[i].GetRentID() + "#" + myTransactions[i].GetTranISBN() + "#" + myTransactions[i].GetCustomerName() + "#" + myTransactions[i].GetCustomerEmail() + "#" + myTransactions[i].GetRentDate() + "#" + myTransactions[i].GetReturnDate()); } } outFile.Close(); } }
//simple write method to prompt a text file, and write text into it public void Write(Transactions[] myTransactions) { Console.WriteLine("\nWould you like to enter this into a text file? ('yes' or 'no')"); string choice = Console.ReadLine(); if (choice.ToLower() == "yes") { Console.WriteLine("\nEnter the name of the text file: "); string tempTxt = Console.ReadLine(); StreamWriter outFile = new StreamWriter(tempTxt); //Streamwrite the info written out above into a text file of the user's choice for (int i = 0; i < Transactions.GetTranCount(); i++) { outFile.WriteLine(myTransactions[i].GetRentID() + "#" + myTransactions[i].GetTranISBN() + "#" + myTransactions[i].GetCustomerName() + "#" + myTransactions[i].GetCustomerEmail() + "#" + myTransactions[i].GetRentDate() + "#" + myTransactions[i].GetReturnDate()); } outFile.Close(); Console.WriteLine("\nSuccess! Text entered. (Press enter to continue)"); Console.ReadLine(); } }
public static void CustDateSort(Transactions[] myTransactions) { TranUtility.SelectionSortHist(myTransactions); string tempName = myTransactions[0].GetCustomerName(); // string tempDate = myTransactions[0].GetRentDate(); int count = 1; for (int i = 0; i < Transactions.GetTranCount(); i++) { if (tempName == myTransactions[i].GetCustomerName()) { Console.WriteLine(myTransactions[i]); } else { ProcessBreak(myTransactions, ref tempName, ref count, i); } } ProcessBreak(myTransactions, ref tempName, ref count, 0); }
public static void SortDate(Transactions[] myTransactions) { for (int i = 0; i < Transactions.GetTranCount() - 1; i++) { int minIndex = i; for (int j = i + 1; j < Transactions.GetTranCount(); j++) { DateTime dateObjMin = DateTime.Parse(myTransactions[minIndex].GetRentDate()); DateTime dateObjJ = DateTime.Parse(myTransactions[j].GetRentDate()); if (DateTime.Compare(dateObjMin, dateObjJ) == 1) { minIndex = j; } } if (minIndex != i) { Swap(myTransactions, minIndex, i); } } }
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 void TotalReport(Transactions[] myTransactions) { Console.Clear(); BookReport.SortDate(myTransactions); Console.WriteLine("\nTotal rentals by month and year: \n"); Transactions.PrintRent(myTransactions); Console.WriteLine(""); Console.WriteLine(""); string yearStr = myTransactions[0].GetRentDate().Substring(6, 4); int monthCount = 0; string monthStr = myTransactions[0].GetRentDate().Substring(0, 2); Console.WriteLine("\nWould you like to see a report for a specific month? ('yes' or 'no')"); string yes = Console.ReadLine(); if (yes == "yes") { Console.Clear(); Console.WriteLine("Enter a month and year to see the report from that month:\n"); Console.WriteLine("ATTENTION: enter month as 'MM' and year as 'YYYY'"); Console.WriteLine("\nEnter a month:\n"); int monthIntTemp = int.Parse(Console.ReadLine()); string monthTemp = monthIntTemp.ToString(); Console.WriteLine("\nEnter a year:\n"); string yearTemp = Console.ReadLine(); for (int i = 0; i < Transactions.GetTranCount(); i++) { if (yearTemp == myTransactions[i].GetRentDate().Substring(6, 4) && monthTemp == myTransactions[i].GetRentDate().Substring(0, 2)) { monthCount++; } } DateTime date = new DateTime(2020, monthIntTemp, 1); string wordMonth = date.ToString("MMMM"); Console.Clear(); Console.WriteLine("\nNumber of rentals in " + wordMonth + ": " + monthCount); Console.WriteLine(""); for (int i = 0; i < Transactions.GetTranCount(); i++) { if (yearTemp == myTransactions[i].GetRentDate().Substring(6, 4) && monthTemp == myTransactions[i].GetRentDate().Substring(0, 2)) { Console.WriteLine(myTransactions[i]); } } Console.WriteLine("\nPress enter to continue..."); Console.ReadLine(); Console.WriteLine("\nWould you like to write this to a text file? ('yes' or 'no')"); string choice = Console.ReadLine(); if (choice.ToLower() == "yes") { Console.WriteLine("\nEnter the name of the text file: "); string tempTxt = Console.ReadLine(); StreamWriter outFile = new StreamWriter(tempTxt); //Streamwrite the info written out above into a text file of the user's choice for (int i = 0; i < Transactions.GetTranCount(); i++) { if (yearTemp == myTransactions[i].GetRentDate().Substring(6, 4) && monthTemp == myTransactions[i].GetRentDate().Substring(0, 2)) { outFile.WriteLine(myTransactions[i]); } } outFile.Close(); Console.WriteLine("\nSuccess! Text entered. (Press enter to continue)"); Console.ReadLine(); } } }
static void Main(string[] args) { string selection = ""; while (selection != "exit") { selection = GetSelection(); // Book[] myBooks = Book.GetBookData(); // Book.SortAndSend(myBooks); BookFile data = new BookFile("books.txt"); Book[] myBooks = data.ReadBookData(); BookUtility bookUtility = new BookUtility(myBooks); BookFile bookFile = new BookFile("books.txt"); TranFile tranFile = new TranFile("transactions.txt"); Transactions[] myTransactions = new Transactions[200]; TranUtility tranUtility = new TranUtility(myTransactions); BookReport bookReport = new BookReport(myTransactions); if (selection == "add") { myBooks = Book.GetBookData(); Book.SortAndSend(myBooks); } if (selection == "edit") { Book.Edit(myBooks); } if (selection == "view") { 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].GetTitle()); } } Console.WriteLine("\nPress enter to continue: "); Console.ReadLine(); } if (selection == "rent") { //Lets the user input which book ISBN is to be rented myTransactions = Transactions.GetTransactionData(myBooks); Transactions.PrintRent(myTransactions); //Marks the book as rented in the book text file string rentPath = "books.txt"; File.WriteAllText(rentPath, String.Empty); TextWriter twP = new StreamWriter(rentPath, true); twP.Close(); Book.ToFile(myBooks); //write what is in the transactions array to the text file, read the text file and send back to array, sort the array Transactions.ToFile(myTransactions); myTransactions = tranFile.ReadTranData(); tranUtility.SelectionSort(myTransactions); Transactions.PrintRent(myTransactions); //clear and re-send sorted and edited array (update the text file) string tranPath = "transactions.txt"; File.WriteAllText(tranPath, String.Empty); TextWriter tWTran = new StreamWriter(tranPath, true); tWTran.Close(); Transactions.ToFile(myTransactions); } if (selection == "return") { Console.Clear(); myTransactions = tranFile.ReadTranData(); Transactions.PrintRent(myTransactions); //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(); for (int i = 1; i < Transactions.GetTranCount(); i++) { int tempFound = TranUtility.BinarySearch(myTransactions, tempTranISBN); if (tempEmail == myTransactions[i].GetCustomerEmail()) { if (i == tempFound) { //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("M/d/yyyy"); myTransactions[tempFound].SetReturnDate(newReturnDate); } } } string tranPath = "transactions.txt"; File.WriteAllText(tranPath, String.Empty); TextWriter tWTran = new StreamWriter(tranPath, true); tWTran.Close(); Transactions.ToFile(myTransactions); Console.WriteLine("\nBook returned."); Console.WriteLine("\nPress enter to continue: "); Console.ReadLine(); } if (selection == "total") { myTransactions = tranFile.ReadTranData(); tranUtility.SelectionSort(myTransactions); //clear and re-send sorted and edited array (update the text file) string returnPath = "transactions.txt"; File.WriteAllText(returnPath, String.Empty); TextWriter tWReturn = new StreamWriter(returnPath, true); tWReturn.Close(); Transactions.ToFile(myTransactions); BookReport.SortDate(myTransactions); Console.WriteLine("\nTotal rentals by month and year: \n"); Transactions.PrintRent(myTransactions); Console.ReadLine(); Console.WriteLine("Would you like to save these to a text file? (yes or no) "); string answer = Console.ReadLine(); if (answer.ToLower() == "yes") { Console.WriteLine("\nEnter the name of the text file: "); string tempTxt = Console.ReadLine(); StreamWriter outFile = new StreamWriter(tempTxt); for (int i = 0; i < Transactions.GetTranCount(); i++) { outFile.WriteLine(myTransactions[i].GetRentID() + "#" + myTransactions[i].GetTranISBN() + "#" + myTransactions[i].GetCustomerName() + "#" + myTransactions[i].GetCustomerEmail() + "#" + myTransactions[i].GetRentDate() + "#" + myTransactions[i].GetReturnDate()); } outFile.Close(); } } if (selection == "individual") { myTransactions = tranFile.ReadTranData(); tranUtility.SelectionSort(myTransactions); //clear and re-send sorted and edited array (update the text file) string returnPath = "transactions.txt"; File.WriteAllText(returnPath, String.Empty); TextWriter tWReturn = new StreamWriter(returnPath, true); tWReturn.Close(); Transactions.ToFile(myTransactions); Console.WriteLine("\nEnter the customer's email: "); string tempIndvEmail = Console.ReadLine(); Console.WriteLine("\nCustomer rental history: \n"); for (int i = 1; i < Transactions.GetTranCount(); i++) { if (tempIndvEmail == myTransactions[i].GetCustomerEmail()) { Console.WriteLine(myTransactions[i]); } } Console.WriteLine("\nWould you like to enter this into a text file? (yes or no)"); string choice = Console.ReadLine(); if (choice.ToLower() == "yes") { Console.WriteLine("\nEnter the name of the text file: "); string tempTxt = Console.ReadLine(); StreamWriter outFile = new StreamWriter(tempTxt); for (int i = 1; i < Transactions.GetTranCount(); i++) { if (tempIndvEmail == myTransactions[i].GetCustomerEmail()) { outFile.WriteLine(myTransactions[i].GetRentID() + "#" + myTransactions[i].GetTranISBN() + "#" + myTransactions[i].GetCustomerName() + "#" + myTransactions[i].GetCustomerEmail() + "#" + myTransactions[i].GetRentDate() + "#" + myTransactions[i].GetReturnDate()); } } outFile.Close(); } Console.WriteLine("\nPress enter to continue: "); Console.ReadLine(); } if (selection == "historical") { myTransactions = tranFile.ReadTranData(); BookReport.CustDateSort(myTransactions); } } }