public static GPA PersonalReader()throws IOException{ // Read credit information from a JSON file String file = "C:\\Users\\isabe\\Desktop\\code\\code\\src\\STUDENT\\1001.json"; try{ FileReader fileReader = new FileReader(file); Reader reader = new InputStreamReader(new FileInputStream(file),"Utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } reader.close(); fileReader.close(); File file1 = new File("C:\\Users\\isabe\\Desktop\\code\\code\\src\\credit.json"); FileReader fileReader1 = new FileReader(file1); Reader reader1 = new InputStreamReader(new FileInputStream(file1), "Utf-8"); int ch1 = 0; StringBuffer sb2 = new StringBuffer(); while ((ch1 = reader1.read()) != -1) { sb2.append((char) ch1); } fileReader1.close(); reader1.close(); String creditStr = sb2.toString(); // Parse JSON strings using FastJSON library and calculate weighted GPA JSONObject jsonObject2 = JSON.parseObject(creditStr); double weighteGpa = 0.0; double Cre = 0.0; String jsonStr = sb.toString(); JSONObject jsonObject = JSON.parseObject(jsonStr); JSONArray subjectsList = jsonObject.getJSONArray("subjectsList"); Double avgGrade = 0.0; for (int i = 0; i < subjectsList.size(); i++) { JSONObject subject = subjectsList.getJSONObject(i); String subjectName = subject.getString("subject"); JSONObject jsonObject1 = jsonObject2.getJSONObject(subjectName); Object o = jsonObject1.getDouble("credit"); Double grade = subject.getDouble("grade"); weighteGpa += Double.parseDouble(o.toString())*grade; Cre += Double.parseDouble(o.toString()); avgGrade += grade; } avgGrade = avgGrade / subjectsList.size(); System.out.println(avgGrade); // Return GPA object return new GPA(avgGrade+"",(weighteGpa/(10*Cre)-5)+""); } catch (IOException e) { e.printStackTrace(); return null; } }使用tdd修改这段代码
时间: 2023-12-12 10:02:24 浏览: 122
首先,需要为该方法编写测试用例。我们可以编写以下几个测试用例:
1. 当文件路径存在且文件内容正确时,应正确计算加权平均GPA并返回GPA对象。
2. 当文件路径存在但文件内容格式不正确时,应该返回null。
3. 当文件路径不存在时,应该抛出FileNotFoundException异常。
针对这些测试用例,我们可以先编写测试代码:
```
@Test
public void testPersonalReader() throws IOException {
// Test case 1: file path exists and file content is correct
String file = "C:\\Users\\isabe\\Desktop\\code\\code\\src\\STUDENT\\1001.json";
GPA result1 = PersonalReader(file);
assertEquals("3.7", result1.getGPA());
assertEquals("3.0", result1.getWeightedGPA());
// Test case 2: file path exists but file content is incorrect
String file2 = "C:\\Users\\isabe\\Desktop\\code\\code\\src\\STUDENT\\1002.json";
GPA result2 = PersonalReader(file2);
assertNull(result2);
// Test case 3: file path does not exist
String file3 = "C:\\Users\\isabe\\Desktop\\code\\code\\src\\STUDENT\\1003.json";
assertThrows(FileNotFoundException.class, () -> {
PersonalReader(file3);
});
}
```
接下来,我们需要根据测试用例来修改代码。首先,需要将原方法的方法名和参数修改为符合TDD的方式:
```
public static GPA PersonalReader(String file) throws IOException {
// ...
}
```
然后,在方法中增加如下代码:
```
File f = new File(file);
if (!f.exists()) {
throw new FileNotFoundException("File not found: " + file);
}
```
这段代码用于判断文件是否存在,如果不存在则抛出FileNotFoundException异常。
接着,我们需要在方法中增加用于处理文件内容的代码。这里我们使用JSON库来解析JSON文件。因此,需要在方法中添加以下代码:
```
JSONObject jsonObject = JSON.parseObject(sb.toString());
JSONArray subjectsList = jsonObject.getJSONArray("subjectsList");
```
这段代码用于解析JSON文件中的内容,并获取其中的"subjectsList"数组。
最后,我们需要计算GPA并返回GPA对象。可以在方法中添加以下代码:
```
Double weighteGpa = 0.0;
Double totalCredit = 0.0;
Double totalGrade = 0.0;
for (int i = 0; i < subjectsList.size(); i++) {
JSONObject subject = subjectsList.getJSONObject(i);
String subjectName = subject.getString("subject");
JSONObject creditJson = jsonObject2.getJSONObject(subjectName);
Double credit = creditJson.getDouble("credit");
Double grade = subject.getDouble("grade");
weighteGpa += credit * grade;
totalCredit += credit;
totalGrade += grade;
}
Double avgGrade = totalGrade / subjectsList.size();
Double gpa = (weighteGpa / (10 * totalCredit)) - 5;
return new GPA(String.format("%.1f", avgGrade), String.format("%.1f", gpa));
```
这段代码用于根据解析出来的数据计算GPA,并返回GPA对象。
最终的代码如下所示:
```
public static GPA PersonalReader(String file) throws IOException {
// Read credit information from a JSON file
try {
File f = new File(file);
if (!f.exists()) {
throw new FileNotFoundException("File not found: " + file);
}
FileReader fileReader = new FileReader(file);
Reader reader = new InputStreamReader(new FileInputStream(f), "Utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
reader.close();
fileReader.close();
File file1 = new File("C:\\Users\\isabe\\Desktop\\code\\code\\src\\credit.json");
FileReader fileReader1 = new FileReader(file1);
Reader reader1 = new InputStreamReader(new FileInputStream(file1), "Utf-8");
int ch1 = 0;
StringBuffer sb2 = new StringBuffer();
while ((ch1 = reader1.read()) != -1) {
sb2.append((char) ch1);
}
fileReader1.close();
reader1.close();
String creditStr = sb2.toString();
JSONObject jsonObject2 = JSON.parseObject(creditStr);
JSONObject jsonObject = JSON.parseObject(sb.toString());
JSONArray subjectsList = jsonObject.getJSONArray("subjectsList");
Double weighteGpa = 0.0;
Double totalCredit = 0.0;
Double totalGrade = 0.0;
for (int i = 0; i < subjectsList.size(); i++) {
JSONObject subject = subjectsList.getJSONObject(i);
String subjectName = subject.getString("subject");
JSONObject creditJson = jsonObject2.getJSONObject(subjectName);
Double credit = creditJson.getDouble("credit");
Double grade = subject.getDouble("grade");
weighteGpa += credit * grade;
totalCredit += credit;
totalGrade += grade;
}
Double avgGrade = totalGrade / subjectsList.size();
Double gpa = (weighteGpa / (10 * totalCredit)) - 5;
return new GPA(String.format("%.1f", avgGrade), String.format("%.1f", gpa));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
```
阅读全文