Home        Next 

/*

Description: 1) When shares of a stock are sold, the
Capital gain(or loss) is the difference between 
the selling price and the purchase price of the stock. 
This is easy to compute when selling a single share 
of stock but if shares are sold which are purchased 
over a long period of time, the shares being sold 
must be idenfified precisely. The standard method 
for such sales uses the FIFO principal- the shares 
sold are the ones held the longest. For example 
suppose we buy 100 shares at $12 per share then 
buy 100 shares at $14 per share, then 100 shares 
at $15 per share. Now we sell 200 shares at 17 per share.


Queue interface
Queue is a Fundamental data structure and close cousin 
of the stack as queue is a container of objects that 
are inserted and removed according to the first-in 
first-out(FIFO) principle.
*/

public interface Queue 
{ // Returns the number of items in the Queue.
public int Size();

// Returns true if and only if Queue is empty 
// false otherwise.
public boolean Empty();

// Adds Object element to tail of Queue.
public void enqueue(Object element);

// Removes and returns object from front of Queue.
public Object dequeue() throws QueueEmptyException;

// Returns the object at front of Queue(w/o removing it).
public Object Front() throws QueueEmptyException;
}

/*_______________________________*/

 

import java.io.*;
public class main 
{
private static void doBuy(Queue lotqueue, int numshares, double price) 
{
StockLot buy = new StockLot(numshares, price);
lotqueue.enqueue(buy);
System.out.println("Buying " + numshares + " shares at " + price);
}

private static void doSell(Queue lotqueue, int numshares, double price) 
{
System.out.println("Selling " + numshares + " shares at " + price);
System.out.print(" Bought ");

double buybasis = 0.0;
double saleprice = numshares*price;

if (lotqueue.Empty()) 
{
System.out.println("Input error: Trying to sell with no shares!");
System.exit(1);
}
StockLot first = (StockLot)lotqueue.Front();
while (first.getNumShares() <= numshares) 
{
int thisnum = first.getNumShares();
double thisprice = first.getPrice();
System.out.print(thisnum + " at " + thisprice);

numshares -= thisnum;
buybasis += thisnum*thisprice;
lotqueue.dequeue();

if (numshares > 0) 
{
if (lotqueue.Empty()) 
{
System.out.println("\nInput error: Trying to sell more " +
"shares than have been bought!");
System.exit(1);
}
System.out.print(", ");
first = (StockLot)lotqueue.Front();
}
}
if (numshares > 0) {
System.out.print(numshares + " at " + first.getPrice());
buybasis += numshares*first.getPrice();
first.setNumShares(first.getNumShares() - numshares);
}
System.out.println("\n Capital Gain = " + (saleprice - buybasis));
}
/*------------------Main-----------------------------------*/
public static void main(String argv[]) 
{
TextReader input = new TextReader(new InputStreamReader(System.in));
Queue lotqueue = new LinkQueue();
System.out.println("Enter your values in the format"+
"(eg. buy x share(s) at $y each)to exit enter end");
String buysell = null;
try {
buysell = input.readString();

catch (IOException err) 
{
System.out.println("Error reading input! " + err.getMessage());
System.exit(1);
}

while (!buysell.equalsIgnoreCase("end")) 
{
int numshares = 0;
String at = null;
double price = 0.0;

try 
{
numshares = input.readInt();
at = input.readString();
price = input.readDouble();

catch (IOException err) 
{
System.out.println("Error reading input! " + err.getMessage());
System.exit(1);

catch (NumberFormatException err) 
{
System.out.println("Input error: Bad number!");
System.exit(1);
}

if (buysell.equalsIgnoreCase("buy")) 
{
doBuy(lotqueue, numshares, price);

else if (buysell.equalsIgnoreCase("sell")) 
{
doSell(lotqueue, numshares, price);

else 
{
System.out.println("Error! Unknown command: " + buysell);
}
try 
{
buysell = input.readString();

catch (IOException err) 
{
System.out.println("Error reading input! " + err.getMessage());
System.exit(1);
}
}
}
}
/* Output
Z:\lab2b>javac Queue.java
Z:\lab2b>javac TextReader.java
Z:\lab2b>javac Node.java
Z:\lab2b>javac StockLot.java
Z:\lab2b>javac LinkQueue.java
Z:\lab2b>javac QueueEmpty.java
Z:\lab2b>javac main.java
Z:\lab2b>java main
Enter your values in the format(eg. buy x share(s) at $y each)to exit enter end
buy 100 at 12
Buying 100 shares at 12.0
buy 100 at 14
Buying 100 shares at 14.0
buy 100 at 15
Buying 100 shares at 15.0
sell 200 at 17
Selling 200 shares at 17.0
Bought 100 at 12.0, 100 at 14.0
Capital Gain = 800.0
buy 100 at 4.25
Buying 100 shares at 4.25
buy 100 at 4.50
Buying 100 shares at 4.5
buy 100 at 4.75
Buying 100 shares at 4.75
buy 100 at 4.6
Buying 100 shares at 4.6
buy 100 at 4.95
Buying 100 shares at 4.95
sell 400 at 5.4
Selling 400 shares at 5.4
Bought 100 at 15.0, 100 at 4.25, 100 at 4.5, 100 at 4.75
Capital Gain = -690.0
end
*/

/*_____________________________________________*/

/*
Node.java Implementation of a node of a Singly Linked list.
The space usage of a singly linked list with n elements 
is O(n), since it has n node and each node uses O(1) space 
to store references to an element and to the next node.
With a singly linked list, we can easily insert or delete 
an element at the head of the list in O(1) time.
*/
public class Node 
{ //Instance Variable:
private Object element;
private Node next;
//Simple constructors:
Node() 
{
this(null, null);
}

Node(Object e, Node n) 
{
element = e;
next = n;
}
// Accessor Methods:
Object getElement() 
{
return element;
}

Node getNext() 
{
return next;
}
// Modifier Methods:
void setElement(Object newElem) 
{
element = newElem;
}

void setNext(Node newNext) 
{
next = newNext;
}
}

/*__________________________________________*/

import java.io.*;

public class TextReader extends PushbackReader 
{
public TextReader(Reader in) 
{
super(in);
}

private String getInteger() throws IOException 
{
StringBuffer sbuff = new StringBuffer();
int ch = read();

while ((ch != -1) && Character.isWhitespace((char)ch)) 
{
ch = read();
}
if ((ch != -1) && ((char)ch == '-')) 
{
sbuff.append((char)ch);
ch = read();
}
while ((ch != -1) && Character.isDigit((char)ch)) 
{
sbuff.append((char)ch);
ch = read();
}
unread(ch);
return sbuff.toString();
}

public int readInt() throws IOException, NumberFormatException 
{
return Integer.parseInt(getInteger());
}

public long readLong() throws IOException, NumberFormatException 
{
return Long.parseLong(getInteger());
}

private String getFloat() throws IOException 
{
StringBuffer sbuff = new StringBuffer();
int ch = read();

while ((ch != -1) && Character.isWhitespace((char)ch))
ch = read();

if (ch == '-') 
{
sbuff.append((char)ch);
ch = read();
}
while ((ch != -1) && Character.isDigit((char)ch)) 
{
sbuff.append((char)ch);
ch = read();
}
if (ch == '.') 
{
sbuff.append((char)ch);
ch = read();

while ((ch != -1) && Character.isDigit((char)ch)) 
{
sbuff.append((char)ch);
ch = read();
}
}
if ((ch == 'e') || (ch == 'E')) 
{
sbuff.append((char)ch);
ch = read();
if ((ch == '+') || (ch == '-')) 
{
sbuff.append((char)ch);
ch = read();
}
while ((ch != -1) && Character.isDigit((char)ch)) 
{
sbuff.append((char)ch);
ch = read();
}
}
unread(ch);
return sbuff.toString();
}

public float readFloat() throws IOException, NumberFormatException 
{
return Float.valueOf(getFloat()).floatValue();
}

public String readString() throws IOException 
{
StringBuffer sbuff = new StringBuffer();
int ch = read();
while ((ch != -1) && Character.isWhitespace((char)ch))
ch = read();

while ((ch != -1) && !Character.isWhitespace((char)ch)) 
{
sbuff.append((char)ch);
ch = read();
}
unread(ch);
return sbuff.toString();
}

public double readDouble() throws IOException, NumberFormatException 
{
return Double.valueOf(getFloat()).doubleValue();
}

public String readLine() throws IOException 
{
StringBuffer sbuff = new StringBuffer();
int ch = read();

while ((ch != -1) && ((char)ch != '\n')) 
{
sbuff.append((char)ch);
ch = read();
}
return sbuff.toString();
}
}

/*________________________________*/

public class StockLot 
{
private int numShares;
private double price; // Purchase price

StockLot(int num, double pri) 
{
numShares = num;
price = pri;
}

public int getNumShares() 
{
return numShares;
}

public double getPrice() 
{
return price;
}

public void setNumShares(int num) 
{
numShares = num;
}

public void setPrice(double pri) 
{
price = pri;
}
}

/*______________________________*/

public class QueueEmptyException extends RuntimeException 
{
public QueueEmptyException(String err) 
{
super(err);
}
}

/*______________________________*/

public class LinkQueue implements Queue
{
private Node rear, front;// Reference to the head Node
private int size=0; // Number of elements in Queue

public LinkQueue()
{
front = rear = null;
}

public int Size() 
{
return size;
}

public boolean Empty() 
{
return size == 0;
}

public Object Front() throws QueueEmptyException 
{
if (Empty())
throw new QueueEmptyException("Queue is empty.");
else
{
Object o = front.getElement();
return o;
}
}

public void enqueue(Object e) 
{
Node n = new Node();
n.setElement(e);
n.setNext(null);
if(Empty())
front = rear = n;
else
rear.setNext(n);
rear = n; 
size++;
}

public Object dequeue() throws QueueEmptyException 
{
if (Empty())
throw new QueueEmptyException("Queue is empty!.");
Object o = front.getElement(); 
Node n = front;
front = front.getNext();
size--;
if(Empty())
rear = null;
n=null;
return o;
}
}

Home       Next      

 

import java
add in here please