Created
December 8, 2021 14:01
-
-
Save Lanse505/646f54ad399a0f8c1c8f0c545a346e06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Assignment_2_Submission.programs.user.table; | |
namespace Assignment_2_Submission.programs.user{ | |
public class UserProgram : BaseProgram{ | |
private struct PersonStruct{ | |
public PersonStruct(DateTime dob, Gender gender, HairColor hairColor, int lengthOfHair, EyeColor eyeColor){ | |
DoB = dob; | |
Gender = gender; | |
HairColor = hairColor; | |
LengthOfHair = lengthOfHair; | |
EyeColor = eyeColor; | |
} | |
public DateTime DoB { get; set; } | |
public Gender Gender { get; set; } | |
public HairColor HairColor { get; set; } | |
public int LengthOfHair { get; set; } | |
public EyeColor EyeColor { get; set; } | |
} | |
private List<PersonStruct> _users; | |
private static TableWriter TableWriter; | |
public UserProgram() : base("4 - User Storage"){ | |
_users = new List<PersonStruct>(); | |
TableWriter = new TableWriter("Index:", "Date of Birth:", "Gender:", "Hair Color:", "Lenght of Hair:", "Eye Color:"); | |
} | |
public static void RunProgram(){ | |
// Using Struct | |
var person1 = new PersonStruct(new DateTime(1995,09,26), Gender.MALE, HairColor.BROWN, 100, EyeColor.BLUE); // Skapar en ny person utifrån structen | |
// Using Builder | |
var builder = Person.CreatePerson(); // Skapar Buildern | |
builder.dateOfBirth(new DateTime(2000, 01, 01)).gender(Gender.FEMALE).hairColor(HairColor.BLOND).lengthOfHair(100).eyeColor(EyeColor.BROWN); // Setter Parametrarna | |
var person2 = builder.Build(); // "Bygger" Personen | |
builder.dateOfBirth(DateTime.Now).gender(Gender.NON_BINARY).hairColor(HairColor.RED).eyeColor(EyeColor.GREY); | |
var person3 = builder.Build(); | |
TableWriter.AddRow(-1, person1.DoB.ToString().Remove(10), person1.Gender, person1.HairColor, person1.LengthOfHair, person1.EyeColor); | |
TableWriter.AddRow(person2.getIndex(), person2.getDateOfBirth().ToString().Remove(10), person2.getGender(), person2.getHairColor(), person2.getHairLength(), person2.getEyeColor()); | |
TableWriter.AddRow(person3.getIndex(), person3.getDateOfBirth().ToString().Remove(10), person3.getGender(), person3.getHairColor(), person3.getHairLength(), person3.getEyeColor()); | |
TableWriter.Print(); | |
Console.WriteLine(); | |
Console.WriteLine("Press any key to go back to the Main Menu"); | |
Console.ReadKey(); | |
Console.Clear(); | |
Program.ResetProgram(); | |
} | |
} | |
public class Person{ | |
private static List<Person> recordedUsers; // Static stored centralized list of generated objects | |
private int Index; | |
private DateTime DoB; | |
private Gender Gender; | |
private HairColor HairColor; | |
private int LengthOfHair; | |
private EyeColor EyeColor; | |
// Static Constructor to initialize the storage list. | |
static Person() { | |
recordedUsers ??= new List<Person>(); // If Null, Initialize the value as an empty list | |
} | |
// Empty Constructor for use with the Builder | |
private Person() {} | |
// Final Constructor for use with the Builder | |
private Person(DateTime dob, Gender gender, HairColor hairColor, int lengthOfHair, EyeColor eyeColor){ | |
Index = recordedUsers.Count; // Index of the user in the global static list | |
DoB = dob; // Date of Birth [Default: 2000-01-01] | |
Gender = gender; // Gender: Male, Female, Non-Binary [Default: Non-Binary] | |
HairColor = hairColor; // Hair Color: Black, Brown, Auburn, Red, Blond, Grey [Default: Grey] | |
LengthOfHair = lengthOfHair; // Length of Hair [Default: 100] | |
EyeColor = eyeColor; // Eye Color: Brown, Green, Grey, Amber, Hazel, Blue [Default: Grey] | |
recordedUsers.Add(this); // Adds any generated person to a static collection list for the Person Object. | |
} | |
// Use this to generate new Users using the Builder | |
public static PersonBuilder CreatePerson(){ | |
return new(); | |
} | |
// Use this to get Users from the global static storage list | |
public static Person GetPerson(int index) { | |
return recordedUsers[index]; | |
} | |
public void updateGender(Gender newGender){ | |
Gender = newGender; | |
} | |
public void updateHairColor(HairColor newColor){ | |
HairColor = newColor; | |
} | |
public void updateLengthOfHair(int newLength){ | |
LengthOfHair = newLength; | |
} | |
public int getIndex(){ | |
return Index; | |
} | |
public DateTime getDateOfBirth(){ | |
return DoB; | |
} | |
public Gender getGender(){ | |
return Gender; | |
} | |
public HairColor getHairColor(){ | |
return HairColor; | |
} | |
public int getHairLength(){ | |
return LengthOfHair; | |
} | |
public EyeColor getEyeColor(){ | |
return EyeColor; | |
} | |
public class PersonBuilder { | |
private DateTime DoB{ get; set; } | |
private Gender Gender { get; set; } | |
private HairColor HairColor { get; set; } | |
private int LengthOfHair { get; set; } | |
private EyeColor EyeColor { get; set; } | |
public PersonBuilder(){ | |
DoB = new DateTime(2000, 01, 01); | |
Gender = Gender.NON_BINARY; | |
HairColor = HairColor.GREY; | |
LengthOfHair = -1; | |
EyeColor = EyeColor.GREY; | |
} | |
public PersonBuilder dateOfBirth(DateTime dob){ | |
DoB = dob; | |
return this; | |
} | |
public PersonBuilder gender(Gender gender){ | |
Gender = gender; | |
return this; | |
} | |
public PersonBuilder hairColor(HairColor hairColor){ | |
HairColor = hairColor; | |
return this; | |
} | |
public PersonBuilder lengthOfHair(int lengthOfHair){ | |
LengthOfHair = lengthOfHair; | |
return this; | |
} | |
public PersonBuilder eyeColor(EyeColor eyeColor){ | |
EyeColor = eyeColor; | |
return this; | |
} | |
public Person Build(){ | |
return new(DoB, Gender, HairColor, LengthOfHair, EyeColor); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment