-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfrmBookAdd.cs
131 lines (114 loc) · 3.76 KB
/
frmBookAdd.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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;
namespace SA47Team10a_GalaxyLibrary
{
public partial class frmBookAdd : frmTemplate
{
GalaxyEntities context;
int bookPosition = 0;
string newBookID;
Book b;
Form f;
public frmBookAdd()
{
InitializeComponent();
}
private void frmBookAdd_Load(object sender, EventArgs e)
{
context = new GalaxyEntities();
GenerateBookID();
cbxCategory.SelectedIndex = 0;
cbxFloor.SelectedIndex = 0;
}
private void btnConfirm_Click(object sender, EventArgs e)
{
try
{
if (txTitle.Text == "" || txPublisher.Text == "" || txAuthor.Text == "" || txShelf.Text == "" || txPrice.Text == "" || txStock.Text == "")
throw new InvalidInputException();
//Create book record
b = new Book();
b.BookID = newBookID;
b.Title = txTitle.Text;
b.Publisher = txPublisher.Text;
b.Author = txAuthor.Text;
b.BookCategory = cbxCategory.SelectedItem.ToString();
b.Location = "F" + cbxFloor.SelectedItem.ToString() + "-L" + txShelf.Text;
b.Price = Convert.ToDouble(txPrice.Text);
b.TotalStock = Convert.ToInt32(txStock.Text);
b.NumberBorrowed = 0;
b.Availability = "yes";
//Store book record to database
context.Books.Add(b);
context.SaveChanges();
// Inform user and clear page
MessageBox.Show("Book successfully added!");
ResetValues();
}
catch (Exception a)
{
a = new InvalidInputException("Please fill in all fields.");
MessageBox.Show(a.Message);
}
}
private void btnClear_Click(object sender, EventArgs e)
{
ResetValues();
}
private void GenerateBookID()
{
// Auto-generate new Book ID
bookPosition = context.Books.Count() + 1;
newBookID = "G" + PopulateZeroes(bookPosition) + bookPosition.ToString();
// Display
txBookID.Text = newBookID;
}
private string PopulateZeroes(int digits)
{
string zeroes = "";
if (digits < 10)
zeroes = "000";
else if (digits < 100)
zeroes = "00";
else if (digits < 1000)
zeroes = "0";
return zeroes;
}
private void ResetValues()
{
GenerateBookID();
txTitle.Text = "";
txPublisher.Text = "";
txAuthor.Text = "";
cbxFloor.SelectedIndex = 0;
cbxCategory.SelectedIndex = 0;
cbxFloor.Text = "";
txShelf.Text = "";
txPrice.Text = "";
txStock.Text = "";
}
private void btnBack_Click(object sender, EventArgs e)
{
this.Hide();
f = new frm8BookInformation();
f.Location = this.Location;
f.ShowDialog();
this.Close();
}
private void mbtnUpdate_Click(object sender, EventArgs e)
{
this.Hide();
f = new frmBookModify();
f.Location = this.Location;
f.ShowDialog();
this.Close();
}
}
}