Commit 5e1b1011 authored by Yonas Adiel's avatar Yonas Adiel

Add land csv file and reader

parent 87029b01
rootProject.name = 'HelloGradle'
rootProject.name = 'AvatarDuel'
package com.avatarduel;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import com.avatarduel.model.Element;
import com.avatarduel.model.Land;
import com.avatarduel.util.CSVReader;
public class AvatarDuel extends Application {
private static final String LAND_CSV_FILE_PATH = "src/res/card/data/land.csv";
public void loadCards() throws IOException {
File landCSVFile = new File(LAND_CSV_FILE_PATH);
CSVReader landReader = new CSVReader(landCSVFile, ",");
landReader.setSkipHeader(true);
List<String[]> landRows = landReader.read();
for (String[] row : landRows) {
System.out.println(row[1]);
Land l = new Land(row[0], row[2], Element.valueOf(row[1]));
}
}
@Override
public void start(Stage stage) {
Text text = new Text();
text.setText("Avatar Duel!");
text.setText("Loading...");
text.setX(50);
text.setY(50);
......@@ -23,6 +43,13 @@ public class AvatarDuel extends Application {
stage.setTitle("Avatar Duel");
stage.setScene(scene);
stage.show();
try {
this.loadCards();
text.setText("Avatar Duel!");
} catch (IOException e) {
text.setText("Failed to load cards: " + e);
}
}
public static void main(String[] args) {
......
package com.avatarduel.model;
public enum Element {
WATER,
FIRE,
AIR,
EARTH
};
package com.avatarduel.model;
public class Land {
private String name;
private String description;
private Element element;
public Land() {
this.name = "";
this.description = "";
this.element = Element.AIR;
}
public Land(String name, String description, Element element) {
this.name = name;
this.description = description;
this.element = element;
}
}
\ No newline at end of file
package com.avatarduel.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* CSVReader is a reader with given filename and separator.
* IMPORTANT NOTE:
* We assume that the csv doesn't have separator in the cells.
* This implementation only split each line using separator
* into array of values.
*
* @author Laboratorium Programming
*/
public class CSVReader {
private File csvFile;
private String separator;
/**
* If set to true, CSVReader will skip the first line.
* For example, when the csv has header.
*/
private boolean skipFirstLine;
/**
* Creates a new reader from a file, using a separator
* @param csvFile file csv
* @param separator separator of csv file
*/
public CSVReader(File csvFile, String separator) {
this.csvFile = csvFile;
this.separator = separator;
this.skipFirstLine = false;
}
/**
* Set the reader to skip the first line, because the first line
* is the header of csv
* @param isSkippingHeader true if header should be skipped
*/
public void setSkipHeader(boolean isSkippingHeader) {
this.skipFirstLine = isSkippingHeader;
}
/**
* Reads the csv file into list of string array.
* @throws IOException exception when reading csv file.
* for example: file not found error
* @return list of string array. Each list item represent a row.
*/
public List<String[]> read() throws IOException {
String line;
boolean firstLine = true;
FileReader fileReader = new FileReader(this.csvFile);
BufferedReader br = new BufferedReader(fileReader);
ArrayList<String[]> list = new ArrayList<String[]>();
while ((line = br.readLine()) != null) {
if (!firstLine || !this.skipFirstLine) {
String[] row = line.split(this.separator);
// This code will ignore double quotes, if present as first and last character
for (int i = 0; i < row.length; i++) {
String val = row[i];
if (val.length() >= 2 && val.charAt(0) == '"' && val.charAt(val.length() - 1) == '"') {
row[i] = val.substring(1, val.length() - 1);
}
}
list.add(row);
}
firstLine = false;
}
return list;
}
}
\ No newline at end of file
name,element,description
"Eastern Water Temple","AIR","One of the two temples exclusively housing female airbenders."
"Western Water Temple","AIR","One of the two temples exclusively housing female airbenders."
"Southern Water Temple","AIR","One of the two temples exclusively housing male airbenders. Childhood home of Avatar Aang."
"Northern Water Temple","AIR","One of the two temples exclusively housing male airbenders."
"Foggy Swamp","WATER","Place for immigrants of the Southern Water Tribe with an extensive array of fauna and flora."
"Spirit Oasis","WATER","A verdant and lush garden located in a cave in the Northern Water Tribe's capital city at the North Pole."
"Northern Water Tribe","WATER","The largest division of its nation and is located on an island situated near the North Pole."
"Southern Water Tribe","WATER","Minor division of the Water Tribe. Its people are scattered in tiny villages and settlements located on an island by the South Pole."
"Si Wong Desert","EARTH","The largest desert in the world."
"Ba Sing Se","EARTH","The monolithic capital of the Earth Kingdom."
"Kyoshi Island","EARTH","Famous as the birthplace of Avatar Kyoshi, and as the home of the elite Kyoshi Warriors."
"Omashu","EARTH","The second largest city in the Earth Kingdom and the capital of one of its provinces, ruled by King Bumi."
"Fire Nation Capital","FIRE","The seat of the government for the Fire Nation"
"Boiling Rock","FIRE","A massive Fire Nation prison."
"Shu Jing","FIRE","A town located in the Fire Nation."
"Fire Fountain City","FIRE","A large, industrial city located in the Fire Nation."
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