Doing a ATM Machine for my computer science project and I wanted to know how to overwrite a specific string of text in a text document. Ex: The program asks the user how much they want to deposit; program adds deposit amount to total balance.
Fairly new to coding so I know most of the basics (First semester in college) like arrays, structs and StreamReader and StreamWriter so pls no bully for lack of knowledge.
RBC002
Ariel Bendahan
1337
50000
^ Text file for reference
StreamWriter myfilewrite = new StreamWriter("AccountInfo.txt");
switch (choice)
{
case 1:
Console.Write("Entrez le montant à déposer : ");
atmFunctions.DepositAmount= Convert.ToInt32(Console.ReadLine());
while (atmFunctions.DepositAmount < 2 || atmFunctions.DepositAmount > 20000)
{
Console.Write("Entrez le montant à déposer (min $2 max $20,000) : ");
atmFunctions.DepositAmount= Convert.ToInt32(Console.ReadLine());
}
myfilewrite
*Stuck here; can't figure out what to put.*
DisplayTransaction(bankAccount.AccountNumber,bankAccount.Name,bankAccount.NIP,bankAccount.TotalBalance);
break;
}
^ Code segement in which I'm stuck at
If you are just trying to append a line to your file, then it should be as siimple as:
StreamWriter myfilewrite = new StreamWriter("AccountInfo.txt");
switch (choice)
{
case 1:
Console.Write("Entrez le montant à déposer : ");
var amountToWrite = Console.ReadLine();
atmFunctions.DepositAmount= Convert.ToInt32(amountToWrite);
while (atmFunctions.DepositAmount < 2 || atmFunctions.DepositAmount > 20000)
{
Console.Write("Entrez le montant à déposer (min $2 max $20,000) : ");
amountToWrite = Console.ReadLine();
atmFunctions.DepositAmount= Convert.ToInt32(amountToWrite);
}
myfilewrite.Write(amountToWrite);
DisplayTransaction(bankAccount.AccountNumber,bankAccount.Name,bankAccount.NIP,bankAccount.TotalBalance);
break;
}
Just call .Write()
on your StreamWriter
and pass it the string you want to append to the file.
I fixed my issue by adding the deposit amount to the total balance, then using StreamWriter to re-write the entire file and the new total balance. I am very sorry if I wasted anyone's time due to me forgetting what my teacher taught me.
The contents of the file should reflect the data stored in your program. When a deposit is made, you read the account info from the file (or database), modify the data in memory, and then write the results back.
This is the most basic data model, with some simplistic functionality. It can hold account information and read/write into a single file. This cannot handle multiple accounts from the data file and assumes the data in the file is precisely in the following order.
if anything is not as expected the program is going to blow up. But it works as intended from the question.
public class Account
{
public string Number { get; set; }
public int NIP { get; set; }
public string Owner { get; set; }
public decimal Balance { get; set; }
public void WriteToFile(string fileName)
{
StreamWriter writer = new StreamWriter(fileName);
writer.WriteLine(Number);
writer.WriteLine(Owner);
writer.WriteLine(NIP);
writer.WriteLine(Balance);
writer.Close();
}
public void ReadFromFile(string fileName)
{
StreamReader reader = new StreamReader(fileName);
Number = reader.ReadLine();
Owner = reader.ReadLine();
NIP = int.Parse(reader.ReadLine());
Balance = decimal.Parse(reader.ReadLine());
reader.Close();
}
}
and with a file AccountInfo.txt
contents
RBC002
Ariel Bendahan
1337
50000
I am able to read this information using
static void Main(string[] args)
{
const string fileName = "AccountInfo.txt";
Account account = new Account();
account.ReadFromFile(fileName);
// account holds the data from the file
Console.WriteLine($"Account: {account.Number}");
Console.WriteLine($"Owner: {account.Owner}");
Console.WriteLine($"NIP: {account.NIP}");
Console.WriteLine($"Balance: {account.Balance}");
}
For example, the account data is updated in memory and then written to the file (overwriting the old data).
static void Main(string[] args)
{
...
Console.WriteLine("Enter amount to withdraw:");
decimal withdraw = decimal.Parse(Console.ReadLine());
if (withdraw >= 2 && withdraw <= 20000)
{
account.Balance -= withdraw;
account.WriteToFile(fileName);
Console.WriteLine($"Dispensing {withdraw}");
Thread.Sleep(950);
}
else
{
Console.WriteLine("Withdrawal amoumt must be between 2 and 20,000");
}
Console.WriteLine($"Balance: {account.Balance}");
...
}
The key here is the account.WriteToFile(fileName);
immediately after account.Balance -= withdraw;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.