2012-02-15 50 views
0

我該如何開始。我知道如何製作這種方法,但是我不知道究竟要把它放在裏面。我製作了一個歌曲班,其中包含了歌曲,藝術家和歌曲的排名。我從另一個txt文件中獲取此信息。宋應該有一個靜態解析方法,它採用帶有製表符分隔部分的字符串並返回一個Song對象。 這是我開始使用的代碼,但我不知道這是否合法。我也不知道它是否真的可以正常工作。如何製作一個採用製表符分隔的部分的字符串的靜態解析方法?

public class Billboard { 
private int year; 
private int rank; 
private String artist; 
private String title; 


public void setYear(int Y){ 
    this.year = Y; 
} 
public void setRank(int R){ 
    this.rank = R; 
} 
public void setArtist(String A){ 
    this.artist = A; 
} 
public void setTitle(String T){ 
    this.title = T; 
} 
public int getYear(){ 
    return this.year; 
} 
public int getRank(){ 
    return this.rank; 
} 
public String getArtist(){ 
    return this.artist; 
} 
public String getTitle(){ 
    return this.title; 
} 

public void Song (String Line) { 
String[] token = line.split("\t"); 
title = token[0]; 
artist = token[1]; 
year = Integer.parseInt(token[2]); 

    } 

}

+0

如果你想知道它是否有效,那麼我建議你爲它編寫一個單元測試。 – 2012-02-15 06:23:47

回答

0
public void Song (String Line) 

將其更改爲

public static Song createSong (String aLine_in) 

注意的變化:

  1. static關鍵字添加。

  2. 方法名稱以小寫字母開頭。 (找到更合適的名稱)

  3. 局部變量/方法參數以小寫字母開頭。因爲它是一個參數_in是前綴。

可以調用靜態方法如下:

Song aSong = Song.createSong("...\t...\t..."); 
1

你不能讓這種方法靜態的,因爲它訪問對象變量:title, artist, year

所以你需要使用一個構造函數,或將一個對象傳入方法中:

public Billboard (String Line) { 
    String[] token = line.split("\t"); 
    this.title = token[0]; 
    this.artist = token[1]; 
    this.year = Integer.parseInt(token[2]); 
} 

public static Billboard parseString(String Line) { 
    return parseString(line, new Billboard()); 
} 

public static Billboard parseString(String Line, Billboard billboard) { 
    String[] token = line.split("\t"); 
    billboard.setTitle(token[0]); 
    billboard.setArtist(token[1]); 
    billboard.setYear(Integer.parseInt(token[2])); 
    return billboard; 
} 
1

Song類:

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import utils.Utils; 

public class Song { 

    private String title; 
    private String artist; 
    private int rank; 
    private int year; 

    public Song(String title, String artist, int rank, int year){ 
     this.title = title; 
     this.artist = artist; 
     this.year = year; 
     this.rank = rank; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public String getArtist() { 
     return artist; 
    } 

    public int getRank() { 
     return rank; 
    } 

    public int getYear() { 
     return year; 
    } 

    public static Song parseLine(String line, String delimiter) { 
     String[] parts = Utils.splitLine(line, delimiter); 
     return new Song(parts[0], parts[1], Integer.parseInt(parts[2]),Integer.parseInt(parts[3])); 
    } 

    public static List<Song> readFile(String dataFile) { 
     List<Song> lines = new ArrayList<Song>(); 
     String line = null; 


     try{ 
      FileReader fr = new FileReader(new File(dataFile)); 
      BufferedReader br = new BufferedReader(fr); 

      while((line = br.readLine()) != null){ 
       lines.add(parseLine(line, "\t")); 
      } 

     }catch(IOException e){ 
      e.printStackTrace(); 
     } 

     return lines; 
    } 

     @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((artist == null) ? 0 : artist.hashCode()); 
     result = prime * result + rank; 
     result = prime * result + ((title == null) ? 0 : title.hashCode()); 
     result = prime * result + year; 
     return result; 
    } 

    @Override 
public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Song other = (Song) obj; 
     if (artist == null) { 
      if (other.artist != null) 
       return false; 
     } else if (!artist.equals(other.artist)) 
      return false; 
     if (rank != other.rank) 
      return false; 
     if (title == null) { 
      if (other.title != null) 
       return false; 
     } else if (!title.equals(other.title)) 
      return false; 
     if (year != other.year) 
      return false; 
     return true; 
    } 


}  

Utils.splitLine():

public class Utils { 

    public static String[] splitLine(String line, String delimiter) { 
     if(delimiter.equals("|")){ 
      delimiter = "\\|"; 
     } 

     if(delimiter.equals(".")){ 
      delimiter = "\\."; 
      } 

     String[] rtn = line.split(delimiter); 
     for(int i=0;i<rtn.length;i++){ 
      rtn[i] = rtn[i].trim(); 
     } 

     return rtn; 
    } 
} 

測試:用於readFile()

import static org.junit.Assert.*; 

import java.util.ArrayList; 
import java.util.List; 

import org.junit.Test; 


public class HelpTest { 

    @Test 
    public void test_parseLine() { 
     // tabs betwen the title artist year and rank 
     String line = "Run Runaway Slade 20 1984"; 
     Song expected = new Song("Run Runaway","Slade", 20, 1984); 
     Song actual = Song.parseLine(line, "\t"); 
     assertEquals(expected, actual); 

    } 

    @Test 
    public void test_readFile() { 
     String path = "src/testSongData"; 
     Song song = new Song("Run Runaway","Slade", 20, 1984); 
     List<Song> expected = new ArrayList<Song>(); 
     expected.add(song); 
     List<Song> actual = Song.readFile(path); 
     assertEquals(expected, actual); 

    } 

} 

文本文件src/testSongData

Run Runaway Slade 20 1984 
相關問題