Advertisement
Staniell

Untitled

Nov 12th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.28 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using System.IO;
  9.  
  10. namespace ConsoleApp1
  11. {
  12.     /*Belolo, Gio Staniell E.
  13.       IT111L - B54 */
  14.  
  15.  
  16.     class VotersInfo
  17.     {
  18.         //Inheritance and Encapsulation
  19.         Validation check = new Validation();
  20.         public void addInfo(ArrayList info)
  21.         {
  22.  
  23.  
  24.             bool result = true;
  25.  
  26.             for (int i = 0; i < 6; i++)
  27.             {
  28.                 if (info[i] is string)
  29.                 {
  30.                     if (check.checkInput(info[i].ToString()) == false)
  31.                     {
  32.                         result = false;
  33.                         break;
  34.                     }
  35.                 }
  36.                 else if (info[i] is byte)
  37.                 {
  38.                     if (check.checkInput(Convert.ToByte(info[i])) == false)
  39.                     {
  40.                         result = false;
  41.                         break;
  42.                     }
  43.                 }
  44.  
  45.             }
  46.  
  47.             if (result)
  48.             {
  49.                 FileStream voterFile = new FileStream("Voters.txt", FileMode.OpenOrCreate);
  50.                 StreamReader lineread = new StreamReader(voterFile);
  51.                 string firstLine = lineread.ReadLine();
  52.                 voterFile.Close();
  53.                 if (firstLine == null)
  54.                     File.AppendAllText ("Voters.txt", "Voters ID|Voters Name|Gender|Age|Barangay Code|Municipality Code");
  55.  
  56.                 voterFile = new FileStream("Voters.txt", FileMode.Open);
  57.                 lineread = new StreamReader(voterFile);
  58.                 string checkline = lineread.ReadLine();
  59.                 checkline = lineread.ReadLine();
  60.  
  61.                
  62.                 string votersList = Environment.NewLine + info[0] + "|" + info[1] + "|" + info[2] + "|" + info[3] + "|" + info[4] + "|" + info[5] + "|";
  63.                 lineread.Close();
  64.                 voterFile.Close();
  65.                 File.AppendAllText("Voters.txt", votersList);
  66.                 Console.WriteLine("\nVoters data added!");
  67.             }
  68.             else
  69.                 Console.WriteLine("Data not added");
  70.  
  71.         }
  72.  
  73.         public void summaryInfo()
  74.         {
  75.             FileStream voterFile = new FileStream("Voters.txt", FileMode.OpenOrCreate);
  76.             StreamReader lineread = new StreamReader(voterFile);
  77.             string firstLine = lineread.ReadLine();
  78.             voterFile.Close();
  79.             if (firstLine == null)
  80.                 File.AppendAllText("Voters.txt", "Voters ID|Voters Name|Gender|Age|Barangay Code|Municipality Code");
  81.  
  82.             voterFile = new FileStream("Voters.txt", FileMode.Open);
  83.             lineread = new StreamReader(voterFile);
  84.             string oneline = lineread.ReadLine();
  85.             oneline = lineread.ReadLine();
  86.  
  87.             string[] votersContent;
  88.  
  89.             if (oneline != null)
  90.                 votersContent = oneline.Split('|');
  91.             int male = 0, female = 0, teenagers = 0, pulo = 0, mamatid = 0,
  92.                 balete = 0, lagyo = 0,cabuyao = 0, lipa = 0;
  93.  
  94.             while (oneline != null)
  95.             {
  96.                 votersContent = oneline.Split('|');
  97.                 oneline = lineread.ReadLine();
  98.                 if (votersContent[2] == "F")
  99.                         female++;
  100.                 if (votersContent[2] == "M")
  101.                         male++;
  102.                 if (Int32.Parse(votersContent[3]) > 18 && Int32.Parse(votersContent[3]) < 21)
  103.                     teenagers++;
  104.                 if (votersContent[5] == "1" && votersContent[4] == "A")
  105.                     pulo++;
  106.                 if (votersContent[5] == "1" && votersContent[4] == "B")
  107.                     mamatid++;
  108.                 if (votersContent[5] == "2" && votersContent[4] == "A")
  109.                     balete++;
  110.                 if (votersContent[5] == "2" && votersContent[4] == "B")
  111.                     lagyo++;
  112.  
  113.  
  114.             }
  115.             Console.WriteLine("Number of Residents per Municipality");
  116.             Console.WriteLine("Cabuyao - {0}", pulo + mamatid);
  117.             Console.WriteLine("Lipa - {0}\n", balete + lagyo);
  118.  
  119.             Console.WriteLine("Number of Residents per Barangay");
  120.             Console.WriteLine("Pulo - {0}", pulo);
  121.             Console.WriteLine("Mamatid - {0}", mamatid);
  122.  
  123.             Console.WriteLine("Balete - {0}", balete);
  124.             Console.WriteLine("Lagyo - {0}\n", lagyo);
  125.  
  126.             Console.WriteLine("Male Voters - {0}", male);
  127.             Console.WriteLine("Female Voters - {0}", female);
  128.             Console.WriteLine("Teenage Voters - {0}", teenagers);
  129.  
  130.            
  131.         }
  132.  
  133.         public void eraseData()
  134.         {
  135.             try
  136.             {
  137.                 string idToDelete;
  138.                 Console.Write("Enter the voter's ID to be deleted:");
  139.                 idToDelete = Console.ReadLine();
  140.                 var tempFile = Path.GetTempFileName();
  141.                 var linesToKeep = File.ReadLines("Voters.txt").Select(l => l.Split('|')).Where(l => l[0] != idToDelete).Select(l => string.Join("|", l));
  142.  
  143.                 File.WriteAllLines(tempFile, linesToKeep);
  144.  
  145.                 File.Delete("Voters.txt");
  146.                 File.Move(tempFile, "Voters.txt");
  147.                 Console.WriteLine("{0} info has been deleted");
  148.             }
  149.             catch
  150.             {
  151.                 Console.WriteLine("Voter's ID does not exist");
  152.             }
  153.         }
  154.     }
  155.  
  156.  
  157.     class Validation
  158.     {
  159.         //Polymorphism
  160.         public bool checkInput(string info)
  161.         {
  162.             if (info.Length <= 0)
  163.             {
  164.                 Console.WriteLine("\nEnter a valid name");
  165.                 return false;
  166.             }
  167.  
  168.             if (info.Length == 1)
  169.             {
  170.                 switch (info)
  171.                 {
  172.                     case "M":
  173.                         return true;
  174.                     case "F":
  175.                         return true;
  176.                     case "A":
  177.                         return true;
  178.                     case "B":
  179.                         return true;
  180.                     default:
  181.                         return false;
  182.                 }
  183.             }
  184.  
  185.             return true;
  186.         }
  187.         public bool checkInput(byte int_info)
  188.         {
  189.             if (int_info < 18)
  190.             {
  191.                 Console.WriteLine("Enter a valid age");
  192.                 return false;
  193.             }
  194.             return true;
  195.         }
  196.  
  197.         public bool checkInput(sbyte int_info)
  198.         {
  199.             if (int_info != 1 || int_info !=2)
  200.             {
  201.                 Console.WriteLine("Municipality code must be 1 or 2 only");
  202.                 return false;
  203.             }
  204.             return true;
  205.         }
  206.     }
  207.  
  208.     class Program
  209.     {
  210.         static void Main(string[] args)
  211.         {
  212.             bool condition = true;
  213.             string votersName, gender, barangayCode, votersID;
  214.             byte voterAge;
  215.             sbyte municipalityCode;
  216.  
  217.             while (condition)
  218.             {
  219.                 Console.WriteLine("\n1 - Data Entry\n2 - Summary of Information\n3 - Erase a data\n4 - Exit\n");
  220.  
  221.                 Console.Write("Select a command:");
  222.                 string choice = Console.ReadLine();
  223.                 Console.WriteLine("\n");
  224.                 VotersInfo voter = new VotersInfo();
  225.  
  226.            
  227.                 switch (choice)
  228.                 {
  229.                     case "1":
  230.  
  231.                         var voters_info = new ArrayList();
  232.  
  233.                         Console.Write("Voter's ID:");
  234.                         votersID = Console.ReadLine();
  235.                         Console.Write("Voter's Name:");
  236.                         votersName = Console.ReadLine();
  237.  
  238.                         Console.Write("Gender:");
  239.                         gender = Console.ReadLine();
  240.                         Console.Write("Age:");
  241.                         byte.TryParse(Console.ReadLine(), out voterAge);
  242.                      
  243.  
  244.                        
  245.  
  246.                         Console.Write("Barangay Code:");
  247.                         barangayCode = Console.ReadLine();
  248.  
  249.                         Console.Write("Municipality Code:");
  250.                         sbyte.TryParse(Console.ReadLine(), out municipalityCode);
  251.  
  252.  
  253.                         voters_info.Add(votersID);
  254.                         voters_info.Add(votersName);
  255.                         voters_info.Add(gender);
  256.                         voters_info.Add(voterAge);
  257.                         voters_info.Add(barangayCode);
  258.                         voters_info.Add(municipalityCode);
  259.  
  260.                         voter.addInfo(voters_info);
  261.  
  262.  
  263.                         break;
  264.                     case "2":
  265.                         voter.summaryInfo();
  266.                         Console.ReadKey();
  267.                         break;
  268.                     case "3":
  269.                         voter.eraseData();
  270.                         Console.ReadKey();
  271.                         break;
  272.                     case "4":
  273.                         condition = false;
  274.                         break;
  275.                     default:
  276.                         Console.WriteLine("Enter a valid number");
  277.                         break;
  278.                 }
  279.             }
  280.         }
  281.     }
  282. }
  283.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement