Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

Students also viewed these Databases questions

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago