In chapter 1 you developed a BankAccount structure for Parkville Bank. The struc
ID: 3727146 • Letter: I
Question
In chapter 1 you developed a BankAccount structure for Parkville Bank. The structure contains two public fields for the integer account number and double account balance. Using the same structure, write a main()function in which you create two BankAccount objects. Add statements to the main()function to do the following: » Prompt the user for account numbers and beginning balances for the two BankAccounts. When testing your program, use four-digit account numbers, but the program does not have to enforce the rule.
» Complete each account number by adding a check digit that is the remainder when the account number is divided by 5.
» Prompt the user for a dollar amount to be transferred from the first account to the second account.
» Display the full account numbers (including check digits) and starting balances for the two accounts, then display the ending balances after the transfer amount has been deducted from the first account and added to the second
Explanation / Answer
As you have not provided the details of BankAccount.
I assumed that structure and completed the program.
import java.util.Scanner;
public class BankAccount {
private int accountNumber;
private double balance;
// constructor
public BankAccount() {
}
// getters and setters
public static void main(String[] args) {
// Create two objects
BankAccount account1 = new BankAccount();
BankAccount account2 = new BankAccount();
// prompt user for reading data
// Read inputs
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the account number : ");
int acc1 = sc.nextInt();
int checkDigit1 = acc1 % 5;
account1.setAccountNumber(acc1 + checkDigit1);
System.out.println("Please enter the initial amount in $ : ");
double amount1 = sc.nextDouble();
account1.setBalance(amount1);
//
System.out.println("Please enter the account number : ");
int acc2 = sc.nextInt();
int checkDigit2 = acc2 % 5;
account2.setAccountNumber(acc2 + checkDigit2);
System.out.println("Please enter the initial amount in $ : ");
double amount2 = sc.nextDouble();
account2.setBalance(amount2);
//print initial details
System.out.println(account1);
System.out.println(account2);
//read amount to transfer from account1 to account2
System.out.println("Please enter the to transfer in $ : ");
double amount = sc.nextDouble();
//minus this amount from account 1 and add it account 2
account1.setBalance(account1.getBalance()-amount);
account2.setBalance(account1.getBalance()+amount);
//print again
System.out.println(account1);
System.out.println(account2);
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "BankAccount [accountNumber=" + accountNumber + ", balance=" + balance + "]";
}
}
Output
Please enter the account number :
1234
Please enter the initial amount in $ :
1000
Please enter the account number :
5432
Please enter the initial amount in $ :
500
BankAccount [accountNumber=1238, balance=1000.0]
BankAccount [accountNumber=5434, balance=500.0]
Please enter the to transfer in $ :
200
BankAccount [accountNumber=1238, balance=800.0]
BankAccount [accountNumber=5434, balance=1000.0]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.