Question
Design classes for Currency, Rupee, and Dollar. Write a program that randomly generates Rupee and Dollar objects and write them into a file using object serialization. Write another program to read that file, convert to Rupee if it reads a Dollar, while leave the value as it is if it reads a Rupee.
Program
(credits to Kumresan Sir)
SerializationWrite.java
import java.io.*;
import java.util.*;
class Currency implements Serializable
{
protected String currency;
protected int amount;
public Currency(String cur, int amt)
{
this.currency = cur;
this.amount = amt;
}
public String toString()
{
return currency + amount;
}
public String dollarToRupee(int amt)
{
return "Rs" + amt * 45;
}
}
class Rupee extends Currency
{
public Rupee(int amt)
{
super("Rs",amt);
}
}
class Dollar extends Currency
{
public Dollar(int amt)
{
super("$",amt);
}
}
public class SerializationWrite
{
public static void main(String args[])
{
Random r = new Random();
try
{
Currency currency;
FileOutputStream fos = new FileOutputStream("serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("Writing to file using Object Serialization:");
for(int i=1;i<=25;i++)
{
Object[] obj = { new Rupee(r.nextInt(5000)),new Dollar(r.nextInt(5000)) };
currency = (Currency)obj[r.nextInt(2)]; // RANDOM Objects for Rs and $
System.out.println(currency);
oos.writeObject(currency);
oos.flush();
}
oos.close();
}
catch(Exception e)
{
System.out.println("Exception during Serialization: " + e);
}
}
}
SerializationRead.java
import java.io.*;
import java.util.*;
public class SerializationRead
{
public static void main(String args[])
{
try
{
Currency obj;
FileInputStream fis = new FileInputStream("serial.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println("Reading from file using Object Serialization:");
for(int i=1;i<=25;i++)
{
obj = (Currency)ois.readObject();
if((obj.currency).equals("$"))
System.out.println(obj + " = " + obj.dollarToRupee(obj.amount));
else
System.out.println(obj + " = " + obj);
}
ois.close();
}
catch(Exception e)
{
System.out.println("Exception during deserialization." + e);
}
}
}
Output
vivek@ubuntu:~/Desktop$ javac SerializationWrite.java
vivek@ubuntu:~/Desktop$ java SerializationWrite
Writing to file using Object Serialization:
$4645
Rs105
$2497
$892
Rs1053
Rs1270
$1991
Rs4923
Rs4443
Rs3537
Rs2914
$53
$561
$4692
Rs860
$2764
Rs752
$1629
$2880
Rs2358
Rs3561
$3796
Rs341
Rs2010
Rs427
vivek@ubuntu:~/Desktop$ javac SerializationRead.java
vivek@ubuntu:~/Desktop$ java SerializationRead
Reading from file using Object Serialization:
$4645 = Rs209025
Rs105 = Rs105
$2497 = Rs112365
$892 = Rs40140
Rs1053 = Rs1053
Rs1270 = Rs1270
$1991 = Rs89595
Rs4923 = Rs4923
Rs4443 = Rs4443
Rs3537 = Rs3537
Rs2914 = Rs2914
$53 = Rs2385
$561 = Rs25245
$4692 = Rs211140
Rs860 = Rs860
$2764 = Rs124380
Rs752 = Rs752
$1629 = Rs73305
$2880 = Rs129600
Rs2358 = Rs2358
Rs3561 = Rs3561
$3796 = Rs170820
Rs341 = Rs341
Rs2010 = Rs2010
Rs427 = Rs427
Download Source Codes
SerializationWrite.java
SerializationRead.java
No comments:
Post a Comment