Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how can you make a void method that transfer money from one account to the other on java

package banking;

public class Account {
    String name;
    double balance;

    public Account() {
        name = "";
        balance = 0.0;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getName() {
        return name;
    }

    public double getBalance() {
        return balance;
    }

    public void addFunds(double addedAmount) {
        balance = balance + addedAmount;
    }

    public void withdraw(double withdrawnAmount) {
        balance = balance - withdrawnAmount;        
    }

    public void transfer(double amount, Account from, Account to) { //here is the transfer method, if i can improve this or alter it in order to make it easier to run using user input from the main file, let me know
        if(from.balance >= amount){
            from.balance = from.balance - amount;
            to.balance = to.balance + amount;
            System.out.println("Funds successfully transfered.");
        } else {
                System.out.println("Insufficient funds");
            }
        }
    }

package banking;
import java.util.Scanner;

public class BankSimulator {
    public static void main(String[] args) {

            System.out.println("Hello and welcome to the banking system. Please enter a name to create an account, no spaces: ");
            Scanner scan = new Scanner(System.in);
            Account a1 = new Account();
            a1.setName(scan.next());
            System.out.println("Account name: " + a1.getName());

        int count = 0;
        while(count == 0) {

                System.out.println("What would you like to do next?"  + "
" +
                "Change account name: press 1"  + "
" +
                "See account name: press 2"  + "
" +
                "Check balance: press 3"  + "
" + 
                "Add money to balance: press 4"  + "
" +
                "Withdraw money from balance: press 5" + "
" +
                "Exit program: press 7: " + "
" +
                "To transfer funds between accounts: press 6");

                int toDo = scan.nextInt();

            if(toDo == 1) {

                    System.out.println("Enter new account name: ");
                    a1.setName(scan.next());
                    System.out.println("Account name: " + a1.getName());
            }
            else if(toDo == 2) {
                System.out.println("Account name: " + a1.getName());
            }
            else if(toDo == 3) {
                System.out.println("Current balance: $" + a1.getBalance());
            }
            else if(toDo == 4) {
                System.out.println("Desired amount to add: $");
                a1.addFunds(scan.nextDouble());
                System.out.println("Money successfully added to balance."); 
            }
            else if(toDo == 5) {
                System.out.println("Desired amount to withdraw: $");
                a1.withdraw(scan.nextDouble());
                System.out.println("Money successfully withdrawn from balance.");
            }
            else if(toDo == 6) {
                System.out.println("Enter the account you would like to transfer money from:");
                String fromAccount = scan.next();
                System.out.println("Enter the account you would like to transfer money to:");
                String toAccount = scan.next();
                System.out.println("Enter the amount of money you would like to transfer: $");
                double moneyToTransfer = scan.nextDouble();
        //this is what i need help with, I don't know what to do with these three things, and since the first two arent accounts, i cant run the transfer method on them


            }
            else if(toDo == 7) {
                System.out.println("Thank you for using our banking system. Until next time.");
                count = 1;

            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java Least prime factor of numbers till n 
Java :: javacal 
Java :: java resultset to object 
Java :: bloomreach clone session 
Java :: hgjhghj 
Java :: Java public no-arg constructors 
Java :: PlatformException (PlatformException(unknown, java.lang.Exception: Client is offline, {code: unknown, message: java.lang.Exception: Client is offline}, null)) 
Java :: jmonkey shapes 
Java :: copy and deletion of div by pressing button in javasript 
Java :: java package keyword 
Java :: Android java parse class name via intent 
Java :: .throwFor in java 
Java :: do i have to import files from the same package in java 
Java :: lelen suratlari ramkasidagi oltinlar 
Java :: java lambda expression in priorityqueue 
Java :: android diagonal gradle 
Java :: mei mei bad 
Java :: while(++i<5)f*=i;System.out.println(f); 
Java :: number of zeros in a binary array 
Java :: BasicAWSCredentials 
Java :: merced A class 
Java :: Join Two Variables Together In Java 
Java :: pojo api testing 
Java :: what singleton java spring 
Java :: Class inheritance and encapsulation 
Java :: spring down load 
Java :: We would like to make a member of a class can access in all subclasses regardless of what package the subclass is in. Which one of the following keywords would achieve this? 
Java :: print java object inherited classes 
Java :: how to get address of object in java 
Java :: similar thing as pair in c++ in java 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =