Step 2 on Virtual C# 2012 exercise 4.11 doesn\'t seem to work on the program, I
ID: 3839086 • Letter: S
Question
Step 2 on Virtual C# 2012 exercise 4.11 doesn't seem to work on the program, I get multiple errors, is there something missing on this solution?
All of my code is here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Account
{
///Michael Cole
/// 5/12/2017
/// <summary>
/// Account class with constructor to inialize instance variable.
/// </summary>
public class Account
{
private decimal balance; // Instance variable that stores the balance
//constructor
public Account(decimal initialBalance)
{
Balance = initialBalance; // set balance using property
} // end Account constructor
//credit (add) an amount to the account
public void Credit(decimal amount)
{
Balance = Balance + amount; //add amount to balance
} //end method credit
//a property to get and set the account balance
public decimal Balance
{
get
{
return balance;
} //end get
set
{
//validate that value is greater than or equal to 0;
//if it is not, balance is left unchanged
if (value >= 0)
balance = value;
} //end set
} //end property balance
} // end class account
public class AccountTest
{
private object account1;
private object account2;
public decimal Balance { get; private set; }
//Main method begins execution of c# app
public static void Main(string[] args)
{
Account account1 = new Account(50.00M); //Create Account object
Account account2 = new Account(-7.53M); // Create Account object
// display initial balance of each object using a property
Console.WriteLine("account1 balance: {0:C}",
account1.Balance); // display Balance property
Console.WriteLine("account2 balance: {0:C} ",
account2.Balance); // display Balance property
decimal depositAmount; // deposit amount read from user
//prompt and obtain user input
Console.Write("Enter deposit amount for account1; ");
depositAmount = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("adding {0:C} to account1 balance ",
depositAmount);
account1.Credit(depositAmount); // add to account1 balance
// display balances
Console.WriteLine("account1 balance: {0:C}",
account1.Balance);
Console.WriteLine("account2 balance: {0:C} ",
account2.Balance);
//prompt and obtain user input
Console.Write("Enter deposit amount for account2: ");
depositAmount = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("adding {0:C} to account2 balance ",
depositAmount);
account2.Credit(depositAmount); // add to account2 balance
// display balances
Console.WriteLine("account1 balance: {0:C}", account1.Balance);
Console.WriteLine("account2 balance: {0:C}", account2.Balance);
} //end Main
/*
* method to withdraw money from the account
* @param amount is the money to be withdrawn
* return true if withdrawel successful, false otherwise
*/
public bool Debit(decimal amount)
{
// check if the account balance is less than the money to withdrawn
if (Balance < amount)
{
//display the error message
Console.WriteLine("Debit amount exceeded account balance");
return false;
}
else
//deduct the ammount
Balance = Balance - amount;
return true;
}
bool isDebit;
decimal withDrawAmount;
//Enter the amount to be withdrawn from account1
Console.WriteLine("Enter amount to withdraw from account1:");
withDrawAmount=Convert.ToDecimal(Console.ReadLine());
//call the method to withdraw the amount
isDebit=account1.Debit(withDrawAmount);
//display a message if the amount was withdrawn successfully
if(isDebit)
Console.WriteLine("Amount withdrawn successfully. New balance for account1 is:{0:C) ",account1.Balance);
// Enter the amount for account2
Console.WriteLine("Enter amount to withdraw from account2:");
withDrawAmount=Convert.ToDecimal(Console.ReadLine());
// call the amount to withdraw the amount
isDebit=account2.Debit(withDrawAmount);
// display a message if the amount was withdrawn successfully
if(isDebit)
Console.WriteLine("Amount withdrawn successfully. New balance for account2 is:{0:C) ", account2.Balance);
} // End class Account test
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Account
{
///Michael Cole
/// 5/12/2017
/// <summary>
/// Account class with constructor to inialize instance variable.
/// </summary>
public class Account
{
private decimal balance; // Instance variable that stores the balance
//constructor
public Account(decimal initialBalance)
{
Balance = initialBalance; // set balance using property
} // end Account constructor
//credit (add) an amount to the account
public void Credit(decimal amount)
{
Balance = Balance + amount; //add amount to balance
} //end method credit
//a property to get and set the account balance
/*
* method to withdraw money from the account
* @param amount is the money to be withdrawn
* return true if withdrawel successful, false otherwise
*/
public bool Debit(decimal amount)
{
// check if the account balance is less than the money to withdrawn
if (Balance < amount)
{
//display the error message
Console.WriteLine("Debit amount exceeded account balance");
return false;
}
else
//deduct the ammount
Balance = Balance - amount;
return true;
}
public decimal Balance
{
get
{
return balance;
} //end get
set
{
//validate that value is greater than or equal to 0;
//if it is not, balance is left unchanged
if (value >= 0)
balance = value;
} //end set
} //end property balance
} // end class account
public class AccountTest
{
private object account1;
private object account2;
public decimal Balance { get; private set; }
//Main method begins execution of c# app
public static void Main(string[] args)
{
Account account1 = new Account(50.00M); //Create Account object
Account account2 = new Account(-7.53M); // Create Account object
// display initial balance of each object using a property
Console.WriteLine("account1 balance: {0:C}",
account1.Balance); // display Balance property
Console.WriteLine("account2 balance: {0:C} ",
account2.Balance); // display Balance property
decimal depositAmount; // deposit amount read from user
//prompt and obtain user input
Console.Write("Enter deposit amount for account1; ");
depositAmount = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("adding {0:C} to account1 balance ",
depositAmount);
account1.Credit(depositAmount); // add to account1 balance
// display balances
Console.WriteLine("account1 balance: {0:C}",
account1.Balance);
Console.WriteLine("account2 balance: {0:C} ",
account2.Balance);
//prompt and obtain user input
Console.Write("Enter deposit amount for account2: ");
depositAmount = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("adding {0:C} to account2 balance ",
depositAmount);
account2.Credit(depositAmount); // add to account2 balance
// display balances
Console.WriteLine("account1 balance: {0:C}", account1.Balance);
Console.WriteLine("account2 balance: {0:C}", account2.Balance);
bool isDebit;
decimal withDrawAmount;
//Enter the amount to be withdrawn from account1
Console.WriteLine("Enter amount to withdraw from account1:");
withDrawAmount = Convert.ToDecimal(Console.ReadLine());
//call the method to withdraw the amount
isDebit = account1.Debit(withDrawAmount);
//display a message if the amount was withdrawn successfully
if (isDebit)
Console.WriteLine("Amount withdrawn successfully. New balance for account1 is:{0:C} ", account1.Balance);
// Enter the amount for account2
Console.WriteLine("Enter amount to withdraw from account2:");
withDrawAmount = Convert.ToDecimal(Console.ReadLine());
// call the amount to withdraw the amount
isDebit = account2.Debit(withDrawAmount);
// display a message if the amount was withdrawn successfully
if (isDebit)
Console.WriteLine("Amount withdrawn successfully. New balance for account2 is:{0:C} ", account2.Balance);
} //end Main
} // End class Account test
}
Some part of the code was placed incorrectly hence it was compiling. Rearranging those part makes the code work. I have fixed that. Kindly check the code provided.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.