首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java理论:货币、期货和金属分析类设计1

Java理论:货币、期货和金属分析类设计1
EN

Code Review用户
提问于 2013-05-26 20:24:31
回答 2查看 242关注 0票数 1

从这里开始,这是一个延续:

https://stackoverflow.com/questions/16761207/java-theory-classes-design-for-currency-futures-and-metals-analysis

我现在已经创建了两个类。

1)历史资料

2)期货。

下面是代码:它执行以下操作:接收一个文件,检查它是否是一个文件,根据文件的大小动态创建一个数组,然后将其打印为一个字符串。正如你所看到的,这仍然是程序性的。它可以工作,但我不使用OOP主体。

代码语言:javascript
复制
import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;

/*
 * This will allow me to uploadFutures historical csv data into an arraylist
 * I then want to perform analysis on it
*/


public class Futures extends HistoricalData{


// absolute path of my file
File esFuturesData = new File("C:/Users/Administrator/Desktop/Stock Project/Tick Data Source Files/TextFiles/Completed/test.txt");


// need to declare an list to loop through it
public ArrayList<E> myList = new ArrayList<E>();

// need a method to see if the file can be read.
public boolean isaFile()
{
return esFuturesData.isFile();
}

// need a method to get the size of the file  ..
public int sizeofFile() throws IOException{
// variable z for size of file
    int z = 0;
if(isaFile()){
    //  read in the file esFuturesData and store it in object reader
BufferedReader reader = null;
try{
        reader = new BufferedReader(new FileReader(esFuturesData));
        // need to find out how many rows are in the file
            while(reader.readLine() != null)
            {
            //when there is new line increment z by 1
            z  +=1;

            }               
} catch (IOException ex) {
    System.err.println();
    }
finally{
    if(reader != null){
        reader.close();
    }
}
}
else{
    System.out.println("This is not a file");
}
return z;
}



// need to create a dynamic String array based on the size of the file
public void createArray(){
    // reset the rows of the file to 0.
    int rowCount =0;
    // create a testData array based on the number of rows method and that is of 4 columns
    myList = new ArrayList[sizeofFile()][5];
    // we need to read in the file..
    // catch any errors..
    BufferedReader reader = null;
    try{
    reader = new BufferedReader(new FileReader(esFuturesData));
    // we need to create a line of type String to store what we are putting through it..
    String line = null;
    // need to keep reading until there is nothing left.
    while((line=reader.readLine()) != null){
    // we need to put the line into the array testData
    StringTokenizer p = new StringTokenizer(line,",");
    // while there is something to read
    while(p.hasMoreTokens()){
    // loop through the first column and add a 1        

        for (int columnCount = 0; columnCount<1;columnCount++){
        // need to convert to string representation of an int

            myList[rowCount][columnCount] = "0";
        }

    // loop through the columns
    for (int columnCount = 1; columnCount<5;columnCount++){
        myList [rowCount][columnCount] = p.nextToken();
        //System.out.printf("%s", testData [x][y]);
    } // end of for loop
    rowCount++;
    } // end of while loop
    } // end of other while loop        
    } // end of try braces
    catch(Exception e){
        System.err.println();           
    } // end of catch
} // end of create array braces

// method that prints the file as a String
public void printArrayAsString(){

    // Some Housekeeping
    System.out.printf("  ID");
    System.out.printf(" Date");
    System.out.printf(" Time  ");
    System.out.printf("  Price");
    System.out.printf("   Volume");
    System.out.println();

    // need a loop to go through each row number    
        for(int rowCount = 0; rowCount < myList.length; rowCount++ ){
    // print out each row number
    //  System.out.printf("%s", x);
    // print out a space
        System.out.printf("   ");
        {
        for(int columnCount = 0; columnCount < 5; columnCount++ ){
        System.out.printf("%s", myList [rowCount][columnCount]);
        System.out.printf("   ");
        }
        System.out.println();
        }
        }
}
}
EN

回答 2

Code Review用户

回答已采纳

发布于 2013-05-27 07:53:20

首先,您的缩进、大括号样式和空格的使用都是混合/混乱的,如果可用,请在您最喜欢的IDE中点击“格式代码”(点击"Format Code“)。

代码语言:javascript
复制
// reads in a esfutures file and stores it as esfuturesdataobject 

Java文档通常如下所示:

代码语言:javascript
复制
/**
 * This is an important variable
 */
private String awesomeness = null;

这样就可以让IDE和其他系统接收到它们和f.e。直接展示给你。

代码语言:javascript
复制
File esfuturesdata = new File("C://Users//Administrator//Desktop//Stock Project//Tick Data Source Files//TextFiles//Completed//test.txt");
  • Java中的变量名应该是下
  • 你有一条硬编码的路,这就是你想要的吗?
  • Windows使用需要转义的反斜杠\,不需要转义的斜杠/,所以它是一个或另一个: C:\Users\Administrator\.C:/用户/管理员/.
  • 考虑添加一个可见性修饰符(privatepublic)以提高清晰度。
代码语言:javascript
复制
// need to declare an tesdata Array for looping through my data
String [][] testData;

考虑一下在这种情况下List<T>是否更有趣。

代码语言:javascript
复制
BufferedReader reader = new BufferedReader(new FileReader(futuresdata));

读者和类似的应该在使用后关闭。关于这样做的可能设计如下:

代码语言:javascript
复制
BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader(file));
    // Usage goes here.
} catch (IOException ex) {
    // Exception handling goes here.
} finally {
    if(reader != null) {
        reader.close();
    }
}
代码语言:javascript
复制
catch (Exception e)

不要捕获一般的异常,只捕获必要的异常并优雅地处理它们。还要考虑是否需要抛出这些文件,因为如果由于任何原因无法读取文件,应用程序将很高兴地继续运行。

代码语言:javascript
复制
System.out.println("e");

那是什么?!更不用说错误应该是stderr,也就是System.err.println()

代码语言:javascript
复制
// reset the rows of the file to 0.
int x =0;

如果该变量包含行数,那么也请给它一个有意义的名称,如rowsrowCount

代码语言:javascript
复制
}// end of futures class brackets

如果您的代码结构良好且意图正确,则此类注释是不必要的。

票数 3
EN

Code Review用户

发布于 2013-05-27 13:06:00

我不知道我是否理解当前的课程设计。HistoricalData是什么样子的?

想想你正在做的不同的事情,它们代表了什么,以及你想用它们做什么。然后将它们建模为类。

例如,我看到一些表示CSV文件中一行的内容。我可以看到它有ID,日期,价格等东西。目前,这些似乎只是数组中的字符串。考虑使用适当数据类型的属性(例如,日期和时间的java.util.Date )创建一个类来表示这一点。也许类似于HistoricalDataElement(?)也许你能想出一个更好的名字。您会将CSV文件中一行中的数据称为什么?

创建一个单独的类来表示HistoricalDataElements的集合也可能是有意义的。这可能是一个很好的地方,可以将一些操作在整个数据集上的函数放在这里。您必须决定这对您的场景是否有意义。也许你会有多个类来做不同类型的分析。

您还应该考虑将解析分离到它自己的类中。

以下是我正在思考的一个非常粗略的概要:

代码语言:javascript
复制
public class HistoricalDataElement {
    private String id;
    private Date dateTime;
    private BigDecimal price;
    private long volume;
    // getters/setters
}

public class HistoricalData {
    private List<HistoricalDataElement> historicalDataElements;
    public long doSomeAnalysis() {...}
    public long doSomeOtherAnalysis() {...}
    // etc
}

public class HistoricalDataParser {
    public HistoricalData parse(File file) {...}
}
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/26637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档