コード例 #1
0
        public static void SaveToCsv(PeopleModel profile)
        {
            List <string>    lines = new List <string>();
            List <LoanModel> loans = profile.Loans;

            string output = "";
            string header = "";

            try
            {
                header = File.ReadAllLines(profile.FileName).First() + "\n";
            }
            catch
            {
                MessageBox.Show("Unable to access file");
                return;
            }

            int i = 0;

            foreach (var loan in loans)
            {
                string loanOutput = $"{i}" + ","
                                    + $"{loan.LoanAmount}" + ","
                                    + $"{loan.Apr}" + ","
                                    + $"{loan.Months}" + "\n";
                output = string.Concat(output, loanOutput);
                i++;
            }

            output = string.Concat(header, output);

            try
            {
                File.WriteAllText(profile.FileName, output);
            }
            catch
            {
                MessageBox.Show("Unable to save to file:" + $"{profile.FileName}");
                return;
            }

            MessageBox.Show("Saved to file:" + $"{profile.FileName}");
        }
コード例 #2
0
        // TODO -- Create function that references this file from index.csv
        public static void OpenFromCsv(PeopleModel profile)
        {
            List <string> lines = new List <string>();

            // Read lines from file
            try {
                lines = File.ReadAllLines(profile.FileName).ToList();
            }
            catch {
                MessageBox.Show("Unable to open file");
                return;
            }

            // Parse through each line and add to loan list
            if (lines.Count() > 1 && lines[1] != "")
            {
                foreach (var line in lines.Skip(1))
                {
                    string[] entries = line.Split(',');

                    int     id;
                    decimal total;
                    decimal apr;
                    int     term;

                    Int32.TryParse(entries[0], out id);
                    Decimal.TryParse(entries[1], out total);
                    Decimal.TryParse(entries[2], out apr);
                    Int32.TryParse(entries[3], out term);

                    LoanModel tempLoan = new LoanModel(id, total, apr, term);

                    profile.AddToLoanList(tempLoan);
                }
            }


            lines = null;
        }