Thank you for your help.
Class BookSale.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication1
{
[Serializable()]public class BookSale
{
private string strTitle;
private int intQuantity;
private decimal decPrice, decExtendedPrice;
private static decimal decSalesTotal;
private static int intSalesCount;
public BookSale(string Title, int Quantity, decimal Price)
{
this.Title = Title;
this.Quantity = Quantity;
this.Price = Price;
CalculateExtendedPrice();
AddToTotals();
}
public string Title
{
set
{
strTitle = value;
}
get
{
return strTitle;
}
}
public int Quantity
{
set
{
intQuantity = value;
}
get
{
return intQuantity;
}
}
public decimal Price
{
set
{
decPrice = value;
}
get
{
return decPrice;
}
}
public decimal ExtendedPrice
{
set
{
decExtendedPrice = value;
}
get
{
return decExtendedPrice;
}
}
public static int SalesCount
{
get
{
return intSalesCount;
}
}
public static decimal SalesTotal
{
get
{
return decSalesTotal;
}
}
private void CalculateExtendedPrice()
{
decExtendedPrice = intQuantity * decPrice;
}
private void AddToTotals()
{
decSalesTotal += decExtendedPrice;
intSalesCount++;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private BookSale booksaleobject;
private void calculatebtn_Click(object sender, System.EventArgs e)
{
try
{
booksaleobject = new BookSale(titletxt.Text, int.Parse(quantitytxt.Text), decimal.Parse(pricetxt.Text));
extendtxt.Text = booksaleobject.ExtendedPrice.ToString("N");
}
catch
{
MessageBox.Show("Error in quantity or price field","Invalid data");
}
}
private void clearbtn_Click(object sender, System.EventArgs e)
{
titletxt.Clear();
quantitytxt.Clear();
pricetxt.Clear();
extendtxt.Clear();
titletxt.Focus();
}
private void savebtn_Click(object sender, EventArgs e)
{
FileStream bookFileStream = new FileStream("Books.txt", FileMode.Create);
BinaryFormatter bookFormatter = new BinaryFormatter();
bookFormatter.Serialize(bookFileStream, booksaleobject);
bookFileStream.Close();
}
private void retrievebtn_Click(object sender, System.EventArgs e)
{
BinaryFormatter bookFormatter = new BinaryFormatter();
FileStream bookFileStream;
try
{
bookFileStream = new FileStream("Books.txt", FileMode.Open);
booksaleobject = (BookSale)bookFormatter.Deserialize(bookFileStream);
bookFileStream.Close();
titletxt.Text = booksaleobject.Title;
quantitytxt.Text = booksaleobject.Quantity.ToString();
pricetxt.Text = booksaleobject.Price.ToString("N");
extendtxt.Text = booksaleobject.ExtendedPrice.ToString("C");
}
catch
{
MessageBox.Show("No Saved object found", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}

New Topic/Question
Reply




MultiQuote


|