Question: Java Help! Ask the user for an account balance. Show, in descending order, all the accounts that have a balance greater than what the user
Java Help!
Ask the user for an account balance. Show, in descending order, all the accounts that have a balance greater than what the user input. Each entry is int, string, long, double, boolean (name length, name, credit card number, balance, cashback).
I am having trouble displaying the required information
Enter a balance 9000ENTER Accounts with a balance of at least $9000.00 (sorted by balance) Name Account Number Balance Cash Back Brand Hallam 3573877643495486 9985.21 No Paco Verty 4508271490627227 9890.51 No Stanislaw Dhenin 4405942746261912 9869.27 No Eachelle Balderstone 30526110612015 9866.30 No Reube Worsnop 3551244602153760 9409.97 Yes Tiphanie Oland 5100172198301454 9315.15 No Jordan Rylstone 201715141501700 9135.90 Yes 7 results
------
Enter a balance 8000ENTER Accounts with a balance of at least $8000.00 (sorted by balance) Name Account Number Balance Cash Back Brand Hallam 3573877643495486 9985.21 No Paco Verty 4508271490627227 9890.51 No Stanislaw Dhenin 4405942746261912 9869.27 No Eachelle Balderstone 30526110612015 9866.30 No Reube Worsnop 3551244602153760 9409.97 Yes Tiphanie Oland 5100172198301454 9315.15 No Jordan Rylstone 201715141501700 9135.90 Yes Anjela Himsworth 3573904891259172 8985.27 Yes Howie Royson 3581572129932389 8965.07 Yes Blinni Mattke 3549214734886202 8960.76 No Dorotea Nolli 6396392530990977 8790.59 Yes Carita Savill 6767642427889745 8738.77 No Mateo Mollene 5100174906912671 8659.35 Yes Cathleen Schurcke 4041598930132416 8596.39 Yes Adriana Bru 3574931681854879 8482.46 Yes Orlando Nutbeem 6372756913380048 8346.07 No Leland Vasilyev 6394213548410265 8249.76 No Ambrosi Fussie 3581429661693202 8207.40 Yes Valentine Montford 3533184590527943 8176.80 Yes Sarette Springell 5100146117467372 8161.69 Yes Rich Yakovl 490337929898976334 8099.58 Yes Conney Sizeland 3588215263928408 8036.12 Yes 22 results
I suppose you have to use a comparable class along with a main method to produce this information
I have this code so far, however I am getting stuck on how to display the information I need from a binary file while using a comparable class
import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class AccountBalance { 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 (sorted by balance)%n", input); System.out.printf("%20s%20s%10s%12s%n","Name", "Card Number", "Balance", "Cash Back?"); int results = 0; ArrayList list = new ArrayList<>(); try { FileInputStream fstream = new FileInputStream("accounts-with-names.dat"); DataInputStream inputFile = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); boolean eof = false; while (!eof) { try { Character name = inputFile.readChar(); long cardNumber = inputFile.readLong(); double balance = inputFile.readDouble(); boolean cashBack = inputFile.readBoolean(); Account acc = new Account(cardNumber, balance, cashBack); list.add(acc); } catch (EOFException e) { eof = true; } } inputFile.close(); } catch (IOException e) { e.printStackTrace(); } for (Account x : list) { if (x.getBalance() >= input) { System.out.printf("%20d%10.2f%12s%n", x.getCardNumber(), x.getBalance(), x.getCashback()); results++; } } System.out.printf("%34d results%n", results); } public class Account implements Serializable, Comparable{ private String name; private long cardNumber; private double balance; private boolean cashback; public Account(String name, long cardNumber, double balance, boolean cashback) { this.name = name; this.cardNumber = cardNumber; this.balance = balance; this.cashback = cashback; } public String name(){return name;} public long getCardNumber() { return cardNumber; } public double getBalance() { return balance; } public String getCashback() { return cashback ? "Yes" : "No"; } } } can an expert help me produce the required information I need, specifically focusing on producing the names, account number, balance, cashback, and results that I need to display in descending order such as
Brand Hallam 3573877643495486 9985.21 No Paco Verty 4508271490627227 9890.51 No Stanislaw Dhenin 4405942746261912 9869.27 No Eachelle Balderstone 30526110612015 9866.30 No Reube Worsnop 3551244602153760 9409.97 Yes Tiphanie Oland 5100172198301454 9315.15 No Jordan Rylstone 201715141501700 9135.90 Yes 7 results\
I do not know what is going wrong with my code or what to do in order to fix it to produce this information from a binary file
I may need to use objectinputstream, array list, and collection sort list somewhere in the code but I am not sure
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
