Created
June 13, 2012 05:18
-
-
Save NikkiBuck/2922001 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.IO; | |
// Prog 118 | |
//Program name: NicoleBuckProject3, File name: NicoleBuckProject4, Date: 6/12/12 | |
//Nicole Buck | |
//Program allows users to enter and compare book details | |
namespace NicoleBuckProject3 | |
{ | |
public partial class bookCatalog : Form | |
{ | |
List<Book> books = new List<Book>(); | |
public bookCatalog() | |
{ | |
InitializeComponent(); | |
LoadingFile(); | |
Summary(); | |
UpdateListBox(); | |
} | |
//Reset button | |
private void resetButton_Click(object sender, EventArgs e) | |
{ | |
DialogResult answer = | |
MessageBox.Show("Are you sure you want to reset the fields?", "Reset fields", | |
MessageBoxButtons.YesNo, | |
MessageBoxIcon.Warning, | |
MessageBoxDefaultButton.Button2); | |
if (answer == DialogResult.Yes) | |
{ | |
isbnTextBox.Clear(); | |
lastNameTextBox.Clear(); | |
firstNameTextBox.Clear(); | |
titleTextBox.Clear(); | |
yearTextBox.Clear(); | |
priceTextBox.Clear(); | |
} | |
} | |
//exit button | |
private void exitButton_Click(object sender, EventArgs e) | |
{ | |
DialogResult exit = | |
MessageBox.Show("Are you sure you want to save and exit?", "Exit Application", | |
MessageBoxButtons.YesNo, | |
MessageBoxIcon.Warning, | |
MessageBoxDefaultButton.Button2); | |
if (exit == DialogResult.Yes) | |
{ | |
SavingFile(); | |
Application.Exit(); | |
} | |
} | |
private void addBookButton_Click(object sender, EventArgs e) | |
{ | |
Book book = new Book(); | |
book.Isbn = new ISBN(isbnTextBox.Text); | |
book.LastName = lastNameTextBox.Text; | |
book.FirstName = firstNameTextBox.Text; | |
book.Year = Convert.ToInt32(yearTextBox.Text); | |
book.Price = Convert.ToDouble(priceTextBox.Text); | |
//Used to compare ISBNs and to clear out text boxes if it is valid | |
foreach (Book item in books) | |
{ | |
if (Equals(book.Isbn) == false) | |
{ | |
isbnTextBox.Clear(); | |
lastNameTextBox.Clear(); | |
firstNameTextBox.Clear(); | |
titleTextBox.Clear(); | |
yearTextBox.Clear(); | |
priceTextBox.Clear(); | |
} | |
} | |
books.Add(book); | |
UpdateListBox(); | |
Summary(); | |
} | |
public void UpdateListBox() | |
{ | |
summaryListBox.Items.Clear(); | |
foreach (Book book in books) | |
{ | |
summaryListBox.Items.Add(book.ToString()); | |
} | |
} | |
public void SavingFile() | |
{ | |
StreamWriter writer = new StreamWriter("books.txt"); | |
foreach (Book book in books) | |
{ | |
writer.WriteLine(book.ToString()); | |
} | |
} | |
public void LoadingFile() | |
{ | |
const char DELIM = '\t'; | |
string recordIn = ""; | |
string[] parts; | |
FileStream file = new FileStream("books.txt", FileMode.Open, FileAccess.Read); | |
StreamReader reader = new StreamReader(file); | |
while (reader.Peek() != -1) | |
{ | |
Book book = new Book(); | |
recordIn = reader.ReadLine(); | |
parts = recordIn.Split(DELIM); | |
if (parts.Length == 6) | |
{ | |
book.Isbn = new ISBN(parts[0]); | |
book.LastName = parts[1]; | |
book.FirstName = parts[2]; | |
book.Title = parts[3]; | |
book.Year = Convert.ToInt32(parts[4]); | |
book.Price = Convert.ToDouble(parts[5]); | |
if (book.Valid() && NewBook(book)) | |
{ | |
books.Add(book); | |
} | |
} | |
} | |
reader.Close(); | |
file.Close(); | |
} | |
private bool NewBook(Book ckbook) | |
{ | |
foreach (Book book in books) | |
{ | |
if (ckbook.Isbn.Equals(book.Isbn)) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
//remove tab | |
private void removeButton_Click(object sender, EventArgs e) | |
{ | |
ISBN isbn = new ISBN(removeTextBox.Text); | |
if (removeIsbnRadioButton.Checked) | |
{ | |
foreach (Book book in books) | |
{ | |
if (book.Isbn.Normalize().Equals(isbn.Normalize())) | |
{ | |
books.Remove(book); | |
UpdateListBox(); | |
break; | |
} | |
} | |
} | |
else if (removeLastNameRadioButton.Checked) | |
{ | |
foreach (Book book in books) | |
{ | |
if (book.LastName.ToLower() == removeTextBox.Text.ToLower()) | |
{ | |
books.Remove(book); | |
UpdateListBox(); | |
break; | |
} | |
} | |
} | |
else if (removeTitleRadioButton.Checked) | |
{ | |
foreach (Book book in books) | |
{ | |
if (book.Title.ToLower() == removeTextBox.Text.ToLower()) | |
{ | |
books.Remove(book); | |
UpdateListBox(); | |
break; | |
} | |
} | |
} | |
Summary(); | |
} | |
//search tab | |
private void searchButton_Click(object sender, EventArgs e) | |
{ | |
ISBN isbn = new ISBN(searchBookTextBox.Text); | |
if (searchIsbnRadioButton.Checked) | |
{ | |
foreach (Book book in books) | |
{ | |
if (book.Isbn.Normalize().Equals(isbn.Normalize())) | |
{ | |
MessageBox.Show("Book found! \n" + ShowInfo(book), "Search Books", | |
MessageBoxButtons.OK, | |
MessageBoxIcon.Exclamation, | |
MessageBoxDefaultButton.Button1); | |
break; | |
} | |
} | |
} | |
else if (searchLastNameRadioButton.Checked) | |
{ | |
foreach (Book book in books) | |
{ | |
if (book.LastName == searchBookTextBox.Text) | |
{ | |
MessageBox.Show("Book found! \n" + ShowInfo(book), "Search Books", | |
MessageBoxButtons.OK); | |
break; | |
} | |
} | |
} | |
else if (searchTitleRadioButton.Checked) | |
{ | |
foreach (Book book in books) | |
{ | |
if (book.Title == searchBookTextBox.Text) | |
{ | |
MessageBox.Show("Book found! \n" + ShowInfo(book), "Search Books", | |
MessageBoxButtons.OK); | |
} | |
} | |
} | |
} | |
//for the search tab | |
private string ShowInfo(Book book) | |
{ | |
string summary = ""; | |
summary += "\n"; | |
summary += book.Isbn.ToString(); | |
summary += "\n"; | |
summary += book.LastName + " " + book.FirstName; | |
summary += "\n"; | |
summary += book.Title; | |
summary += "\n"; | |
summary += book.Year; | |
summary += "\n"; | |
summary += book.Price; | |
return summary; | |
} | |
//for the summary tab | |
//change it to void | |
// | |
private void Summary() | |
{ | |
if (books.Count() == 0) | |
{ | |
return; | |
} | |
summaryTextBox.Text = ""; | |
summaryTextBox.Text += "Total Count: " + books.Count(); | |
summaryTextBox.Text += "\n"; | |
summaryTextBox.Text += "Total Cost: " + TotalCost(); | |
summaryTextBox.Text += "\n"; | |
summaryTextBox.Text += "Most Expensive" + MostExpensive(); | |
summaryTextBox.Text += "\n"; | |
summaryTextBox.Text += "Least Expensive" + LeastExpensive(); | |
summaryTextBox.Text += "\n"; | |
summaryTextBox.Text += "Newest Book: " + NewestBook(); | |
summaryTextBox.Text += "\n"; | |
summaryTextBox.Text += "Oldest Book" + OldestBook(); | |
return; | |
} | |
//total cost | |
private double TotalCost() | |
{ | |
double prices = books.First().Price; | |
foreach (Book book in books) | |
{ | |
if (prices < book.Price) | |
{ | |
prices = book.Price; | |
} | |
} | |
return prices; | |
} | |
//most expensive book | |
public double MostExpensive() | |
{ | |
double price = books.First().Price; | |
foreach (Book book in books) | |
{ | |
if (price < book.Price) | |
{ | |
price = book.Price; | |
} | |
} | |
return price; | |
} | |
//least expensive book | |
public double LeastExpensive() | |
{ | |
double price = books.First().Price; | |
foreach (Book book in books) | |
{ | |
if (price > book.Price) | |
{ | |
price = book.Price; | |
} | |
} | |
return price; | |
} | |
//oldest book | |
public int OldestBook() | |
{ | |
int year = books.First().Year; | |
foreach (Book book in books) | |
{ | |
if (year > book.Year) | |
{ | |
year = book.Year; | |
} | |
} | |
return year; | |
} | |
//newest book | |
public int NewestBook() | |
{ | |
int year = books.First().Year; | |
foreach (Book book in books) | |
{ | |
if (year < book.Year) | |
{ | |
year = book.Year; | |
} | |
} | |
return year; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment