账单1.1

使用方法和函数,形参; 代码清爽一点,用double.parse 代替convert.todouble;
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Schema;
namespace Test1
{
    class Program
    {
        class Txthander// 操作TXT文件类
        {
            public void GetAlltext(string filefullname)//方法:读取文件中的所有内容
            {
                string str1 = File.ReadAllText(filefullname);
                Console.WriteLine(str1);
            }
            public void GetFirstLine(string filefullname)//读取文件中的第一行
            {
                string[] strs1 = File.ReadAllLines(filefullname);
                int a = strs1.Length;
                Console.WriteLine("首行:" + strs1[0]);
              
            }
            public void GetFinalLine(string filefullname)//读取最后一行字符
            {
                string[] strs1 = File.ReadAllLines(filefullname);
                int a = strs1.Length;
                Console.WriteLine("末行:" + strs1[a - 1]);
            }
          
            public void Streamread(string filefullname) //全读出????
            {
                StreamReader sR = File.OpenText(filefullname);
                string nextLine;
                while ((nextLine = sR.ReadLine()) != null)
                {
                    Console.WriteLine(nextLine);
                }
                sR.Close();
            }
            public void WriteStringOverride(string filefullname, string strcontect) //内容写入文件且完全覆盖原TXT内的内容
            {
                Console.WriteLine("please input string what you want to write");
                strcontect = Console.ReadLine();
                File.WriteAllText(filefullname,strcontect);
            }
            public void WriteAfterFinalLine(string finalLine,string filefullname)//最后一行写入内容,不覆盖原来的内容
            {
                StreamWriter sw = new StreamWriter(filefullname, true);
                Console.WriteLine("Please input what your want to input in the final line");
                sw.WriteLine(finalLine);
                sw.Close();
            }
            public string[] TransferFinalLineToNumber(string filefullname)//将file fullname中的内容最后一行转成一个字符串数组
            {
                string[] strs1 = File.ReadAllLines(filefullname);
                int a = strs1.Length;
           
                char[] seperator = { ' ' };
                return strs1[a - 1].Split(seperator);
            }
        }
      
        class ZhangDan : Txthander
        {
            public string TotalValue(string[] valuegroup)//将最后一行,重新计算total值并生成新的末行
            {
                double totalValue = 0;
                for (int i = 0; i < valuegroup.Length - 1; i++)
                {
                    totalValue += double.Parse(valuegroup[i]);
                }
                valuegroup[valuegroup.Length - 1] = Convert.ToString(totalValue);
                string newValueGroup = string.Join(" ", valuegroup);
        
                return newValueGroup;
              
            }
            public void CheckMoney(string filefullname)//进行不同账户中的金额转移
            {
              
                string[] valuegroup = TransferFinalLineToNumber(filefullname);//获取最后一行的数字,转化成数组
                Console.WriteLine("Select the Source");
                int sourceIndex =int.Parse(Console.ReadLine()) - 1;//源的序列号
                double sourcePosition = double.Parse(valuegroup[sourceIndex]); //取出源序列号位置的初始值
                Console.WriteLine("Select the target");
                int targetIndex = int.Parse(Console.ReadLine()) - 1;//目标的序列号
                double targetPosition = double.Parse(valuegroup[targetIndex]);//目标位置的值初始值
                Console.WriteLine("Input the number that you want transfer");
                double numberTotransfer = double.Parse(Console.ReadLine());//想要处理的值
                sourcePosition -= numberTotransfer;
                targetPosition += numberTotransfer;
               valuegroup[sourceIndex] = Convert.ToString(sourcePosition);//  源位置更新后的值
               valuegroup[targetIndex] = Convert.ToString(targetPosition);//目标位置更新后的值
                WriteAfterFinalLine(TotalValue(valuegroup), filefullname);//将valuegroup整理格式后,做为末行写入filefullName中。
 
            }
        }
        static void Main(string[] args)
        {
             string filename = @"J:\学习工作资料\b.txt";
            var ta = new ZhangDan();
            ta.GetFirstLine(filename);//引用基类中的方法
            ta.GetFinalLine(filename);//基类中的方法
            ta.CheckMoney(filename);
            Console.WriteLine("now your money is:");
            ta.GetFirstLine(filename);
            ta.GetFinalLine(filename);
            Console.ReadKey();
            Console.ReadKey();
        }
    }
}
posted @ 2020-05-31 17:46  小白沙  阅读(89)  评论(0编辑  收藏  举报