I have a half built invoice, but I'm needing to add items in a list to get a subtotal and total. How should I go about doing that?
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FinalProject
{
[Serializable]
public class Invoice
{
List<int> price = new List<int>();
public int InvoiceNumber { get; set; }
public int Date{ get; set; }
public int Total { get; set; }
public double Price { get; set; }
double TaxRate = 0.05;
public double result { set; get; }
public double taxresult { set; get; }
public double shippingresult { set; get; }
public double totalresult { set; get; }
public string FullName
{
get
{
return string.Format("{0},{1}\n{2}",
this.Date, this.InvoiceNumber,this.Total);
}
}
public Invoice()
{
this.InvoiceNumber = Int32.MaxValue;
this.Total = Int32.MaxValue;
this.Date = Int32.MaxValue;
}
public Invoice(int invoiceNumber, int total, int date)
{
this.InvoiceNumber = invoiceNumber;
this.Total = total;
this.Date = date;
}
public void CalculateSubtotalAdd()
{
this.result = (Total+Price);
}
public void CalculateTax()
{
this.result = ((Total + Price) * TaxRate);
}
public void CalculateTotal()
{
this.result = ((Total+Price)*TaxRate)+Total;
}
}
}
`
`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;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
namespace FinalProject
{
public partial class Form1 : Form
{
public List<Part> part = new List<Part>();
private Invoice myInvoice = new Invoice();
private void ClearPartForm()
{
this.ItemCodeTextBox.Text = string.Empty;
this.PartNameTextBox.Text = string.Empty;
this.QuantityTextBox.Text = string.Empty;
}
private void BindListBox()
{
this.PartListBox.DataSource = null;
this.PartListBox.DataSource = this.part;
this.PartListBox.DisplayMember = "PartDescription";
}
public Form1()
{
InitializeComponent();
}
private void AddPartButton_Click(object sender, EventArgs e)
{
List<int> price = new List<int>();
price.Add(Convert.ToInt32(this.PriceEachTextBox.Text));
try
{
Part newPart = new Part(
this.PartNameTextBox.Text,
this.ItemCodeTextBox.Text,
Convert.ToDouble(this.PriceEachTextBox.Text),
Convert.ToInt32(this.QuantityTextBox.Text));
this.part.Add(newPart);
this.ClearPartForm();
this.BindListBox();
}
catch (FormatException ex)
{
MessageBox.Show("Please fill all text boxes");
}
}
private void DeletePartButton_Click(object sender, EventArgs e)
{
int selectedIndex = this.PartListBox.SelectedIndex;
if (selectedIndex >= 0)
{
this.part.RemoveAt(selectedIndex);
this.ClearPartForm();
this.BindListBox();
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
DialogResult userChoice = DialogResult.None;
string fileName = string.Empty;
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.CheckFileExists = false;
userChoice = saveFileDialog.ShowDialog();
fileName = saveFileDialog.FileName;
}
if (userChoice == DialogResult.OK && fileName !=
string.Empty)
{
try
{
using (FileStream fileOut = new FileStream(
fileName, FileMode.OpenOrCreate, FileAccess.Write))
{
BinaryFormatter formatter = new BinaryFormatter();
foreach (Part aPart in this.part)
{
formatter.Serialize(fileOut, aPart);
}
this.part.Clear();
this.part.TrimExcess();
this.BindListBox();
}
}
catch (IOException ioEx)
{
MessageBox.Show(ioEx.Message);
}
catch (SerializationException ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void OpenButton_Click(object sender, EventArgs e)
{
DialogResult userChoice = DialogResult.None;
string fileName = string.Empty;
using (OpenFileDialog fileChooser = new OpenFileDialog())
{
userChoice = fileChooser.ShowDialog();
fileName = fileChooser.FileName;
}
if (userChoice == DialogResult.OK && fileName != string.Empty)
{
try
{
using (FileStream input = new FileStream(
fileName, FileMode.Open, FileAccess.Read))
{
BinaryFormatter reader = new BinaryFormatter();
while (input.Position != input.Length)
{
Part savedPart = (Part)reader.Deserialize(input);
this.part.Add(savedPart);
}
this.BindListBox();
}
}
catch (IOException ioEx)
{
}
catch (SerializationException ex)
{
}
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void CalculateButton_Click(object sender, EventArgs e)
{
}
private void PartListBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void InvoiceButton_Click(object sender, EventArgs e)
{
StreamWriter writeFile = new StreamWriter(@"F:\Invoice.txt",true);
string space = " ";
string bar = "-----------------------------------------";
writeFile.WriteLine(InvoiceTextBox.Text);
writeFile.WriteLine();
writeFile.WriteLine(CustomerNameBox.Text);
writeFile.WriteLine(StreetBox.Text);
writeFile.WriteLine("{0},{1}",CityBox.Text,StateBox.Text);
writeFile.WriteLine(ZipBox.Text);
writeFile.WriteLine();
writeFile.WriteLine(bar);
writeFile.Close();
}
}
}`