【问题标题】:How can I store and update totals each time I run the program每次运行程序时如何存储和更新总数
【发布时间】:2013-06-08 01:44:11
【问题描述】:

我正在编写一个计算gas费用的程序。

用户输入:

  • gas 价格
  • 这辆车能装多少加仑
  • 当前有多少气体进入油箱(估计为 0、0.25、0.5、0.75)。

计算完成,然后显示的是:

  • 当前总计(本次加满的 gas 价格)
  • 每月总计(假设用户每月加气 2.5 次)
  • 年度总数(每月 * 12)

我现在要做的是创建一个提示,询问“创建一辆新车?”或“使用当前汽车”:

  • 如果是创建新车,总计从 0 开始。
  • 如果使用的是当前汽车,则应从存储位置提取总数。

我正在寻找一种将输入存储在数据库中的方法(或者文件会更好地工作吗?),以便它可以在我每次启动程序时跟踪总数,然后关闭它。

例如,如果我加满油并且我当前的总额为 32.54 美元,我希望每月和每年的总额从 0.00 美元更改为 32.54 美元。然后下次打开它,每月和每年应该显示 32.54 美元,直到我添加另一个当前总计。

单击“计算”按钮时的当前代码:

// Variables
double gasPrice;
double gasInTank;      // How much gas is in the tank
double lessGas = 0.0;  // Essentially, what needs to be filled up.
double currentTotal;
double monthlyExpense;
double annualExpense;
double maxGallons;

gasPrice = double.Parse(txtGasPrice.Text);
maxGallons = double.Parse(txtMaxGas.Text);
gasInTank = double.Parse(txtGasInTank.Text);

if (gasInTank == 0)
{
    lessGas = maxGallons;
}
else if (gasInTank == .25)
{
    lessGas = maxGallons * .75;
}
else if (gasInTank == .5)
{
    lessGas = maxGallons / 2;
}
else if (gasInTank == .75)
{
    lessGas = maxGallons * .25;
}
else
{
    MessageBox.Show("Accepted values are : '0', '.25', '.5', and '.75'");
}

currentTotal = gasPrice * lessGas;
monthlyExpense = currentTotal * 2.5;
annualExpense = monthlyExpense * 12;
lblCurrentShow.Text = currentTotal.ToString("c");
lblMonthShow.Text = monthlyExpense.ToString("c");
lblAnnualShow.Text = annualExpense.ToString("c");

很抱歉,如果这很难理解,或者是一个问题的“菜鸟”,但自从我在 C# 中做任何事情以来已经至少一年了,我正在尝试刷新我之前的思维方式我从大学毕业就开始了我的第一份工作,是一名程序员。

【问题讨论】:

  • 最简单的做法是将本地变量包装成一个类型,将其序列化,然后将该数据写入磁盘上的文件。当程序加载时,查找该文件并将内容反序列化回该类型的实例并像往常一样继续。
  • 不相关,但maxGallons - 0 的目的不是什么?
  • @Brian,我想如果它只是 = maxGallons 会更有意义。
  • @asawyer,我必须研究一下,因为我还没有学会如何去做。

标签: c#


【解决方案1】:

这实际上取决于几个因素,其中最重要的是有多少用户将使用此应用程序以及使用频率。此外,应用程序运行的位置很重要,这是桌面还是 Web。 话虽如此,我会假设它是一个桌面,供 1 个用户使用。

存储信息的几个选项:

  1. 将值存储在本地 XML 文件中 - 使用 XmlSerializer (XmlSerializer of Simple ObjectXmlSerializer example)
  2. 将值存储在 sqlite 数据库中 - 使用 System.Data.SQLite (用于 SQLite 的 ADO.NET 适配器) (SQLite example)
  3. 缓存值 - OutputCacheProvider ( System.Runtime.Caching.dll 包含用于非 Web 应用程序的 chaching API) (OutputCacheProvider example)

【讨论】:

  • 目前,它仅适用于 1 位用户。它也是一个 Windows 窗体,所以它适用于我的桌面。我会阅读您的选择并进行调查。
  • 如果您是初学者,我建议您使用序列化,这是最简单的,也是很有价值的练习。
  • “序列化包含返回复杂对象的字段的类”的示例是否与我的问题有关?还是序列化 XmlElement 和 XmlNode?​​span>
  • 链接下方有“采购订单示例”,查看它以了解如何应用序列化。
  • 添加了简单对象示例
【解决方案2】:

首先,您需要将要保留的值提升到字段或属性中

在这种情况下,我认为只是 currentTotal 所以,

public class yourClass
{
    public double CurrentTotal {get;set;}

    protected void yourButtonClickCode()
    {
        // use the proeprty ref here instead of the local
    }

}

现在退出时,您希望将其值保存到本地文件:

public class yourClass
{
    public double CurrentTotal {get;set;}

    protected void yourButtonClickCode()
    {
        // use the proeprty ref here instead of the local
    }

    protected void onExit() 
    {
       System.IO.File.WriteAllText(@"your path.txt", this.CurrentTotal.ToString());
    }    

}

现在我们正在保存数据,我们也可以编写代码来加载它:

public class yourClass
{
    public double CurrentTotal {get;set;}

    protected void yourButtonClickCode()
    {
        // use the proeprty ref here instead of the local
    }

    public yourClass()
    {
       if(System.IO.File.Exists(@"your file.txt"))
       {
           this.CurrentTotal = double.Parse( System.IO.File.ReadAllText(@"your file.txt"));
       }
       else
       {
           this.CurrentTotal = 0d;
       }
    }

    protected void onExit()
    {
       System.IO.File.WriteAllText(@"your path.txt", this.CurrentTotal.ToString());
    }    

}

如何让onExit 事件工作取决于这是什么类型的项目(wpf/winforms/console 等)

这不适用于多辆汽车等,我会让你去解决这些细节,但这是一般的想法。

如果它是一个数据库,你会用数据库调用替换那些 IO 调用。

【讨论】:

  • 谢谢,我要重读一遍,试试看。它让球滚动。项目类型为 Windows 窗体
  • 解析这样的文件真的很痛苦,如果他要修改/添加部分到他的程序等,这不是一个好的风格。
  • 至少添加一些内存管理,例如“使用”和 try-catch
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 2012-09-11
相关资源
最近更新 更多