Goal: Learn C# and ASP.NET MVC
We will build a self-checkout machine used in a supermarket together.
Implement new requirements to store.rb and store_test.rb. Reading Ruby syntax will be useful to do this task.
-
Receipts show costs including cents (example: apple $5.00)
-
Customers can purchase products which returns the receipt
store.Purchase(new string[] {"0001", "0002"}) //=>
apple $5.00
orange $10.00
total $15.00
- Owner can view purchase summary as data array
store.PurchaseSummary() //=>
[
["Time","Number of Products","Cost"],
["17/07/2015","2","20.00"],
["18/07/2015","1","15.99"]
]
- Owner can add discount to a product
store.AddDiscount("0001", 1) //=> $1 discount to product "0001"
- Owner can delete discount to a product
store.DeleteDiscount("0001") //=> delete discount to product "0001"
git checkout task1
Create Store.cs to Countdown project
Create StoreTest.cs to Countdown.Tests project
TDD to implement requirements
User can add an item to store
store.AddItem("0001", "apple", 10) // add $10 apple with barcode 0001
User can count stored items
store.ItemCount() //=> 10 (10 items)
User can calculate total cost of given items
store.CalculateCost(new string[] {"0001", "0001"}) //=> 20 (two apples costs $20)
User can print receipt of given items
store.PrintReceipt(new string[] {"0001", "0001"}) //=>
apple $10
apple $10
total $20
Install Visual Studio 2013
Here is basic ruby syntax useful to this task.
public class Item
{
public Item()
{
}
}
public class Item
{
public Item(string name)
{
}
}
Item item1 = new Item();
Item item2 = new Item("apple");
var item2 = new Item("apple"); // var represent any type
public class Item
{
private string name;
public Item(string n)
{
name = n;
}
public print()
{
Console.WriteLine(name);
}
}
new Item("apple").print(); //=> "apple"
- declear method
public class Item
{
public int price(int quantity)
{
return 10 * quantity;
}
}
new Item("apple").print(); //=> "apple"
- call method
int price = new Item().price(5); //=> 50
- declear default accessors
public class Item
{
// Use accessors if variable should be accessable from outside
public string Name { get; set; } // name starts with capital letter
public Item(string name)
{
Name = name;
}
}
// Access data through set accessor
Item item = new Item("apple");
item.Name //=> "apple"
- if statement
if (1 == 1)
{
Console.WriteLine("case1");
}
else if (2 == 2)
{
Console.WriteLine("case2");
}
else
{
Console.WriteLine("case3");
}
end
- switch statement
switch (1)
{
case 1:
Console.WriteLine("case1");
break;
case 2:
Console.WriteLine("case2");
break;
default:
Console.WriteLine("default");
break;
}
- create string
string name = "apple";
- append string
string name = "apple";
Console.WriteLine(name + " orange"); //=> apple orange
- combine strings
string name = "apple";
Console.WriteLine("having {0}", name); //=> having apple
Array size can't be changed after initilized.
Values in Array can be changed.
- create array with data
string[] array = new string[] {"apple", "orange"};
- get length
string[] array = new string[] {"apple", "orange"};
array.Length; //=> 2
- change data
string[] array = new string[] {"apple", "orange"};
array[0] = "jazz apple";
- get index
string[] array = new string[] {"apple", "orange"};
Array.IndexOf(array, "apple"); // => 0
// Array class provides many methods used in List class
- shorten initializer
// new string[] can be skipped in this case (won't work with 'var array')
string[] array = {"apple", "orange"};
[More info](https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110) .aspx)
- create empty list
List<string> list = new List<string>();
- create list with data
List<string> list = new List<string>() {"apple", "orange"};
- add item to array
List<string> list = new List<string>();
list.Add("milk");
- get item by index
List<string> list = new List<string>() {"apple"};
list[0]; //=> "apple"
- find index of item
List<string> list = new List<string>() {"apple", "orange"};
list.IndexOf("apple"); //=> 0
- loop through each item
List<string> list = new List<string>() {"apple", "orange"};
foreach (string item in list)
{
Console.WriteLine(item);
}
list.ForEach(item => Console.WriteLine(item)); // Short version
- map to create a new array with different value
List<int> list = new List<int>() {1, 2, 3};
List<int> newList = list.Select(item => item * 3); //=> [3, 6, 9]
- Inject to compose data
// update 0 5 times (0 -> 1 -> 3 -> 6 -> 10 -> 15) and return
List<int> list = new List<int>() {1, 2, 3, 4, 5};
int sum = list.Aggregate(0, (total, number) => total + number); //=> 15
-
namespace using System.Collections.Generic;
-
create empty dictionary
Dictionary<string, int> dic = new Dictionary<string, int>();
// key => string, value => int
- add key and value pair
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("key1", 100); // exception on exisiting key
dic["key1"] = 100; // override value on existing key
- get value from key
Dictionary<string, int> dic = new Dictionary<string, int>();
hash = {"key" => "value"}
hash["key"] //=> "value" // exception to key not exist
-
namespace using Microsoft.VisualStudio.TestTools.UnitTesting;
-
basic format
[TestClass]
public class StoreTest // Test Store class
{
[TestInitialize]
public void InitWithSomething()
{
// Called before each test
}
[TestMethod]
public void TestSomething()
{
// Single test
Assert.AreEqual(2, 1 + 1);
}
}
- assert two value
int expected = 2;
int actual = 1 + 1;
Assert.AreEqual(expected, actual);
- assert two lists
var a = new List<int> {1, 2, 3};
var b = new List<int> {1, 2, 3};
CollectionAssert.Equals(a, b);