Click here to Skip to main content
16,004,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone,

I'm new to these forums, and I'm also just learning C# I'm trying to learn the language. I'm just trying to learn the language by working on projects, but I'm running into some problems. I'm new here, so please don't rake me through the coals :(

I want to make a bank account that creates random account numbers, using object oriented methods.

I'm trying to add randomly generated account numbers to Account objects, but I'm running into some problems, and I haven't been able to find any solutions online. I know there's tons of resources out there, but I'm still having issues trying to piece everything together.

1) I want to first add a private static object Random to my class, which I named "Account" - separate class from the main program

2) Then I tried to create a random number generating function to my "Account" class, which you can see is named "random3digInt". It should return a value, and I don't really know if it needs parameters.

3) In the constructor, I want to call the random3digInt function so that random numbers can be generated and assigned to the account number value - I'm guessing whatever I have in the parameter list is wrong


C#
class Account
  {
     const double DEFAULT_INTEREST = 2.5;
     private int accountNo; // accountNumber
     private double balance; // account balance
     private double interestRate; // percent annual interest rate
     private string message;
     private Random rg;





     public void random3digitInt() // add to the account class a random number generator function
         // that takes no parameters and returns and int value
     {
         int AccountNo;
         AccountNo = rg.Next(100, 999);
         return;
     }



      public Account(int acctNo, double bal, double ir)  // constructor

   {
     acctNo= Randorandom3digitInt();  // my attempt to call the random3digitInt() function


         message = ""; // empty message
        if (bal >= 0)
        {
           balance = bal;
        }
        else
        {



... didn't copy all the code


In my main program, I have this: (and I know it's all very wrong)

C#
static void Main(string[] args)


      {
         Account acct = new Account(acctNo,100,2.5); // account #1 with $100 and 2.5% annual rate

          acct.display();
         int choice;

         double bal, r, amt; // balance, annual rate, amount
         int y; // number of years
         double fut; // future value




Thanks!
Posted

Concentrating on the Random... you didn't make it static and you didn't initialize it.


And of course there's lots more to neaten up in there, but start with that and let us know what errors or behaviour you want to correct.
 
Share this answer
 
hey hi,


write yout main method and constructor like below: IF you want AccountNo

C#
 public Account(int acctNo, double bal, double ir,out int accno)  // constructor

   {
       acctNo= Randorandom3digitInt();  // my attempt to call the random3digitInt() function

       accno=acctNo;    
      
   
         message = ""; // empty message
        if (bal >= 0)
        {
           balance = bal;
        }
        else
        {
}

//Main method

static void Main(string[] args)


      {
         int accno;  
         Account acct = new Account(acctNo,100,2.5,out accno); // account #1 with $100 and 2.5% annual rate

        //  acct.display();

//So here you can used accno IF you want to retrieve accountNo only

  console.writeline(accno);

          
         int choice;

         double bal, r, amt; // balance, annual rate, amount
         int y; // number of years
         double fut; // future value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900