I use class Card which contains 2 enumerated properties (suite - hearts diamonds spades and clubs) and card value from 2 to A. And overrides ToString() method to returns something like Ah Ad etc. All ok, but enum value can't starts with number, therefore my card value enumerated looks like x2, x3, x4 ... it is not beautiful.
Also need simple approach to parse few cards from single string.
Who know the best approach to design this class?
Couldn't you assign Jack, Queen, King, and Ace to be 11, 12, 13, and 14, respectively? It'd end up looking something like:
public class Card
{
public int Value { get; private set; }
public enum SuitType
{
Clubs, Spades, Hearts, Diamonds
}
public SuitType Suit { get; private set; }
public Card(int value, SuitType suit)
{
Suit = suit;
Value = value;
}
public Card(string input)
{
if (input == null || input.Length < 2 || input.Length > 2)
throw new ArgumentException();
switch (input[0])
{
case 'C': case 'c':
Suit = SuitType.Clubs;
break;
case 'S': case 's':
Suit = SuitType.Spades;
break;
case 'H': case 'h':
Suit = SuitType.Hearts;
break;
case 'D': case 'd':
Suit = SuitType.Diamonds;
break;
default:
throw new ArgumentException();
}
int uncheckedValue = (int)input[1];
if (uncheckedValue > 14 || uncheckedValue < 1)
throw new ArgumentException();
Value = uncheckedValue;
}
public string encode()
{
string encodedCard = "";
switch (Suit)
{
case SuitType.Clubs:
encodedCard += 'c';
break;
case SuitType.Spades:
encodedCard += 's';
break;
case SuitType.Hearts:
encodedCard += 'h';
break;
case SuitType.Diamonds:
encodedCard += 'd';
break;
}
encodedCard += (char) Value;
return encodedCard;
}
public override string ToString()
{
string output = "";
if (Value > 10)
{
switch (Value)
{
case 11:
output += "Jack";
break;
case 12:
output += "Queen";
break;
case 13:
output += "King";
break;
case 14:
output += "Ace";
break;
}
}
else
{
output += Value;
}
output += " of " + System.Enum.GetName(typeof(SuitType), Suit);
return output;
}
}
Edit:
I added some string functionality.
I took structure of Card(string input) from Jon Hanna's answer.
There's an obvious numeric value for the pip-cards, and we can add J=11, Q=12, K=13.
It may be more convenient to have A=14 than A=1 depending on the game being modelled (so one can more simply compute different relative values of hands).
Enums gives no real advantage, especially since enums allow out-of-range values unless you explicitly check for them (e.g. there is nothing to stop someone assigning (CardValue)54 to the card-value enumeration value).
ToString can be aided with an array of the values {null,"1","2","3","4","5","6","7","8","9","10","J","Q","K"}. Likewise {'♥','♦','♠','♣'} could give a nicer output.
Parsing always trickier than outputting a string, even if you are very strict in what you accept, as you have to deal with the potential for invalid input. A simple approach would be:
private Card(string input)
{
if(input == null)
throw new ArgumentNullException();
if(input.length < 2 || input.length > 3)
throw new ArgumentException();
switch(input[input.Length - 1])
{
case 'H': case 'h': case '♥':
_suit = Suit.Hearts;
break;
case 'D': case 'd': case '♦':
_suit = Suit.Diamonds;
break;
case 'S': case 's': case '♠':
_suit = Suit.Spades;
break;
case 'C': case 'c': case '♣':
_suit = Suit.Clubs;
break;
default:
throw new ArgumentException();
}
switch(input[0])
{
case "J": case "j":
_cardValue = 11;
break;
case "Q": case "q":
_cardValue = 12;
break;
case "K": case "k":
_cardValue = 13;
break;
case "A": case "a":
_cardValue = 1;
break;
default:
if(!int.TryParse(input.substring(0, input.Length - 1), out _cardValue) || _cardValue < 2 || _cardVaue > 10)
throw new ArgumentException;
break;
}
}
public static Card Parse(string cardString)
{
return new Card(cardString);
}
You might want to add a static method that read a larger string, yield returning cards as it parsed, to allow for easier encoding of several cards.
When I first started on the card.dll, I was using enumerations for suits and card rankings but then I didn't want to have to deal with that same issue and writing extra code to compensate for the strings, there for I wrote a abstract class Info with only two variables
(Flag (byte)) and (Name(string)) to be implemented by the Rank class and Suit class which would be members of the Card class. I have found this to work a lot better for naming conventions and filtering purposes. I love using enums but having to work around variable naming can be a hassle so sometimes it is best not to if you have to get the variable name as string.
So when the Card constructor get called the card ID is entered and then it passes into the Rank and Suit which will then separate what the ID means in code (101 = 100 (suit flag) +
1 (rank flag)). The protected abstract SetName(int cardID) and SetFlag(int cardID) while handle the rest from there in the info's constructor via Rank and Suit. No more issues with the enumeration and it can still be filtered by number via the Flag.
This card naming system uses a 1 through 4 * 100 (telling the suit flag) + 1 through 13 (for card rank). 500 + 14 through 16 are Little Joker, Big Joker, and Wild.
public class Card
{
short id;
public Card(string zFile)
{
this.id = Convert.ToInt16(zFile.Split('.')[0].Trim());
this.Rank = new Rank(id);
this.Suit = new Suit(id);
}
public override string ToString()
{
if (Suit.Flag == 5)
return Suit.Name;
return string.Concat(Rank.Name, " of ", Suit.Name);
}
public override int GetHashCode()
{
return id;
}
public Rank Rank { get; private set; }
public Suit Suit { get; private set; }
public static Card GetGreaterRank(Card value1, Card value2)
{
return (value1.Rank >= value2.Rank) ? value1 : value2;
}
public static bool CompareRank(Card value1, Card value2)
{
return (value1.Rank.Flag == value2.Rank.Flag);
}
public static bool CompareSuit(Card value1, Card value2)
{
return (value1.Suit.Flag == value2.Suit.Flag);
}
};
public abstract class Info
{
protected Info(short cardID)
{
Flag = SetFlag(cardID);
}
protected string SetName(short cardID, params string[] names)
{
for (int i = 0; i < names.Length; i++)
{
if (Flag == (i + 1))
return names[i];
}
return "Unknown";
}
protected abstract byte SetFlag(short cardID);
public static implicit operator byte(Info info)
{
return info.Flag;
}
public byte Flag { get; protected set; }
public string Name { get; protected set; }
};
public class Rank : Info
{
internal Rank(short cardID) : base(cardID)
{
string name = SetName(cardID, "A","2","3","4","5","6","7",
"8","9","10","J","Q","K","Little Joker","Big Joker","Wild");
Name = (name == "Unknown") ? string.Concat(name, " Rank") : name;
}
protected override byte SetFlag(short cardID)
{
return Convert.ToByte(cardID.ToString().Remove(0, 1));
}
};
public class Suit : Info
{
internal Suit(short cardID) : base(cardID)
{
string name = SetName(cardID,"Clubs","Diamonds","Hearts","Spades");
Name = (name == "Unknown") ? string.Concat(name, " Suit") ? name;
}
protected override byte SetFlag(short cardID)
{
return Convert.ToByte(cardID.ToString().Remove(1));
}
};
So now if you have your card image file named 101.png and pass it into the Card ctor it will pass to the Rank and Suit getting the info for you. Really all you are doing in giving the image file a code(numeric) for a name.
I would probably start out with 2 enums, 1 representing the Suits and 1 representing the Faces. Then declare a public property "Suit" and a public property "Face" based off of these enums. You will also probably need an array with the different unique values that a card can have (i.e. 1 throught 13).
You can start enums with number (although it is preferred to start at zero)
public enum Card
{
Two = 2,
Three,
Four,
...
}
Scratch what I wrote before, this is better.
using System;
enum Suit
{
Clubs,
Hearts,
Diamonds,
Spades
}
class Card
{
Suit Suit
{
get;
private set;
}
int Value
{
get;
private set;
}
Card(Suit suit, int value)
{
Suit = suit;
Value = value;
}
private const string[] valsToString = new string[] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
bool IsValid()
{
return Value >= 2 && Value <= 14;
}
override string ToString()
{
return string.Format("{0} of {1}", valsToString[Value - 2], Suit);
}
}
Related
Hello I'm a first year student and for one of our assignements about C# we are asked to make a blackjack game which includes a Playing Card Class with a property "cardValue" which has an int value from 1 to 13 and a property "description" which has the name of the card "1, 2, ..., Jack, Queen, King". I'm trying to use the "cardValue" property to determine the "description".
This is the code I have written so far:
class Card
{
public int Cardvalue{ get; private set; } = new Random().Next(1, 14);
private string description;
public string Description
{
get { return description; }
set
{
switch (Cardvalue)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
description= Cardvalue.ToString();
break;
case 11:
description= "Jack";
break;
case 12:
description= "Queen";
break;
case 13:
description= "King";
break;
}
}
}
}
I already figured out it doesn't work because I can't use "cardValue" to determine "description". But I'm struggling with finding a solution which does work. I know it may possibly be a very basic question but I would really appreciate some help!
You are missing something important the suit !
This is a potential solution:
public enum Suit
{
Clubs,
Diamonds,
Spades,
Hearts
}
public class Card
{
public string DisplayName { get; set; }
public Suit Suit { get; set; }
public int Value { get; set; }
}
Leverage of the built-in Queue<> object in C#. That collection has almost all the functionality we need to implement a deck of cards.
public static class DeckCreator
{
public static Queue<Card> CreateCards()
{
Queue<Card> cards = new Queue<Card>();
for(int i = 2; i <= 14; i++)
{
foreach(Suit suit in Enum.GetValues(typeof(Suit)))
{
cards.Enqueue(new Card()
{
Suit = suit,
Value = i,
DisplayName = GetShortName(i, suit)
});
}
}
return Shuffle(cards);
}
}
Finally, we need to handle the GetShortName() method. We don't want to be displaying "Ace of Spades" every time that card appears, rather we want to show the short name "AS" (or "5C" for 5 of Clubs, or "KD" for King of Diamonds, and so on). So we need a method to assign this short name (change it to our needs ie: replace J with Jack) to each card, like so:
public static class DeckCreator
{
public static Queue<Card> CreateCards() { ... }
private static string GetShortName(int value, Suit suit)
{
string valueDisplay = "";
if (value >= 2 && value <= 10)
{
valueDisplay = value.ToString();
}
else if (value == 11)
{
valueDisplay = "J";
}
else if (value == 12)
{
valueDisplay = "Q";
}
else if (value == 13)
{
valueDisplay = "K";
}
else if (value == 14)
{
valueDisplay = "A";
}
return valueDisplay + Enum.GetName(typeof(Suit), suit)[0];
}
}
I solved it this way, using the way #madreflection described and #Flydog57 clarified:
class Card
{
public int Cardvalue{ get; private set; } = new Random().Next(1, 14);
public string Description
{
get
{
switch (Cardvalue)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
return Cardvalue.ToString();
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
default:
return "";
}
}
}
}
P.S. I first also tried using a constructor like #Taekahn suggested but I couldn't figure out how I would have to do that as it didn't seem to want to work either. So any help is still welcomed.
I also know that jack, queen and king is worth 10 in blackjack but the assignement told us to use the values of 11, 12 and 13.
I have written this method that "guesses" a correct font-awesome icon to apply to an expense based on user input from a form submission - validation is done before calling this method. If no conditions match, it returns a generic icon:
public static class IconService
{
public static string GuessExpenseIcon(string input)
{
string expenseName = input.ToLower();
string expenseIcon;
switch (expenseName)
{
case string a when a.Contains("phone"):
case string b when b.Contains("mobile"):
expenseIcon = "fas fa-mobile-alt";
break;
case string a when a.Contains("rent"):
case string b when b.Contains("mortgage"):
case string c when c.Contains("house"):
case string d when d.Contains("flat"):
case string e when e.Contains("apartment"):
expenseIcon = "fas fa-home";
break;
case string a when a.Contains("gas"):
case string b when b.Contains("util"):
expenseIcon = "fas fa-burn";
break;
case string a when a.Contains("electric"):
case string b when b.Contains("power"):
expenseIcon = "fas fa-bolt";
break;
case string a when a.Contains("petrol"):
case string b when b.Contains("diesel"):
case string c when c.Contains("fuel"):
expenseIcon = "fas fa-gas-pump";
break;
case string a when a.Contains("food"):
case string b when b.Contains("groceries"):
case string c when c.Contains("eat"):
case string d when d.Contains("take"):
expenseIcon = "fas fa-utensils";
break;
case string a when a.Contains("water"):
expenseIcon = "fas fa-shower";
break;
case string a when a.Contains("car"):
case string b when b.Contains("van"):
expenseIcon = "fas fa-car";
break;
case string a when a.Contains("internet"):
case string b when b.Contains("network"):
expenseIcon = "fas fa-wifi";
break;
case string a when a.Contains("spotify"):
expenseIcon = "fab fa-spotify";
break;
case string a when a.Contains("bus"):
case string b when b.Contains("coach"):
expenseIcon = "fas fa-bus";
break;
case string a when a.Contains("charity"):
case string b when b.Contains("donation"):
expenseIcon = "fas fa-hand-holding-heart";
break;
case string a when a.Contains("aws"):
expenseIcon = "fab fa-aws";
break;
default:
expenseIcon = "fas fa-money-bill-alt";
break;
}
return expenseIcon;
}
}
My question is: is a switch statement this large the best way to achieve this?
I know I may just be prematurely optimizing as I haven't noticed negative performance but for some reason it just doesn't seem right to me.
I'd define a Dictionary and use that:
public static class IconService
{
private static Dictionary<string, string> _expenseIcons = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
{ "phone", "fa-mobile-alt" },
{ "mobile", "fa-mobile-alt" },
{ "rent", "fa-mobile-alt" },
{ "mortgage", "fa-mobile-alt" },
{ "house", "fa-mobile-alt" },
{ "flat", "fa-mobile-alt" },
{ "apartment", "fa-mobile-alt" }
/* etc */
};
public static string GuessExpenseIcon(string input)
{
if (_expenseIcons.TryGetValue(input, out string expenseIcon)) // if the icon is found in the dictionary
{
return $"fas {expenseIcon}";
}
// default
return "fas fa-money-bill-alt";
}
}
You've said that you need to kind of guess it from a string, a naive tokenization (splitting by string) might work:
public static string GuessExpenseIcon(string input)
{
string[] parts = input.Split();
foreach (string part in parts)
{
if (_expenseIcons.TryGetValue(part, out string expenseIcon)) // if the icon is found in the dictionary
{
return $"fas {expenseIcon}";
}
}
// default
return "fas fa-money-bill-alt";
}
I've used a case-insensitive string comparer for the dictionary key, so you don't need to do the .ToLower() bit. I've also taken the common "fas" part of the icon out from what we store in the dictionary, since every icon has it.
I don't think this is necessarily better than your switch statement, but it's more elegant solution, and it also opens up the possibility of changing how _expenseIcons is configured. For example, you could load it from a config file, etc.
In terms of efficiency, we're initialising the dictionary once for the lifetime of the application / appdomain. The efficiency of lookups in the dictionary itself is close to O(1):
Retrieving a value by using its key is very fast, close to O(1), because the Dictionary<TKey,TValue> class is implemented as a hash table.
How about this?
class MatchPattern
{
public string [] Patterns {get;set;}
// This is to asume that you have more logic
// If all the logic of GetIcon is to return a simple string, you can
// replace with
// public string IconName {get;set;}
// instead.
public Func<string, string> GetIcon {get;set;}
}
public class IconService
{
private MatchPatterns[] _patterns;
public IconService()
{
_patterns=new []
{
new MatchPattern
{
Patterns=new[]{"phone", "mobile"},
GetIcon=(x)=>"fas fa-mobile-alt"
},
new MatchPattern
{
Patterns=new[]{"rent", "mortgage", "house", "flat", "apartment"},
GetIcon=(x)=>"fas fa-home"
}
,
// Here is more
}
}
public static string GuessExpenseIcon(string input)
{
foreach(var pattern in _patterns)
{
if(pattern.Any(item=>input.contains(item))
{
return pattern.GetIcon.Invoke(input);
}
}
}
}
The use of Func is to cover the scenario that you still have some slightly different logic between different cases. If that is not the case, you can use a simply IconName instead of GetIcon Func.
class MatchPattern
{
public string [] Patterns {get;set;}
public string IconName {get;set;}
}
You can work with a list for each case
foreach (var str in new string[] { "phone","mobile" }) {
if (expenseName.Contains(str)){
expenseIcon = "fas fa-mobile-alt";
}
}
It might also be worth to look into Lambda expressions, those have a shorter syntax in Java, but i am not sure how well this is supported in C#
I have a method that I would like to pass an option to, but as a switch. It is hard for me to explain so I will include the class:
class Special
{
int Var1;
int Var2;
public void Add1(int option, int variable)
{
int ToReturn;
ToReturn = variable + 1;
switch(option)
{
case 1:
Var1 = ToReturn;
break;
case 2:
Var2 = ToReturn;
break;
}
}
}
Is there a way I can limit the value of option to 1 or 2? Can I add any information to the Add1 so that when it is called it informs the programmer as to which values it may be, and what each option does?
I have given an example class, but my class contains many more variables this may apply to.
Yes:
public enum Option
{
Option1,
Option2
}
public void Add1(Option option, int variable)
{
int ToReturn;
ToReturn = variable + 1;
switch(option)
{
case Option1:
Var1 = ToReturn;
break;
case Option2:
Var2 = ToReturn;
break;
}
}
Looks like you want to use enumerations, basically placeholders for statically defined values.
I could think of a number of ways to do what you are asking however if I was in your scenario (providing I have understood your question correctly) I would be using an enum...
enum Options {
Option1 = 1,
Option2 = 2
}
class Special
{
int Var1;
int Var2;
public void Add1(Options option, int variable)
{
int ToReturn;
ToReturn = variable + 1;
switch(option)
{
case Options.Option1:
Var1 = ToReturn;
break;
case Options.Option2:
Var2 = ToReturn;
break;
}
}
}
To give some background I'm trying to solve the Project Euler Problem 54 involving poker hands. Though there's infinite approaches to this. What I would like to do is enumerate through a list of strings, for example:
{ "8C", "TS", "KC", "9H", "4S" };
I would like to "get" an instance of class card with properties value, and suit, for each respective string. I've not yet utilized get/set so maybe there is an obvious approach to this I'm missing.
Ultimately I would like to have a list of objects type Card, I don't mind building all the card's ahead of time, such that "2H" returns an instance of type Card where suit = Hearts, and value = 2, for example.
I know this code is wrong, but it should give an idea of what I'm trying to do. Any suggestions would be appreciated.
class Card
{
public string suit;
public int value;
public string cardname
{
get
{
if (cardname == "2H") Card TwoH = new Card();
TwoH.suit = "Hearts"
TwoH.value = 2;
return TwoH;
}
}
}
Why not make a constructor that fills suit and value based on a string parameter
public Card(string name)
{
switch(name)
{
case "2H":
this.suit = "Hearts";
this.value = 2;
break;
//...
}
}
This might not be the exact solution you seem to be asking for but if the values you'll be getting (eg 2H, 3C etc) are all 2 characters long, then you can try this:
public class Card
{
public string suit { get; set; }
public int value { get; set; }
public static Card GetCard(string cardName)
{
string tmpSuit;
int tmpValue;
char[] cardNameParts = cardName.ToCharArray();
switch(charNameParts[0])
{
case "A":
tmpValue = 1;
break;
case "2":
tmpValue = 2;
break;
...
}
switch(charNameParts[1])
{
case "H":
tmpSuit= "Hearts";
break;
case "C":
tmpSuit= "Clubs";
break;
...
}
return new Card() { suit = tmpSuit, value = tmpValue };
}
}
I would do it like that:
public class Card
{
public string Suit { get; set; }
public int Value { get; set; }
public static Card FromString(string s)
{
if (s == "2H") return new Card() { Suit = "Hearts", Value = 2 };
else if (s == "....")
...
else return null;
}
}
I have converted your suit and value field into properties and instead of some getter method which in your case wouldn't work I have added a static method.
You can use it like this Card card2H = Card.FromString("2H");
Maybe use two switch statements, first
switch (cardname[0])
{
...
}
then
switch (cardname[1])
{
...
}
Before that, check that cardname.Length == 2. In each switch, have a default section where you throw an exception in case the char value doesn't make sense.
Say I have a list of member, each of which is a custom object:
public class pail
{
public string milk;
public string water;
public string butter;
public string beer;
}
public class AddToPail()
{
private List<pail> _pailList = new List<pail>();
PSVM(String[] args)
{
for(int i = 0; i < 200; i++)
{
pail newPail = new Pail();
switch(i)
{
case 1:
{
newPail.milk = "This pail has milk";
}
break;
case 2:
{
newPail.butter = "This pail has butter";
}
break;
case 3:
{
newPail.water = "This pail has water";
}
break;
case 4:
{
newPail.beer = "This pail has beer";
}
break;
}
_pailList.Add(newPail);
}
foreach (pail thisPail in _pailList)
{
using (StreamWriter SW = new StreamWriter(#"C:\pail.txt")
{
if (!thisPail.milk.IsNullOrEmpty())
{
SW.WriteLine(thisPail.milk);
}
else if (!thisPail.butter.IsNullOrEmpty())
{
SW.WriteLine(thisPail.butter);
}
else if (!thisPail.beer.IsNullOrEmpty())
{
SW.WriteLine(thisPail.beer);
}
else if (!thisPail.water.IsNullOrEmpty())
{
SW.WriteLine(thisPail.water);
}
else
{
Console.Writeline("oops");
}
}
}
}
}
Say I want to set up a StreamWriter that only prints the true values without having to write a million if, else if, else statements... is there an easy way or library to do this in C#? I'm basically looking for a way to only print out true values in a neat, concise way. Does anyone have any advice as to how I should approach this?
Thank you very much!
EDIT
So the ultimate goal of this is that I have an object that has around 20 members. The object is automatically populated, and the populating script can leave some of the members empty. I'd like to be able to print the members in a CSV format, and not have to have 20 if statements to see if a particular member in the object has been instantiated before outputting via the streamwriter.
Edit 2
I changed my code to be a little closer to what I needed it to do. Sorry for the previous poor explanation.
I think you should refactor your program a little bit. For starters, I would use an enum for bucket contents:
public enum EBucketContents { Milk, Water, Butter, Beer };
Then, instead of having a list of booleans, you can use a dictionary:
var pail = Dictionary<EBucketContents,bool>();
Now it's a simple matter to only output the ones that are true:
foreach( var kvp in pail.Where( x => x.Value ) ) {
SW.WriteLine( "pail has " + kvp.Key.ToString().ToLower() )
}
If you just want to save some typing, use this extension method:
internal static class Extensions
{
public static void WriteLineIf(this TextWriter tw, bool condition, string text)
{
if (condition)
{
tw.WriteLine(text);
}
}
}
But it looks like only one of those bools can be true, since you're using else if blocks.
In that case, use and enum
internal enum Pail
{
Butter,
Milk,
Water,
Beer
}
Can you just use a Dictionary where the key is the field name and the value is the fields value. This way you don't need to check if the output is filled or not - you just output all fields
Your populating script can populate the dictionary keys only if they are set
Then your streamwriter can just go
foreach(KeyValuePair<string, string> kvp in fieldsDict)
sw.Write("Key: " + kvp.Key + ", Value: " + kvp.Value);
Or even just a list of string/or enum
e.g.
public class pail
{
public List<string> Fields = new List<string>();
}
public class AddToPail()
{
private List<pail> _pailList = new List<pail>();
PSVM(String[] args)
{
for(int i = 0; i < 200; i++)
{
pail newPail = new Pail();
switch(i)
{
case 1:
{
newPail.Fields.Add("This pail has milk");
}
break;
*** SNIP
Of course using a Dictionary could solve your problem , but I'm not really fond of this kind of solution, since it makes you lose some control over what you are putting in, e.g you could end up with a pail having airplanes... I'd refactor your code in something like this, trying to give every class its own responsabilities (BTW I don't like AddToPail as a class name, it's more a method name):
public class Pail
{
public string milk;
public string water;
public string butter;
public string beer;
private bool everythingEmpty = true;
public Pail(int i)
{
switch(i)
{
case 1:
{
milk = "This pail has milk";
everythingEmpty = false;
}
break;
case 2:
{
butter = "This pail has butter";
everythingEmpty = false;
}
break;
case 3:
{
water = "This pail has water";
everythingEmpty = false;
}
break;
case 4:
{
beer = "This pail has beer";
everythingEmpty = false;
}
break;
}
}
public void WriteToStream(StreamWriter SW)
{
if (everythingEmpty)
{
Console.Writeline("oops");
return;
}
WriteToStream(milk, SW);
WriteToStream(butter, SW);
WriteToStream(beer, SW);
WriteToStream(water, SW);
}
public static void WriteToStream(string content, StreamWriter SW)
{
if (!content.IsNullOrEmpty())
{
SW.WriteLine(content);
}
}
}
public class AddToPail()
{
private List<pail> _pailList = new List<pail>();
PSVM(String[] args)
{
for(int i = 0; i < 200; i++)
{
pail newPail = new Pail(i);
_pailList.Add(newPail);
}
foreach (pail thisPail in _pailList)
{
using (StreamWriter SW = new StreamWriter(#"C:\pail.txt")
{
thisPail.WriteToStream(SW);
}
}
}
}