第一个class 是Athlete
构造函数
public Athlete(java.lang.String name, Country country)
参数
name - 运动员的名字。
country - 运动员代表的国家。
方法
public java.lang.String getName()
返回运动员的姓名
public Country getCountry()
返回运动员代表的国家。
public int getGolds()
返回运动员赢得的金牌数。
public int getSilvers()
返回运动员赢得的银牌数量。
public int getBronzes()
返回运动员赢得的铜牌数量。
public void addBronze()
授予运动员一枚铜牌。
public void addSilver()
授予运动员一枚银牌。
public void addGold()
授予运动员一枚金牌。
public static Athlete load(java.lang.String path)
从文件创建一个新的运动员对象。文件结构如下:第一行姓名,第二行国家名称,第三行是金牌数,第四行是银牌数,第五行是铜牌数线。如果文件不存在,则返回 null。
参数:
path - 要读取的文件的路径。
返回:
创建的运动员对象。
public static void save ( Athlete athlete, java.lang.String path)
将给定的运动员对象写入文件。文件结构如下:第一行姓名,第二行国家名称,第三行是金牌数,第四行是银牌数,第五行是铜牌数线。如果运动员不存在,或者没有给出路径,则什么都不做。
第二个class是Country
构造函数
public Country(java.lang.String name)
参数
name - 国家名称。
方法
public java.lang.String getName()
返回国家名称。
public int getNumAthletes()
返回国家派出的运动员人数。
public int getGolds()
返回代表国家的运动员赢得的金牌数
public int getSilvers()
返回代表国家的运动员赢得的银牌数量。
public int getBronzes()
代表运动员获得铜牌的数量。
public Athlete getAthlete(int index)
返回数组中给定索引处的运动员。如果不存在这样的运动员,则返回 null。
public void setAthlete(Athlete athlete, int index)
将运动员数组中的给定索引设置为给定的运动员。如果索引不存在,则什么都不做。
public static Country firstOnMedalTallyTable(Country[] countries)
确定哪个国家在奖牌榜上。排名靠前的国家是获得金牌最多的国家。如果金牌出现并列,则并列国家之间以银牌分隔。如果仍然是平局,则返回获得铜牌最多的国家。如果仍然有平局,则返回平局之一。如果没有给出国家,则返回 null。
返回位居榜首的国家
第三个class是OlympicVillage
构造函数
public OlympicVillage(Country[] countries)
方法
public Country getCountryByName(java.lang.String name)
返回具有给定名称的国家/地区,如果不存在则返回 null。
public int getNumCountries()
返回村庄中的国家数量。
public Country getCountryWithMostUniqueMedallists()
返回村中奖牌获得者最多的国家(即获得至少一枚奖牌的运动员最多的国家)。如果有平局,则返回任何平局国家。
public Country getCountryAtTopOfTable()
返回位居奖牌榜首位的国家。
有啥要求吗,测试demo有吗。我帮你写。
代码太长了,已分享百度云盘
那我就随便写写了
这是运动员类的,看起来国家应该还有存运动员的list或数组
public class Athlete {
private String name;
private Country country;
private int golds;
private int silvers;
private int bronzes;
public Athlete(String name, Country country){
this.name=name;
this.country=country;
}
public static void save ( Athlete athlete, java.lang.String path){
try {
fileWriterMethod(path, athlete.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return name +"\n"+country+"\n"+golds+"\n"+silvers+"\n"+bronzes;
}
public static void fileWriterMethod(String filepath, String content) throws IOException {
try (FileWriter fileWriter = new FileWriter(filepath)) {
fileWriter.append(content);
}
}
public static Athlete load(String path){
FileInputStream fis = null;
try {
fis = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line = null;
//按行读取
Integer index = 0;
String name ="";
String contry = "";
int gold = 0,silver = 0,copper = 0;
while((line = br.readLine())!=null){
index++;
switch (index) {
case 1:
name = line;
case 2:
contry = line;
case 3:
gold = Integer.parseInt(line);
case 4:
silver = Integer.parseInt(line);
case 5:
copper = Integer.parseInt(line);
}
}
Athlete athlete = new Athlete(name, new Country(contry));
athlete.golds = gold;
athlete.silvers = silver;
athlete.bronzes = copper;
br.close();
return athlete;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String getName() {
return name;
}
public Country getCountry() {
return country;
}
public int getGolds() {
return golds;
}
public void addGold() {
golds++;
}
public int getSilvers() {
return silvers;
}
public void addSilver() {
silvers++;
}
public int getBronzes() {
return bronzes;
}
public void addBronze() {
bronzes++;
}
}
我先粘一个类,如果你觉得是你想要的,那请回复。再给你发其他的
package com.test.myOlympic;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
public class Athlete implements Serializable{
/**
* 运动员类
* @author Administrator
*/
private static final long serialVersionUID = 1L;
/**
* 运动员的名字
*/
private String name;
/**
* 运动员代表的国家
*/
private String country;
/**
* 获得金牌数
*/
private int golds;
/**
* 获得银牌数
*/
private int silvers;
/**
* 获得铜牌数
*/
private int bronzes;
/**
* 构造方法
* @param name 名字
* @param country 国家
*/
public Athlete(String name, String country) {
super();
this.name = name;
this.country = country;
}
/**
* 授予运动员一枚铜牌。
*/
public void addBronze() {
this.bronzes++;
}
/**
* 授予运动员一枚银牌。
*/
public void addSilver() {
this.silvers++;
}
/**
* 授予运动员一枚金牌牌。
*/
public void addGold() {
this.golds++;
}
/**
* 从文件创建一个新的运动员对象
* @param path 文件路径
* @return
*/
public static Athlete load(java.lang.String path) {
Athlete athlete=null;
File file = new File(path);
if(!file.exists()) { //文件不存在返回空
System.err.println("文件目录:"+path+"不存在,请先创建目录和文件");
return null;
}
if(!file.isFile()) {//如果不是文件返回空
System.err.println("文件:"+path+"不存在,请先创建文件");
return null;
}
//StringBuilder result = new StringBuilder();
String [] strLineArra=new String [5]; //构造一个字符串数组,把读文件的行数存到里面
try{
//构造一个BufferedReader类来读取文件
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String s = null;
int index=0;
while((s = br.readLine())!=null){ //使用readLine方法,一次读一行
strLineArra[index]=s;
index++;
if(index==4) { //不管这个文件有几行,只读前面5行
break;
}
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
if(strLineArra.length>0) {
athlete=new Athlete(strLineArra[0],strLineArra[1]);
if(strLineArra[2]!=null) { //判断空以防错误
athlete.setGolds(Integer.parseInt(strLineArra[2]));
}
if(strLineArra[3]!=null) { //判断空以防错误
athlete.setSilvers(Integer.parseInt(strLineArra[3]));
}
if(strLineArra[4]!=null) { //判断空以防错误
athlete.setBronzes(Integer.parseInt(strLineArra[4]));
}
}
return athlete;
}
/**
* 将给定的运动员对象写入文件
* @param athlete 运动员对象
* @param path 文件路径
*/
public static void save (Athlete athlete, java.lang.String path) {
File file = new File(path);
if (!file.exists()) { // 文件目录不存什么也不做
System.err.println("文件目录:"+path+"不存在,请先创建目录和文件");
return;
}
if (!file.isFile()) { // 不是文件什么也不做
System.err.println("文件:"+path.substring(path.lastIndexOf("/"),path.length())+"不存在,请先创建文件");
return;
}
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw= new FileWriter(file);
bw= new BufferedWriter(fw);
bw.write(athlete.getName()+"\t\n"+athlete.getCountry()+"\t\n"+athlete.getGolds()+"\t\n"+athlete.getSilvers()+"\t\n"+athlete.getBronzes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bw!=null) {//关闭文件流
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fw!=null) { //关闭文件流
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("运动员信息保存成功,请打开:"+path+"查看");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getGolds() {
return golds;
}
public void setGolds(int golds) {
this.golds = golds;
}
public int getSilvers() {
return silvers;
}
public void setSilvers(int silvers) {
this.silvers = silvers;
}
public int getBronzes() {
return bronzes;
}
public void setBronzes(int bronzes) {
this.bronzes = bronzes;
}
@Override
public String toString() {
return "Athlete [name=" + name + ", country=" + country + ", golds=" + golds + ", silvers=" + silvers
+ ", bronzes=" + bronzes + "]";
}
public static void main(String[] args) {
//测试1、读取文件,创建 Athlete 对象
Athlete athlete=Athlete.load("D://test/Athlete.txt");
System.out.println(athlete);
/**运行结果:Athlete [name=全禅红, country=中国, golds=1, silvers=0, bronzes=0]**/
//测试2、把运动员信息写到文件
Athlete.save(athlete, "D://test/quanhongchan.txt");
/**运行结果:运动员信息保存成功,请打开:D://test/quanhongchan.txt查看**/
}
}