Question: In JAVA I need help with adjusting my code. This code is outputing incorrectly what do ineed to do to fix it ? The Question

In JAVA I need help with adjusting my code. This code is outputing incorrectly what do ineed to do to fix it? The Question is: 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. ****MY CODE "PERSON"*** 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);
//System.out.printf("%s%20s%10.2f%10s
", name, account, balance, back ? "Yes" : "No");
} 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());
}}
****"DEMO" CODE GIVEN**** import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Person test1= new Person("Eachelle Balderstone",
30526110612015L,
9866.30,
false);
Person test2= new Person("Brand Hallam",
3573877643495486L,
9985.21,
false);
Person test3= new Person("Tiphanie Oland",
5100172198301454L,
9315.15,
true);
test1.save();
test2.save();
test3.save();
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter file name");
String filename = keyboard.nextLine();
FileInputStream istream = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(istream);
Person p =(Person)ois.readObject();
ois.close();
System.out.println(p);
}
}
In JAVA I need help with adjusting my code. This

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 Programming Questions!