Converting between c++ and c#

Hi,

I have just made a program in C++ that deals with bank accounts but I have now been told that I need to write it in C#.

Anyway I was wondering how easy a task this is and are there any major differences between the two that I should know about as I have not really used C#.

I also have to make it a web form.

Basically my program contains a class that has all the functions and variables associated with a customer's bank account.

I have created an array for the class.

I have a menu which consists of function that displays, a function that gets menu choice and a function that acts of the choice, this contains a switch statement and a number of cases that correspond with the user's menu choice.

There are sub functions associated with the act on choice function

Don't worry I am not expecting anyone to do this for me or write any of the code and I was merely wondering how easy it is and what differences I should be aware of.

I will post my C++ code below but need to do it in chunks as it is too long for one message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

//Start of class BankAccount

class BankAccount
{
	public:
		BankAccount ();//declaring constructor 
		~BankAccount ();//declaring destructor
		//Accessor Functions
        string get_name () const;
        string get_account_number () const;
        double get_balance () const;
        int get_number_of_accounts_created() const;
        double get_total_deposit () const;
		double get_total_withdrawal () const;
		double get_new_interest_rate () const;
		//Mutator Functions
		void set_customers_name ();
        void set_account_number ();
        void set_balance ();
        void set_total_deposit (double deposit);
		void set_total_withdrawal (double withdrawal);
		void set_new_interest_rate ();
		void calculate_balance_after_interest (double interest_rate);
				
	private:
		static int number_of_accounts_created;//set at static and initialised as zero - 1 is added each time new account created
		string new_customers_fullname;//customer's full name
		string new_account_number;//customer's unique account number
		double new_balance;//customer's existing balance
		double total_deposit;//total amount deposited to customer's account
		double total_withdrawal;//total amount withdrawn from customer's account
		double new_interest_rate;//interest rate applied to customer's balance
};//end of class declaration

//constructor that initialises private member variables
BankAccount::BankAccount ()
{
	new_customers_fullname = " ";
    new_account_number = "0";
    new_balance = 0;
    total_deposit = 0;
    total_withdrawal = 0;
    new_interest_rate = 1.67;//initial interest rate of 1.67%
}//end of constructor

//destructor
BankAccount::~BankAccount ()
{
}//end of destructor

//member function that allows the customer's fullname to be set
void BankAccount::set_customers_name()
{
	string first_name, last_name;
	cout << "Please enter new customer's first name: ";
	cin >> first_name;
	cout << "Please enter new customer's last name: ";
	cin >> last_name;
	new_customers_fullname = first_name + " " + last_name;	
}//end of member function

//member function that returns customer's fullname
string BankAccount::get_name() const
{
	return new_customers_fullname;
}//end of member function

//member function that creates customer's account number
void BankAccount::set_account_number ()
{
	number_of_accounts_created++;//initially set to zero and adds 1 each time a new account is created
    int last_six_digits = 112280 + number_of_accounts_created;//creating last 6 digits of new account number
	ostringstream convert;//declaring variable
	convert << last_six_digits;//converting int to string
	string first_two_digits = "00";//creating first two digits of new account number
	new_account_number = first_two_digits + convert.str();//creating new account number
}//end of member function

//member function that returns the customer's account number
string BankAccount::get_account_number () const
{
	return new_account_number;
}//end of member function

//member function that sets customer's initial balance
void BankAccount::set_balance ()
{
	cout << "Please enter customer's balance: " << (char)156;
    cin >> new_balance;
}//end of member function

//member function that returns customer's initial balance
double BankAccount::get_balance () const
{
	return new_balance;       
}//end of member function

//member function that calculates the total amount deposited to account and updates balance
void BankAccount::set_total_deposit (double deposit)
{
     new_balance += deposit;//calculating new balance
     total_deposit += deposit;//calculating amount deposited
}//end of member function

//member function that returns total amount deposited to account
double BankAccount::get_total_deposit () const
{
     return total_deposit;       
}//end of member function

//member function that calculates the total amount withdrawn from account and updates balance
void BankAccount::set_total_withdrawal (double withdrawal)
{
	if(withdrawal < new_balance)//test if there are sufficient funds in account
	{
		new_balance -= withdrawal;
		total_withdrawal += withdrawal;
	}
	else
		cout << "Insufficient funds available." << endl;
}//end of member function

//member function that returns total amount withdrawn from account
double BankAccount::get_total_withdrawal () const
{
	return total_withdrawal;
}//end of member function

//member function that allows user to set new interest rate
void BankAccount::set_new_interest_rate ()
{
	cout << "Current interest rate is: " << new_interest_rate << "%" << endl;
	cout << "\nDo you want to set new interest rate (y or n): ";
	string answer;
	cin >> answer;
	double interest_rate = 0;
	if (answer == "y")
	{
		cout << "\nPlease enter new interest rate as a percentage: ";
		cin >> new_interest_rate;
		interest_rate = (new_interest_rate + 100)/100;
		calculate_balance_after_interest (interest_rate);
	}//end of if statement
	else 
	{
		cout << "Interest rate has not been changed." << endl;
		interest_rate = (new_interest_rate + 100)/100;
		calculate_balance_after_interest (interest_rate);
	}//end of else statement
}//end of member function

//member function that returns the current interest rate
double BankAccount::get_new_interest_rate () const
{
	return new_interest_rate;
}//end of member function

//member function that calculates new balance after interest added
void BankAccount::calculate_balance_after_interest (double interest_rate)
{
	new_balance = new_balance * interest_rate;
}

//member function that returns the number of accounts created
int BankAccount::get_number_of_accounts_created() const
{
	return number_of_accounts_created;
}//end of member function

//End of class BankAccount
//******************************************************************************
//Global Variables & Objects
const int MAX = 10;
BankAccount new_customer[MAX];
int BankAccount::number_of_accounts_created = 0;//initialising private member variable

//End of Global variables & Objects
//******************************************************************************

//Function declarations
void display_menu ();
int get_choice ();
void act_on_choice ();
void add_new_customer (int i);
void print_customer_information ();
void make_deposit ();
void make_withdrawal ();
int test_if_account_exists (string account_number);
void set_interest_rate ();


//End of function declarations
//******************************************************************************

int main ()
{
    act_on_choice ();//function call
    
    system ("PAUSE");
    return 0;
}//end of main 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//Function definitions
//******************************************************************************

//Function displays menu on screen
void display_menu ()
{
	system ("CLS");
    cout << "Menu" << endl;
    cout << "======" << endl;
    cout << "(1) - Open New Account" << endl;
    cout << "(2) - Make Deposit" << endl;
    cout << "(3) - Make Withdrawl" << endl;
    cout << "(4) - Add Interest" << endl;
    cout << "(5) - Display Account Information" << endl;
    cout << "(6) - Exit" << endl;
    cout << "=================================" << endl << endl;
}//end of function

//Function prompts user to make choice from menu and loops until user makes valid choice and then returns their choice
int get_choice ()
{
	int choice = 0;//resets or initialises value of choice to ensure while loop entered
	while (choice < 1 || choice > 6)//while loop used to keep displaying menu until user enters a valid menu choice
	{
		display_menu ();//calling function display_menu()
		cout << "Please enter your menu choice: ";//user prompted to select from menu
		//read input from user
		cin >> choice;//selection variable initialised using value inputted by user
		cin.clear();//clears error state of stream
		cin.ignore(100, '\n');//discard characters remaining in the input buffer
	}//end of while loop
	return choice;//users menu choice returned by function
}//end of function

//function prompts user to enter new customer's name and balance and then creates an account number 
void add_new_customer (int i)
{
    new_customer[i].set_customers_name();//request user to enter customer's name
    new_customer[i].set_account_number();//creates account number for new customer
    new_customer[i].set_balance();//request user to enter customer's balance
}//end of function

//function tests if account exists and allows deposits to be made to the account if it exists
void make_deposit ()
{
    cout << "Deposit Money in Account" << endl;//display message
    cout << "==========================" << endl;
    cout << "Please enter 8 digit account number: ";
    string account_number;//account number that deposit is added
    cin >> account_number;
    int element = test_if_account_exists (account_number);
	if (element != -1)//only entered if account exists
	{
		cout << "\nPlease enter amount being deposited: " << (char)156;
		double deposit;//amount being deposited to account
		cin >> deposit;
		new_customer [element].set_total_deposit (deposit);//function call to calculate new account balance and total amount deposited to account
	}//end of if statement
	else
		cout << "Account Number does not exist." << endl;//end of else statement
}//end of function

//function tests if account exists and allows withdrawals to be made to the account if it exists
void make_withdrawal ()
{
	cout << "Withdraw Money from Account" << endl;//display message
    cout << "==========================" << endl;
    cout << "Please enter 8 digit account number: ";
    string account_number;//account number that withdrawal is from
    cin >> account_number;
    int element = test_if_account_exists (account_number);
	if (element != -1)//only entered if account exists
	{
		cout << "\nPlease enter amount being withdrawn: " << (char)156;
		double withdrawal;//amount being withdrawn from account
		cin >> withdrawal;
		new_customer [element].set_total_withdrawal (withdrawal);//function call to calculate new account balance and total amount withdrawn from account
	}//end of if statement
	else
		cout << "Account Number does not exist." << endl;//end of else statement
}//end of function

//function tests if account exists and allows user to set new interest rate and calculates new balance after interest added
void set_interest_rate ()
{
	cout << "Set Interest Rate" << endl;//display message
    cout << "==========================" << endl;
    cout << "Please enter 8 digit account number: ";
    string account_number;//account number that withdrawal is from
    cin >> account_number;
    int element = test_if_account_exists (account_number);
	if (element != -1)//only entered if account exists
	{
		new_customer [element].set_new_interest_rate ();//function call that sets interest rate and calculate new balance
	}//end of if statement
	else
		cout << "Account Number does not exist." << endl;//end of else statement
}//end of function

//function tests if any accounts created or if function exists - returns i if function exists and returns -1 if no accounts created or account does not exist
int test_if_account_exists (string account_number)
{
    if(new_customer [0].get_number_of_accounts_created() == 0)//tests if any accounts have been created
	{
		cout << "No new accounts created." << endl;//message printed if no accounts created
		return -1;//value returned if no accounts have been created
	}//end of if statement
	else 
	{
		int number_of_accounts = new_customer [0].get_number_of_accounts_created();//variable hold the value of number of accounts created and is used for test condition in for loop
		for (int i = 0; i < number_of_accounts; i++)//for loop goes through each array element and tests if account number exists
		{
			string test = new_customer[i].get_account_number ();//initialises test with account number from each element of the array new_customer []
			if (test == account_number)//tests to see if account exists
				return i;//value returned if account exists - i used to call the correct element of the array new_customer []
		}//end of for statement
		return -1;//value returned if account does not exist
	}//end of else statement
}

//function prints each customer's account information
void print_customer_information ()
{
    if(new_customer [0].get_number_of_accounts_created() == 0)//tests if any accounts have been created
		cout << "No new accounts created." << endl;//message printed if no accounts created
	else//enters this block if at least 1 account has been created
	{
		int number_of_accounts = new_customer [0].get_number_of_accounts_created();//variable hold the value of number of accounts created and is used for test condition in for loop
		cout << "Account Information" << endl;//display message
		cout << "====================" << endl;
						
		for (int i = 0; i < number_of_accounts; i++)
		{
			cout << "Customer's Name: " << new_customer [i].get_name() << endl;//prints each customer's name
			cout << "Customer's Account Number: " << new_customer [i].get_account_number() << endl;//prints each customer's account number
			cout << std::fixed;
            cout << "Customer's Balance: " << (char)156 << std::setprecision(2) << new_customer [i].get_balance() << endl;//prints each customer's balance
			cout << "Amount Deposited: " << (char)156 << new_customer [i].get_total_deposit() << endl;//prints total amount deposited in customer's account
			cout << "Amount Withdrawn: " << (char)156 << new_customer [i].get_total_withdrawal() << endl;//prints total amount withdrawn from customer's account
			cout << "Interest Rate: " << new_customer [i].get_new_interest_rate () << "%" << endl;
			cout << "\n===============================================" << endl;
		}//end of for loop
	}//end of else statement 
}//end of function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Function selects correct switch case based on user's menu choice
void act_on_choice ()
{
	int choice = 0;
	int i = 0;//this sets the element of the array object new_customer []
    do//this do while loop continues until user selects option 6 from menu and then it terminated
	{
		choice = get_choice ();//declaring and initialising variable "choice" with value returned from get_choice()- variable holds user's menu choice
        switch (choice)
        {
			case 1:{//Asks user to input new account information and creates an account number for the new customer
					system("CLS");
                    add_new_customer (i);//fucntion call
                    i++;//adds 1 so new array created next time a new customer is added
                    system ("PAUSE");
                    break;
                    }//end of case 1
            case 2:{//Prompts user to enter account number and tests if it account exists - if account exists user is prompted to enter amount being deposited in account
                    system("CLS"); 
                    make_deposit ();//function call
                    system ("PAUSE");
					break;
                   }//end of case 2
            case 3://Prompts user to enter account number and tests is account exists - if account exists user is prompted to enter amount being withdrawn from account
                    system("CLS"); 
					make_withdrawal ();//function call
					system ("PAUSE");
					break;
            case 4://Prompts user to enter account number and tests if account exists - if account exists user can set interest rate and calculate new balance after interest added
                    system("CLS"); 
					set_interest_rate ();//function call
					system ("PAUSE");
					break;
			case 5://prints out all customers' information
                    system("CLS");
					print_customer_information ();//function call
					system ("PAUSE");
					break;//end of case 5		
            default://corresponds to option 6 in menu - exit program
                    cout << "You exited menu" << endl;//display message to user
                    break;//end of default case
        }//end of switch statement
    }while (choice != 6);//end of do while loop
}//end of function 
Topic archived. No new replies allowed.