0

I'm new to programming and this class-things are doing my head in.

Here is my code (type is based on userinput)

public static Account CreateAccount(int type)
{
    switch (type) 
    { 
        case 1:
            SaveAcc savings = new SaveAcc();  
        break;

        default:
            Console.WriteLine("No such choice");
        break;

    }
    return new Account();
}

These are my classes:

class Account 
{
    protected int balance;
    protected int accountnr = 1;
    protected bool credit;

    public Account()
    {
        newNr++;
        accountnr = newNr;
    }

    public override string ToString()
    {
        return "AccNr: " + Nr.ToString("G") + ", balance: " + balance.ToString("C");
    }
}

class SaveAcc: Account
{
    public int rate;
    public SaveAcc()
    {
        credit = true;
        rate = 0.03;
    }

    public override string ToString()
    {
       return "AccNr: " + Nr.ToString("G") + ", balance: " + balance.ToString("C") + credit.ToString();
    }
}

When I create a SavAcc object the "wrong" override is being called. My goal is to display all the information given by the override ToString method located in the SavAcc inherited class. Am I missing something obvious?

Jason Watkins
  • 3,766
  • 1
  • 25
  • 39
153qa
  • 1
  • 1
  • Where is `SavAcc`? What is `SaveAcc`? Where you calling it? – leppie Mar 31 '16 at 20:36
  • Typo, SalAcc is supposed to be SaveAcc @leppie – 153qa Mar 31 '16 at 20:37
  • 2
    OK, in `case 1`, you never return the created instance. You probably meant to. – leppie Mar 31 '16 at 20:41
  • @leppie is correct. Your switch is ultimately meaningless as written. – Anthony Pegram Mar 31 '16 at 20:41
  • 1
    Read this http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and then explain why your code works to a rubber duck. The point at which you cannot is where the bug is. – Eric Lippert Mar 31 '16 at 22:12
  • 2
    Also, tk ths opprnty to stp usng abbrvs n yr cd. It makes it incredibly hard to read, and hard to read is the opposite of what you want as a beginner. It's not a `SaveAcc`. Its a `SavingsAccount`. It's not an `accountnr`, its an `accountNumber`. – Eric Lippert Mar 31 '16 at 22:14
  • Also, pet peeve, I know this is a standard exercise, but it always bugs me. This is *nothing whatsoever* like how actual accounts are modeled in software. A bank account is a write-at-the-end-only list of transactions, not a mutable balance. A mutable balance cannot be audited! – Eric Lippert Mar 31 '16 at 22:15

1 Answers1

0

Change this:

switch (type) 
{ 
        case 1:
            SaveAcc savings = new SaveAcc();  
        break;

        default:
            Console.WriteLine("No such choice");
        break;

}
return new Account();

To this:

switch (type) 
{ 
    case 1:
        return new SaveAcc();  
    default:
        Console.WriteLine("No such choice");
        return new Account();
}
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Thank you, why isn't the break needed though? – 153qa Mar 31 '16 at 20:51
  • 1
    `break` tells the switch not to "fall through" into the next case. Because `return` causes the entire method to exit right then and there, it's not possible to continue executing the switch, so there's no need to prevent it by including `break` – Jason Watkins Mar 31 '16 at 20:59