Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- namespace ConsoleApp1
- {
- /*Belolo, Gio Staniell E.
- IT111L - B54 */
- class VotersInfo
- {
- //Inheritance and Encapsulation
- Validation check = new Validation();
- public void addInfo(ArrayList info)
- {
- bool result = true;
- for (int i = 0; i < 6; i++)
- {
- if (info[i] is string)
- {
- if (check.checkInput(info[i].ToString()) == false)
- {
- result = false;
- break;
- }
- }
- else if (info[i] is byte)
- {
- if (check.checkInput(Convert.ToByte(info[i])) == false)
- {
- result = false;
- break;
- }
- }
- }
- if (result)
- {
- FileStream voterFile = new FileStream("Voters.txt", FileMode.OpenOrCreate);
- StreamReader lineread = new StreamReader(voterFile);
- string firstLine = lineread.ReadLine();
- voterFile.Close();
- if (firstLine == null)
- File.AppendAllText ("Voters.txt", "Voters ID|Voters Name|Gender|Age|Barangay Code|Municipality Code");
- voterFile = new FileStream("Voters.txt", FileMode.Open);
- lineread = new StreamReader(voterFile);
- string checkline = lineread.ReadLine();
- checkline = lineread.ReadLine();
- string votersList = Environment.NewLine + info[0] + "|" + info[1] + "|" + info[2] + "|" + info[3] + "|" + info[4] + "|" + info[5] + "|";
- lineread.Close();
- voterFile.Close();
- File.AppendAllText("Voters.txt", votersList);
- Console.WriteLine("\nVoters data added!");
- }
- else
- Console.WriteLine("Data not added");
- }
- public void summaryInfo()
- {
- FileStream voterFile = new FileStream("Voters.txt", FileMode.OpenOrCreate);
- StreamReader lineread = new StreamReader(voterFile);
- string firstLine = lineread.ReadLine();
- voterFile.Close();
- if (firstLine == null)
- File.AppendAllText("Voters.txt", "Voters ID|Voters Name|Gender|Age|Barangay Code|Municipality Code");
- voterFile = new FileStream("Voters.txt", FileMode.Open);
- lineread = new StreamReader(voterFile);
- string oneline = lineread.ReadLine();
- oneline = lineread.ReadLine();
- string[] votersContent;
- if (oneline != null)
- votersContent = oneline.Split('|');
- int male = 0, female = 0, teenagers = 0, pulo = 0, mamatid = 0,
- balete = 0, lagyo = 0,cabuyao = 0, lipa = 0;
- while (oneline != null)
- {
- votersContent = oneline.Split('|');
- oneline = lineread.ReadLine();
- if (votersContent[2] == "F")
- female++;
- if (votersContent[2] == "M")
- male++;
- if (Int32.Parse(votersContent[3]) > 18 && Int32.Parse(votersContent[3]) < 21)
- teenagers++;
- if (votersContent[5] == "1" && votersContent[4] == "A")
- pulo++;
- if (votersContent[5] == "1" && votersContent[4] == "B")
- mamatid++;
- if (votersContent[5] == "2" && votersContent[4] == "A")
- balete++;
- if (votersContent[5] == "2" && votersContent[4] == "B")
- lagyo++;
- }
- Console.WriteLine("Number of Residents per Municipality");
- Console.WriteLine("Cabuyao - {0}", pulo + mamatid);
- Console.WriteLine("Lipa - {0}\n", balete + lagyo);
- Console.WriteLine("Number of Residents per Barangay");
- Console.WriteLine("Pulo - {0}", pulo);
- Console.WriteLine("Mamatid - {0}", mamatid);
- Console.WriteLine("Balete - {0}", balete);
- Console.WriteLine("Lagyo - {0}\n", lagyo);
- Console.WriteLine("Male Voters - {0}", male);
- Console.WriteLine("Female Voters - {0}", female);
- Console.WriteLine("Teenage Voters - {0}", teenagers);
- }
- public void eraseData()
- {
- try
- {
- string idToDelete;
- Console.Write("Enter the voter's ID to be deleted:");
- idToDelete = Console.ReadLine();
- var tempFile = Path.GetTempFileName();
- var linesToKeep = File.ReadLines("Voters.txt").Select(l => l.Split('|')).Where(l => l[0] != idToDelete).Select(l => string.Join("|", l));
- File.WriteAllLines(tempFile, linesToKeep);
- File.Delete("Voters.txt");
- File.Move(tempFile, "Voters.txt");
- Console.WriteLine("{0} info has been deleted");
- }
- catch
- {
- Console.WriteLine("Voter's ID does not exist");
- }
- }
- }
- class Validation
- {
- //Polymorphism
- public bool checkInput(string info)
- {
- if (info.Length <= 0)
- {
- Console.WriteLine("\nEnter a valid name");
- return false;
- }
- if (info.Length == 1)
- {
- switch (info)
- {
- case "M":
- return true;
- case "F":
- return true;
- case "A":
- return true;
- case "B":
- return true;
- default:
- return false;
- }
- }
- return true;
- }
- public bool checkInput(byte int_info)
- {
- if (int_info < 18)
- {
- Console.WriteLine("Enter a valid age");
- return false;
- }
- return true;
- }
- public bool checkInput(sbyte int_info)
- {
- if (int_info != 1 || int_info !=2)
- {
- Console.WriteLine("Municipality code must be 1 or 2 only");
- return false;
- }
- return true;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- bool condition = true;
- string votersName, gender, barangayCode, votersID;
- byte voterAge;
- sbyte municipalityCode;
- while (condition)
- {
- Console.WriteLine("\n1 - Data Entry\n2 - Summary of Information\n3 - Erase a data\n4 - Exit\n");
- Console.Write("Select a command:");
- string choice = Console.ReadLine();
- Console.WriteLine("\n");
- VotersInfo voter = new VotersInfo();
- switch (choice)
- {
- case "1":
- var voters_info = new ArrayList();
- Console.Write("Voter's ID:");
- votersID = Console.ReadLine();
- Console.Write("Voter's Name:");
- votersName = Console.ReadLine();
- Console.Write("Gender:");
- gender = Console.ReadLine();
- Console.Write("Age:");
- byte.TryParse(Console.ReadLine(), out voterAge);
- Console.Write("Barangay Code:");
- barangayCode = Console.ReadLine();
- Console.Write("Municipality Code:");
- sbyte.TryParse(Console.ReadLine(), out municipalityCode);
- voters_info.Add(votersID);
- voters_info.Add(votersName);
- voters_info.Add(gender);
- voters_info.Add(voterAge);
- voters_info.Add(barangayCode);
- voters_info.Add(municipalityCode);
- voter.addInfo(voters_info);
- break;
- case "2":
- voter.summaryInfo();
- Console.ReadKey();
- break;
- case "3":
- voter.eraseData();
- Console.ReadKey();
- break;
- case "4":
- condition = false;
- break;
- default:
- Console.WriteLine("Enter a valid number");
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement