Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming with C# The purpose of this assignment is simply to get you going in

ID: 3863247 • Letter: P

Question

Programming with C#

The purpose of this assignment is simply to get you going in case you forgot how to program over the break and to get you up to speed on using Visual Studio and C#. It should be a review of creating a class, reading input from the user and calling a few methods in the class.

Create a C# application that defines a single class, Account. This class should have an instance variable (double) which represents the starting balance. There should also be a variable indicating the number of transactions. Finally there should be an array of doubles that contains the transactions. You can assume there will be no more than 50 transactions. [If you want to be slightly more professional, you could have a parallel array that keeps track of the type of transaction…or an array of a Transaction class that keeps track of both type and amount.] The constructor should accept a single argument, a double, which is the starting balance. In addition to these instance variables, the class should contain the following methods:

deposit – adds entry to the transaction array; returns void

withdraw – adds negated entry to the transaction array; returns void

addInterest – adds entry to the transaction array that increases the current balance by .05%; returns void

getBalance – returns the current balance. The method should walk through the array to determine the balance; the class should not have a variable that has the current balance.

showTransactions – lists all the transactions that have been entered, including the beginning balance and the current balance.

The main method should do the following in a loop:

ask whether to continue

display a menu that allows the user to choose from the four actions described above: (D)eposit, (W)ithdraw, (C)alculateInterest, (S)howBalance., (Q)uit

The program will perform the appropriate action. If the user is making a deposit or withdrawal, the program will ask for the amount. The user should be expected to enter only positive values.

When the customer chooses to quit, the program will call showTransactions and then exit.

Note: output should only be performed in the main method; your member methods should perform the calculation and return the answer. The only exception to this is ShowTransactions, which is allowed to perform output. Your methods should all be member methods (no statics).

You can enter these numbers in whatever manner is easiest for you. A straight forward way to read a double is:

String s = Console.ReadLine();

double d = Double.Parse(s);

and for a character:

char c = Console.ReadLine()[0];

This is all in C#, which I am very unfamiliar with. I have some code done which I will post below:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;


namespace Program0
{
class Account
{
public double balance { get; set; }
public int numberOfTransactions { get; set; }
public void Deposit(double d)
{
this.balance += d;
this.numberOfTransactions += 1;
}
public void Withdrawal(double d)
{
this.balance -= d;
this.numberOfTransactions += 1;
}
public double GetBalance()
{
return this.balance;
}
public int getNumberOfTransactions()
{
return numberOfTransactions;
}
class Program
{
static void Main(string[] args)
{
Account b = new Account();
b.Deposit(200);
b.Withdrawal(50);
char input = ' ';
while (true)
{
Console.WriteLine("MENU");
Console.WriteLine("Please enter the letter that you want to do:");
Console.WriteLine("(D)eposit");
Console.WriteLine("(W)ithdraw");
Console.WriteLine("(C)alculateInterest");
Console.WriteLine("(S)howBalance");
Console.WriteLine("(Q)uit");
char menuchoice = char.Parse(Console.ReadLine());
switch (menuchoice)
{
case 'D':
case 'd':
double amount = 0;
Console.WriteLine("Enter an amount to deposit");
break;
case 'W':
case 'w':
Console.WriteLine("Enter an amount to withdraw");
break;
case 'C':
case 'c':
Console.WriteLine("Calculate the interest");
break;
case 'S':
case 's':
Console.WriteLine("Show all transactions");
break;
case 'Q':
case 'q':
Console.WriteLine("exit");
break;
}
//Console.WriteLine(b.GetBalance());
Console.WriteLine(b.balance);
Console.ReadLine();
}
}
}
}
}

The above code is what I have so far, but it is not right. This is what needs to be done:

-The Account object needs an Array of doubles to hold the transactions

-You are not supposed to keep track of the balance, but calculate it anytime it is asked for. This means that I should start with an initial balance, then add up everything in the transaction array.

I don't understand how to code the showTransactions

Explanation / Answer

using System;
using System.Collections;
using System.Text;
using System.Threading.Tasks;

namespace Program0
{
    public class Transaction
    {
        public String type { get; set; }
        public double amount { get; set; }
        public static int count = 0;
      
        public Transaction(){}
      
        public Transaction(String st, double amt)
        {
            type = st;
            amount = amt;
            count++;
        }
      
        public static int getNumberOfTransactions()
        {
            return count;
        }
    }
    
    public class Account
    {
        public double starting_balance { get; set; }     
        //array of objects of transaction class
        public ArrayList transactions = new ArrayList(50);
    
        //Parameterized constructor with one argument for starting balance
        public Account(double bal)
        {
            starting_balance = bal;
        }
    
        public void deposit(double amt)
        {
            Transaction transaction1 = new Transaction("Deposit",amt);
            transactions.Add(transaction1);
        }
    
        public double getBalance()
        {
            double balance = starting_balance;
            for (int i=0; i<transactions.Count; ++i)
            {
                Transaction t = (Transaction) transactions[i];
                if(t.type == "Deposit")
                    balance += t.amount;
                else if(t.type == "Withdrawal")
                    balance -= t.amount;
                else
                    balance += t.amount;
            }
            return balance;
        }
    
        public void withdraw(double amt)
        {
            Transaction transaction2 = new Transaction("Withdrawal", amt);
            transactions.Add(transaction2);
        }
    
        public void addInterest()
        {
            Transaction transaction3 = new Transaction();
            transaction3.type = "Adding Interest";
            transaction3.amount = (0.05 * this.getBalance())/100;
            transactions.Add(transaction3);
        }
    
        public void showTransactions()
        {
            Console.WriteLine("Opening Balance: ",starting_balance);
            for (int i=0; i<transactions.Count; ++i)     
            {
                Transaction t = (Transaction) transactions[i];
                Console.WriteLine("Transaction type: ",t.type);
                Console.WriteLine("Transaction amount: ",t.amount);
            }
            Console.WriteLine("Current Balance: ",this.getBalance());       
            Console.WriteLine("Number of Transctions: ",Transaction.getNumberOfTransactions());
        }
      
    }
    public static class Bank_Account
    {
        static void Main(string[] args)
        {
            Account acc = new Account(1200.000000);
            char c = Console.ReadLine()[0];
            while(c != 'Q' || c != 'q')
            {
                Console.WriteLine("MENU");
                Console.WriteLine("Please choose an action to perform:");
                Console.WriteLine("(D)eposit");
                Console.WriteLine("(W)ithdraw");
                Console.WriteLine("(C)alculateInterest");
                Console.WriteLine("(S)howBalance");
                Console.WriteLine("(Q)uit");
                char choice = char.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 'D':
                    case 'd':
                        Console.WriteLine("Enter an amount to deposit");
                        String s1 = Console.ReadLine();
                        double d1 = Double.Parse(s1);
                        acc.deposit(d1);
                        break;
                    case 'W':
                    case 'w':
                        Console.WriteLine("Enter an amount to withdraw");
                        String s2 = Console.ReadLine();
                        double d2 = Double.Parse(s2);
                        acc.withdraw(d2);
                        break;
                    case 'C':
                    case 'c':
                        Console.WriteLine("Interest added");
                        acc.addInterest();
                        break;
                    case 'S':
                    case 's':
                        Console.WriteLine("Transaction History");
                        Console.WriteLine("-------------------");
                        acc.showTransactions();
                        break;
                    }
                }
            }
        }
}