a. Write a class Account with a constructor that accepts a series of charge acco
ID: 3774382 • Letter: A
Question
a. Write a class Account with a constructor that accepts a series of charge accounts as its argument. These numbers should be stored in an array records that is initiated in the class as a private attribute. Then, write an accessor that accepts an account number as its argument. If this test account number is in the array records, true should be returned, otherwise, false. Your Account class should support the given class AccountApplication as shown in the below. The execution of both Account.java and AccountApplication.java should display a message indicating whether the number is valid or invalid.
package account;
import java.util.Scanner;
public class AccountApplication {
public static void main(String[] args) {
int[] data = {5658845,4250125,7895122,8777541, 8451277,1302850,8080152,4562555, 5552012,5050552,7825877,1250255, 1005231,6545231,3852085,7576651, 7881200,4581002};
Account a = new Account(data);
Scanner kb = new Scanner (System.in);
System.out.println("Input your account number:");
int acc = kb.nextInt(); System.out.println("The account is valid? "
+a.valid(acc)); } }
Explanation / Answer
AccountApplication.java
import java.util.Scanner;
public class AccountApplication {
public static void main(String[] args) {
int[] data = {5658845,4250125,7895122,8777541, 8451277,1302850,8080152,4562555, 5552012,5050552,7825877,1250255, 1005231,6545231,3852085,7576651, 7881200,4581002};
Account a = new Account(data);
Scanner kb = new Scanner (System.in);
System.out.println("Input your account number:");
int acc = kb.nextInt(); System.out.println("The account is valid? "
+a.valid(acc)); } }
Account.java
public class Account {
int accounts[];
public Account(int accs[]){
accounts = accs;
}
public boolean valid(int acc){
for(int i=0; i<accounts.length; i++){
if(accounts[i] == acc){
return true;
}
}
return false;
}
}
Output:
Input your account number:
7895122
The account is valid? true
Input your account number:
11111
The account is valid? false
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.