Question 5: 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.     NOTE: I just need help displaying the information. When I run my code, all the information is messed up, and I do not know how to fix it. ================================================== Given file Demo for Question 5  ------------------------------------------- import java.io.*; 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); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Question 5: 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.

 

 

NOTE: I just need help displaying the information. When I run my code, all the information is messed up, and I do not know how to fix it.

==================================================

Given file Demo for Question 5 

-------------------------------------------

import java.io.*;
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);
}
}

==============================================

This code below is from question 3 (part of it is mine and it worked)

-------------------------------------------------------------------

import java.util.ArrayList;
import java.util.Collections;

public class Question3Demo {

//Question3Demo was givin in Question 3

//i don't know if Question3Done code is important to the question

//Look at my code at the end


public static void main(String[] args) {
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);

ArrayList<Person> list = new ArrayList<>();
list.add(test1);
list.add(test2);
list.add(test3);
Collections.sort(list);

System.out.printf("%20s%20s%10s%10s\n", "Name", "Account Number", "Balance", "Cash Back");
for (Person current : list) {
System.out.println(current);
}
}
}

 //this was MY code for question 3, and it passed(below)

 

 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.util.*;

 

public class Person implements Comparable<Person>,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;

    }

 

    @Override

    public String toString() {

        String s ;

        if(!back)

        {

            s ="No";

        }

        else

        {

            s ="Yes";

        }

        return String.format("%20s%20s%10.2f%10s", name, account, balance, s);

    }

    public int compareTo(Person st) {

        int k=name.compareTo(st.name);

        if(k==0.0)

            return 0;

        else if(k<1)

        return -1;

else

        return 1;

    }

 

    public void save() {

        try {

//accountNumber is the credit card number from the object

            FileOutputStream fileOut = new FileOutputStream(account+".dat");

            ObjectOutputStream out = new ObjectOutputStream(fileOut);

//This method writes the java class out to a binary file,it throws exception if

//the serializable interface is not implemented in the class

            out.writeObject(this);

            out.close();

            fileOut.close();

//Confirming the file write

 

//displaying problem starts here for me, and I am unsure how to fix it. (should output like the photo)

            System.out.println("Enter file name");

 

            System.out.print(account );

            System.out.print(".dat\n");

 

            System.out.print(" "+name );

            System.out.print(account+balance);

 

 

 

        } catch (IOException i) {

            System.out.println("File Writing error");

        }

    }

}

 

 

Test Case 1
Enter file name \n
30526110612015.dat ENTER
Eachelle Balderstone
Test Case 2
Enter file name \n
3573877643495486.dat ENTER
Test Case 3
Brand Hallam 3573877643495486 9985.21
Enter file name \n
5100172198301454.dat
Tiphanie Oland
30526110612015
ENTER
9866.30
5100172198301454 9315.15
No \n
No \n
Yes In
Transcribed Image Text:Test Case 1 Enter file name \n 30526110612015.dat ENTER Eachelle Balderstone Test Case 2 Enter file name \n 3573877643495486.dat ENTER Test Case 3 Brand Hallam 3573877643495486 9985.21 Enter file name \n 5100172198301454.dat Tiphanie Oland 30526110612015 ENTER 9866.30 5100172198301454 9315.15 No \n No \n Yes In
Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Files and Directory
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education