Question
Java Help! Ask the user for a balance. From the accounts.dat file, display all the accounts that have a balance of at least what the
Java Help!
Ask the user for a balance. From the accounts.dat file, display all the accounts that have a balance of at least what the user entered.
accounts.dat is a binary file. Each entry contains a credit card number (long), a balance (double), and a cash back flag (boolean).
I need this required output(IN THIS FORMAT);
Enter a balance 9000ENTER Accounts with a balance of at least $9000.00 Card Number Balance Cash Back? 201715141501700 9135.90 Yes 4508271490627227 9890.51 No 3573877643495486 9985.21 No 5100172198301454 9315.15 No 3551244602153760 9409.97 Yes 4405942746261912 9869.27 No 30526110612015 9866.30 No 7 results
---------
Enter a balance 8000ENTER Accounts with a balance of at least $8000.00 Card Number Balance Cash Back? 201715141501700 9135.90 Yes 5100174906912671 8659.35 Yes 3581572129932389 8965.07 Yes 3549214734886202 8960.76 No 4508271490627227 9890.51 No 4041598930132416 8596.39 Yes 3573904891259172 8985.27 Yes 3574931681854879 8482.46 Yes 3573877643495486 9985.21 No 3581429661693202 8207.40 Yes 6394213548410265 8249.76 No 3588215263928408 8036.12 Yes 5100172198301454 9315.15 No 6396392530990977 8790.59 Yes 6372756913380048 8346.07 No 3551244602153760 9409.97 Yes 5100146117467372 8161.69 Yes 4405942746261912 9869.27 No 3533184590527943 8176.80 Yes 490337929898976334 8099.58 Yes 6767642427889745 8738.77 No 30526110612015 9866.30 No 22 results
I have this code so far but it Is not displaying this part of the output without an error:
Card Number Balance Cash Back? 201715141501700 9135.90 Yes 4508271490627227 9890.51 No 3573877643495486 9985.21 No 5100172198301454 9315.15 No 3551244602153760 9409.97 Yes 4405942746261912 9869.27 No 30526110612015 9866.30 No 7 results\
can someone explain what I'm doing wrong or fix my coding so I display the required output
import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Balance { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a balance"); double input = keyboard.nextDouble(); System.out.printf("Accounts with a balance of at least $%.2f", input); System.out.print(" (sorted by balance) "); System.out.printf("%20s%10s%12s ", "Account Number", "Balance", "Cash Back"); int results = 0; ArrayListlist = new ArrayList<>(); try { FileInputStream fstream = new FileInputStream("accounts.dat"); DataInputStream inputFile = new DataInputStream(fstream); boolean eof = false; while (!eof) { try { long cardNumber = inputFile.readLong(); double balance = inputFile.readDouble(); boolean cashBack = inputFile.readBoolean(); Balance bl = new Balance(cardNumber, balance, cashBack); list.add(account); } catch (EOFException e) { eof = true; } } inputFile.close(); } catch (IOException e) { e.printStackTrace(); } for (Account x : list) { if (x.getBalance() >= input && x.getBalance() <= 10000) { System.out.printf("%20s%10.2f%12s ", x.getCardNumber(), x.getBalance(), x.getCashback()); results++; } } System.out.printf("%34s results ", results); } public class Account { private long cardNumber; private double balance; private boolean cashback; public Account(long cardNumber, double balance, boolean cashback) { this.cardNumber = cardNumber; this.balance = balance; this.cashback = cashback; } public long getCardNumber() { return cardNumber; } public double getBalance() { return balance; } public String getCashback() { if (cashback) return "Yes"; else return "No"; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started