Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.64 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6.  
  7. namespace binaryfiles
  8. {
  9.  
  10. public class Book
  11. {
  12. string author;
  13. public string Author { get { return author; } }
  14.  
  15. string name;
  16. public string Name { get { return name; } }
  17.  
  18. int code;
  19. public int Code { get { return code; } }
  20.  
  21. public Book(string aa, string nn, int cc)
  22. {
  23. author = aa;
  24. name = nn;
  25. code = cc;
  26. }
  27. }
  28.  
  29. public class Listing
  30. {
  31. string FILE_NAME = "book.data";
  32. public List<Book> li;
  33.  
  34. public Listing()
  35. {
  36. li = new List<Book>();
  37.  
  38. if (File.Exists(FILE_NAME))
  39. {
  40. using (FileStream fs = new FileStream(FILE_NAME, FileMode.Open))
  41. {
  42. BinaryReader br = new BinaryReader(fs);
  43. int k = br.ReadInt32();//количество карточек в файле
  44. string aa;
  45. string nn;
  46. int cc;
  47. for (int i = 0; i < k; i++)
  48. {
  49. aa = br.ReadString();
  50. nn = br.ReadString();
  51. cc = br.ReadInt32();
  52. li.Add(new Book(aa, nn, cc));
  53. }
  54. }
  55. }
  56. }
  57.  
  58. public void Save()
  59. {
  60. using (FileStream fs = new FileStream(FILE_NAME, FileMode.Create))
  61. {
  62. BinaryWriter bw = new BinaryWriter(fs);
  63. int k = li.Count;
  64. bw.Write((int)k);
  65. for (int i = 0; i < k; i++)
  66. {
  67. bw.Write((string)li[i].Author);
  68. bw.Write((string)li[i].Name);
  69. bw.Write((int)li[i].Code);
  70. }
  71. bw.Close();
  72. fs.Close();
  73. }
  74. }
  75.  
  76. public void Search()
  77. {
  78. Form dialog = new Form();
  79. dialog.Size = new Size(300, 350);
  80. dialog.StartPosition = FormStartPosition.CenterParent;
  81.  
  82. Label ll = new Label();
  83. ll.Location = new Point(10, 10);
  84. ll.Text = "Введите слово:";
  85. TextBox tt = new TextBox();
  86. tt.Location = new Point(10, ll.Top + ll.Height + 5);
  87. tt.Size = new Size(250, 20);
  88. Button bbb = new Button();
  89. bbb.Text = "OK";
  90. bbb.Location = new Point(120, 250);
  91. bbb.DialogResult = DialogResult.OK;
  92.  
  93. dialog.Controls.Add(ll);
  94. dialog.Controls.Add(tt);
  95. dialog.Controls.Add(bbb);
  96.  
  97. if (dialog.ShowDialog() == DialogResult.OK)
  98. {
  99. string pat;
  100. pat = tt.Text;
  101.  
  102. Console.WriteLine("{0,20} {1,20} {2,20}\n", "Автор:", "Заглавие:", "Шифр: ");
  103. foreach (Book p in li)
  104. {
  105. string s;
  106. s = p.Author + p.Name + p.Code.ToString();
  107. if (s.IndexOf(pat) >= 0)
  108. {
  109. //Console.WriteLine(pat);
  110. //Console.WriteLine(s);
  111. //Console.WriteLine(s.IndexOf(pat));
  112. Console.WriteLine("{0,20} {1,20} {2,20}\n", p.Author, p.Name, p.Code);
  113. }
  114. }
  115. Console.WriteLine("Итого {0} кн.\n", li.Count);
  116. }
  117. }
  118.  
  119. public void Print()
  120. {
  121.  
  122. Console.WriteLine("{0,20} {1,20} {2,20}\n", "Автор:", "Заглавие:", "Шифр: ");
  123. foreach (Book p in li)
  124. {
  125. string s;
  126. s = p.Author + p.Name + p.Code.ToString();
  127. Console.WriteLine("{0,20} {1,20} {2,20}\n", p.Author, p.Name, p.Code);
  128.  
  129. }
  130. Console.WriteLine("Итого {0} кн.\n", li.Count);
  131. }
  132.  
  133. public void AddBook(string aa, string nn, int cc)
  134. {
  135. li.Add(new Book(aa, nn, cc));
  136. }
  137.  
  138. public void Clear()
  139. {
  140. li.Clear();
  141. }
  142. }
  143.  
  144. public class Form1 : Form
  145. {
  146. Listing l = new Listing();
  147. Label la = new Label();
  148. TextBox tb = new TextBox();
  149. Label la1 = new Label();
  150. TextBox tb1 = new TextBox();
  151. Label la2 = new Label();
  152. TextBox tb2 = new TextBox();
  153. Button b0 = new Button();
  154. Button b1 = new Button();
  155. Button b2 = new Button();
  156. Button b3 = new Button();
  157. Button b4 = new Button();
  158. Button b5 = new Button();
  159.  
  160. Button bb = new Button();
  161. Button bb1 = new Button();
  162.  
  163. public int n = 0;
  164.  
  165. public Form1()
  166. {
  167.  
  168. la.Location = new Point(10, 10);
  169. la.Text = "Автор:";
  170.  
  171. tb.Location = new Point(10, la.Top + la.Height + 2);
  172. tb.Size = new Size(200, 20);
  173.  
  174. la1.Location = new Point(10, tb.Top + tb.Height + 10);
  175. la1.Text = "Заглавие:";
  176.  
  177. tb1.Location = new Point(10, la1.Top + la1.Height + 2);
  178. tb1.Size = new Size(200, 20);
  179.  
  180. la2.Location = new Point(10, tb1.Top + tb1.Height + 10);
  181. la2.Text = "Шифр:";
  182.  
  183. tb2.Location = new Point(10, la2.Top + la2.Height + 2);
  184. tb2.Size = new Size(100, 20);
  185.  
  186. b0.Location = new Point(10, 410);
  187. b0.Text = "Search";
  188. b0.Click += new EventHandler(b0_Click);
  189.  
  190. b1.Location = new Point(b0.Left + b0.Width + 10, 410);
  191. b1.Text = "Add";
  192. b1.Click += new EventHandler(b1_Click);
  193.  
  194. b2.Location = new Point(b1.Left + b1.Width + 10, 410);
  195. b2.Text = "Save";
  196. b2.Click += new EventHandler(b2_Click);
  197.  
  198.  
  199. b3.Location = new Point(b2.Left + b2.Width + 10, 410);
  200. b3.Text = "Browse";
  201. b3.Click += new EventHandler(b3_Click);
  202.  
  203. b4.Location = new Point(b3.Left + b3.Width + 10, 410);
  204. b4.Text = "Print";
  205. b4.Click += new EventHandler(b4_Click);
  206.  
  207. b5.Location = new Point(b4.Left + b4.Width + 10, 410);
  208. b5.Text = "Clear";
  209. b5.Click += new EventHandler(b5_Click);
  210.  
  211. bb.Text = "<";
  212. bb.Location = new Point(b1.Left + b1.Width + 10, 370);
  213. bb.Click += new EventHandler(bb_Click);
  214.  
  215. bb1.Text = ">";
  216. bb1.Location = new Point(b2.Left + b2.Width + 10, 370);
  217. bb1.Click += new EventHandler(bb1_Click);
  218.  
  219. this.Size = new Size(550, 500);
  220. this.StartPosition = FormStartPosition.CenterScreen;
  221. this.Controls.Add(la);
  222. this.Controls.Add(tb);
  223. this.Controls.Add(la1);
  224. this.Controls.Add(tb1);
  225. this.Controls.Add(la2);
  226. this.Controls.Add(tb2);
  227. this.Controls.Add(b0);
  228. this.Controls.Add(b1);
  229. this.Controls.Add(b2);
  230. this.Controls.Add(b3);
  231. this.Controls.Add(b4);
  232. this.Controls.Add(b5);
  233. this.Controls.Add(bb);
  234. this.Controls.Add(bb1);
  235.  
  236. la.Hide();
  237. tb.Hide();
  238. la1.Hide();
  239. tb1.Hide();
  240. la2.Hide();
  241. tb2.Hide();
  242.  
  243. bb.Hide();
  244. bb1.Hide();
  245. }
  246.  
  247. void b0_Click(object sender, EventArgs e)
  248. {
  249. bb.Hide();
  250. bb1.Hide();
  251. l.Search();
  252. }
  253. void b1_Click(object sender, EventArgs e)
  254. {
  255. bb.Hide();
  256. bb1.Hide();
  257.  
  258. Form dialog = new Form();
  259. dialog.Size = new Size(300, 350);
  260. dialog.StartPosition = FormStartPosition.CenterParent;
  261.  
  262. Label l1 = new Label();
  263. l1.Location = new Point(10, 10);
  264. l1.Text = "Автор:";
  265. TextBox t1 = new TextBox();
  266. t1.Location = new Point(10, l1.Top + l1.Height + 2);
  267. t1.Size = new Size(200, 20);
  268.  
  269. Label l2 = new Label();
  270. l2.Location = new Point(10, t1.Top + t1.Height + 10);
  271. l2.Text = "Заглавие:";
  272. TextBox t2 = new TextBox();
  273. t2.Location = new Point(10, l2.Top + l2.Height + 2);
  274. t2.Size = new Size(200, 20);
  275.  
  276. Label l3 = new Label();
  277. l3.Location = new Point(10, t2.Top + t2.Height + 10);
  278. l3.Text = "Шифр:";
  279. TextBox t3 = new TextBox();
  280. t3.Location = new Point(10, l3.Top + l3.Height + 2);
  281. t3.Size = new Size(100, 20);
  282.  
  283. Button b = new Button();
  284. b.Text = "OK";
  285. b.Location = new Point(120, 250);
  286. b.DialogResult = DialogResult.OK;
  287.  
  288. dialog.Controls.Add(l1);
  289. dialog.Controls.Add(t1);
  290. dialog.Controls.Add(l2);
  291. dialog.Controls.Add(t2);
  292. dialog.Controls.Add(l3);
  293. dialog.Controls.Add(t3);
  294. dialog.Controls.Add(b);
  295.  
  296. if (dialog.ShowDialog() == DialogResult.OK)
  297. {
  298. l.AddBook(t1.Text, t2.Text, int.Parse(t3.Text));
  299. }
  300.  
  301. }
  302. void b2_Click(object sender, EventArgs e)
  303. {
  304. bb.Hide();
  305. bb1.Hide();
  306. l.Save();
  307. }
  308.  
  309. void b3_Click(object sender, EventArgs e)
  310. {
  311. la.Show();
  312. tb.Show();
  313. la1.Show();
  314. tb1.Show();
  315. la2.Show();
  316. tb2.Show();
  317.  
  318. bb.Show();
  319. bb1.Show();
  320. if (l.li.Count > 0)
  321. {
  322. tb.Text = l.li[n].Author;
  323. tb1.Text = l.li[n].Name;
  324. tb2.Text = (l.li[n].Code).ToString();
  325. }
  326. else
  327. {
  328. la.Hide();
  329. tb.Hide();
  330. la1.Hide();
  331. tb1.Hide();
  332. la2.Hide();
  333. tb2.Hide();
  334.  
  335. bb.Hide();
  336. bb1.Hide();
  337. }
  338. }
  339.  
  340. void b4_Click(object sender, EventArgs e)
  341. {
  342. bb.Hide();
  343. bb1.Hide();
  344. l.Print();
  345. }
  346.  
  347. void b5_Click(object sender, EventArgs e)
  348. {
  349. l.Clear();
  350.  
  351. la.Hide();
  352. tb.Hide();
  353. la1.Hide();
  354. tb1.Hide();
  355. la2.Hide();
  356. tb2.Hide();
  357.  
  358. bb.Hide();
  359. bb1.Hide();
  360. }
  361.  
  362. void bb1_Click(object sender, EventArgs e)//>
  363. {
  364. if (n < l.li.Count - 1)
  365. {
  366. n++;
  367. tb.Text = l.li[n].Author;
  368. tb1.Text = l.li[n].Name;
  369. tb2.Text = (l.li[n].Code).ToString();
  370. }
  371.  
  372. }
  373. void bb_Click(object sender, EventArgs e)//<
  374. {
  375. if (n > 0)
  376. {
  377. n--;
  378. tb.Text = l.li[n].Author;
  379. tb1.Text = l.li[n].Name;
  380. tb2.Text = (l.li[n].Code).ToString();
  381. }
  382. }
  383. }
  384.  
  385. public class Program
  386. {
  387. [STAThread]
  388. static void Main()
  389. {
  390. Form1 f = new Form1();
  391. Application.Run(f);
  392. //Listing l = new Listing();
  393. //l.Print();
  394. //l.Save();
  395.  
  396. }
  397. }
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement