1. Trang chủ
  2. » Công Nghệ Thông Tin

Assignment 2 Advanced Programming (1651 Distinction)

71 8 3

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Advanced Programming
Tác giả Bui Quang Minh
Người hướng dẫn Nguyen The Nghia
Trường học Btec
Chuyên ngành Computing
Thể loại assignment
Định dạng
Số trang 71
Dung lượng 2,67 MB

Nội dung

Học phần này giới thiệu cho sinh viên các khái niệm cốt lõi của lập trình cùng với phần giới thiệu về các thuật toán và đặc điểm của mô hình lập trình. Trong số các chủ đề trong phần này bao gồm: giới thiệu về thuật toán, lập trình hướng đối tượng và hướng sự kiện, các cân nhắc về bảo mật, môi trường phát triển tích hợp và quy trình gỡ lỗi.

Trang 1

ASSIGNMENT 2 FRONT SHEET

Student declaration

I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism I understand that making a false declaration

is a form of malpractice

Student’s signature Grading grid

Trang 2

Summative Feedback: Resubmission Feedback:

Lecturer Signature:

Trang 3

Table of Contents

INTRODUCTION 4

CHAPTER 1 SCENARIO ANALYSIS 5

I Scenario 5

II Diagrams 6

1 Class diagrams 6

2 UseCase diagrams 9

CHAPTER 2 IMPLEMENTATION 12

I Code 12

1 Fund class 12

2 StockBroker class 13

3 Investor class 15

4 FundService class 18

5 StockBrokerService class 19

6 InvestorService class 21

7 Data class 22

8 CheckValid class 22

9 Main method 24

10 CreateAccountPage class 26

11 BrokerPage class 27

12 InvestorPage class 28

II Program Execution 30

1 Broker 30

2 Investor 33

3 Create Account 35

CHAPTER 3 DISCUSSION 37

I Design patterns 37

1 Creational patterns 37

1.1 Singleton pattern 37

1.2 Factory method pattern 40

2 Structural patterns 43

2.1 Façade pattern 43

Trang 4

2.2 Composite pattern 46

3 Behavioral patterns 49

3.1 Command pattern 49

3.2 Observer pattern 53

II Similar design patterns 56

1 Sington and Factory method patterns 56

1.1 Singleton pattern 56

1.2 Factory method pattern 58

2 Observer and Composite patterns 61

2.1 Observer pattern 61

2.2 Composite pattern 63

3 Detailed explaination 63

3.1 Singleton and Factory method patterns 63

3.2 Observer and composite patterns 64

III Usage of my design pattern 66

1 Observer design pattern 66

2 Singleton pattern 66

CONCLUSION 67

REFERENCE LIST 70

Trang 5

Next, I will dive into the technical side of things We'll look at the code, which is like the instructions that make software work Then, we'll see how these instructions come to life through program execution

In this last chapter, I will discuss design patterns, which are like problem-solving techniques for software Also, explore different design patterns and talk about how they can be used Finally, I will look at the advantages and disadvantages of the design pattern I chose

Trang 6

CHAPTER 1 SCENARIO ANALYSIS

I Scenario

"Have you ever wondered how to make investing in funds easier, especially for people who don't have much experience in this complex world? That's the puzzle I'm tackling with the 'MB Capital Fund Management System.'

Investing can be like putting together a big puzzle There are lots of pieces to consider, from choosing the right funds to managing your money and ensuring everything goes smoothly People often struggle with these parts, and that's where our system comes in

Now, let me introduce you to the 'MB Capital Fund Management System.' It's like a helpful tool we're building to make investing simpler I'm using special techniques to put all these pieces together in a neat and organized way I'll also create easy-to-understand drawings to map out how the software works

By the end of this project, I'll have real experience in building software that solves these investment puzzles, making it easier for everyone It's like learning to build with building blocks while solving real-life challenges So, let's dive into the MB Capital Fund Management System!"

Figure 1 Capital Fund illustration

Source: salesforce, 2016

Trang 7

 Name: This represents the name of the investor

 Gmail: The Gmail address is used as a unique identifier for login purposes

 Password: The password is also used for secure login

 Pin: A personal identification number, which is an additional security measure

 Balance: This reflects the amount of money available for placing orders

Trang 8

 Withdraw: Investors can withdraw money from their balance, provided the withdrawal amount is both positive and within the available balance

 PlaceOrder: Investors can use this method to buy new funds The method associates the investor with the purchased fund

 DisplayFunds: This method displays all the funds owned by the investor

Fund Class:

Attributes:

 Name: The name of the fund

 Ticket: A code or identifier for the fund, akin to a unique reference

 InceptionDate: This is the date when the fund was initiated

 ManagementFee: The management fee associated with the fund

 Amount: The amount of money invested in the fund by the investor

 Name: The name of the stockbroker

 Gmail: The Gmail address for the stockbroker's login

 Password: The password used by the stockbroker for secure login

 isMarketChange: A state variable (likely a boolean) to track changes in the market, used in the Observer pattern

Trang 9

Abstract Observer Class:

Methods:

 Update: This method is a part of the Observer pattern It allows observers (in this case, investors)

to be notified of changes

 Investor (Inherited from Observer):

Figure 3 Class Diagram (2)

Each of these services follows the singleton pattern, which ensures that there is only one instance of each service throughout the application

FundService:

Attributes:

 List of Funds: This collection holds all the funds available in the system

Methods:

 AddFund: Used to add a new fund to the list of funds

 GetFund: Retrieves a specific fund from the list using a unique identifier, such as the fund's ticket

 GetFunds: Returns a list of all available funds

 DisplayFunds: Displays all the funds, which can be helpful for users to view the available investment options

InvestorService:

Attributes:

 List of Investors: This collection contains information about all the investors registered in the system

Trang 10

 GetBrokers: Returns a list of all stockbrokers

 GetBroker: Retrieves specific stockbroker information from the list

 DisplayMyInvestors: Displays the list of investors that a stockbroker is managing

Trang 11

Figure 4 Stockbroker Use Case Diagram

For the Investor:

Trang 13

private string name;

private string ticket;

private DateTime inceptionDate;

private double managementFee;

private decimal amount;

public string Name

{

get { return name; }

set { name = value; }

}

public string Ticket

{

get { return ticket; }

set { ticket = value; }

}

public DateTime InceptionDate

{

get { return inceptionDate; }

set { inceptionDate = value; }

}

public double ManagementFee

{

get { return managementFee; }

set { managementFee = value; }

}

public decimal Amount

{

get { return amount; }

set { amount = value; }

 Name: stores the title of the fund

 Ticket: used to hold a unique code or identifier for the fund

 InceptionDate: represents the date when the fund was initiated or created It uses the DateTime data type to store the creation date

Trang 14

 ManagementFee: stores the percentage of management fees associated with the fund It represents the ongoing expenses that investors may incur

 Amount: keeps track of the amount of money invested in this fund by investor It starts at 0 and increases as investor deposit funds

private string name;

private string gmail;

private string password;

private List<Investor> myInvestors = new List<Investor>();

private Boolean isMarketChange;

public string Name

{

get { return name; }

set { name = value; }

}

public string Gmail

{

get { return gmail; }

set { gmail = value; }

}

public string Password

{

get { return password; }

set { password = value; }

}

public Boolean IsMarketChange

{

get { return isMarketChange; }

set { isMarketChange = value; }

Trang 15

string text = "*) My Investors List \n" ;

text += String.Format( "|{0,-10}|{1,-20}|{2,-20}|\n" , "Name" , "Gmail" , "Joined Funds" ); int count = 1;

foreach (var investor in myInvestors)

{

if (myInvestors.Count() == count)

{

text += String.Format( "|{0,-10}|{1,-20}|" , investor.Name, investor.Gmail);

var joinedFunds = string Join( "-" , investor.myFunds.Select(fund => fund.Ticket)); text += string Format( "{0,-20}|" , joinedFunds);

}

else

{

text += String.Format( "|{0,-10}|{1,-20}|" , investor.Name, investor.Gmail);

var joinedFunds = string Join( "-" , investor.myFunds.Select(fund => fund.Ticket)); text += string Format( "{0,-20}|\n" , joinedFunds);

 Name: stores the name of the stockbroker

 Gmail: used for the stockbroker's email address It serves as a login identifier

 Password: holds the stockbroker's password, which is also used for login

 myInvestors: a list that maintains the investors managed by the stockbroker It's used to keep track

of the investors associated with this stockbroker

 IsMarketChange: is a Boolean value indicating whether a market change has occurred This is a part of the observer pattern for notifying investors

 The NotifyInvestors method sends notifications to all investors in the stockbroker's list, informing them about the market change It passes the ticket and a message to the Update method of each investor (part of the observer pattern)

 The ToString method overrides the default behavior It formats and returns a string representing the stockbroker's details, including their name and Gmail

Trang 16

 The DisplayMyInvestors method is used to generate a formatted string that displays a list of investors managed by the stockbroker It provides information about the investors' names, Gmail addresses, and the funds they have joined

3 Investor class

public class Investor : Observer

{

private string name;

private string gmail;

private string password;

private string pin;

private decimal balance;

public StockBroker myStockBroker;

public List<Fund> myFunds;

public string Name

Trang 18

text = "*) My Funds List \n" ;

text += String.Format( "|{0,-38}|{1,-7}|{2,-15}|{3,-15}|\n" , "Name" , "Ticket" , "Management Fee" , "Amount" );

foreach (Fund f in myFunds)

 Name: stores the name of the investor

 Gmail: holds the investor's email address, which serves as a login identifier

 Password: used to store the investor's password for login

 Pin: represents the personal identification number (PIN) of the investor

 Balance: keeps track of the investor's current financial balance This balance is used to place orders for funds and manage investments

 myStockBroker: refers to the stockbroker associated with the investor Each investor is managed

by a particular stockbroker

 myFunds: maintains a list of funds owned by the investor

Method:

Trang 19

 The Deposit method is used to add money to the investor's balance It checks whether the deposited amount is greater than zero

 The Withdraw method allows the investor to withdraw money from their balance It verifies that the withdrawal amount is greater than zero and doesn't exceed the available balance

 The PlaceOrder method is used for buying new funds or accumulating money in an existing fund

It checks if the investor has enough balance to place an order If the fund is new, it creates a new fund instance and deducts the money from the balance, adding the fund to the list If the fund already exists in the investor's portfolio, it accumulates the money in that fund

 The DisplayMyFunds method generates a formatted string displaying the list of funds owned by the investor It provides details such as the fund's name, ticket, management fee, and the amount invested in each fund

 The ToString method returns a comprehensive string containing the investor's personal information and details of the associated stockbroker

 The Update method is part of the observer pattern It is called when the investor receives notifications about market changes This method prints a message to the console, informing the investor about the change in a specific fund and the associated message

4 FundService class

public sealed class FundService

{

private static FundService instance;

private List<Fund> funds = null ;

// Add Fund to Fund List

public void AddFund(Fund fund)

Trang 20

return funds;

}

public void DisplayFunds()

{

Console.WriteLine( "*) Funds List" );

Console.WriteLine(String.Format( "|{0,-38}|{1,-7}|{2,-15}|{3,-15}|" , "Name" , "Ticket" ,

"Inception Date" , "Management Fee" ));

foreach (var fund in funds)

Private Instance and Constructor:

 The class has a private static field called instance, which holds the single instance of the FundService class

 The constructor of the FundService is private, it can only be instantiated from within the class Methods:

 The GetInstance method is a public static method that provides access to the single instance of the FundService This method checks if the instance field is null If it's null, it creates a new instance

of the FundService To ensure thread safety, it uses a lock statement to make sure that only one thread can create the instance

 The AddFund method allows me to add funds to the service

 The GetFund method allows me to retrieve a specific fund from the list of funds based on the fund's ticket

 The DisplayFunds method is responsible for displaying the list of funds

5 StockBrokerService class

public sealed class StockBrokerService

{

private static StockBrokerService instance;

private List<StockBroker> brokers = null ;

Trang 21

Private Instance and Constructor:

 The class has a private static field, which holds the single instance of the StockBrokerService class

 The constructor of the StockBrokerService is private, it can only be instantiated from within the class

Method:

 The GetInstance method is a public static method that provides access to the single instance of the StockBrokerService This method checks if the instance field is null If it's null, it creates a new instance of the StockBrokerService To ensure thread safety, it uses a lock statement to make sure that only one thread can create the instance

 The AddBroker method allows me to add stockbroker to the service

 The DisplayBrokers method is responsible for displaying the list of stock brokers

 The GetBrokers method returns a list of all stock brokers

 The GetBroker method allows me to retrieve a specific stockbroker from the list based on the index

 The CheckAccount method checks if a stock broker's account with a specific Gmail and password exists If a matching account is found, it returns the stock broker; otherwise, it returns null

 The DisplayMyInvestors method displays the list of investors managed by a specific stock broker

Trang 22

6 InvestorService class

public sealed class InvestorService

{

private static InvestorService instance;

private List<Investor> investors = null ;

Private Instance and Constructor:

 Like the other Singleton classes, the InvestorService class has a private static field which holds the single instance of the InvestorService class

 The constructor of the InvestorService class is also private, preventing external code from creating new instances The instance can only be instantiated from within the class

Method:

 The GetInstance method is a public static method that provides access to the single instance of the InvestorService class This method checks if the instance field is null If it's null, it creates a new instance of the InvestorService To ensure thread safety, it uses a lock statement to make sure that only one thread can create the instance

Trang 23

 The CheckAccount method allows me to check if an investor's account with a specific Gmail and password exists If a matching account is found, it returns the investor; otherwise, it returns null

 The AddInvestor method is responsible for adding new investors to the service Before adding an investor, it checks if an investor with the same Gmail already exists If an investor with the same Gmail is found, it returns false to indicate that the addition failed If no matching Gmail is found, the investor is added to the list of investors, and the method returns true

// Initialize Services (Singleton Patterns)

StockBrokerService brokerService = StockBrokerService.GetInstance();

InvestorService investorService = InvestorService.GetInstance();

FundService fundService = FundService.GetInstance();

// Initialize some stockBrokers

StockBroker broker = new StockBroker( "Thanh Thao" , "thanhthao@gmail.com" , "123" );

StockBroker broker2 = new StockBroker( "Khanh Quynh" , "khanhquynh@gmail.com" , "123" );

// Add brokers to Data Storage

brokerService.AddBroker(broker);

brokerService.AddBroker(broker2);

// Initialize some Investors

investorService.AddInvestor( new Investor( "Long" , "long@gmail.com" , "123456" , "12412" , 20000, broker));

investorService.AddInvestor( new Investor( "Nghia" , "nghia@gmail.com" , "678910" , "45743" ,

Trang 24

Console.Write( "Choose a Stockbroker that you trust: " );

string input = Console.ReadLine();

if ( int TryParse(input, out selectedIndex) && selectedIndex >= 1 && selectedIndex <= length)

Trang 25

 This method checks if a user's input can be parsed as an integer and falls within a specified range (between min and max)

CheckValidBroker:

 This method is used to validate the selection of a stockbroker from a list

 The method ensures that the user's input is a valid integer index that corresponds to an existing stockbroker

checkValidAmount:

 This method checks if a user's input can be parsed as a decimal and whether the value is greater than zero

checkValidFund:

 This method checks if a given ticket code exists in a list of funds

 It compares the provided ticket with the Ticket property of each fund in the list

// Initialize Services (Singleton Patterns)

StockBrokerService brokerService = StockBrokerService.GetInstance();

InvestorService investorService = InvestorService.GetInstance();

FundService fundService = FundService.GetInstance();

InvestorPage investorUI = new InvestorPage(fundService);

BrokerPage brokerUI = new BrokerPage(fundService, brokerService);

CreateAccountPage createAccountPageUI = new CreateAccountPage(investorService,

brokerService);

while ( true )

{

Console.WriteLine( "=========== WELCOME TO MBCAPITAL ===========" );

Console.WriteLine( "=> HOME PAGE" );

Trang 26

StockBroker broker = brokerService.CheckAccount(brokerGmail,

string password = Console.ReadLine();

Investor investor = investorService.CheckAccount(gmail, password);

Trang 27

Three UI classes are initialized, each representing different interfaces in your application: InvestorPage, BrokerPage, CreateAccountPage

The main menu is displayed, offering several options:

1 Logging in as a stockbroker

2 Logging in as an investor

3 Creating a new account

4 Exiting the application

10 CreateAccountPage class

internal class CreateAccountPage

{

private readonly InvestorService investorService;

private readonly StockBrokerService brokerService;

public CreateAccountPage (InvestorService investorService, StockBrokerService brokerService) {

this investorService = investorService;

this brokerService = brokerService;

string name = Console.ReadLine();

Console.Write( "Gmail (include @): " );

string gmail = Console.ReadLine();

Console.Write( "Password (at least 6 characters): " );

string password = Console.ReadLine();

Console.Write( "PIN (exactly 5 characters): " );

string pin = Console.ReadLine();

Console.Write( "Balance (greater than 0): " );

string balance = Console.ReadLine();

Trang 28

11 BrokerPage class

public class BrokerPage

{

private readonly FundService fundService;

private readonly StockBrokerService brokerService;

public BrokerPage (FundService fundService, StockBrokerService brokerService)

{

this fundService = fundService;

this brokerService = brokerService;

Console.WriteLine( $"** WELCOME BACK {broker.Name} **" );

Console.WriteLine( "1 View Funds" );

Console.WriteLine( "2 Manage Invester" );

Trang 29

12 InvestorPage class

public class InvestorPage

{

private readonly FundService fundService;

public InvestorPage (FundService fundService)

Console.WriteLine( $"** WELCOME BACK {investor.Name} **" );

Console.WriteLine( "1 View Funds" );

Console.WriteLine( "2 Place Order" );

Console.WriteLine( "3 Deposit Money" );

Console.WriteLine( "4 Withdraw Money" );

Trang 31

II Program Execution

1 Broker

Trang 32

Figure 6 Option 1 in main menu

The program starts by displaying the main menu, where users can choose to log in as a StockBroker, Investor, create a new account, or exit the program

The user chooses option 1 to log in as a StockBroker The program asks for the broker's Gmail and password for authentication

In this case, it fails to authenticate and displays "Try Again" because the combination of Gmail and password is not correct

The program returns to the main menu, where the user is given another opportunity to choose an option The user chooses option 1 again to log in as a StockBroker, and this time, user provides a correct Gmail and the correct password

Authentication is successful, and the program navigates to Broker Page

Trang 33

Figure 7 Option 1 and 2 in Broker menu

Select option 1, "View Funds." The program displays a list of available funds The funds are shown with details such as Name, Ticket, Inception Date, and Management Fee

Back in the main menu, Choose option 2, "Manage Investors." The program displays a list of the StockBroker's managed investors In this case, two investors, "Long" and "Nghia," are displayed along with their Gmail addresses

Figure 8 Option 3 in Broker menu

The StockBroker, chooses option 3, "Notify" The program prompts "Choose a ticket that changes." Next, the program asks for the "Trend" The program proceeds to notify the investors who have invested in the fund with the ticket It displays the notification message for each investor (Observer pattern)

Trang 34

2 Investor

Figure 9 Option 2 in main menu

The user chooses option 2 to log in as an Investor The program asks for the Investor’s Gmail and password for authentication This time, user provides a correct Gmail and the correct password

Authentication is successful, and the program navigates to Investor Page

Figure 10 Option 1 and 6 in Investor menu

Select option 1, which is "View Funds," The program displays a list of available funds For example, it shows information about different investment funds

Trang 35

Select option 6, which is "View Profile," The program then displays the detailed information of the Investor

Figure 11 Option 3 in Investor menu

Choose option 3, which is "Deposit Money." The program requests user to enter the amount of money they want to deposit The program checks whether the entered amount is greater than zero (valid deposit) If the user enters a valid deposit amount At this time, the program processes the valid deposit and updates user's account balance

Figure 12 Option 4 in Investor menu

Ngày đăng: 02/02/2024, 10:27

w