In: Computer Science
Write the program WritePatientRecords that allows a doctor’s staff to enter data about patients and saves the data to a file called Patients.txt. The output should be in the following format: p#, PATIENT_NAME, BALANCE. Create a Patient class that contains fields for ID number, name, and current balance owed to the doctor’s office.
using System;
using static System.Console;
using System.IO;
class WritePatientRecords
{
static void Main()
{
// Your code here
}
}
/*C # program that prompts user to enter id ,name
and balance owed to doctor .Then prompt continues until user enters
0 to stop reading. Then write the data to the file,Patients.txt .
*/
//WritePatientRecords.cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WritePatientRecords
{
class WritePatientRecords
{
static void
Main(string[] args)
{
//string set to file name
string filename="Patients.txt";
//create an object,writer
StreamWriter writer = new
StreamWriter(filename);
//declare variables id,name and balance
int id;
string name;
decimal balance;
Console.Write ("Enter Patient #id or 0 to exit: ");
//read id and parse to integer value
int.TryParse (Console .ReadLine () , out id);
/*Read data from user until id is not 0*/
while (id!=0)
{
Console.Write("Enter Patient Name: ");
name=Console.ReadLine();
Console.Write("Enter balance: ");
decimal.TryParse(Console.ReadLine(), out balance );
//write the data to file
writer.WriteLine("{0},{1},{2}", id, name, balance);
Console.WriteLine();
Console.Write("Enter Patient #id or 0 to exit: ");
int.TryParse(Console.ReadLine(), out id);
}
//clsoe the writer object
writer.Close();
Console.WriteLine("Data write to file," + filename);
Console.ReadLine();
}
}
}
----------------------------------------------------------------------------------------------------------------
//Patient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WritePatientRecords
{
//Patient class defintion
class Patient
{
//instance
variables
private int pId;
private string
pName;
private decimal
pBalance;
/*Constructor to set the
values to
this class
instance variables */
Patient(int id, string
name, decimal balance)
{
pId = id;
pName = name;
pBalance = balance;
}
//PID property
public int PID
{
set { value = pId; }
get { return pId; }
}
//Name property
public string Name
{
set { value = pName ; }
get { return pName; }
}
//Balance property
public decimal
Balance
{
set { value = pBalance ; }
get { return pBalance; }
}
} //end of the Patient
}
----------------------------------------------------------------------------------------------------------------
Sample Output:
Patients.txt file