text file

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi all,

I have been on before and thanks to all that gave advise! But I am stuck on this one thing still - I cannot get the program to recall or read in every customer only the last entered within the text file. I need to be able to enter a pin number and account
number and in turn the file is read and the account info accessed. It will only do all this for last entry. I need your help!!
I have attached the entire code if someone has time or spots the prob easy let me know and thanks in advance!!
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

public class Program
{

public string name{get;set;}
public string address{get;set;}
public int pCode{get;set;}
public int accountno {get;set;}
public double balance{get;set;}
public string birthdate{get;set;}
public string accountname { get;set; }



// Default Constructor
public Program()
{
this.name = String.Empty;
this.address = String.Empty;
this.pCode = 0000;
this.accountno = 000000;
this.balance = 0.0;
this.birthdate = String.Empty;
this.accountname = String.Empty;
}


// Parameterized Constructor
public Program(string n, string m, int pCode, int acc, double b, string o, string k)
{
this.name = n;
this.address = m;
this.pCode = pCode;
this.accountno = acc;
this.balance = b;
this.birthdate = o;
this.accountname = k;
}

public void print()
{
Console.WriteLine("The Name Is : " + this.name
+ " , Address Is : " + this.address
+ " , PinCode Is : " + this.pCode
+ ", Accountno Is : " + this.accountno
+ " , Balance Is : " + this.balance
+ ", BirthDate Is : " + this.birthdate
+ " , AccountName Is : " + this.accountname);
}

//ArrayList recordList;
Program record;

// Add a new User Record/customer
private void addNewRecord()
{
Console.WriteLine("Enter Name");
string Name = Console.ReadLine();
Console.WriteLine("Enter Address");
string Address = Console.ReadLine();
Console.WriteLine("Enter Pin");
int PinCode = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter Account no.");
int Accountno = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Deposit Amount");
int Balance = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Date of Birth");
string birthdate = Console.ReadLine();
Console.WriteLine();
string accountname = "Deposit";
Console.WriteLine("Your new Account has been created");
Console.ReadLine();

FileStream fs = new FileStream("C:\acc.txt", FileMode.Append, FileAccess.Write, FileShare.None);
StreamWriter sw = new StreamWriter(fs);

sw.WriteLine(Name + "," + Address + "," + PinCode + "," + Accountno + "," + Balance + "," + birthdate + "," + accountname);

sw.Close();
fs.Close();
accounts();
}

// Loads all records from file and add to ArrayList(recordList)
ArrayList recordList = null;
protected void Page_Load(object sender, EventArgs e)
{
loadRecord();
}
public void loadRecord()
{
TextReader tr = File.OpenText(@"c:\acc.txt");
recordList = new ArrayList();

string name, line;
string address;
int pCode;
int accountno;
double balance;
string birthdate;
string accountname;

string[] data = null;

line = tr.ReadLine();

while (line != null) // reading a file line by line
{
data = line.Split(,); // splitting each record at every ,
name = data[0]; // after splitting at 1st index is name
address = data[1];
pCode = int.Parse(data[2]); // after splitting at 2nd index is pincode
accountno = int.Parse(data[3]);// after splitting at 3nd index is account number
balance = double.Parse(data[4]); // after splitting at 4rd index is balance
birthdate = data[5];
accountname = data[6];

Program r = new Program();
r.name = data[0];
r.address = data[1];
r.pCode = int.Parse(data[2]);
r.accountno = int.Parse(data[3]);
r.balance = double.Parse(data[4]);
r.birthdate = data[5];
r.accountname = data[6];
recordList.Add(r); // adding Record object to arrayList

line = tr.ReadLine();
}
tr.Close();
}

// Saving the record to file.
public void saveRecord(ArrayList recordList)
{
TextWriter tw = File.CreateText(@"C:\acc.txt");
for (int i = 0; i < recordList.Count; i++)
{
Program record = (Program)recordList;
tw.WriteLine(record.name + "," + record.address + "," + record.pCode + "," + record.accountno + "," + record.balance + "," + record.birthdate + "," + record.accountname);

}
}

// The main ATM function
public void enterInAtm()
{
Console.WriteLine();
loadRecord();

Console.WriteLine("---------------OPTIONS----------------");
Console.WriteLine("| 1. Withdraw |");
Console.WriteLine("| 2. Deposit |");
Console.WriteLine("| 3. Check Balance |");
Console.WriteLine("| 4. Return to Main Menu |");
Console.WriteLine("--------------------------------------");

int option = int.Parse(Console.ReadLine()); // Asks user what to do (withraw or deposit)

switch (option)
{
case 1: // if user input 1 then withdraw
Console.WriteLine("----------WITHDRAWAL----------- ");
Console.WriteLine("Enter The Amount To Withdraw : ");
double amount = double.Parse(Console.ReadLine());

if (amount > record.balance)
{
Console.WriteLine("You do not have sufficient funds");
goto case 4;
}


else if (amount < 0)
{
Console.WriteLine("Withdraw amount can not be negative");
}
else
{
record.balance = record.balance - amount;
Console.WriteLine("Your new Balance is Euro:" + record.balance);
Console.ReadLine();
enterInAtm();
}

break;

case 2: // if user input 2 then deposit
Console.WriteLine("----------DEPOSIT----------- ");
Console.WriteLine("Enter The Amount To Deposit : ");
double deposit = double.Parse(Console.ReadLine());

if (deposit < 0)
{
Console.WriteLine("Deposit amount cannot be negative");
}
else
{
record.balance = record.balance + deposit;
Console.WriteLine("Your Balance is Euro:" + record.balance);
Console.ReadLine();
enterInAtm();
}
break;

case 3: // if user input 2 then deposit
Console.WriteLine("----------BALANCE----------- ");
Console.WriteLine("Your Balance is Euro:" + record.balance);
Console.ReadLine();
enterInAtm();
break;

case 4:
accounts();
break;
{

// saveRecord(); // saving the balance after user transactions(withdraw or deposit) back to file.


//Console.WriteLine("Return to Main Menu");


frontmenu();

}
}
}

private void accounts()
{

//record = new Record();
loadRecord();


Console.WriteLine("---------------LOGIN---------------- ");
Console.WriteLine("Enter Your PinCode : ");
int pinCode = int.Parse(Console.ReadLine()); // Asks user to enter his/her Pincode

Console.WriteLine("Enter Your Accountno : ");
int accountno = int.Parse(Console.ReadLine()); // Asks user to enter his/her Account number

for (int j = 0; j < recordList.Count; j++)

record = (Program)recordList[j];
if (record.pCode == pinCode && record.accountno == accountno) // if pincode is correct then
{
Console.WriteLine("Welcome , " + record.name.ToUpper());
Console.WriteLine();

Console.WriteLine("---------------OPTIONS----------------");
Console.WriteLine("| 1. Enter Deposit Account |");
Console.WriteLine("| 2. Enter Loan Account |");
Console.WriteLine("| 3. Apply for Overdraft |");
Console.WriteLine("| 4. Return to Main Menu |");
Console.WriteLine("| 5. Exit |");
Console.WriteLine("--------------------------------------");
}
else
{
Console.WriteLine("Incorrect Pin - Return to menu");
Console.WriteLine();
frontmenu();
}


int acctype = int.Parse(Console.ReadLine()); // asks user to enter in atm or exit

switch (acctype)
{
case 1:

for (int j = 0; j < recordList.Count; j++)

record = (Program)recordList[j];

if (record.accountname == "Deposit") // if account number is correct then continue
{
goto case 6;
}

else
{
Console.WriteLine("You do not have a Deposit Account");
Console.ReadLine();
accounts();
}
break;

case 2:

for (int j = 0; j < recordList.Count; j++)

record = (Program)recordList[j];
if (record.accountname == "Loan") // if account number is correct then continue
{
goto case 6;
}

else

Console.WriteLine("You do not have a Loan Account");
Console.ReadLine();
accounts();
break;

case 3:
overdraft();
break;

case 4:
frontmenu();
break;

case 5:
Environment.Exit(0);
break;

case 6:
enterInAtm();
break;

}
}

private void overdraft()
{
loadRecord();
{
Console.WriteLine();
Console.WriteLine("---------------Apply for overdraft------------------");

Console.WriteLine("Enter Overdraft Amount Required");
int overd = int.Parse(Console.ReadLine());

Console.WriteLine("Enter Your Account Number : ");
int accountno = int.Parse(Console.ReadLine()); // Asks user to enter his/her Pincode

for (int j = 0; j < recordList.Count; j++)

record = (Program)recordList[j];
if (record.accountno == accountno) // if account number is correct then continue
{
Console.WriteLine("Account Holders Name is: " + record.name.ToUpper());
Console.WriteLine("Over Draft Request Amount: " + overd);
Console.ReadLine();

}
Console.WriteLine("Enter 1 - Confirm your details or 2 - Menu");
int confirm = int.Parse(Console.ReadLine());

if (confirm == 1)
{
Console.WriteLine("Your Request is being Processed");
accounts();
}
else
frontmenu();

Console.ReadLine();
}
}

public static void Main(string[] args)
{
// Front end menu
Program menu = new Program();
menu.frontmenu();
}

private void frontmenu()
{
// Front end menu
Console.WriteLine("---------------MAIN MENU--------------");
Console.WriteLine("| 1. Enter as Customer |");
Console.WriteLine("| 2. Open new account |");
Console.WriteLine("| 3. Enter as Manager |");
Console.WriteLine("| 4. Exit |");
Console.WriteLine("--------------------------------------");
int input = int.Parse(Console.ReadLine()); // asks user to enter in atm or exit

switch (input)
{
case 1:
Program record = new Program();
record.accounts();
break;

case 2: // if user input 2 then open new account
Program add = new Program();
add.addNewRecord();
break;

case 3: // jump to managers options
Program man = new Program();
man.manager();
break;

case 4:
Environment.Exit(0);
break;
}
}

private void manager()
{
// Manger Log in.
int a = 215363;
int num;

Console.WriteLine(a);
Console.WriteLine("nWelcomenPlease enter your 6 digit PIN # or Enter 1 to Exit");
num = Convert.ToInt32(Console.ReadLine());

//if (num == 1)exit to main

if (num != a)
{
{
Console.WriteLine("Incorrect Pin..Please enter your 6 digit PIN #");
num = Convert.ToInt32(Console.ReadLine());
}

if (num != a)
{
Console.WriteLine("Incorrect Pin..Please enter your 6 digit PIN #");
num = Convert.ToInt32(Console.ReadLine());
}

if (num != a)
{
Console.WriteLine("Third Incorrect Pin..Access DENIED!");
Console.ReadLine();
manager();
}
}
Console.WriteLine("------Your Manager Details are Aurthorised------");
Console.WriteLine();


manageroptions();
}

private void manageroptions()
{
loadRecord();
Console.WriteLine("------------------OPTIONS----------------------");
Console.WriteLine("| 1. View a Customers Details |");
Console.WriteLine("| 2. Copy all Customer details to a New File |");
Console.WriteLine("| 3. Reset Entire System |");
Console.WriteLine("| 4. Exit |");
Console.WriteLine("| 5. Return to Main Menu |");
Console.WriteLine("-----------------------------------------------");

int Choice = int.Parse(Console.ReadLine()); // asks user to enter in atm or exit

switch (Choice)
{
case 1:
Console.WriteLine("Enter Customers Account Number : ");
int accountno = int.Parse(Console.ReadLine()); // Asks user to enter his/her Pincode

for (int i = 0; i < recordList.Count; i++)

record = (Program)recordList;
if (record.accountno == accountno) // if account number is correct then continue


Console.WriteLine("Account Holder is: " + record.name.ToUpper());
Console.WriteLine("Address: " + record.address.ToUpper());
Console.WriteLine("Pin Code: " + record.pCode);
Console.WriteLine("Account number " + record.accountno);
Console.WriteLine("Current Balance: " + record.balance);
Console.WriteLine("Date of Birth " + record.birthdate);
Console.WriteLine("Account Type: " + record.accountname);
Console.ReadLine();
Console.WriteLine("To alter Customer Details Access external acc.txt file");
Console.ReadLine();
manageroptions();

break;




case 2:

string fileName = "acc.txt";
string sourcePath = @"C:";
string targetPath = @"C:TESTacc";

// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);

// To copy a folders contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))

System.IO.Directory.CreateDirectory(targetPath);

System.IO.File.Copy(sourceFile, destFile, true);

if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);

// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
Console.WriteLine("Customer Files have sucessfully been copied");
Console.ReadLine();
manageroptions();
}
}

