Question
public static void main(String[] args) { Scanner keybd = new Scanner(System.in); String filepath; System.out.print(Please enter transactions file name: ); filepath = keybd.nextLine().trim(); Processing sp =
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
String filepath;
System.out.print("Please enter transactions file name: ");
filepath = keybd.nextLine().trim();
Processing sp = new Processing(filepath);
try
{
sp.loadFile();
sp.processTransactions();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
keybd.close();
}
}
private String filepath;
private LinkedQueue
private LinkedQueue
private int availableQty;
private int gains;
public Processing(String FilePath)
{
this.filepath = FilePath;
allTransactions = new LinkedQueue
buyTransactions = new LinkedQueue
}
public void loadFile() throws FileNotFoundException
{
Scanner File = new Scanner(new File(filepath));
System.out.println("The following transactions were read from the file:");
while(File.hasNextLine())
{
String line = File.nextLine().trim().toLowerCase();
if(line.isEmpty())
continue;
String[] tokens = line.split("(\\s)+");
boolean sell = tokens[0].equals("sell");
int quantity = Integer.parseInt(tokens[1]);
int price = Integer.parseInt(tokens[4].substring(1));
Transaction t = new Transaction(sell, quantity, price);
allTransactions.add(t);
System.out.println(t);
}
File.close();
}
/*
*
*
*
*
*
*
*
*/
public void processTransactions()
{
boolean error = false;
gains = 0;
availableQty = 0;
while(!allTransactions.isEmpty() && !error)
{
Transaction t = allTransactions.remove();
System.out.println("Processing transaction: " + t);
if(!t.isSell())
{
buyTransactions.add(t);
availableQty += t.getNumber();
}
else
{
int sellQty = t.getNumber();
while(sellQty > 0)
{
if(buyTransactions.isEmpty())
{
error = true;
break;
}
Transaction bt = buyTransactions.peek();
if(bt.getNumber() <= sellQty)
{
gains += (t.getPrice() - bt.getPrice()) * bt.getNumber();
sellQty -= bt.getNumber();
buyTransactions.remove();
}
else
{
gains += (t.getPrice() - bt.getPrice()) * sellQty;
bt.changeNumber(bt.getNumber() - sellQty);
sellQty = 0;
}
}
if(sellQty != 0)
{
System.out.println("Error: attempt to sell non-existing shares!");
}
else
availableQty -= t.getNumber();
}
}
if(!error)
{
System.out.println(" Capital Gain/Loss : $" + gains);
System.out.println("There are " + availableQty + " shares left");
}
}
}
Q.can you help me write method specification for each method?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started