package testaccount1; import java.util.Scanner; public class TestAccount1 { publ
ID: 3667964 • Letter: P
Question
package testaccount1;
import java.util.Scanner;
public class TestAccount1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Account[] accounts = new Account[10];
for (int i = 0; i < accounts.length; i++) {
accounts[i] = new Account(i, 100);
}
int option = 0;
int id = askForID(accounts);
while (option != 4) {
/*line 16*/ Account user = getAccount(accounts, id);
System.out.print(
"Main Menu "
+ "1: check balance "
+ "2: withdraw "
+ "3: deposit "
+ "4: exit "
+ "Enter a choice: ");
option = input.nextInt();
switch (option) {
case 1:
System.out.println("The Balance is $" + user.getBalance() + ". ");
break;
case 2:
System.out.print("Enter an amount to withdraw: ");
user.withdraw(input.nextDouble());
break;
case 3:
System.out.print("Enter an amount to deposit: ");
user.withdraw(input.nextDouble());
break;
case 4:
System.out.println("You are logged out.");
break;
default:
System.out.print("Invalid input - Enter z number 1-4");
}
}
}
public static int askForID(Account[] accounts) {
Scanner input = new Scanner(System.in);
int id = 0;
boolean isValidID = false;
while (!isValidID) {
System.out.print("Enter an ID : ");
id = input.nextInt();
/*line 53*/ if (!hasID(accounts, id)) {
System.out.println("INVAILD ID: Try again.");
} else {
isValidID = true;
}
}
return id;
}
/*line 62*/
public static Account getAccount(Account[] accounts, int id) {
for (Account account : accounts) {
if (id == account.getId()) {
return account;
}
return null;
}
}
public static boolean hasID(Account[] accounts, int id) {
for (Account account : accounts) {
if (id == account.getId()) {
return true;
}
}
return false;
}
}
run:
Enter an ID : 776
INVAILD ID: Try again.
Enter an ID : 4
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at testaccount1.TestAccount1.getAccount(TestAccount1.java:62)
at testaccount1.TestAccount1.main(TestAccount1.java:16)
C:UsersuserAppDataLocalNetBeansCache8.1executor-snippets un.xml:53: Java returned: 1
BUILD FAILED (total time: 13 seconds)
Explanation / Answer
TestAccount.java
// header file
import java.util.Scanner;
// create class TestAccount
public class TestAccount {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
pickAccount(createAccounts());
}
public static Account[] createAccounts(){
Account[] accounts = new Account[10];
for(int i = 0; i < accounts.length; i++){
accounts[i] = new Account(i, 100);
}
return accounts;
}
// getting id from user
public static void pickAccount(Account[] accounts){
while(true){ //Assignment says this should be infinate
System.out.print("Please enter an ID: ");
int i = Integer.parseInt(input.nextLine());
if (i >= 0 && i <= 9) mainMenu(accounts[i]);
else System.out.println("Invalid id");
}
}
// main menu
public static void mainMenu(Account account){
int choice;
do{
System.out.println("");
System.out.println("Main Menu");
System.out.println("1. Check balance");
System.out.println("2. Withdraw");
System.out.println("3. Deposit");
System.out.println("4. Exit");
System.out.print("Enter a choice: ");
choice = Integer.parseInt(input.nextLine());
if(choice==1) balance(account);
else if(choice==2) withdraw(account);
else if(choice==3) deposit(account);
else if(choice!=4) System.out.println("Invalid choice");
}while (choice != 4);
}
// display balance
public static void balance(Account account){
System.out.printf("The balance is: $%.2f ", account.getBalance());
}
// amount to withdraw
public static void withdraw(Account account){
System.out.print("Enter an amount to withdraw: ");
account.withdraw(Double.parseDouble(input.nextLine()));
}
// amount to deposit
public static void deposit(Account account){
System.out.print("Enter an amount to deposit: ");
account.deposit(Double.parseDouble(input.nextLine()));
}
}
Account.java
import java.util.Date;
//Account lass
public class Account {
//MEMBER VARIABLES
private int id = 0;
private double balance = 0.0;
private double annualInterestRate;
private Date dateCreated = new Date();
//CONSTRUCTORS
Account(){}
Account(int id, double balance){
setId(id);
setBalance(balance);
}
//SETS
public void setId(int id) {this.id = id;}
public void setBalance(double balance) {this.balance = balance;}
public void setAnnualInterestRate(double annualInterestRate){this.annualInterestRate = annualInterestRate;}
//GETS
public int getId() {return id;}
public double getBalance() {return balance;}
public double getAnnualInterestRate() {return annualInterestRate;}
public Date getDate() {return (Date)dateCreated.clone();}
//METHODS
public double getMonthlyInterestRate(){return getAnnualInterestRate()/12;}
public double getMonthlyInterest(){return getBalance()*(getAnnualInterestRate()/1200);}
public void withdraw(double amount){setBalance(getBalance() - amount);}
public void deposit(double amount){setBalance(getBalance() + amount);}
}
sample output
Please enter an ID: 13641364
Invalid id
Please enter an ID: 1364
Invalid id
Please enter an ID: 1
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice: 1
The balance is: $100.00
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.