else
{
Console.WriteLine("Source path does not exist!");
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
break;

case 3:
{
Console.WriteLine("!!WARNING YOU ARE ABOUT TO DELETE YOU COPIED FILE!!");
Console.WriteLine("Press 1 to continue or 2 to return to options!");

int delete = int.Parse(Console.ReadLine());

if (delete != 1)
goto case 5;


// Delete a file by using File class static method...
if (System.IO.File.Exists(@"C:TESTaccacc.txt"))

try
{
System.IO.File.Delete(@"C:TESTaccacc.txt");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
break;
}

case 4:
Environment.Exit(0);
break;

case 5:
frontmenu(); ;
break;

case 6:
manageroptions(); ;
break;
//interest();
}
}
}

/*private void interest()
{
DateTime startDate = new DateTime(2012, 7, 1);
Account a = new Account(startDate);
ApplyLoanInterest(Deposit);


Console.ReadLine();
}

static void ApplyLoanInterest(Deposit a)
{

double days = DateTime.Today.Subtract(a.startDate).TotalDays;

if (days % 365 == 1 && days != 1)
{
a.applyInterest(6.0D);
Console.WriteLine("Interest Applied");
}
else
{
Console.WriteLine("Interest Not Applied");
}
}
static void ApplyLoanInterest(Loan a)
{

double days = DateTime.Today.Subtract(a.startDate).TotalDays;

if (days % 365 == 1 && days != 1)
{
a.applyInterest(0.5D);
Console.WriteLine("Interest Applied to Loan");
}
else
{
Console.WriteLine("Interest Not Applied");
}
}

}
class Account
{
public DateTime startDate;

public Account(DateTime t)
{
startDate = t;
}
public void applyInterest(double interest)
{

}
}*/


[/code]
<br/>

View the full article
 
Back
Top