Skip to content

Instantly share code, notes, and snippets.

@SophieDePaula
Last active June 26, 2017 20:04
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 SophieDePaula/9707125cc092d83fc31a846d05775d3a to your computer and use it in GitHub Desktop.
Save SophieDePaula/9707125cc092d83fc31a846d05775d3a to your computer and use it in GitHub Desktop.
Code Challenge for Horoscope
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Robot
{
class Program
{
static void Main(string[] args)
{
Robot my_robot = Robot.Instance;
my_robot.InstallChip(new SortChip());
List<int> SortedArray = my_robot.executeChipe().Arr;
my_robot.InstallChip(new TotalChip());
int TotalSum = my_robot.executeChipe().Number;
PrintResults(SortedArray, TotalSum, my_robot.NumOfChips, my_robot.Name);
}
static void PrintResults(List<int> SortedArray, int TotalSum, int NumberOfChips, string Name)
{
string sorted_array = "";
for (int i = 0; i < SortedArray.Count; i++)
{
if (i < SortedArray.Count - 1)
sorted_array += SortedArray[i] + ", ";
else
sorted_array += SortedArray[i];
}
Console.WriteLine("Hi! I'm " + Name + " and thoes are my results:");
Console.WriteLine("Sorted array: " + sorted_array);
Console.WriteLine("Total sum: " + TotalSum.ToString());
Console.WriteLine("Number of chips installed: " + NumberOfChips.ToString());
Console.ReadLine();
}
}
public sealed class Robot
{
private string name = "R2-D2";
private static Robot instance = null;
Chip InstalledChip = null;
private List<Chip> ChipsArchive = new List<Chip>();
public List<int> arr = new List<int>() { 3, 5, 1, 0, 7, 4, 99 };
public string Name
{
get
{
return this.name;
}
}
public static Robot Instance
{
get
{
if (instance == null)
instance = new Robot();
return instance;
}
}
public int NumOfChips
{
get
{
return this.ChipsArchive.Count;
}
}
private Robot() { }
public void InstallChip(Chip New_Chip)
{
ChipsArchive.Add(New_Chip);
InstalledChip = ChipsArchive.Last();
}
public Type executeChipe()
{
return this.InstalledChip.Action(this.arr);
}
}
// I can also use an Interface
public abstract class Chip
{
public virtual Type Action(List<int> arr) { return new Type(); }
}
public class SortChip : Chip
{
public override Type Action(List<int> arr)
{
string asc;
Console.WriteLine("Sort in ascending order? yes/no");
asc = Console.ReadLine();
Type res = new Type();
while (asc.ToLower() != "yes" && asc.ToLower() != "no")
{
Console.WriteLine("Sort in ascending order? yes/no");
asc = Console.ReadLine();
}
if (asc.ToLower() == "yes")
{
arr.Sort();
}
else
{
arr.Sort();
arr.Reverse();
}
res.Arr = arr;
return res;
}
}
public class TotalChip : Chip
{
public override Type Action(List<int> arr)
{
int sum = 0;
Type res = new Type();
for (int i = 0; i < arr.Count; i++)
{
sum += arr[i];
}
res.Number = sum;
return res;
}
}
public class Type
{
int number;
List<int> arr;
public int Number
{
get
{
return this.number;
}
set
{
this.number = value;
}
}
public List<int> Arr
{
get
{
return this.arr;
}
set
{
this.arr = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpeningHours
{
class Program
{
static void Main(string[] args)
{
List<string> days = new List<string> {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
string opening_time1 = "10:00a - 06:00m";
string opening_time2 = "10:00a - 07:00m";
string close = "Closed";
List<string> opening_Time = new List<string>() {opening_time1, opening_time1, opening_time1, opening_time2, opening_time1, opening_time1, close };
Week MyWeek = new Week(days, opening_Time);
string p = "";
for(int i = 0; i < MyWeek.days.Count; i++)
{
p = MyWeek.days[i];
for (int j = 0; j < (10 - days[i].Length); j++)
{
p += " ";
}
p += MyWeek.OpeningTime[i];
Console.WriteLine(p);
}
Console.ReadLine();
}
}
public class Week
{
public List<DayOfWeek> WeekDays = new List<DayOfWeek>();
public List<string> days = new List<string>();
public List<string> OpeningTime = new List<string>();
public Week(List<string> days, List<string> OpeningTime)
{
this.days = days;
this.OpeningTime = OpeningTime;
for(int i = 0; i < this.days.Count; i++)
{
WeekDays.Add(new DayOfWeek(days[i], OpeningTime[i]));
}
}
}
public class DayOfWeek
{
string name;
string OpeningTime;
public DayOfWeek(string name, string opening_time)
{
this.name = name;
this.OpeningTime = opening_time;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment