C# Cognizant Handson Solutions

AccountDetails Handson Solution Cognizant

Create a public class Account with the following members:

  •  private attributes 

    int id;

   String  accountType; 

   double balance;

  • Add  public properties for the all above three fields.

Constructors

  • Create an empty parameter constructor.
  • Also create a  3- parameter constructor to set the values for the given properties.

Methods

  • Create a method WithDraw which should take amount as input and return a boolean .
  • public bool Withdraw (double amount) —

This method should deduct the amount from the balance and return true. Before deducting the amount from the balance ensure there is enough balance. If there is no enough balance return false. 

  • Add a method GetDetails that returns  the details exactly as given in the sample output.

public String GetDetails()

  • Create a public class Program for the Main method .

a) From the Main method create object for Account and call the GetDetails method and display the details.

b) In the Main method, enter the amount to be withdrawn from user and call WithDraw method by passing this amount. If WithDraw method returns ‘true’ then display new balance(i.e after deduction)

AccountDetails.cs

//Implement code here
using System;
public class Account
{
    private int id;
    private string accountType;
    private double balance;
    
    public int Id
    {
        get{ return id;}
        set{ id = value;}
    }
    
    public string AccountType
    {
        get{ return accountType;}
        set{ accountType = value;}
    }
    public double Balance
    {
        get{ return balance;}
        set{ balance = value;}
    }
    public Account(){}
    public Account(int id, string accountType, double balance)
    {
        this.id = id;
        this.accountType = accountType;
        this.balance = balance;
    }
    
    public bool WithDraw(double amount)
    {
        if(balance>amount)
        {
            balance -= amount;
            return true;
        }
        return false;
    }
    public string GetDetails()
    {
        return ("Account Id: " + id + "\nAccount Type: " + accountType + "\nBalance: "+balance);
    }
}
public class Program
{
    static void Main(string[] args)
    {
		int id;
		String accountType;
		double balance, withdraw;
		
        Account ac = new Account();
        
        Console.WriteLine("Enter account id");
        id = Convert.ToInt32(Console.ReadLine());
        
        Console.WriteLine("Enter account type");
        accountType = Console.ReadLine();
        
        Console.WriteLine("Enter account balance");
        balance = Convert.ToDouble(Console.ReadLine());
        
        Console.WriteLine("Enter amount to withdraw");
        withdraw = Convert.ToDouble(Console.ReadLine());
        
        ac = new Account(id, accountType, balance);
        Console.WriteLine(ac.GetDetails());
       
        if(ac.WithDraw(withdraw))
        {
            Console.WriteLine("New Balance: " + ac.Balance);
        }
    }
}

Similar Posts