First day

This commit is contained in:
2015-06-18 10:52:10 +02:00
parent b28cbf7ad2
commit 1a61a2a7f7
36 changed files with 1566 additions and 0 deletions

42
Exercise1/Exercise1.sln Normal file
View File

@@ -0,0 +1,42 @@
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercise1", "Exercise1\Exercise1.csproj", "{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StoreData", "StoreData\StoreData.csproj", "{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Debug|Any CPU.ActiveCfg = Debug|x86
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Debug|Mixed Platforms.Build.0 = Debug|x86
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Debug|x86.ActiveCfg = Debug|x86
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Debug|x86.Build.0 = Debug|x86
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Release|Any CPU.ActiveCfg = Release|x86
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Release|Mixed Platforms.ActiveCfg = Release|x86
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Release|Mixed Platforms.Build.0 = Release|x86
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Release|x86.ActiveCfg = Release|x86
{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}.Release|x86.Build.0 = Release|x86
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Debug|x86.ActiveCfg = Debug|Any CPU
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Release|Any CPU.Build.0 = Release|Any CPU
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,27 @@
using StoreData;
namespace Exercise1
{
public class Account : IAccount
{
private decimal balance = 0;
public decimal Balance
{
get { return balance; }
}
public void Add(decimal amount)
{
balance += amount;
}
public void Remove(decimal amount)
{
if (!(amount > balance))
{
balance -= amount;
}
}
}
}

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{BE0E4F97-1C52-4DD1-9229-10CDDFB6661F}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Exercise1</RootNamespace>
<AssemblyName>Exercise1</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Account.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ShoppingCart.cs" />
<Compile Include="ShoppingDemo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StoreData\StoreData.csproj">
<Project>{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}</Project>
<Name>StoreData</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,23 @@
using System;
using System.Linq;
using System.Text;
namespace Exercise1
{
class Program
{
static void Main(string[] args)
{
// Setup a test account
var account = new Account();
account.Add(1500);
// Create an empty shopping cart
var cart = new ShoppingCart();
// Run the demo
var demo = new ShoppingDemo(cart, account);
demo.Run();
}
}
}

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Resources;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Exercise1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Exercise1.Properties")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7A332E26-38F4-4649-B445-3BAFDBD15431")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: NeutralResourcesLanguageAttribute("sv-SE")]

View File

@@ -0,0 +1,47 @@
using System.Collections.Generic;
using StoreData;
namespace Exercise1
{
public class ShoppingCart : IShoppingCart
{
private List<Product> products = new List<Product>();
public void AddProduct(Product p)
{
products.Add(p);
}
public void RemoveProductByNumber(int index)
{
products.RemoveAt(index);
}
public void Clear()
{
products.Clear();
}
public decimal TotalSum()
{
decimal sum = 0;
foreach (var product in Products)
{
sum += product.Price;
}
return sum;
}
public bool CartIsEmpty()
{
return (products.Count == 0);
}
public List<Product> Products
{
get { return products; }
}
}
}

View File

@@ -0,0 +1,293 @@
using System;
using System.Collections.Generic;
using StoreData;
namespace Exercise1
{
public class ShoppingDemo
{
private IShoppingCart cart;
private IAccount account;
private List<Product> store;
/// <summary>
/// Constructor
/// </summary>
/// <param name="cart"></param>
/// <param name="account"></param>
public ShoppingDemo(IShoppingCart cart, IAccount account)
{
PopulateStore();
this.cart = cart;
this.account = account;
}
/// <summary>
/// Populate the store with a bunch of book titles
/// </summary>
private void PopulateStore()
{
store = new List<Product>
{
new Product {Name = "Professional C# 4 and .NET 4", Price = 354},
new Product {Name = "Beginning ASP.NET E-Commerce in C#", Price = 260},
new Product {Name = "Objektorienterad analys och design", Price = 418},
new Product {Name = "Improving Software Organizations", Price = 486},
new Product {Name = "Operating System Concepts", Price = 514},
new Product {Name = "Compilers: Principles, techniques and Tools", Price = 1265},
new Product {Name = "Pro Git", Price = 221},
new Product {Name = "Design Patterns", Price = 315}
};
}
/// <summary>
/// Run the demo application
/// </summary>
public void Run()
{
var running = true;
while (running)
{
PrintMainMenu();
var input = Console.ReadLine();
if (input == "6")
running = false;
else
MakeChoice(input);
}
}
/// <summary>
/// Make a choice from the main menu
/// </summary>
/// <param name="input"></param>
private void MakeChoice(string input)
{
switch (input)
{
case "1":
AddProductDialog();
break;
case "2":
RemoveProductDialog();
break;
case "3":
EmptyShoppingCartDialog();
break;
case "4":
ListProductsDialog();
break;
case "5":
PaymentDialog();
break;
}
}
/// <summary>
/// Print a list of all products
/// </summary>
private void ListProductsDialog()
{
Console.Clear();
Console.WriteLine("List of all products in shopping cart:");
Console.WriteLine("--------------------------------------");
Console.WriteLine();
var listOfProducts = cart.Products;
if (listOfProducts.Count == 0)
Console.WriteLine("-- The shopping cart is empty --");
for (int i=0; i<listOfProducts.Count; i++)
{
Console.WriteLine("{0}) {1}.PRICE: {2} SEK", i+1, listOfProducts[i].Name, listOfProducts[i].Price);
}
Console.WriteLine();
WaitForInput();
}
/// <summary>
/// Shown when it's time to pay
/// </summary>
private void PaymentDialog()
{
Console.Clear();
if (cart.CartIsEmpty())
{
Console.WriteLine("\n -- The cart is empty, you have nothing to pay yet. --\n");
WaitForInput();
return;
}
var sum = cart.TotalSum();
Console.WriteLine("TOTAL : " + sum);
Console.WriteLine("YOUR BALANCE: " + account.Balance);
Console.WriteLine();
if (sum > account.Balance)
{
Console.WriteLine("We're sorry, but you don't have enough funds on your account");
}
else
{
Console.Write("Do you want to pay? (y/n) ");
var input = Console.ReadLine();
if (input.ToLower() == "n")
return;
Console.WriteLine("Thank you! Your order has been processed!");
account.Remove(sum);
cart.Clear();
}
WaitForInput();
}
/// <summary>
/// Clear the shopping cart
/// </summary>
private void EmptyShoppingCartDialog()
{
cart.Clear();
Console.Clear();
Console.WriteLine();
Console.WriteLine(" -- Cleared shopping cart --");
Console.WriteLine();
WaitForInput();
}
/// <summary>
/// Shown when removing a product
/// </summary>
private void RemoveProductDialog()
{
Console.Clear();
Console.WriteLine("List of all products in shopping cart:");
Console.WriteLine("--------------------------------------");
Console.WriteLine();
var listOfProducts = cart.Products;
if (listOfProducts.Count == 0)
{
Console.WriteLine("-- The shopping cart is empty --\n");
WaitForInput();
return;
}
for (int i = 0; i < listOfProducts.Count; i++)
{
Console.WriteLine("{0}) {1}.PRICE: {2} SEK", i + 1, listOfProducts[i].Name, listOfProducts[i].Price);
}
Console.WriteLine();
Console.Write("Select an item to REMOVE: ");
var input = Console.ReadLine();
int coercedInput;
if (!int.TryParse(input, out coercedInput))
{
Console.WriteLine("Must enter a number!");
}
else
{
var products = cart.Products;
if (coercedInput > products.Count || coercedInput <= 0)
Console.WriteLine("Not a valid number!");
else
{
cart.RemoveProductByNumber(coercedInput-1);
Console.WriteLine("Removed item from cart!");
}
}
Console.WriteLine();
WaitForInput();
}
/// <summary>
/// The dialogue shown when adding a product to the cart
/// </summary>
private void AddProductDialog()
{
Console.Clear();
Console.WriteLine("Products in store");
Console.WriteLine("-----------------");
Console.WriteLine();
for (int i = 0; i < store.Count; i++)
{
Console.WriteLine("{0}) {1}. PRICE: {2} SEK", i+1, store[i].Name, store[i].Price);
}
Console.WriteLine();
Console.Write("Enter a product number: ");
var input = Console.ReadLine();
int coercedInput;
if (!int.TryParse(input, out coercedInput))
{
Console.WriteLine("Must enter a number!");
}
else
{
if (coercedInput > store.Count || coercedInput <= 0)
Console.WriteLine("Not a valid number!");
else
{
cart.AddProduct(store[coercedInput-1]);
Console.WriteLine("Added Item!");
}
}
WaitForInput();
}
/// <summary>
/// Shows the main menu
/// </summary>
public void PrintMainMenu()
{
Console.Clear();
Console.WriteLine("Main Menu");
Console.WriteLine("---------");
Console.WriteLine("YOUR BALANCE: " + account.Balance);
Console.WriteLine("TOTAL: " + cart.TotalSum());
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("1. Add product to shopping cart");
Console.WriteLine("2. Remove product from shopping cart");
Console.WriteLine("3. Empty shopping cart");
Console.WriteLine("4. List products in shopping cart");
Console.WriteLine("5. Pay");
Console.WriteLine("6. Exit");
Console.WriteLine();
Console.Write("Make a choice: ");
}
/// <summary>
/// Just a method to wait for the user to press enter
/// </summary>
private void WaitForInput()
{
Console.WriteLine("Press Enter...");
Console.ReadLine();
}
}
}

View File

@@ -0,0 +1,22 @@
namespace StoreData
{
public interface IAccount
{
/// <summary>
/// Check the balance
/// </summary>
decimal Balance { get; }
/// <summary>
/// Add money to this account
/// </summary>
/// <param name="amount">The amount to add</param>
void Add(decimal amount);
/// <summary>
/// Remove money from this account
/// </summary>
/// <param name="amount">The amount to remove</param>
void Remove(decimal amount);
}
}

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
namespace StoreData
{
public interface IShoppingCart
{
/// <summary>
/// This method adds a product to the shopping cart
/// </summary>
/// <param name="p">The product to add</param>
void AddProduct(Product p);
/// <summary>
/// Remove product by number
/// </summary>
/// <param name="index">The index of the product to remove</param>
void RemoveProductByNumber(int index);
/// <summary>
/// Clears the entire shopping cart
/// </summary>
void Clear();
/// <summary>
/// Calculate the sum of the cost of all products
/// </summary>
/// <returns></returns>
decimal TotalSum();
/// <summary>
/// Returns true if cart is empty, otherwise false
/// </summary>
bool CartIsEmpty();
List<Product> Products { get; }
}
}

View File

@@ -0,0 +1,8 @@
namespace StoreData
{
public class Product
{
public decimal Price { get; set; }
public string Name { get; set; }
}
}

View File

@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("StoreData.Properties")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StoreData.Properties")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7A332E26-38F4-4649-B445-3BAFDBD15431")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A3587AB8-ED1A-4C9F-9981-42863DB18AF9}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>StoreData</RootNamespace>
<AssemblyName>StoreData</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IAccount.cs" />
<Compile Include="IShoppingCart.cs" />
<Compile Include="Product.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>