Question: Add a method called save to your Person class. This method will write the object out to a binary file. Use the Serializable format. Name

Add a method called save to your Person class. This method will write the object out to a binary file. Use the Serializable format. Name the file accountNumber.dat where accountNumber is the credit card number from the object.

I need this required output(IN THIS FORMAT)

Enter file name 30526110612015.datENTER Eachelle Balderstone 30526110612015 9866.30 No

------

Enter file name 3573877643495486.datENTER Brand Hallam 3573877643495486 9985.21 No

--------

Enter file name 5100172198301454.datENTER Tiphanie Oland 5100172198301454 9315.15 Yes

I am getting this output with my current code

Enter file name 30526110612015.datENTER Eachelle Balderstone30526110612015 9866.30 No

------

Enter file name 3573877643495486.datENTER Brand Hallam 3573877643495486 9985.21 No

----

Enter file name 5100172198301454.datENTER Tiphanie Oland 5100172198301454 9315.15 Yes

Can someone fix my coding so that it matches the required output in bold

import java.io.*; import java.util.ArrayList; import java.util.Collections; class Person implements Comparable, Serializable { String name; long account; double balance; boolean back; public Person(String name, long account, double balance, boolean back) { this.name = name; this.account = account; this.balance = balance; this.back = back; } public void save() { try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(account + ".dat"))) { out.writeObject(this); } catch (IOException e) { System.err.println("Error saving object: " + e); } } @Override public String toString() { String s; if (!back) { s = "No"; } else { s = "Yes"; } return String.format("%-20s%-20d%14.2f%13s ", name, account, balance, s); } @Override public int compareTo(Person st) { return this.name.compareTo(st.name); } public static void main(String[] args) { Person test1 = new Person("Eachelle Balderstone", 30526110612015L, 9866.30, false); test1.save(); Person test2 = new Person("Brand Hallam", 3573877643495486L, 9985.21, false); test2.save(); Person test3 = new Person("Tiphanie Oland", 5100172198301454L, 9315.15, true); test3.save(); ArrayList list = new ArrayList<>(); list.add(test1); list.add(test2); list.add(test3); Collections.sort(list); System.out.printf("Enter file name%n%s.datENTER%n", test1.account); System.out.println(test1.toString()); System.out.printf("Enter file name%n%s.datENTER%n", test2.account); System.out.println(test2.toString()); System.out.printf("Enter file name%n%s.datENTER%n", test3.account); System.out.println(test3.toString()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!