Commit 3afa32be authored by RAHUL SHARMA's avatar RAHUL SHARMA

Initial Commit

parents
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_9" default="true" project-jdk-name="9.0" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/OrderMatching.iml" filepath="$PROJECT_DIR$/OrderMatching.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
public class Matcher {
public enum MatchType {FULL_MATCH, PARTIAL_MATCH, NO_MATCH}
public MatchType match(Transaction t1, Transaction t2) {
if (t1.getCustomerId().equals(t2.getCustomerId())) {
return MatchType.NO_MATCH;
}
if (!t1.getStock().equals(t2.getStock())) {
return MatchType.NO_MATCH;
}
if (!t1.getTypeOfRequest().equals(t2.getTypeOfRequest())) {
return MatchType.NO_MATCH;
}
//current trans is sell
if (t1.getTypeOfRequest().equals("Buy") && t2.getTypeOfRequest().equals("Sell")) {
if (t1.getPricePerUnit() < t2.getPricePerUnit()) {
return MatchType.NO_MATCH;
}
//selling quantity of current transaction exhausts
else{
if(t2.getQuantity() <= t1.getQuantity()){
return MatchType.FULL_MATCH;
}
else{
return MatchType.PARTIAL_MATCH;
}
}
}
// current trans is buy
else{
if(t1.getPricePerUnit() > t2.getPricePerUnit()){
return MatchType.NO_MATCH;
}
// buying quantity of current trans exhausts
else {
if(t2.getQuantity() <= t1.getQuantity()){
return MatchType.FULL_MATCH;
}
else {
return MatchType.PARTIAL_MATCH;
}
}
}
}
}
\ No newline at end of file
import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class StockExchange {
public void run() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Matcher matcher = new Matcher();
boolean shouldContinue = true;
ArrayList<Transaction> transactionList = new ArrayList<>();
while (shouldContinue) {
// get/parse user input
Transaction currentTransaction = new Transaction();
System.out.println("New Transacrion: \"Time Quantity Price Customer_Id Stock Type\" ");
String inputString = reader.readLine();
StringTokenizer stringTokenizer = new StringTokenizer(inputString);
if (stringTokenizer.hasMoreTokens()) {
currentTransaction.setTime(Integer.parseInt(stringTokenizer.nextToken()));
currentTransaction.setQuantity(Integer.parseInt(stringTokenizer.nextToken()));
currentTransaction.setPricePerUnit(Float.parseFloat(stringTokenizer.nextToken()));
currentTransaction.setCustomerId(stringTokenizer.nextToken());
currentTransaction.setStock(stringTokenizer.nextToken());
currentTransaction.setTypeOfRequest(stringTokenizer.nextToken());
}
if (!transactionList.isEmpty()) {
for (int i = 0; i < transactionList.size(); i++) {
Matcher.MatchType matchType = matcher.match(transactionList.get(i), currentTransaction);
if (matchType == Matcher.MatchType.FULL_MATCH) {
// modify ith transaction
transactionList.get(i).setQuantity(transactionList.get(i).getQuantity() - currentTransaction.getQuantity() );
if(transactionList.get(i).getQuantity() == 0){
transactionList.remove(i);
}
System.out.println(currentTransaction.getCustomerId()+" successfully traded "+ transactionList.get(i).getQuantity()+" stocks with "+transactionList.get(i).getCustomerId());
break;
} else if (matchType == Matcher.MatchType.PARTIAL_MATCH) {
// modify current Transaction
// drop i'th transaction
currentTransaction.setQuantity(currentTransaction.getQuantity() - transactionList.get(i).getQuantity());
transactionList.remove(i);
System.out.println(currentTransaction.getCustomerId()+" successfully traded "+ transactionList.get(i).getQuantity()+" stocks with "+transactionList.get(i).getCustomerId());
} else if (matchType == Matcher.MatchType.NO_MATCH) {
// move on
continue;
}
if(i == transactionList.size()-1 && currentTransaction.getQuantity() > 0) {
transactionList.add(currentTransaction);
}
}
} else {
// add this transaction if list is empty
transactionList.add(currentTransaction);
}
System.out.println("Do you want to continue? y|n");
String userResponseString = reader.readLine();
if (userResponseString.equals("n")) {
shouldContinue = false;
}
}
}
}
import java.io.IOException;
public class TestStockExchange {
public static void main(String[] args) throws IOException {
StockExchange stockExchange = new StockExchange();
stockExchange.run();
}
}
import java.util.Comparator;
public class Transaction implements Comparator<Transaction> {
private int time;
private int quantity;
private float pricePerUnit;
private String customerId;
private String stock;
private String typeOfRequest;
private int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getPricePerUnit() {
return pricePerUnit;
}
public void setPricePerUnit(float pricePerUnit) {
this.pricePerUnit = pricePerUnit;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getStock() {
return stock;
}
public void setStock(String stock) {
this.stock = stock;
}
public String getTypeOfRequest() {
return typeOfRequest;
}
public void setTypeOfRequest(String typeOfRequest) {
this.typeOfRequest = typeOfRequest;
}
@Override
public String toString() {
return "Transaction{" +
"time=" + time +
", quantity=" + quantity +
", pricePerUnit=" + pricePerUnit +
", customerId='" + customerId + '\'' +
", stock='" + stock + '\'' +
", typeOfRequest='" + typeOfRequest + '\'' +
'}';
}
// add comparator
@Override
public int compare(Transaction o1, Transaction o2) {
if(o1.getTime() == o2.getTime()){
return 0;
}
else if(o1.getTime() > o2.getTime()){
return 1;
}
else {
return -1;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment