在我的应用程序中添加一个文件以跟踪帐户详细信息
本文关键字:文件 一个 跟踪 详细信息 应用程序 我的 添加 | 更新日期: 2023-09-27 18:14:19
我有一个c#作业。创建一个银行应用程序,允许出纳员为客户开立账户并执行存取款交易。不需要GUI。我使用了泛型(我不知道这是否正确)。我的代码如下。但现在我尝试添加一个。txt文件,这样创建的任何帐户都会存储在该文件中,因为当我运行代码并创建帐户时,它会在我停止运行时消失。我也在硬编码账号,我不知道该怎么做。我尝试使用随机数生成,但是当我运行代码时,它每次都会生成一个新的随机数。所以我把它拿出来,请帮帮我!!!!下面是我的代码。
The Generic Account class is below.
public class Account<T>: Person where T:class
{
private static List<Account<T>> _accounts;
private double _AccountBalance;
private string _AccountName;
private long _AccountNumber;
public Account(string Name, long accountnumber, double initialBalance)
{
AccountBalance = initialBalance;
AccountName = Name;
AccountNumber = accountnumber;
}
public double AccountBalance
{
get { return _AccountBalance; }
set { _AccountBalance = value; }
}
public string AccountName
{
get { return _AccountName; }
set { _AccountName = value; }
}
public long AccountNumber
{
get { return _AccountNumber; }
set { _AccountNumber = value; }
}
public static List<Account<T>> Accounts
{
get { return _accounts; }
set { _accounts = value; }
}
public static List<Account<T>> saveAccount(Account<T> a)
{
Accounts.Add(a);
return Accounts;
}
public virtual string getBalance(long Accountnumber)
{
return string.Format("{0:C}", AccountBalance); //currency format
}
public virtual double makeDeposit(long Accountnumber, double amount)
{
AccountBalance = AccountBalance + amount;
return AccountBalance;
}
public virtual double makeWithdrawal(long Accountnumber, double amount)
{
AccountBalance = AccountBalance - amount;
return AccountBalance;
}
public static StringBuilder AccountInfo(Account<T> a)
{
StringBuilder customerAccountInfo = new StringBuilder();
customerAccountInfo.Append("'nAccount Name: " + a.AccountName);
customerAccountInfo.Append("'nAccount Number: " + a.AccountNumber);
customerAccountInfo.Append("'nType of Account: " + typeof(T));
customerAccountInfo.Append("'nPresent account balance: " + a.getBalance(a.AccountNumber));
return customerAccountInfo;
}
}
public class Current : Account<Current>
{
public Current(string accountName, long accountNumber, double accountBalance)
: base(accountName, accountNumber, accountBalance)
{
}
public static double overdraftWithdraw(Account<Current> a, long Accountnumber, double amount)
{
a.AccountBalance = a.AccountBalance - amount;
return a.AccountBalance;
}
}
}
public class Savings: Account<Savings>
{
public Savings(string accountName, long accountNumber, double accountBalance)
: base(accountName, accountNumber, accountBalance) { }
public static double InterestOnAccount(Account<Savings> a,long accountnumber)
{
a.AccountBalance = a.AccountBalance + (BankPolicy<Savings>.interest_rate * a.AccountBalance);
return a.AccountBalance;
}
}
}
这个银行策略类说明了不同账户类型的银行策略。
public class BankPolicy<T> where T:class
{
private static double MinimumAccBalance;
private static double InterestRate;
public static double minimumBalance
{
get { return MinimumAccBalance; }
set { MinimumAccBalance = value; }
}
public static double interest_rate
{
get { return InterestRate; }
set { InterestRate = value; }
}
}
}
请帮帮我。:)
我现在正在使用一个列表来保存。但是每次我重新启动程序,它就消失了。
private static List<Account<T>> _accounts;
public static List<Account<T>> Accounts
{
get { return _accounts; }
set { _accounts = value; }
}
public static List<Account<T>> saveAccount(Account<T> a)
{
Accounts.Add(a);
return Accounts;
}
一个简单的谷歌搜索应该会让你找到你的方式。使用下面的搜索查询:c#读取和写入文本文件给你如下结果:
如何使用Visual c#读取和写入文本文件如何在c#中读取和写入文件-堆栈溢出最简单的读取和写入文件的方法-堆栈溢出c#使用StreamWriter - Dot Net perl
这应该让你开始。只是一个旁注,除非赋值需要使用泛型,否则对于您想要做的事情来说,它可能是多余的。
Cba遍历您的代码,但如果您将"using System.IO;"放在顶部,下面的位将工作。
private void Save(string file, string text)
{
File.WriteAllText(file,text);
}
这是一个文件的路径(例如,一个"。txt"文件)。并保存给定的文本。我想对于每个帐户,您可以像这样将所有信息连接起来。
Person person; //example of an instance of "Person" class.
string text = person.PersonName+" : "+person.Gender;//etc
string filename = "C:/examplefilename.txt";
Save(filename, text);
显然你可以调整"text"的值以适合你自己的使用。