Skip to content

Instantly share code, notes, and snippets.

@SlamJammington
Last active November 28, 2022 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SlamJammington/1ae049234b88a51af27f940040b6c1d8 to your computer and use it in GitHub Desktop.
Save SlamJammington/1ae049234b88a51af27f940040b6c1d8 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
/*
* WinLossRecorder
* Records, retrieves, and writes wins and losses for a given username
*/
namespace WinLossRecorder
{
public class WinLossRecorderCL
{
public WinLossRecorderCL()
{
if (File.Exists("WinRecord.txt"))
{
return;
}
else
{
File.CreateText("WinRecord.txt");
File.CreateText("WinRecord.txt").Close();
}
}
// displays record for given user
public void displayWinLossRecord(string user)
{
int userWins = 0;
int userLosses = 0;
string winText = user + " win";
string lossText = user + " loss";
var winFile = File.ReadAllLines("WinRecord.txt");
foreach (string entry in winFile)
{
if (winText == user)
{
userWins = userWins + 1;
}
else if (lossText == user)
{
userLosses = userLosses + 1;
}
}
if(userLosses == 0 && userWins == 0)
{
Console.WriteLine("No log for " + user + " found!");
return;
}
decimal winLossRatio = userWins / userLosses;
Console.WriteLine(user + "'s stats \n ━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Console.WriteLine("Wins : " + userWins.ToString() + "\nLosses : " + userLosses.ToString() + "\nW/L Ratio : " + winLossRatio.ToString());
}
/* adds a win/loss record for given user
* user : username
* status : win or loss, valid params : "win","loss"
*/
public void addWinLossRecord(string user, string status)
{
// don't need to validate "status" = as it's not user input
status = status.ToLower();
File.AppendAllText("WinRecord.txt", user + " " + status);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment