1

I am using eclipse to look at my code and the most common error that comes up is "Syntax error on token(s), misplaced construct(s)" I'm not sure what I am doing wrong, but I am fairly new to Java.

My code is supposed to withdraw an indicated (user-input) amount from a bank account, I started off with $10,000 and set up the program so that if the withdrawal amount is less than 0 or greater than $10,000 it will trigger an assertion error.

  class ThreadsUnitProject2 {
public static void main(Sting args [])
// Field member 
private int balance; 

public void BankAccount() 
{ 
balance = 10000; 
} 

public int withdraw(int amount) 
{   
// Subtract requested amount from balance 
balance-=amount; 

// Return requested amount 
return amount; 
} 


public int getBalance() 
{ 
return balance; 
} 


import java.util.Scanner; 

class BankAccountTester extends BankAccount
{ 
public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 

    BankAccount myAccount = new BankAccount(); 

    System.out.println("Balance = " + myAccount.getBalance()); 

    System.out.print("Enter amount to be withdrawn: "); 
int amount = scan.nextInt(); 

    assert (amount >= 0 && amount <= myAccount.getBalance()):"You can't withdraw that amount!"; 

    myAccount.withdraw(amount); 

    System.out.println("Balance = " + myAccount.getBalance()); 
} 

Thank you for all of your help!

Kristen
  • 29
  • 2
  • 2
  • 3

2 Answers2

0

rename your class "ThreadsUnitProject2" to BankAccount and remove main() method from that and make BankAccountTester class public and finally remove void from BankAccount constructor

Ashish
  • 1,121
  • 2
  • 15
  • 25
0

After seeing so many issues in your code I decided I should just fix it and let you try to learn from the solution seen below.

This should be the first Class file.

public class BankAccount {

    private int balance;

    public BankAccount() {      //constructor
        balance = 10000;
    }

    public int withdraw(int amount) {
        balance -= amount;
        return amount;
    }

    public int getBalance() {
        return balance;
    }
}

This should be your second Class file. This one will contain your main method which will test your BankAccount class.

import java.util.Scanner;

public class BankAccountTester {
    public static void main(String[] args) {
        Scanner     scan      = new Scanner(System.in);
        BankAccount myAccount = new BankAccount();

        System.out.println("Balance = " + myAccount.getBalance());
        System.out.print  ("Enter amount to be withdrawn: ");
        int amount = scan.nextInt();

        assert (amount >= 0 && amount <= myAccount.getBalance()) : "You can't withdraw that amount!";
        myAccount.withdraw(amount);

        System.out.println("NewBalance = " + myAccount.getBalance());
    }
}

Please read and review this link to proper coding conventions.

Tdorno
  • 1,571
  • 4
  • 22
  • 32
  • Also, assert should not be used for program logic as when deployed they are disabled by default and need to be enabled with the -ea argument. Good explaination here: http://stackoverflow.com/questions/2758224/assertion-in-java – LINEMAN78 Dec 14 '13 at 01:10