//Akshat Shah
//IT 313


/////////////////////////////////////////////////////////TRANSACTION.JAVA////////////////////////////////////////////////////////////////

//Creating the package path
package IT313.proj4.Shahakshat;

//implementing the comparable interface
public class    Transaction implements Comparable{

    //Creating the variables
    private int id = 0;
    private String buyer;
    private String seller;
    private double amount = 0;
    private String memo;
    private String timestamp;

    //creating the constructor
    public Transaction(int id, String buyer, String seller, double amount, String memo, String timestamp) {
        this.id = id;
        this.buyer = buyer;
        this.seller = seller;
        this.amount = amount;
        this.memo = memo;
        this.timestamp = timestamp;
    }

        //Creating the get method, so that When I call the method it returns the value that I had set
        public int getId () {
            return id;
        }

        public String getSeller () {
            return seller;
        }

        public String getBuyer () {
            return buyer;
        }

        public double getAmount () {
            return amount;
        }

        public String getMemo () {
            return memo;
        }

        public String getTimestamp () {
            return timestamp;
        }


        //overriding the string method to to customize it my way
        @Override
        public String toString () {
            return "ID: " + this.getId() + ", " +
                    "Buyer: " + this.getBuyer() + ", " +
                    "Seller: " + this.getSeller() + ", " +
                    "Amount: $" + this.getAmount() + ", " +
                    "Memo: " + this.getMemo() + ", " +
                    "Timestamp: " + this.getTimestamp();

        }

        //Creating the compare to and creating the if else conditions
        @Override
        public int compareTo (Transaction other){
            //return (int)(this.id - other.id);
            if (this.id > other.id) {
                return 1;
            }
             else if (this.id == other.id){
                return 0;
            }
            else {
                return -1;
            }
        }

}

/////////////////////////////////////////////////////////MOBILE-TRANSACTION.JAVA/////////////////////////////////////////////////////////

//Akshat Shah
//IT 313

//Creating the package path
package IT313.proj4.Shahakshat;

//Using inheritance to extend the transaction class with this class
public class MobileTransaction extends Transaction {

    //Declaring the strings
    public String storage;
    public String memory;


    //Creating the constructor
    public MobileTransaction(int id, String buyer, String seller, double amount,String storage, String memory, String memo, String timestamp) {
        super(id, buyer, seller, amount, memo, timestamp);
        this.memory = memory;
        this.storage = storage;
    }

    //Creating the get methods
    public String getMemory() {
        return memory;
    }

    public String getStorage() {
        return storage;
    }

    //overriding the string method to to customize it my way
    @Override
    public String toString() {
        return "ID: " + this.getId() + ", " +
                "Buyer: " + this.getBuyer() + ", " +
                "Seller: " + this.getSeller() + ", " +
                "Amount: $" + this.getAmount() + ", " +
                "Memo: " + this.getMemo() + ", " +
                "Storage: " + this.getStorage() + ", " +
                "Memory: " + this.getMemory() + ", " +
                "Timestamp: " + this.getTimestamp();
    }
}

/////////////////////////////////////////////////////////TV-TRANSACTION.JAVA/////////////////////////////////////////////////////////////
//Akshat Shah
//IT 313

package IT313.proj4.Shahakshat;
public class TvTransaction extends Transaction {

    public int screensize;
    public String resolution;


    public TvTransaction(int id, String buyer, String seller, double amount, int screensize, String resolution, String memo, String timestamp) {
        super(id, buyer, seller, amount, memo, timestamp);
        this.screensize = screensize;
        this.resolution = resolution;
    }

    public int getScreensize(){
        return screensize;
    }

    public String getResolution(){
        return resolution;
    }

    @Override
    public String toString() {
        return "ID: " + this.getId() + ", " +
                "Buyer: " + this.getBuyer() + ", " +
                "Seller: " + this.getSeller() + ", " +
                "Amount: $" + this.getAmount() + ", " +
                "Memo: " + this.getMemo() + ", " +
                "Screen Size: " + this.getScreensize() + ", " +
                "Resolution: " + this.getResolution() + ", " +
                "Timestamp: " + this.getTimestamp();
    }
}
/////////////////////////////////////////////////////////TRANSACTION-MANAGER.JAVA////////////////////////////////////////////////////////

//Akshat Shah
//IT 313
//Due on 11/07/2019
//Submitted on 11/08/2019

//Creating the package path
package IT313.proj4.Shahakshat;

import java.io.*;
import java.util.Iterator;
import java.util.ArrayList;


