Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Managment of an bank account

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Managment of an bank account

    Hey guys!

    So i have this assignment where i need to write a program to manage some bank accounts and i'm completly lost. This is what I've got so far

     
    class Customer {
      final String firstName;
      final String lastName;
      final String phoneNumber;
     
      Customer(String firstName,String lastName,String phoneNumber){
           this.firstName = firstName;
    	   this.lastName = lastName;
    	   this.phoneNumber = phoneNumber;
     
    	 }
    	 static Customer readCustomer {
    	 String firstName = In.readString();
    	 String lastName = In.readString();
    	 String phoneNumber = In.readString();
     
    	 if(In.done()) return new Customer(firstName,lastName, phoneNumber);
    	 else return null;
     
    	 }
     
    	 void print() {
    	 Out.format("%-15s %-15s %-15s", firstName,lastName,phoneNumber);
    	 }
    	 }
     
    class Account {
       final int accountNo;
       final double overdraft;
       final double accountBalance;
     
     
       Account(int accountNo,double overdraft,double accountBalance) {
            this.accountNo = accountNo;
    		this.overdraft = overdraft;
    		this.accountBalance = accountBalance;
     
    		accountBalance = 0;
    		}
     
    		static Account readAccount {
    		int accountNo = In.readInt();
    		double overdraft = In.readDouble();
     
    		if(In.done()) return new Account(accountNo,overdraft);
    		else return null;
    		}
     
    		void print() {
    		Out.format("%-15d %-15f", accountNo,overdraft);
    		}
     
     
    	boolean deposit(double amount) {
    	     double amount;
    		 Out.print("How much would you like to deposit:");
    		 amount = In.readDouble();
    		 if(amount>0) {
    		 return accountBalance = accountBalance+amount;
    		 else return null;
    		 }
     
    	 boolean withdraw(double amount) {
    	     double amount;
    		 Out.print("How much would you like to withdraw:");
    		 amount = In.readDouble();
    		 if(accountBalance>=amount) {
    		return accountBalance = accountBalance-amount;
    		else return null;
    		 }
     
    		 boolean isCovered(double amount) {
    		   double amount;
    		   amount = In.readDouble();
    		   if(accountBalance>=amount)
    		  return accountBalance;
    	      else return null;
    		 }
     
    		   boolean transfer(targetAccount,double amount);
    		   double amount;
    		   Out.println("How much would you like to transfer:");
    		   amount = In.readDouble();
    		   int targetAccount;
    		   Out.println("To whom would you like to transfer:");
    		   if(amount<=0 && accountBalance==0) {
    		   return null;
    		   else return targetAccount = amount;
    }	 
    class Banking {
    static final int CAPACITY = 100;
    Account[] accounts = new Account[CAPACITY];
    int nOfaccounts;
     
    int createAccount(String firstName, String lastName, String phone, double overdraft) {
    	Out.println("Enter your first name:");
    	String firstName = In.readLine();
    	Out.println("Enter your last name:");
    	String lastName = In.readLine();
    	Out.println("Enter your phone number:");
    	String phone = In.readLine();
    	Out.println("Enter the value of overdraft:");
    	double overdraft = In.readDouble();
    	if (nOfaccounts == accounts.length) {
          return -1;
    	} else return 
     
     
    	boolean deposit(int accountNo,double amount) {

    So my question is, why do i need to write in both the account and in banking class the methods deposit withdraw and so on... And also how do i return values with these boolean methods, when i try return(true) or false , when i compile it i get an error.Could anyone help?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Managment of an bank account

    when i compile it i get an error.
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Managment of an bank account

    Hey, I have solved that problem (I think at least) but now i'm encountering a new one;

    Banking.java:112: error: '(' or '[' expected
    } else return new accountNo;

     
    class Customer {
      final String firstName;
      final String lastName;
      final String phoneNumber;
     
      Customer(String firstName,String lastName,String phoneNumber){
           this.firstName = firstName;
    	   this.lastName = lastName;
    	   this.phoneNumber = phoneNumber;
     
    	 }
    	 static Customer readCustomer() {
    	 String firstName = In.readString();
    	 String lastName = In.readString();
    	 String phoneNumber = In.readString();
     
    	 if(In.done()) return new Customer(firstName,lastName, phoneNumber);
    	 else return null;
     
    	 }
     
    	 void print() {
    	 Out.format("%-15s %-15s %-15s", firstName,lastName,phoneNumber);
    	 }
    	 }
     
    class Account {
       final int accountNo;
       final double overdraft;
       final double accountBalance;
     
     
       Account(int accountNo,double overdraft,double accountBalance) {
            this.accountNo = accountNo;
    		this.overdraft = overdraft;
    		this.accountBalance = accountBalance;
     
    		accountBalance = 0;
    		}
     
    		static Account readAccount() {
    		int accountNo = In.readInt();
    		double overdraft = In.readDouble();
     
    		if(In.done()) return new Account(accountNo,overdraft);
    		else return null;
    		}
     
    		void print() {
    		Out.format("%-15d %-15f", accountNo,overdraft);
    		}
     
     
    	boolean deposit(double amount) {
    	     double amount;
    		 Out.print("How much would you like to deposit:");
    		 amount = In.readDouble();
    		 if(amount>0) {
    		 accountBalance = accountBalance+amount;
    		 return true;
    		 }
    		 else return false;
    	}
     
    	 boolean withdraw(double amount) {
    	     double amount;
    		 Out.print("How much would you like to withdraw:");
    		 amount = In.readDouble();
    		 if(accountBalance>=amount) {
    		 accountBalance = accountBalance-amount;
    		 return true;
    		 }
    		else return false;
    	 }
     
    		 boolean isCovered(double amount) {
    		   double amount;
    		   amount = In.readDouble();
    		   if(accountBalance>=amount){
    		  return true;
    		   }
    		   else return false;
    		 }
     
    		   boolean transfer(int targetAccount,double amount){
    		   double amount;
    		   Out.println("How much would you like to transfer:");
    		   amount = In.readDouble();
    		   int targetAccount;
    		   Out.println("To whom would you like to transfer:");
    		   if(amount<=0 && accountBalance==0) {
    		   return false;
    		   }
    		   else return true;
    }	 
    class Banking {
    static final int CAPACITY = 100;
    Account[] accounts = new Account[CAPACITY];
    int nOfaccounts;
     
    int createAccount(String firstName, String lastName, String phone, double overdraft) {
    	Out.println("Enter your first name:");
    	String firstName = In.readLine();
    	Out.println("Enter your last name:");
    	String lastName = In.readLine();
    	Out.println("Enter your phone number:");
    	String phone = In.readLine();
    	Out.println("Enter the value of overdraft:");
    	double overdraft = In.readDouble();
    	if (nOfaccounts == accounts.length) {
          return -1;
    	} else return new accountNo;
     
    	nOfaccounts++;
    }
    	boolean deposit(int accountNo,double amount) {

    What I am trying to achieve in this part of the code is what the assingments actually says to do, it says to create a new account and if the capacity of the bank is full(100 place) to return -1 and if its not to create the account and return a new account number. I'm not sure if I am doing this part right so i posted the code maybe you can figure out the mistake. PS.I didnt get to trying to finish the drawing program, gonna do that later.

    Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Managment of an bank account

    The code's formatting needs work to make the code easier to read and understand.
    Statements within {} need to be indented

    	} else return new accountNo;
    The new statement is a call to a class's constructor. That call needs to pass arguments inside ()s to the constructor:
       new ClassName(some args);  // call ClassName's constructor
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Managment of an bank account

     
    } else return new createAccount(int accountNo);

    Better?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Managment of an bank account

    Better?
    Did you try it? What happened?

    What is the method declared to return? An instance of a class or an int value?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Managment of an bank account

    I didn't try it I want to finish the entire program, but what I'm trying to achieve with this return is to create an new account with first name last name etc... and after the account is created i want it to get an account number (like 0 1 2 3 up to 100), and i want to be able to refrence a certain account through its account number later in the code.I will try to finish the program run it, if it flops i'll post it and try to figure out a solution.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Managment of an bank account

    I didn't try it I want to finish the entire program
    That is the wrong way to write code. You should compile it frequently to catch errors early and to have a shorter list of problems to solve.
    Do not wait until the entire program is typed in before trying to compile it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Managment of an bank account

    Well I'm done now so that is that. I know its wrong and I'm trying to fix that but i'm kinda running out of time so I'm just trying to speed.

    class Customer {
      final String firstName;
      final String lastName;
      final String phoneNumber;
     
      Customer(String firstName,String lastName,String phoneNumber){
           this.firstName = firstName;
    	   this.lastName = lastName;
    	   this.phoneNumber = phoneNumber;
     
    	 }
    	 static Customer readCustomer() {
    	 String firstName = In.readString();
    	 String lastName = In.readString();
    	 String phoneNumber = In.readString();
     
    	 if(In.done()) return new Customer(firstName,lastName, phoneNumber);
    	 else return null;
     
    	 }
     
    	 void print() {
    	 Out.format("%-15s %-15s %-15s", firstName,lastName,phoneNumber);
    	 }
    	 }
     
    class Account {
       final int accountNo;
       final double overdraft;
       final double accountBalance;
     
     
       Account(int accountNo,double overdraft,double accountBalance) {
            this.accountNo = accountNo;
    		this.overdraft = overdraft;
    		this.accountBalance = accountBalance;
     
    		accountBalance = 0;
    		}
     
    		static Account readAccount() {
    		int accountNo = In.readInt();
    		double overdraft = In.readDouble();
     
    		if(In.done()) return new Account(accountNo,overdraft);
    		else return null;
    		}
     
    		void print() {
    		Out.format("%-15d %-15f", accountNo,overdraft);
    		}
     
     
    	boolean deposit(double amount) {
    	     double amount;
    		 Out.print("How much would you like to deposit:");
    		 amount = In.readDouble();
    		 if(amount>0) {
    		 accountBalance = accountBalance+amount;
    		 return true;
    		 }
    		 else return false;
    	}
     
    	 boolean withdraw(double amount) {
    	     double amount;
    		 Out.print("How much would you like to withdraw:");
    		 amount = In.readDouble();
    		 if(accountBalance>=amount) {
    		 accountBalance = accountBalance-amount;
    		 return true;
    		 }
    		else return false;
    	 }
     
    		 boolean isCovered(double amount) {
    		   double amount;
    		   amount = In.readDouble();
    		   if(accountBalance>=amount){
    		  return true;
    		   }
    		   else return false;
    		 }
     
    		   boolean transfer(int targetAccount,double amount){
    		   double amount;
    		   Out.println("How much would you like to transfer:");
    		   amount = In.readDouble();
    		   int targetAccount;
    		   Out.println("To whom would you like to transfer:");
    		   if(amount<=0 && accountBalance==0) {
    		   return false;
    		   }
    		   else return true;
    }	 
    class Banking {
    static final int CAPACITY = 100;
    Account[] accounts = new Account[CAPACITY];
    int nOfaccounts;
     
    int createAccount(String firstName, String lastName, String phone, double overdraft) {
        Out.println("Enter your first name:");
        String firstName = In.readLine();
        Out.println("Enter your last name:");
        String lastName = In.readLine();
    	Out.println("Enter your phone number:");
    	String phone = In.readLine();
    	Out.println("Enter the value of overdraft:");
    	double overdraft = In.readDouble();
    	if (nOfaccounts == accounts.length) {
          return -1;
    	} else return new createAccount(int accountNo);
     
    	nOfaccounts++;
    }
    	boolean withdraw(int accountNo,double amount) {
    		Out.println("To which account would you like to withdraw");
    		int accountNo = In.readInt();
    		Out.println("How much would you like to withdraw");
    		double amount = In.readDouble();
    		if(accountNo == false){
    			return -1;
    		} else accountNo = accountBalance - amount;
     
    	}
     
    	boolean deposit(int accountNo,double amount) {
    		Out.println("To which account would you like to deposit");
    		int accountNo = In.readInt();
    		Out.println("How much would you like to deposit");
    		double amount = In.readDouble();
    		if(accountNo = false){
    			return -1;
    		} else accountNo = accountBalance + amount;
     
    	}
     
    	boolean transfer(int fromNo, int toNo, double amount) {
    		Out.println("From which account would you like to transfer funds:");
    		int fromNo = In.readInt();
    		Out.println("To which account would you like teh funds to be transfered to:");
    		int toNo = In.readInt();
    		Out.println("How much would you like to transfer:");
    		double amount = In.readDouble();
    		if(fromNo = false && toNo = false){
    			return -1;
    		}else fromNo = fromNo - amount;
    		      toNo = toNo + amount;
    	}
     
    	boolean getBalance(int accountNo) {
    		Out.println("Choose an account:");
    		int accountNo = In.readInt();
    		if(accountNo = false) {
    			return -1;
    		} else Out.println("The balance is:" + accountBalance);
     
    	}
     
    	boolean getBalance() {
    		for(int i = 0; i<accounts.length; i++){
    		Out.println(firstName+","+lastName+","+accountNo);
    		}
    	}
     
    	public static void main(String[] args){
    		Banking b = new Banking ();
    		int ch;
    		do {
    			Out.println("1 to create an account");
    			Out.println("2 to deposit");
    			Out.println("3 to withdraw");
    			Out.println("4 to transfer");
    			Out.println("5 to check balance");
    			Out.println("6 to exit menu");
    			Out.println("What would you like");
    			ch= In.readInt();
    		    switch(ch) {
    				case 1:
    				b.createAccount;
    				break;
    				case 2:
    				b.deposit;
    				break;
    				case 3:
    				b.withdraw;
    				break;
    				case 4:
    				b.transfer;
    				break;
    				case 5:
    				b.getBalance;
    				break;
    				case 6:
    				Out.println("Goodbye!");
    				break;
    			}
    		}while(ch!=6);
     
     
    	}
    }

    Still the issue with creating a new account and assigning the account number to it persits.
    Banking.java:112: error: '.class' expected
    } else return new createAccount(int accountNo);
    ^
    Banking.java:112: error: ';' expected
    } else return new createAccount(int accountNo);
    ^
    Banking.java:180: error: not a statement
    b.createAccount;
    ^
    Banking.java:183: error: not a statement
    b.deposit;
    ^
    Banking.java:186: error: not a statement
    b.withdraw;
    ^
    Banking.java:189: error: not a statement
    b.transfer;
    ^
    Banking.java:192: error: not a statement
    b.getBalance;
    ^
    Banking.java:202: error: reached end of file while parsing
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Managment of an bank account

    Banking.java:112: error: '.class' expected
    } else return new createAccount(int accountNo)
    Is there a class named createAccount? That looks more like the name of a method than a class name.
    method names are verbs, class names are nouns.
    What is the method that code is inside of declared to return?

    Banking.java:180: error: not a statement
    b.createAccount;
    That looks like the name of method. To call a method you need to follow the name of the method with ()s and provide any optional parameters it takes. See the tutorial: https://docs.oracle.com/javase/tutor...arguments.html

    Save this link to the tutorial: https://docs.oracle.com/javase/tutor...ybigindex.html

    --- Update ---

    The code's formatting needs work to make the code easier to read and understand.
    Statements within {} need to be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Managment of an bank account

    Yes the part with the return is actually a method not a class,i'll change that (the method is inside the class Banking). Now about the calling of the methods, i actually tried putting the parameters in the ()s but i get an error

    Banking.java:180: error: ')' expected
    b.createAccount(String firstName, String lastName, String phone, double overdraft);
    ^
    Banking.java:180: error: not a statement
    b.createAccount(String firstName, String lastName, String phone, double overdraft);
    ^
    Banking.java:180: error: ';' expected
    b.createAccount(String firstName, String lastName, String phone, double overdraft);
    ^
    Banking.java:180: error: ';' expected
    b.createAccount(String firstName, String lastName, String phone, double overdraft);
    ^
    Banking.java:180: error: not a statement
    b.createAccount(String firstName, String lastName, String phone, double overdraft);
    ^
    Banking.java:180: error: ';' expected
    b.createAccount(String firstName, String lastName, String phone, double overdraft);
    ^
    Banking.java:180: error: ';' expected
    b.createAccount(String firstName, String lastName, String phone, double overdraft);
    ^

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Managment of an bank account

    Please read this: https://docs.oracle.com/javase/tutor...arguments.html
    again about how to pass arguments to a method.
    Hint: do not include the data type.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Managment of an bank account

    Okay i took the time to read it and i got the fact that i dont need to pass the type(string int etc...) but just the actuall name. I did that,it fixed the issue.But the issue with creating a new Account persists still even after i changed the return value to the class name.

     
    if (nOfaccounts == accounts.length) {
          return -1;
    	} else return new Banking(int createAccount);
    and i get these errors

    Banking.java:113: error: '.class' expected
    } else return new Banking(int createAccount);
    ^
    Banking.java:113: error: ';' expected
    } else return new Banking(int createAccount);
    ^
    2 errors

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Managment of an bank account

    That code does not make sense. If createAccount is supposed to return an int value,
    then the following is not an int:
    new Banking(int createAccount);
    Replace that with an int value representing a new account number.

    However there is a logic problem. A method named createAccount should return an instance of an Account class, not an int value. It should create a new Account object, not generate a new number for an account.


    Also the arguments passed to a method or constructor should not include a data type like int
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Managment of an bank account

    Okay,I get what your saying; so lets say we do that we use the create account method to create a new object.Than how would i get a new account number for that.You see I'm susposed to create a new account and assing it an account number like 1 per say. How would i do that?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Managment of an bank account

    how would i get a new account number
    Write a method that generates and returns a new account number. The method would probably be in the class for the bank or company that needs account numbers for its account holders. The rules for creating a new account number could be something this. The first number is 10037.
    Every account number after that would be the last number given out +37. So the account numbers would be 10037, 10074, 10111, etc
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Managment of an bank account

    Okay I will try something like that, although I need to finish the drawing assignment as well.

Similar Threads

  1. Bank Account
    By ChristianBethel in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 8th, 2014, 03:08 PM
  2. bank account
    By rutchel alferez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 9th, 2013, 08:31 AM
  3. Needs help in my program of a bank account!
    By YunJe in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 26th, 2012, 06:38 AM
  4. Bank Account HELP!!!! PLEASE.
    By metaleddie13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 16th, 2011, 08:06 PM
  5. Bank account GUI using swing
    By AlanM595 in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2009, 04:39 AM