using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace inventory
{
public partial class Form1 : Form
{
public SortedList<int, data_format> db = new SortedList<int, data_format>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("regular");
comboBox1.Items.Add("used");
comboBox1.Items.Add("bulk");
comboBox1.SelectedIndex = 0;
search.Enabled = false;
add.Enabled = false;
delete.Enabled = false;
}
private void save_Click(object sender, EventArgs e)
{
openFileDialog2.Filter = "Text|*.txt";
string file_path=null;
if (openFileDialog2.ShowDialog() == DialogResult.OK)
{
file_path = openFileDialog2.FileName;
}
FileStream stream = new FileStream(file_path, FileMode.Create, FileAccess.Write);
StreamWriter writter = new StreamWriter(stream);
foreach (int key_ in db.Keys)
{
double price_ = Math.Round(db[key_].price,2);
string data = string.Format("{0,-5}{1,-10}{2, -10}{3,-20}", key_.ToString(), price_.ToString(), manufacture.Text, description.Text);
writter.Write(data + "\n");
}
writter.Close();
stream.Close();
toolStripStatusLabel1.Text = "file saved";
}
private void part_number_TextChanged(object sender, EventArgs e)
{
//part # 0-99
if (Regex.IsMatch(part_number.Text, "^[0-9]{1}$|^[1-9]{1}[0-9]{1}$"))
{
search.Enabled = true;
add.Enabled = true;
delete.Enabled = true;
}
else
{
part_number.Text = "invalid";
search.Enabled = false;
add.Enabled = false;
delete.Enabled = false;
}
}
private void price_TextChanged(object sender, EventArgs e)
{
//price type double
try
{
double value = Convert.ToDouble(price.Text);
add.Enabled = true;
delete.Enabled = true;
}
catch (Exception e1)
{
price.Text = "invalid";
add.Enabled = false;
delete.Enabled = false;
}
}
private void add_Click(object sender, EventArgs e)
{
data_format item = new data_format();
item.part_number = Convert.ToInt32(part_number.Text);
item.price = Convert.ToDouble(price.Text);
item.description = description.Text;
item.manufacture = manufacture.Text;
db[item.part_number] = item;
toolStripStatusLabel1.Text = "part # added/updated";
}
private void delete_Click(object sender, EventArgs e)
{
int key = Convert.ToInt32(part_number.Text);
if (db.ContainsKey(key))
{
db.Remove(key);
toolStripStatusLabel1.Text = "part # deleted";
}
else
{
toolStripStatusLabel1.Text = "part # not found";
}
}
private void search_Click(object sender, EventArgs e)
{
int key = Convert.ToInt32(part_number.Text);
if (db.ContainsKey(key))
{
price.Text = db[key].price.ToString();
manufacture.Text = db[key].manufacture;
description.Text = db[key].description;
toolStripStatusLabel1.Text = "deleted";
}
else
{
toolStripStatusLabel1.Text = "part # not found";
}
}
private void refresh_Click(object sender, EventArgs e)
{
richTextBox1.Text = "# $"+"\n";
foreach (int key_ in db.Keys)
{
string newline = key_.ToString() + " " + db[key_].price.ToString() + "\n";
richTextBox1.Text += newline;
}
}
private void load_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Text|*.txt";
string file_path = null;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
file_path = openFileDialog1.FileName;
}
FileStream stream = new FileStream(file_path, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(stream);
string data = reader.ReadLine();
while (data != null)
{
int key_ = Convert.ToInt32(data.Substring(0, 2));
double price_ = Convert.ToDouble(data.Substring(5, 10));
string manufacture_ = data.Substring(15, 10);
string description_ = data.Substring(25, 20);
data_format item = new data_format();
item.part_number = key_;
item.price = price_;
item.description = description_;
item.manufacture = manufacture_;
db[key_] = item;
data = reader.ReadLine();
}
stream.Close();
reader.Close();
toolStripStatusLabel1.Text = "file loaded";
}
private void calculate_Click(object sender, EventArgs e)
{
Object selectedItem = comboBox1.SelectedItem;
string purchase_type = selectedItem.ToString();
int quantity_ = Convert.ToInt32(quantity.Text);
double price_ = Convert.ToDouble(price.Text);
double total_ = price_ * quantity_;
switch (purchase_type)
{
case "bulk":
if (quantity_ < 10)
{
}
else if (quantity_ < 20)
{
total_ -= 0.5 * quantity_; //bulk purchase discount
}
else
{
total_ -= quantity_;
}
break;
case "used":
total_ *= 0.9;
break;
default:
break;
}
total.Text = total_.ToString();
}
private void print_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = this.printDocument1;
printPreviewDialog1.ShowDialog();
DialogResult print = printDialog1.ShowDialog();
if (print == DialogResult.OK)
{
printDocument1.PrinterSettings.PrinterName = printDialog1.PrinterSettings.PrinterName;
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font f = new Font(new FontFamily("arial"), 10, FontStyle.Regular);
SolidBrush b = new SolidBrush(Color.Black);
e.Graphics.DrawString("student name", f, b, 0, 0);
int column = 0;
int row = 15;
e.Graphics.DrawString("part#", f, b, column, row);
e.Graphics.DrawString("price", f, b, column + 100, row);
e.Graphics.DrawString("manufacture", f, b, column + 200, row);
e.Graphics.DrawString("description", f, b, column + 300, row);
int line = 1;
foreach (int key_ in db.Keys)
{
e.Graphics.DrawString(key_.ToString(), f, b, column, row * (line + 1));
e.Graphics.DrawString(db[key_].price.ToString(), f, b, column + 100, row * (line + 1));
e.Graphics.DrawString(db[key_].manufacture, f, b, column + 200, row * (line + 1));
e.Graphics.DrawString(db[key_].description, f, b, column + 300, row * (line + 1));
line++;
}
e.HasMorePages = false;
}
}
public class data_format
{
public int part_number;
public double price;
public string manufacture;
public string description;
}
}
No comments:
Post a Comment