Thursday, September 19, 2024 9:15:51 PM
> settings

Customize


Authenticate

> Customer.cs
/*
 * Bryan
 * Customer Payment
 */

using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace bryan_customerPayment
{
    public partial class Customer : Form
    {
        // We are going to need these... No I wasn't told to use it, but apparently "reading customer names from a file" is a requirement.
        private XmlDocument _customersXML = new XmlDocument();
        private Payment _paymentForm = new Payment();

        public Customer()
        {
            InitializeComponent();
        }

        private void Customer_Load(object sender, EventArgs e)
        {
            try
            {
                // Load em
                _customersXML.Load("customers.xml");

                // Get our customers
                XmlNodeList customersNode = _customersXML.SelectNodes("//customers/customer");

                // Yawnn.... Just display some names
                foreach (XmlNode customer in customersNode)
                    cb_Customers.Items.Add(customer.Attributes["name"].Value);

                cb_Customers.SelectedIndex = 0;
            }
            catch (FileNotFoundException)
            {
                // HERPADERP
                MessageBox.Show("customer.xml file was not found. Make sure to copy the customers.xml file included with this program to the output directory.");

                // Buh bye!
                Application.Exit();
            }
        }

        // Second place winner for the most code in this project!
        private void cb_Customers_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Yup
            tb_PaymentInformation.Enabled = !cb_Customers.SelectedItem.ToString().Equals("");
            btn_SelectPayment.Enabled = !cb_Customers.SelectedItem.ToString().Equals("");

            // Get the customer and their billing info
            XmlNode selectedCustomerNode = _customersXML.SelectSingleNode($"//customers/customer[@name='{cb_Customers.SelectedItem.ToString()}']");
            XmlNode billingInfo = selectedCustomerNode.SelectSingleNode("billing_method");

            switch (billingInfo.Attributes["type"].Value)
            {
                case "credit":
                    // Make sure we actually have card info.
                    if (billingInfo.SelectSingleNode("card") != null)
                    {
                        // Just a long string, enjoy!
                        tb_PaymentInformation.Text = $"Charge to credit card.{Environment.NewLine}" +
                            $"{Environment.NewLine}" +
                            $"Card type: {billingInfo.SelectSingleNode("card").Attributes["type"].Value}{Environment.NewLine}" +
                            $"Card number: {billingInfo.SelectSingleNode("card").Attributes["number"].Value}{Environment.NewLine}" +
                            $"Expiration date: {billingInfo.SelectSingleNode("card").Attributes["exp"].Value}{Environment.NewLine}" +
                            $"Default billing: {billingInfo.SelectSingleNode("is_default").InnerText}{Environment.NewLine}";
                    }
                    break;
                case "bill":
                    tb_PaymentInformation.Text = $"Bill Customer.{Environment.NewLine}" +
                        $"{Environment.NewLine}" +
                        $"Default billing: {billingInfo.SelectSingleNode("is_default").InnerText}{Environment.NewLine}";
                    break;
                default:
                    tb_PaymentInformation.Text = "";
                    break;
            }
        }

        // I wish I could Application.Exit() my life
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // Did you guess it? It's our first place winner for the most code in this project!
        private void btn_Save_Click(object sender, EventArgs e)
        {
            // Same shit, different method
            XmlNode selectedCustomerNode = _customersXML.SelectSingleNode($"//customers/customer[@name='{cb_Customers.SelectedItem.ToString()}']");
            XmlNode billingInfo = selectedCustomerNode.SelectSingleNode("billing_method");

            // Set the type and is_default since we already have this info
            billingInfo.Attributes["type"].Value = _paymentForm.payment_type;
            billingInfo.SelectSingleNode("is_default").InnerText = _paymentForm.is_default ? "true" : "false";

            // Did you know?
            if (_paymentForm.payment_type == "credit")
            {
                // Checks, checks, checks!
                if (billingInfo.SelectSingleNode("card") == null)
                {
                    // Card > type, number, exp
                    XmlNode card = _customersXML.CreateElement("card");
                    XmlAttribute type = _customersXML.CreateAttribute("type");
                    XmlAttribute number = _customersXML.CreateAttribute("number");
                    XmlAttribute exp = _customersXML.CreateAttribute("exp");

                    // Give them a reason to live
                    type.Value = _paymentForm.card_type;
                    number.Value = _paymentForm.card_number;
                    exp.Value = $"{_paymentForm.exp_month}/{_paymentForm.exp_year}";

                    // Now become siblings!
                    card.Attributes.Append(type);
                    card.Attributes.Append(number);
                    card.Attributes.Append(exp);

                    // Oh look, you have a parent now. Other orphans would be jealous
                    billingInfo.AppendChild(card);
                }
                else
                {
                    // We already have a card
                    XmlNode cardNode = billingInfo.SelectSingleNode("card");

                    // Just... Yeah....
                    cardNode.Attributes["type"].Value = _paymentForm.card_type;
                    cardNode.Attributes["number"].Value = _paymentForm.card_number;
                    cardNode.Attributes["exp"].Value = $"{_paymentForm.exp_month}/{_paymentForm.exp_year}";
                }
            }
            else
            {
                // Remove that shit if we have it!, we don't need it.
                if (billingInfo.SelectSingleNode("card") != null)
                {
                    billingInfo.RemoveChild(billingInfo.SelectSingleNode("card"));
                }
            }

            // Save... Duh
            _customersXML.Save("customers.xml");

            // Derp the user because the user has a IQ of a squirrel... Or is that the attention span? Either way...
            MessageBox.Show($"Sucessfully saved payment information for {cb_Customers.SelectedItem.ToString()}", "Saved Payment");

            // No double saving!
            btn_Save.Enabled = false;
        }

        // Third place and in last!
        private void btn_SelectPayment_Click(object sender, EventArgs e)
        {
            // ....
            btn_Save.Enabled = false;

            // Show our payment dialog and be blocking, like a fat lineman
            _paymentForm.ShowDialog(this);

            // Since there is such thing as cancel.
            if (_paymentForm.payment_type != "")
            {
                // Self explanatory
                switch (_paymentForm.payment_type)
                {
                    case "credit":
                        tb_PaymentInformation.Text = $"Charge to credit card.{Environment.NewLine}" +
                            $"{Environment.NewLine}" +
                            $"Card type: {_paymentForm.card_type}{Environment.NewLine}" +
                            $"Card number: {_paymentForm.card_number}{Environment.NewLine}" +
                            $"Expiration date: {_paymentForm.exp_month}/{_paymentForm.exp_year}{Environment.NewLine}" +
                            $"Default billing: {_paymentForm.is_default}{Environment.NewLine}";
                        break;
                    case "bill":
                        tb_PaymentInformation.Text = $"Bill Customer.{Environment.NewLine}" +
                            $"{Environment.NewLine}" +
                            $"Default billing: {_paymentForm.is_default}{Environment.NewLine}";
                        break;
                    default:
                        tb_PaymentInformation.Text = "";
                        break;
                }

                // Make sure the derper can save
                btn_Save.Enabled = true;
            }
        }
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b