-1

I'm not good in java, still learning. My task is to create bank account as follows.

Develop a Java Application based on SDLC that allows the Bank teller to create a Bank account for Bank customer with an initial starting balance of funds. Both the bank teller and the bank customer can deposit the money in the customer account. Only the bank customer can withdraw money from his account up to 400,000. The account must always have more than 10,000 after any withdrawal. The application should have a loop where the customer withdraws money from the account until a minimum amount (10,000)required in the account is left. When the customer tries to withdraw money that will leave the balance of account less than 10,000 the transaction must be declined

package simpleBankAccount; 

public class MkomaziAccount {
        private int accountNo;
        private String accountName; // user name for the account.
        private double accBalance; // Account balance
        private double setDailyAmount = 400000.00; // Amount to withdraw per day.
        private static double defaultBalance = 10000.00;
        public int day, i = 1;
    
        public MkomaziAccount(int accountNo, String accountName, double accBalance, double setDailyAmount,
                double defaultBalance) {
            if (accBalance < 0) {
                System.out.println("Starting balance cann't be less than 0. \n Balance set to 0.00Tshs");
            } else {
                this.accountNo = accountNo;
                this.accountName = accountName;
                this.accBalance = accBalance;
                this.setDailyAmount = setDailyAmount;
                this.defaultBalance = defaultBalance;
                System.out.println("Account initialized. \nBalance set to Tshs" + this.accBalance);
            }
        }
    
        public int getAccountNumber() {
            return accountNo;
        }
    
        public String getAccountName() {
            return accountName;
        }
    
        public double getAccountBal() {
            return accBalance;
        }
    
    //method to withdraw funds
        public void transferFunds(double amount) {
            if (amount > accBalance) {
                System.out.printf("Unable to withdraw Tshs%.2f. Insuffient balance. \n", amount);
            } else if (amount > this.setDailyAmount) {
                System.out.printf("You cannot withdraw more than Tshs%.2f" + this.setDailyAmount + "per day.\n",
                        setDailyAmount);
            } else if (amount == defaultBalance || accBalance == defaultBalance) {
                System.out.printf("You cannot withdraw that amount of %.2f. \nYour balance is: %.2f", amount, accBalance);
            } else {
                for (i = 1; i <= day; i++) {
                    this.accBalance -= amount;
                    i++;
                    System.out.printf("The amount of Tshs%.2f successfully withdrawn. \nYour new balance is Tshs%.2f\n",
                            amount, this.accBalance);
                }
            }
        }
    
    //Method to deposit money
        public void addFunds(double nextDouble) {
            if (nextDouble < 0) {
                System.out.println("You cannot deposit negative amount");
            } else {
                this.accBalance += nextDouble;
            }
        }
    }

    
import java.io.PrintStream;
    import java.util.Scanner;
    
    public class MkomaziAccountTest {
     private static Scanner Mkomazi = new Scanner(System.in);
    
        public static void main(String[] args) {
            boolean quit = false;
            int choice;
            int accountNo;
            String accountName;
            double accBalance;
            final double defaultBalance = 10000.00;
        
            System.out.printf("%24s\n","Mkomazi Bank Application V1.0");
            System.out.println("============================================");
            System.out.println("Please enter your personal details below.");
            System.out.println("Please enter Account Name");
            accountName = Mkomazi.nextLine();   
            System.out.println("Please enter Account Number");
            accountNo = Mkomazi.nextInt();
            
            System.out.println("Please enter Account Name");
            accountName = Mkomazi.nextLine();
            
            System.out.println("Starting Balance");
            accBalance = Mkomazi.nextDouble();
            
            MkomaziAccount account = new MkomaziAccount(accountNo, accountName, accBalance, accBalance, defaultBalance);
            
            while(!quit) {
                printMainMenu();
                choice = Mkomazi.nextInt();
                
                if (choice == 1) {
                    System.out.printf("Account Name: %s\n Account Balance: Tshs %.2f\n", account.getAccountName(), account.getAccountBal());
                    account.getAccountName();
                } else if (choice == 2) {
                    System.out.println("Enter amount to deposit: ");
                    account.addFunds(Mkomazi.nextDouble());
                } else if (choice == 3) {
                    System.out.println("Enter amount to withdraw: ");
                    account.transferFunds(Mkomazi.nextDouble());
                } else if (choice == 4) {
                    quit = true;
                } else {
                    System.out.println("Invalid choice");
                }
            }
            Mkomazi.close();
            
        }
    
        public static void printMainMenu() {
            System.out.printf("%24s\n" + 
            "=======================================\n" + 
            "Please select an option\n" + 
            "1. Check Balance\n" + 
            "2. Deposit Money\n" + 
            "3. Withdraw Money\n" + 
            "4. Exit the program\n" + 
            "========================================\n" +
            "Choice: ","Mkomazi Bank Application V1.0 ");
                
        }
    
    }

But when press option 1 to check balance, does not print Account number and Account Name. Can someone help? How can i add PINs (check users's PIN) for account Name users.

2kseven
  • 1
  • 1
  • 1
    Did you try debugging your code? – Amongalen Jun 22 '20 at 10:55
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Amongalen Jun 22 '20 at 11:01
  • This is what displays ======================================== Choice: 1 Account Name: Account Balance: Tshs 10000.00 Mkomazi Bank Application V1.0 ======================================= Please select an option 1. Check Balance 2. Deposit Money 3. Withdraw Money 4. Exit the program ======================================== Choice: – 2kseven Jun 22 '20 at 11:56

1 Answers1

0

I run your code and comment this part :

            accountName = Mkomazi.nextLine();   --your accname 
            System.out.println("Please enter Account Number");
            accountNo = Mkomazi.nextInt(); 
            
            System.out.println("Please enter Account Name");
          //accountName = Mkomazi.nextLine(); -- your accname2
            
            System.out.println("Starting Balance");
            accBalance = Mkomazi.nextDouble();
            
            MkomaziAccount account = new MkomaziAccount(accountNo, accountName, accBalance, accBalance, defaultBalance); -- it's use your accname2, accname2 = empty

if you comment second accountName, your code working

Zeo
  • 53
  • 7