Question7(b): Consider a class that stores a Bank account holder's name, account number, ATM card number, account balance and ATM PIN. Write a program to store the data onto a disk file, except for the account balance and ATM PIN. Use serialization and transient variables.
Answer:
import java.io.*;
import java.util.*;
public class UserAccount implements Serializable
{
private transient int atmPin_no;
private String atmCard_no;
private String account_number;
private transient float balance_amount;
UserAccount(int pin, String cardno, String acc_no, float balance) //constructor
{
atmPin_no=pin;
atmCard_no=cardno;
account_number=acc_no;
balance_amount=balance;
}
public String toString()
{
String pin=(atmPin_no==null)?"(n/a)":atmPin_no;
Return "Logon info:\n "+" Account No :"+account_number+"\n Balance:"balance_amount;
}
public static void main(String[] args)
Throws IOException, ClassNotFound Exception
{
UserAccount acc=new
UserAccount(1234,"A983423115","23456789076543",5000.34);
System.out.println("Login is="+acc);
ObjectOutputStream obj=new ObjectOutputStream(new FileInputStream("Login.out"));
Acc=(UserAccount)in.readObject();
System.out.println("login="+acc);
}
}
Answer:
import java.io.*;
import java.util.*;
public class UserAccount implements Serializable
{
private transient int atmPin_no;
private String atmCard_no;
private String account_number;
private transient float balance_amount;
UserAccount(int pin, String cardno, String acc_no, float balance) //constructor
{
atmPin_no=pin;
atmCard_no=cardno;
account_number=acc_no;
balance_amount=balance;
}
public String toString()
{
String pin=(atmPin_no==null)?"(n/a)":atmPin_no;
Return "Logon info:\n "+" Account No :"+account_number+"\n Balance:"balance_amount;
}
public static void main(String[] args)
Throws IOException, ClassNotFound Exception
{
UserAccount acc=new
UserAccount(1234,"A983423115","23456789076543",5000.34);
System.out.println("Login is="+acc);
ObjectOutputStream obj=new ObjectOutputStream(new FileInputStream("Login.out"));
Acc=(UserAccount)in.readObject();
System.out.println("login="+acc);
}
}
No comments:
Post a Comment