Reputation: 29
My tutor set me the task of making a C# program that
This is what I've come up with. It only has to be a small program, but I don't know where I can use a global variable. I was thinking something to do with subtracting tax, but every time I get started I forget what my idea was.
static void nameCheck()
{
Console.WriteLine("Name of employee: ");
string employee = Console.ReadLine();
string[] employees = { "Emp1", "Emp2", "Emp3", "Emp4" };
File.WriteAllLines("C:/Users/Chris/Documents/Visual Studio 2013/Projects/ConsoleApplication38/Employees.txt", employees);
string[] lines = File.ReadAllLines("C:/Users/Chris/Documents/Visual Studio 2013/Projects/ConsoleApplication38/Employees.txt");
int match = 0;
foreach (string line in lines)
{
if (employee != line)
{
match = match + 1;
if (match > 3)
{
Console.WriteLine("That name is not in the employee database, try again:");
nameCheck();
}
}
}
}
static double payRoll(double hours, double wage)
{
double pay = hours * wage;
return pay;
}
static void Main(string[] args)
{
Console.WriteLine(" PAYROLL");
Console.WriteLine("--------------------------------------------------------------------------------");
nameCheck();
Console.WriteLine("Number of hours worked this week: ");
int hours = Convert.ToInt32(Console.ReadLine());
const double wage = 7.50;
double pay = payRoll(hours, wage);
Console.WriteLine("Pay before tax for this employee is £" + pay);
Console.ReadLine();
}
}
Upvotes: 2
Views: 9674
Reputation: 32694
C# doesn't have a specific concept of global variables, but you can achieve the effect with a public static property or field, which you then access through the class. For example:
public class GlobalVariables
{
public static double TaxRate {get; set;}
}
Accessed at GlobalVariabels.TaxRate
.
public allows us to access the variable from outside the class. static means we don't need an instance of the GlobalVariables
class to access it (though you do need to go through the class name outside the context of the class.
And as Preston points out, you can make your GlobalVariables class static, because there's not really any reason to instantiate an instance of it (though it's not necessary).
Upvotes: 9
Reputation: 2734
I have made changes to your logic, first it reads the file once, check if the file exists otherwise it creates it. Added comments and fixed the structure, removed unnecessary variables, and took previous answers into account to help you out.
namespace Temp1
{
using System;
using System.IO;
public class GlobalVariables
{
/// <summary>
/// Wage per hour
/// </summary>
public static double WagePerHour = 7.5;
}
public class Program
{
private static void LoadEmployees()
{
// Get the name of the employee
Console.WriteLine("Name of employee: ");
string employee = Console.ReadLine();
// Get the file path
string filePath = string.Format(
@"{0}\{1}",
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
"Employees.txt");
// Check if the file does not exist and create it
if (!File.Exists(filePath))
{
// Generate sample employees
string[] employees = { "Emp1", "Emp2", "Emp3", "Emp4" };
// Write all the lines in the file
File.WriteAllLines(filePath, employees);
}
// Read all the lines from the file
string[] currentEmployees = File.ReadAllLines(filePath);
// Check the employee name
NameCheck(currentEmployees, employee);
}
/// <summary>
/// Do the name check recursively so you don’t keep loading the file all the time
/// </summary>
/// <param name="names">Array of all the employee names</param>
/// <param name="nameToFind">Name to find</param>
/// <param name="currentPosition">Current position in the array</param>
public static void NameCheck(string[] names, string nameToFind, int currentPosition = 0)
{
if (currentPosition == nameToFind.Length - 1)
{
Console.WriteLine("That name is not in the employee database, try again:");
}
else if (string.Compare(names[currentPosition], nameToFind, StringComparison.InvariantCulture) != 0)
{
currentPosition++;
NameCheck(names, nameToFind, currentPosition);
}
}
/// <summary>
/// Calculate pay roll
/// </summary>
/// <param name="hours"></param>
/// <param name="wage"></param>
/// <returns></returns>
private static double PayRoll(double hours, double wage)
{
return hours * wage;
}
/// <summary>
///
/// </summary>
/// <param name="args"></param>
private static void Main(string[] args)
{
Console.WriteLine(" PAYROLL");
Console.WriteLine("--------------------------------------------------------------------------------");
// Load employees and check if the employee is in the list
LoadEmployees();
// Get the number of hours worked
Console.WriteLine("Number of hours worked this week: ");
double hours = Convert.ToDouble(Console.ReadLine());
// Get the current pay
double pay = PayRoll(hours, GlobalVariables.WagePerHour);
Console.WriteLine("Pay before tax for this employee is £" + pay);
Console.ReadLine();
}
}
}
Upvotes: 0
Reputation: 461
You can also use like
static double payRoll(double hours, double wage)
{
return hours * wage;
}
also
int hours = Convert.ToInt32(Console.ReadLine()); //why this is an Int should be double
Upvotes: 0
Reputation: 5682
For your "global variable," (quotations because see here) look for something that you can move out of your method.
Your best option is the variable that isn't changing between calls to your nameCheck
method - string[] employees = { "Emp1", "Emp2", "Emp3", "Emp4" };
.
Since you didn't post code that shows using multiple classes, moving employees
outside the method is your best bet. If you have already learned multiple classes in class, then go with what you learned there for organizing your code. Take a peek at static as well.
Upvotes: 0
Reputation: 2924
C# is an Object Oriented Programming Language, which means you can access everything via an object which is an implementation of a class. So every variable (member in this situation) is visible if the both class and the member are accessible which can be done by declaring them as public.
Upvotes: -2