-4

I created a class called bank and its subclass called ATM and I wish to declare an instance of the subclass ATM. When i declare it as shown below it gives me an error.

package bank;
import java.util.Scanner;
public class Bank {// Bank class
    protected int PIN,chequing_balance,savings_balance;
    public void set_PIN(int PIN){
   this.PIN= PIN;
}
public int get_PIN(){
    return this.PIN;
}
public void set_chequing_balance(int chequing_balance){
   this.chequing_balance=chequing_balance; 
}    
public int get_chequing_balance(){
    return chequing_balance;
}
public void set_savings_balance(int savings_balance){
   this.savings_balance=savings_balance; 
}    
public int get_savings_balance(){
    return savings_balance;
}




public class ATM extends Bank{// ATM is a subclass of Bank
    private void ATM(){
    PIN=1207605;
    chequing_balance=0;
    savings_balance=0;
}
    public void deposit_savings(int amount){
    this.savings_balance+=amount;
}
    public void withdrawl_savings(int amount){
    this.savings_balance-=amount;
}
        public void transfer_savings_to_chequing(int amount){
   this.chequing_balance+=amount;
   this.savings_balance-=amount;
}
        public void deposit_chequing(int amount){
    this.chequing_balance+=amount;
}
    public void withdrawl_chequing(int amount){
    this.chequing_balance-=amount;
}
        public void transfer_chequing_to_savings(int amount){
   this.savings_balance+=amount;
   this.chequing_balance-=amount;
}

}
    public static void main(String[] args) {
        int key,amount;
        ATM x= new ATM(); // declaration of instance of ATM subclass
        Scanner input= new Scanner(System.in);
       do{
           System.out.println("Please enter transaction type  1.Deposit, 2. Withdraw 3. Transfer ");
           key=input.nextInt();

        }while(key<1||key>3);
   do{
       System.out.println("Please enter desired amount");
       amount=input.nextInt();
   }while(amount<0);
   if (key==1)
   {
       System.out.println("Please enter account desired 1. Chequing 2. Savings");
       key=input.nextInt();
       if (key==1)
       {
           x.deposit_chequing(amount);
       }
       else if(key==2)
       {
           x.deposit_savings(amount);
       }    
   }
   else if (key==2)
   {
       System.out.println("Please enter account desired 1. Chequing 2. Savings");
       key=input.nextInt();
       if(key==1)
       {
           x.deposit_chequing(amount);
       }
       else if(key==2)
       {
           x.deposit_savings(amount);
       }
   }
   else if(key==3)
   {
    System.out.println("Please enter type of transaction you want 1. Chequing to Savings 2. Savings to Chequing");  
    key=input.nextInt();
    if (key==1)
    {
        x.transfer_chequing_to_savings(amount);
    }
    else if (key==2)
    {
        x.transfer_savings_to_chequing(amount);
    }
   }
}
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
noname
  • 1
  • 1
  • 1
  • I created my constructor and still in my main method, when i create a new ATM, the error is 'non static variable this cannot be referenced from a static context' – noname Oct 31 '12 at 04:06

2 Answers2

2
private void ATM(){

That's private and not even a constructor, it's a method. Constructor should not have a return type.

Also, an ATM should not extend a Bank.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • 1
    There is no constructor on the parent either. – doublesharp Oct 31 '12 at 03:39
  • @doublesharp that's no problem, the compiler will set a default public constructor with no parameters for you. – Luiggi Mendoza Oct 31 '12 at 03:40
  • @LuiggiMendoza Yep, that's what I was saying... outside of the default constructor, nothing is set for this hierarchy. – doublesharp Oct 31 '12 at 03:42
  • I have lab in my programming course and the professor wants us to use that model. The ATM is a subclass of the bank class in the sense that a bank has an account and the ATM implements interface (typical usages) of a bank account. In this lab, a couple of assumptions are made including the fact that there will be one bank accoun – noname Oct 31 '12 at 04:01
  • 1
    @ZeyadEmara: If that's the case then I think that's what you should do. Otherwise in the real "Bank has-a ATM" sounds logically more correct than "ATM is-a Bank". – Bhesh Gurung Oct 31 '12 at 04:03
0

Your constructor should not declare a return type it should be:

public ATM() {
   ....
}

Also when you have a subclass you have to make it passes the IS-A test. All subclasses should say yes to this test in order to be an appropriate subclass. ATM IS-A Bank? Hmmm not exactly. You could come up with a superclass for ATM, but Bank might not be it. A Bank might own many ATMs. An ATM IS-A AccountAccessPoint or IS-A CashDispenser. But this is more of a modeling exercise.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • I have lab in my programming course and the professor wants us to use that model. The ATM is a subclass of the bank class in the sense that a bank has an account and the ATM implements interface (typical usages) of a bank account. In this lab, a couple of assumptions are made including the fact that there will be one bank account – noname Oct 31 '12 at 04:00
  • Well sadly your professor is teaching bad habits, but now you know about IS-A relationship and how it can be applied. So you are smarter for asking your question. You might ask your prof why ATM subclasses Bank and why that doesn't pass the IS-A test. Just to see what he/she says. :-) – chubbsondubs Oct 31 '12 at 04:04
  • haha its my first time to program in java. I did Object Oriented in C++ before but java is a bit different – noname Oct 31 '12 at 04:09