Advertisement
MoNoLidThZ

MethodPractice6

Sep 19th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace MethodPractice6 {
  9.     class Program {
  10.         static string fileName = "Employee.txt";
  11.  
  12.         static void Main(string[] args) {
  13.             string empID = readEmployeeID();
  14.             string empName = readLine("Name");
  15.             string[] validGenders = { "m", "f" };
  16.             string empGender = readInputInArray("Gender", validGenders);
  17.             double salary = readInputNumber("Salary");
  18.             int startYear = readInputRange("Start Year", 2000, 2559);
  19.  
  20.             saveFile(empID, empName, empGender, salary, startYear);
  21.  
  22.             Console.WriteLine("\nPress any to close program...");
  23.             Console.ReadKey();
  24.         }
  25.        
  26.         static void saveFile(string empID, string empName, string gender, double salary, int startYear) {
  27.             try {
  28.                 StreamWriter fw = new StreamWriter(fileName,true);
  29.                 fw.WriteLine("{0}/{1}/{2}/{3}/{4}", empID, empName, gender, salary, startYear);
  30.                 fw.Close();
  31.             } catch (Exception) {
  32.                 Console.ForegroundColor = ConsoleColor.Red;
  33.                 Console.WriteLine("Couldn't Write File.");
  34.             }
  35.         }
  36.  
  37.         static string readLine(string name) {
  38.             Console.Write("Input {0} : ", name);
  39.             return Console.ReadLine();
  40.         }
  41.  
  42.         static bool checkEmpID(string empID) {
  43.             if (empID.Length == 4) {
  44.                 if (empID[0].Equals('E')) {
  45.                     try {
  46.                         int empIDIdx = int.Parse(empID.Substring(1, 3));
  47.  
  48.                         return true;
  49.                     } catch (Exception) { }
  50.                 }
  51.             }
  52.             return false;
  53.         }
  54.  
  55.         static string readEmployeeID() {
  56.             string empID = "";
  57.             do {
  58.                 Console.Write("Input EMP ID : ");
  59.                 empID = Console.ReadLine();
  60.             } while (!checkEmpID(empID));
  61.             return empID;
  62.         }
  63.  
  64.         static string readInputInArray(string name, string[] validInputs) {
  65.             string input;
  66.             while (true) {
  67.                 Console.Write("{0,-21} : ", name);
  68.                 input = (Console.ReadLine()).ToLower();
  69.                 if (validInputs.Contains(input)) {
  70.                     return input;
  71.                 }
  72.             }
  73.         }
  74.  
  75.         static int readInputNumber(string name) {
  76.             while (true) {
  77.                 try {
  78.                     Console.Write("Input {0} : ", name);
  79.                     return int.Parse(Console.ReadLine());
  80.                 } catch (Exception) { }
  81.             }
  82.         }
  83.  
  84.         static int readInputRange(string name, int min, int max) {
  85.             int num;
  86.             while (true) {
  87.                 try {
  88.                     Console.Write("Input {0} : ", name);
  89.                     num = int.Parse(Console.ReadLine());
  90.                 } catch (Exception) {
  91.                     num = min - 1; //Workaround
  92.                 }
  93.                 if ((num >= min && num <= max)) {
  94.                     return num;
  95.                 }
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement