02 - Working With Abstraction
02 - Working With Abstraction
Table of Contents
• Project Architecture
– Methods
– Classes
– Projects
• Code Refactoring
• Enumerations
• Static Classes
• Namespaces
2
Splitting Code into Logical Parts
• Project Architecture
Splitting Code into Methods
position[0] = row – 1
position[0] = row + 1 ChangeRow(desiredRow
)
position[0] = row + 3
4
Example: Code Without Methods
5
Example: Code with Methods
foreach (char m in
moves)
{
MoveEnemies();
KillerCheck();
MovePlayer(move);
}
6
Splitting Code into Methods (2)
Console.WriteLine
Override .ToString() to set (bankAcc.ToString())
a global printing format ; 7
Splitting Code into Methods (3)
void Withdraw ( … )
void Deposit ( … )
decimal GetBalance ( …
)
8
Problem: Rhombus of Stars
* * *
* * * *
* * *
*
* *
*
9
Solution: Rhombus of Stars
10
Solution: Rhombus of Stars (2)
11
Splitting Code into Classes
13
Problem: Point in Rectangle
15
Solution: Point in Rectangle (2)
16
Solution: Point in Rectangle (3)
• Refactoring
Refactoring
20
Problem: Student System
21
Syntax and Usage
• Enumerations
Enumerations
23
Enumerations (2)
25
Solution: Hotel Reservation
26
Solution: Hotel Reservation (2)
27
Solution: Hotel Reservation (3)
decimal priceBeforeDiscount =
numberOfDays * pricePerDay * multiplier;
decimal discountedAmount =
priceBeforeDiscount * discountMultiplier;
decimal finalPrice = priceBeforeDiscount - dis-
countedAmount;
return finalPrice; }
}
28
Static Class Members
• Static Classes
Static Class
30
Static Members
31
Static Members (2)
32
Example: Static Members
• Namespaces
Namespaces
System.Collections.Generic.List<int>();
35
Summary
Well
… organized code is easier to work with
We
…can reduce complexity using
Methods
… , Classes and Projects
Enumerations define a fixed set of constants
Static classes cannot be instantiated
Namespaces organize classes
36
Exercises
• Time to practices