I got this code from a Chegg expert I was trying to make it run correctly in C#
ID: 3748780 • Letter: I
Question
I got this code from a Chegg expert I was trying to make it run correctly in C# Mirosocft visual studios. I was getting a few errors so it could not debug. Maybe I was'nt doing something important. I tried pasting it in a new console app after deleating the default :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp37
{
class Program
{
static void Main(string[] args)
{
}
}
}
I even tired to mix and match everything and I am getting stuck on the system. stuff in addition to a few other similar errors.
Here is the code:
//Account.cs
using System.Text.RegularExpressions;
namespace BankAccount
{
public enum AccountType
{
Savings,
Checking
}
public class Account
{
string accountNumber_;
AccountType accountType_;
double accountBalance_;
public Account()
{
accountNumber_ = "00000000000000000000";
accountType_ = AccountType.Savings;
accountBalance_ = 0;
}
public Account(string acNo, AccountType acType, double bal)
{
SetAccountNumber(acNo);
accountType_ = acType;
SetAccountBalance(bal);
}
public string GetAccountNumber()
{
return accountNumber_;
}
public void SetAccountNumber(string accountNo)
{
string accountNoRegx = @"^d{20}$";
if(Regex.Match(accountNo, accountNoRegx).Success)
accountNumber_ = accountNo;
}
public AccountType GetAccountType()
{
return accountType_;
}
public void SetAccountType(AccountType type)
{
accountType_ = type;
}
public double GetAccountBalance()
{
return accountBalance_;
}
public void SetAccountBalance(double amount)
{
accountBalance_ = amount < 0 ? 0 : amount;
}
public override string ToString()
{
return string.Format("Account number: {0} Account type: {1} Account Balance: ${2}", accountNumber_, accountType_, accountBalance_);
}
public override bool Equals(object obj)
{
Account other = obj as Account;
return accountNumber_ == other.accountNumber_ && accountType_ == other.accountType_ && accountBalance_ == other.accountBalance_;
}
}
}
//Bank.cs
using System.Text.RegularExpressions;
namespace BankAccount
{
public class Bank
{
string name_;
string address_;
string phoneNumber_;
Account[] accounts_;
private int GetNoOfAccounts()
{
int noOfAcc = 0;
for (int i = 0; i < accounts_.Length; ++i)
{
if (accounts_[i] != null)
noOfAcc++;
}
return noOfAcc;
}
public Bank()
{
name_ = string.Empty;
address_ = string.Empty;
phoneNumber_ = string.Empty;
accounts_ = new Account[10];
}
public Bank(string name, string address, string phoneNumber, Account[] accounts)
{
name_ = name;
address_ = address;
SetPhoneNumber(phoneNumber);
accounts_ = accounts;
}
public string GetName()
{
return name_;
}
public void SetName(string name)
{
name_ = name;
}
public string GetAddress()
{
return address_;
}
public void SetAddress(string address)
{
address_ = address;
}
public string GetPhoneNumber()
{
return phoneNumber_;
}
public void SetPhoneNumber(string phoneNumber)
{
string phoneNumberRegx = @"^d{3}-d{3}-d{4}$";
if (Regex.Match(phoneNumber, phoneNumberRegx).Success)
phoneNumber_ = phoneNumber;
}
public Account[] GetAccounts()
{
return accounts_;
}
public override string ToString()
{
return string.Format("Bank Name: {0} PhoneNumber: {1} Address: {2} Number Of Accounts: {3}", name_, phoneNumber_, address_, GetNoOfAccounts());
}
public override bool Equals(object obj)
{
Bank other = obj as Bank;
var isNonAcInfoMatches = name_.ToUpper() == other.name_.ToUpper() && phoneNumber_ == other.phoneNumber_ && address_.ToUpper() == other.address_.ToUpper();
if (!isNonAcInfoMatches)
return false;
for (int i = 0; i < accounts_.Length; i++)
{
if (!accounts_[i].Equals(other.accounts_[i]))
return false;
}
return true;
}
public bool AddAccount(Account account)
{
if (account == null)
return false;
int lastIndex = -1;
for (int i = 0; i < accounts_.Length; i++)
{
if (accounts_[i] == null)
lastIndex = i;
else if (accounts_[i].Equals(account))
return false;
}
if (lastIndex != -1)
{
accounts_[lastIndex] = account;
return true;
}
return false;
}
public bool RemoveAccount(Account account)
{
if (account == null)
return false;
for (int i = 0; i < accounts_.Length; i++)
{
if (accounts_[i] == null)
continue;
if (accounts_[i].Equals(account))
{
accounts_[i] = null;
return true;
}
}
return false;
}
}
}
//BankApplication.cs
using System;
namespace BankAccount
{
class BankApplication
{
public static void AddAccounts(Bank bank)
{
for (int i = 0; i < 10; i++)
{
string accountNumber = "0000000000000000000" + i.ToString(); //19 zeroes and a last digit
Account a = new Account();
a.SetAccountNumber(accountNumber);
bank.AddAccount(a);
}
}
static void Main(string[] args)
{
Bank bank = new Bank();
bank.SetName("Bank 1");
bank.SetAddress("Bank 1 Address");
AddAccounts(bank);
bank.RemoveAccount(bank.GetAccounts()[3]);
bank.RemoveAccount(bank.GetAccounts()[7]);
Console.WriteLine(bank.ToString());
foreach(var ac in bank.GetAccounts())
{
if (ac == null)
continue;
Console.WriteLine(ac.ToString());
}
Bank bank2 = new Bank();
bank2.SetName("Bank 2");
bank2.SetAddress("Bank 2 Address");
AddAccounts(bank2);
Console.WriteLine(bank.Equals(bank2));
Account[] accounts = new Account[10];
Bank bank3 = new Bank("Bank 3", "Bank 3 Address", "000-000-0000", accounts);
Account account = new Account();
bank3.AddAccount(account);
Console.WriteLine(bank.RemoveAccount(account));
Console.WriteLine(bank3.ToString());
Console.ReadLine();
}
}
}
Here is the orginal problem it looks right Im just stuck on the two non-static classes:
Implement the following 2 non-static classes. Each class must be in its own C# file. If the following two classes are not each within their own C# file.
1) Account class
Implement private fields with public setters and getters in the Account class - use appropriate data types as discussed in class
AccountNumber - should allow only for 20 digits, including leading zeros, but NO LETTERS - example value: "00045678493857683928". Your setter should set empty if validation fails. - "Numbers" allowing for leading zeros are really just strings. If less than 20 digits or more than 20 digits set to empty string
AccountType - "Savings" or "Checking" - no other types or string should be accepted in this setter. If I pass "MoneyMarket" the setter should default this property to empty. Casing can be ignored - for example, "SAVINGS" and "savings" can be accepted. I will accept strings but can also be enum.
AccountBalance - How much money is in the account. Balance should never be less than zero. If your setter allows for setting a value less than zero you will lose points. You should check the value first, and if it is less than zero, just set the balance to zero.
Implement the following public methods (behaviors) in the Account class
Default constructor - no arguments but the properties are initialized to non-null values.
Parameterized constructor - arguments for all properties can be passed (AccountNumber, AccountType, AccountBalance)
ToString - return a formatted string with labels for each of the following: account number, account type, and balance. AccountBalance should be formatted with a $ and decimals. This should be in a single string on 1 line.
- ex: "Account number: 11111111111111111111 Account type: Checking Account Balance: $500.00"
Equals - accept an object as a parameter, cast it to an Account type, and compare the following
- Whether the Account number, Account type, and Account balance match. If they match return true, else return false
- Ignore mixed casing on Account type - Use ToUpper or ToLower to ignore casing
"CHECKING" should match "checking", etc.
2) Bank class
Implement private fields with public setters and getters in the Bank class - use appropriate data types as discussed in class.
Name - example value: "FirstUSA Bank"
Address - example value: "123 Main St. Cleveland, OH. 44177."
PhoneNumber - example value: "216-999-9999". A phone number value should NOT be settable if the format of the string is anything other than ###-###-####. You will need to check the format before allowing a setting of the value. For example, a value of "123" or "2169999999" should not be settable - it MUST be formatted as "###-###-####"
- Set value to empty if the validation rule fails - you may want to consider implementing the validation as a private helper method, like private bool IsPhoneNumberValid(string phoneNumber) {}
Accounts (use an array with a max size of 10) -
- NOTE: The assignment calls for a max size of 10. If you choose to use any other collection type, you must introduce a constraint to only hold 10, and it must also store the Accounts in sequential order (i.e. order in which they were added), just like a traditional array declared with brackets (i.e. []).
Implement the following public methods (behaviors) in the Bank class
Default constructor - no arguments but the properties (private field values) are initialized to non-null values.
NOTE: The Accounts array itself should not be null after initialization, but the individual instances of Account can be null when the property is initialized.
Parameterized constructor - all property arguments can be passed (name, address, phone number, and account list) - NOTE: The phone number validation is still needed here.
ToString - return a formatted string with labels for each of the following: the Name, address, phone number, and number of non-null accounts
For example: "Bank Name: FirstUSA Bank PhoneNumber: 444-555-6666 Address: 123 Main St. Cleveland, OH 44060 Number Of Accounts: 3" - If you only have 3 non-null accounts, you have 3 accounts. Even though your array holds 10, if you only added 3 accounts, you should only show 3 in your ToString()
Equals - returns true or false - accept an object, cast it to a Bank type, and compare the following
Whether the bank names match, regardless of mixed casing (hint, try comparing using ToUpper or ToLower)
- EX: "FirstUSA Bank" should be equal when compared to "firstusa BANK"
- Whether the addresses match, regardless of mixed casing
- Whether the accounts match in sequential order - NOTE: the accounts will be implementing their own Equals method which you will need to use inside the Bank's Equals method. Do not sort Accounts arrays before comparing.
- Recall that equal compares an object parameter to the class instance itself
- Pseudocode:
Compare non-account info (name/phone/address)
If they don't match return false
If those match, compare each bank's accounts:
for (int i = 0; i < Accounts.Length; i++)
{
if (!Accounts[i].Equals(accountParam[i])) return false; // ! is not operator
}
Otherwise return true, everything matches.
AddAccount - Accept 1 account as a parameter. Make sure parameter is not null - if null do not add the account. Add a non-null instance of account to your Accounts property, to the last available (null) index of the Accounts array. An account should not be added if (a) any account matches using Account .Equals method (see below) OR your account array is already maxed out at 10 entries.
The AddAccount method should be a bool return type. Return a true if the account was added and a false if it was not. You will need to write a loop and check each index to see if the value in the accounts array index is null or not null. For example (if Accounts[0] == null then set Accounts[0] = non-null account instance parameter)
RemoveAccount - Accept 1 account as a parameter. Ensure parameter is not null before proceeding. Find the index of the account that matches.
Use .Equals method on account and a for loop (or some other loop type of your choice). If the Account instance parameter matches, set the index value to null. The RemoveAccount should be a bool type. Return true if an account was removed, otherwise return false . If parameter was null, or no account matched and removal did not occur, return false.
Implement the following in Program.cs
Rename Program.cs to BankApplication.cs - retain the Main() method as your entry point
This should remain a static class.
In Main method:
Instantiate a new Bank instance.
Then, using a loop, Create 10 instances of account and add them to the bank using AddAccount.
With each loop iteration, alter the last digit of the account number so that each account number is unique.
HINT: Here is one way to do it but there are others
for (int i = 0; i < 10; i++)
{
string accountNumber = "0000000000000000000" + i.ToString(); //19 zeroes and a last digit
Account a = new Account();
a.AccountNumber = accountNumber;
//set other properties then add to the bank
bank.AddAccount(a);
}
Any additional directions:
It is up to you whether to use C# properties (i.e. get {} and set {}) OR GetProperty() and SetProperty() methods for the two classes. Either way, each property must have a private field that it references.
The properties, variables, and methods should be named per the coding guidelines. For example, private variables are camel case. Class names and methods are Pascal case. Unlike previous assignments, you will lose points on this assignment if you do not follow the guidelines.
Do not just add Public variables (Fields) to your class. Use properties or set/get methods which modify/retrieve private field values. If you do not use properties with setters/getters, you will lose points.
ToString() should return strings only, do not put Console.WriteLine in your ToString() methods.
Explanation / Answer
The code is correct. Its getting executed too. Make sure you put all the three files in the same folder. As it written in code of all the three files "namspace BankAccount", when you make Console Application name it as BankAccount. Put all the three files Account.cs,Bank.cs and BankApplication.cs in the folder BankAccount
In the Visual Studio,
click on File -> New -> Project. Select Console Application From Visual C#. Give the name for the application as BankAccount since in the code the application has been named as BankAccount in the line namespace BankAccount.
Or you can give any other name for the application. But you have to make the changes accordingly in your code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.