I have this xml file
<?xml version="1.0"?>
<xml>
<accounts>
<user name="User123" cellphone="000000000123" email="n/a" balance="123.12" pin="123" accNumber="123"/>
<user name="User456" cellphone="000000000456" email="445@email.com" balance="456.45" pin="456" accNumber="456"/>
</accounts>
</xml
This file is part of an ATM's database, containing the user's details. I would like to change a spesific value.
Say "User123" is login at the atm and deposit money, it must only get the balance of "User123" and change it.
So far i got this code but it isnt working:
class ATMMachine
{
private const String DATAFILE = "AtmDatabase.xml";
private const int minNum = 2;
private String accNumber;
private String Pin;
private String amount;
private String xmlAccNumber;
private String xmlPin;
private String xmlBalance;
private String xmlName; // get name from xml file
private Boolean IsMember;
private int newBalance; // xmlbalance - minNum - amount
private String stringNewBalance;
private int choice;
static void Main(string[] args)
{
ATMMachine atm = new ATMMachine();
atm.StartAtm();
}
public void StartAtm()
{
Console.Clear(); // So that the next user cant se prev users data
Console.WriteLine("*** -Welcome to the EEI ATM- ***");
Console.WriteLine("** -Please login below- **");
Console.WriteLine("\nAccount number: ");
accNumber = Console.ReadLine();
Console.WriteLine("PIN: ");
Pin = Console.ReadLine();
IsHeMember();
if (IsMember == true)
{
Console.WriteLine("* -Welcome {0}- *", xmlName);
Console.WriteLine("* -Please choose an option below- *");
while (true)
{
Console.WriteLine("\n1 - Check balance.");
Console.WriteLine("2 - Deposit.");
Console.WriteLine("3 - Withdraw.");
Console.WriteLine("4 - Logout.");
Console.Write("\nChoice: ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
GetBalance();
break;
case 2:
AddBalance();
break;
case 3:
WithDraw();
break;
case 4:
break;
default:
Console.WriteLine("Error! Invalid input.");
break;
}
}
}
else if(IsMember == false)
{
Console.WriteLine("Error! Wrong account number / PIN combination.");
}
}
// -end
public Boolean IsHeMember()
{
XmlReader reader = XmlReader.Create(DATAFILE);
while (reader.Read())
{
if (reader.Name == "user")
{
xmlName = reader["name"];
xmlAccNumber = reader["accNumber"];
xmlPin = reader["pin"];
if ((xmlAccNumber == accNumber) && (xmlPin == Pin))
{
IsMember = true;
break;
}
}
}
reader.Close();
return IsMember;
}
// -end
public void GetBalance()
{
XmlReader reader = XmlReader.Create(DATAFILE);
while (reader.Read())
{
if (reader.Name == "user")
{
xmlAccNumber = reader["accNumber"];
xmlPin = reader["pin"];
if ((xmlAccNumber == accNumber) && (xmlPin == Pin))
{
xmlBalance = reader["balance"];
Console.WriteLine(xmlBalance);
break;
}
}
}
}
// -end
public void AddBalance()
{
Console.Write("Enter the amount you would like to deposit: ");
amount = Console.ReadLine();
newBalance = int.Parse(xmlBalance) + int.Parse(amount);
stringNewBalance = newBalance.ToString();
XDocument xDoc = XDocument.Load(DATAFILE);
xDoc.Root.Element("accounts").Attribute("balance").Value = stringNewBalance;
xDoc.Save(DATAFILE);
}
// -end
public void WithDraw()
{
Console.Write("Enter the amount you would like to withdraw: ");
amount = Console.ReadLine();
newBalance = (int.Parse(xmlBalance) - minNum) - int.Parse(amount);
if (newBalance >= 0)
{
XDocument xDoc = XDocument.Load(DATAFILE);
xDoc.Root.Element("accounts").Attribute("balance").Value = stringNewBalance;
xDoc.Save(DATAFILE);
}
else
{
Console.WriteLine("Error! You do not have enough funds in your account.");
}
}
// -end
}
It gives a compiler error when i try to run it.
The errror is:
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: Value cannot be null.
Another problem that i am facing is that "User456" also has an attribute called balance. But I only want to change the value of the user that is currently logedin
What is wrong and how do i fix it?