public class TransactionManager implements
        Serializable, Iterable{

        private ArrayList col;

        public TransactionManager( ) {
            col = new ArrayList( ); };

        //Add method to append a Transaction object to col
        public void add(Transaction theTransaction) {
        col.add(theTransaction);
    }

        //Clear method to clear out col by removing all its objects
        public void clear( ) {
        col.clear( );
    }

        //GetAll
        // public ArrayList getAll(){
        //       return col;
        //}


        //GetAll method to return array list of all objects sorted by id
        public ArrayList getAll(){
            for(Transaction p : col) {
                return col;
            }
            return col;
        }

        //GetCount return number of objects in col
        public int getCount( ) {
            return col.size( );
        }

        //FindBuyer method return array list of all objects from col with specified buyer,sorted by id.
        public ArrayList findBuyer(String buyerName){
            ArrayList output = new ArrayList( );
            for (Transaction buyer: col) {
                if(buyer.getBuyer().equals(buyerName)){
                    output.add(buyer);
                }
            }
            return output;
        }

        //FindSeller return array list of all objects from col with specified seller,sorted by id.
        public ArrayList findSeller(String SellerName) {
            ArrayList output1 = new ArrayList();
            for (Transaction seller : col) {
                if (seller.getSeller().equals(SellerName)) {
                    output1.add(seller);
                }
            }
            return output1;
        }

        //FindId method to return object with specified id.
        public Transaction findId(int Id){
            for(Transaction id : col){
                if(id.getId()==(Id)){
                   return id;
                }
            }
            return null;
        }

        //Save method to Serialize transaction manager to file
        public void save( ) throws IOException {
            ObjectOutputStream outStream =
                    new ObjectOutputStream(
                            new FileOutputStream(
                                    "Transactions.ser"));
            outStream.writeObject(col);
            outStream.close( );
        }

        //Load methods to deserialize transaction manager from file
        @SuppressWarnings("unchecked")
        public void load( ) throws IOException,
                ClassNotFoundException {
            ObjectInputStream inStream =
                    new ObjectInputStream(
                            new FileInputStream(
                                    "Transactions.ser"));
            col = (ArrayList)
                    inStream.readObject( );
            inStream.close( );
        }

        @Override
        public Iterator iterator() {
            return col.iterator( );
        }

}

/////////////////////////////////////////////////////////TEST3.JAVA/////////////////////////////////////////////////////////////

//Akshat Shah
//IT 313

//Importing all the classes
import IT313.proj4.Shahakshat.MobileTransaction;
import IT313.proj4.Shahakshat.Transaction;
import IT313.proj4.Shahakshat.TransactionManager;
import IT313.proj4.Shahakshat.TvTransaction;

//Importing arraylist and excpetions
import java.io.IOException;
import java.util.ArrayList;

public class Test3 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {


        TransactionManager Transactions = new TransactionManager();

        Transactions.add(new Transaction(01, "Samarth", "Google", 3500,
                "My first transaction", "10/29/2019 12.30.00"));

        Transactions.add(new Transaction(02, "Akshat", "Samsung", 3500,
                "My second transaction", "10/29/2019 12.30.00"));

        Transactions.add(new MobileTransaction(
                03, "Samarth", "OnePlus", 350,
                "64GB", "2GB", "My third transaction", "10/30/2019 12.38.00"));

        Transactions.add(new MobileTransaction(
                04, "Nisarg", "OnePlus", 350,
                "64GB", "2GB", "My fourth transaction", "10/30/2019 12.38.00"));

        Transactions.add(new TvTransaction(
                05, "Mehta", "LG", 6500,
                65, "4k OLED", "My fifth transaction", "10/30/2019 12.38.00"));

        Transactions.add(new TvTransaction(
                06, "Akshat", "Sony", 44500,
                75, "4k OLED", "My sixth transaction", "10/30/2019 12.38.00"));

        //Testing the getAll Method
        System.out.println("/////////Printing all objects from arraylist/////////");
        ArrayList lol = Transactions.getAll();
        System.out.println(lol);


        //Testing the size Method
        System.out.println("\n\n" + "/////////Transactions size/////////");
        int size = Transactions.getCount();
        System.out.println("Transactions size: " + size + "\n");

        //Testing the FindBuyer Method
        System.out.println("\n\n" + "/////////Printing arraylist specified by buyer/////////");
        ArrayList buyer = Transactions.findBuyer("Akshat");
        System.out.println(buyer);

        //Testing the FindSeller Method
        System.out.println("\n\n" + "/////////Printing arraylist specified by seller/////////");
        ArrayList seller = Transactions.findSeller("LG");
        System.out.println(seller);

        //Testing the FindId Method
        System.out.println("\n\n" + "/////////Printing arraylist specified by id/////////");
        Transaction id = Transactions.findId(1);
        System.out.println(id);

        //Testing the clear Method
        Transactions.clear();

        //Testing the save Method
        Transactions.save();

        //Testing the size Method after clearing
        System.out.println("\n\n" + "/////////Transactions size after clearing/////////");
        int size1 = Transactions.getCount();
        System.out.println("Transactions size after clearing: " + size1 + "\n");

        //Testing the load Method
        Transactions.load();

    }
}