private void button5_Click(object sender, EventArgs e) { if (motorbikes.premium == false)// WE CHECK IF THE USER IS PREMIUM { foreach (Motorbike motorbike in motorbikes.motorbikes) { double price = motorbike.price * 0.75; // WE DISCOUNT ALL PRICES motorbike.price = (double)misc.adjust(price); // WE ADJUST THE PRICES TO 2 DECIMALS } motorbikes.premium = true; try {//ACTUALIZE THE TEXTBOX IN REAL TIME int pos = listBox1.SelectedIndex; Motorbike mb = motorbikes.motorbikes[pos]; textBox1.Text = ""; textBox1.Text += "Name: " + mb.name + Environment.NewLine; textBox1.Text += "Manufacturer: " + mb.manufacturer + Environment.NewLine; textBox1.Text += "Comment: " + mb.description + Environment.NewLine; textBox1.Text += "Price: " + mb.price.ToString() + "$" + Environment.NewLine; textBox1.Text += "Stock: " + mb.stock.ToString() + Environment.NewLine; button5.Enabled = false; button5.Text = "You are a premium client"; string jsonData = JsonConvert.SerializeObject(motorbikes); File.WriteAllText("data/Motorbikes.json", jsonData); } catch (Exception nfe) {// PER SI DE CAS COSÍ button5.Enabled = false; button5.Text = "You are a premium client"; } MessageBox.Show("You have a discount of 25%");//MESSAGE OF CONFIRMATION } }
private void button1_Click(object sender, EventArgs e) { Form1 form = new Form1(); try { Motorbike motorbike = motorbikes.motorbikes[id];//SEARCH THE MOTORBIKE BY THE ID motorbike.name = (string)misc.normalize(textBox2.Text); motorbike.manufacturer = (string)misc.normalize(textBox4.Text); motorbike.description = textBox5.Text; double price = Convert.ToDouble(textBox3.Text);// WE REWRITE THIS MOTORBIKE WITH THE DATA FROM THE TEXTBOXES motorbike.price = (double)misc.adjust(price); int stock = Convert.ToInt32(textBox6.Text); motorbike.stock = (int)misc.noNegative(stock); string jsonData = JsonConvert.SerializeObject(motorbikes); File.WriteAllText("data/Motorbikes.json", jsonData); //REWRITE THE JSON form.update(); //WE CALL THE UPDATE FUNCTION form.Show(); this.Close(); } catch (Exception ex) { MessageBox.Show("The product could not be modified" + Environment.NewLine + ex.Message);// E R R O R form.Show(); this.Close(); } }
private void button2_Click(object sender, EventArgs e) //Button Añañin { string input = textBox3.Text; //The data from the textbox is stored here for the name try { Motorbike motorbike = new Motorbike(); //We create a new empty motorbike object motorbike.id = this.motorbikes.motorbikes.Count; //The id of this motorbike is the leght of the motorbikes array motorbike.name = (string)misc.normalize(input); //Normalize the name motorbike.manufacturer = (string)misc.normalize(textBox4.Text); //The data from the textbox is stored here for the manufacturer and is normalized motorbike.description = textBox7.Text; //The data from the textbox is stored here for the descruption try // If there is no a decimal number the error will show { double price = Double.Parse(textBox5.Text); motorbike.price = (double)misc.adjust(price);//The data from the textbox is stored here for the price and is adjusted tu 2 decimals } catch (Exception nfe) { MessageBox.Show("The price shoul be a decimal number"); textBox3.Text = ""; textBox4.Text = "";//The error will clean the textboxes textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = ""; return; } try { int quantity = int.Parse(textBox6.Text); //The data from the textbox is stored here for the quantity motorbike.stock = (int)misc.noNegative(quantity); //If the stock is negative it will be 0 motorbikes.motorbikes.Add(motorbike); //We add this object in the array in motorbikes string jsonData = JsonConvert.SerializeObject(motorbikes); //We convert this list motorbikes in JSON File.WriteAllText("data/Motorbikes.json", jsonData); //We add the data from the object in JSON motorbikes to the Json listBox1.Items.Add(motorbike.name); //We add the new motorbike in the listbox textBox3.Text = ""; textBox4.Text = ""; //clean textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = ""; } catch (Exception nfe) { MessageBox.Show("insert a valid quantity"); textBox3.Text = ""; textBox4.Text = "";//The error from the stock textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = ""; return; } } catch (Exception ex) { MessageBox.Show("Please read the HELP message");//general error } }
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) //Show the data from each motorbike in the textbox { try{ //If we select an expty item the program will not crash int pos = listBox1.SelectedIndex; //We save the index in an Integer Motorbike mb = motorbikes.motorbikes[pos]; //We search the motorbike by the position textBox1.Text = ""; textBox1.Text += "Name: " + mb.name + Environment.NewLine; textBox1.Text += "Manufacturer: " + mb.manufacturer + Environment.NewLine; textBox1.Text += "Comment: " + mb.description + Environment.NewLine; textBox1.Text += "Price: " + mb.price.ToString() + "$" + Environment.NewLine; textBox1.Text += "Stock: " + mb.stock.ToString() + Environment.NewLine; }catch (Exception ex) { } }