Guest User

Untitled

a guest
Jun 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Windows.Forms;
  5.  
  6. namespace KL
  7. {
  8. public partial class Form1 : Form
  9. {
  10. Produkt p;
  11.  
  12. List<String> le = new List<String>();
  13.  
  14. List<String> auswahl = new List<String>();
  15.  
  16. string path = @"Produkt.txt";
  17.  
  18. double gesamtSumme = 0;
  19.  
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25. private void button_save_Click(object sender, EventArgs e)
  26. {
  27. p = new Produkt(tb_pnr.Text, tb_pName.Text, Convert.ToDouble(tbPrice.Text), Convert.ToInt32(tb_Count.Text));
  28.  
  29. FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);
  30. StreamWriter sw = new StreamWriter(fs);
  31.  
  32. sw.WriteLine(p.toStr());
  33. sw.Close();
  34. fs.Close();
  35. refresh();
  36. } // erstellt neues Objekt p von Produkt, schreibt in Datei, ruft refresh auf
  37.  
  38. private void Form1_Load(object sender, EventArgs e)
  39. {
  40. refresh();
  41. } // ruft refresh auf
  42.  
  43. private void leereForm()
  44. {
  45. listBox2.Items.Clear();
  46. tb_anz.Text = "";
  47. } // leert zweite Listbox und TextBox
  48.  
  49. private void refresh()
  50. {
  51. listBox1.Items.Clear();
  52.  
  53. le.Clear();
  54.  
  55. FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
  56.  
  57. StreamReader sr = new StreamReader(fs);
  58.  
  59. while (sr.Peek() != -1)
  60. {
  61. le.Add(sr.ReadLine());
  62. }
  63.  
  64. sr.Close();
  65.  
  66. fs.Close();
  67.  
  68. foreach (String li in le)
  69. {
  70. listBox1.Items.Add(li);
  71. }
  72. } // leer erste Listbox und leert Liste Le, liest von Datei, fuellt Liste le, fuellt Listbox1 mit Listeneinträgen
  73.  
  74. private double berechneGesamtPreis()
  75. {
  76. string value = Convert.ToString(le[listBox1.SelectedIndex]);
  77.  
  78. char delimitier = ';';
  79.  
  80. string[] substring = value.Split(delimitier);
  81.  
  82. double sum = Convert.ToInt32(tb_anz.Text) * Convert.ToDouble(substring[2]);
  83.  
  84. createListandFillListbox(sum,substring[1],Convert.ToInt32(tb_anz.Text), Convert.ToInt32(substring[3]));
  85.  
  86. return sum;
  87. } // Teilt Listeneintrag le in Substrings und verteilt in Array, Berechnet Summe pro Auswahl, ruft Funktion auf
  88.  
  89. private double createListandFillListbox(double sum, string name, int count, int lagerbestand)
  90. {
  91.  
  92. if (Convert.ToInt32(tb_anz.Text) <= lagerbestand)
  93. {
  94. lagerbestand = lagerbestand - Convert.ToInt32(tb_anz.Text);
  95. listBox2.Items.Clear();
  96.  
  97. auswahl.Add(count + ";" + name + ";" + Convert.ToString(sum) );
  98.  
  99. gesamtSumme += sum;
  100. foreach (String li in auswahl)
  101. {
  102. listBox2.Items.Add(li);
  103. }
  104. }
  105.  
  106. // else MessageBox.Show("Anzahl zu hoch. Maximal " + p.getCount() + " Stk verfuegbar");
  107. else
  108. {
  109. MessageBox.Show("Anzahl zu hoch.");
  110. }
  111.  
  112.  
  113. return gesamtSumme;
  114.  
  115.  
  116. } // leert Listbox 2, fuegt neue auswahleinträge der Liste auswahl hinzu, errechnet Gesamtsumme
  117.  
  118.  
  119. private int getMenge(int vorhanden,int bestellt)
  120. {
  121. return vorhanden - bestellt;
  122. }
  123.  
  124. private void button_calc_Click(object sender, EventArgs e)
  125. {
  126. if (tb_anz.Text != "" && listBox1.SelectedIndex != -1)
  127. {
  128. berechneGesamtPreis();
  129.  
  130. label7.Text = Convert.ToString(gesamtSumme);
  131. }
  132. else
  133. {
  134. MessageBox.Show("Bitte Listenelement auswählen und Anzahl angeben:");
  135. }
  136.  
  137.  
  138. } // Gibt Gesamtsumme als String aus
  139.  
  140. private void button_new_Click(object sender, EventArgs e)
  141. {
  142. leereForm();
  143.  
  144. listBox2.Items.Clear();
  145.  
  146. auswahl.Clear();
  147.  
  148. label7.Text = Convert.ToString(gesamtSumme = 0);
  149. } // ---
  150. }
  151. }
Add Comment
Please, Sign In to add comment