This code should work.
This is the object Horse. I set the Time and the Number as string for convenience only.
Code:
import java****.PrintStream;
import java.util.Scanner;
public class Horse {
public Horse(String name, String num, String time) {
this.name = name;
this.num = num;
this.time = time;
}
public static Horse read(Scanner sc){
String name, num, time;
if(!sc.hasNext()) return null;
name = sc.nextLine();
if(!sc.hasNext()) return null;
num = sc.nextLine();
if(!sc.hasNext()) return null;
time = sc.nextLine();
return new Horse(name, num, time);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public void print (PrintStream ps){
ps.println(name);
ps.println(num);
ps.println(time);
}
private String name;
private String num;
private String time;
}
this is the part that handles the functions. You can add a new horse or edit an existent one, find the name by the number, but there is not the faster because I do not want to implement it
However, modify the Time in an int or you can use the java time class, your choice. To find the faster you have to save the first horse as the faster one, then compare with the second horse. If the second is faster the second is the new horse to compare with the other, else continue with the first.
Code:
import java****.File;
import java****.FileNotFoundException;
import java****.PrintStream;
import java****.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Gestore {
public Gestore() throws FileNotFoundException{
Scanner in = new Scanner(HORSE);
horses = new ArrayList<Horse>();
Horse scan = Horse.read(in);
while(scan != null){
horses.add(scan);
scan = Horse.read(in);
}
in.close();
}
public void printAll(PrintStream ps){
for(Horse h: horses){
h.print(ps);
}
}
public void newHorse() throws FileNotFoundException{
Horse tempHorse = new Horse(null, null, null);
Scanner in = new Scanner(System.in);
System.out.println("Put the name of the horse:");
tempHorse.setName(in.nextLine());
System.out.println("Put the number of the horse:");
tempHorse.setNum(in.nextLine());
System.out.println("Put the time of the horse:");
tempHorse.setTime(in.nextLine());
horses.add(tempHorse);
updateHorse();
in.close();
}
public void updateHorse() throws FileNotFoundException{
PrintWriter out = new PrintWriter(HORSE);
for (Horse h: horses){
out.println(h.getName());
out.println(h.getNum());
out.println(h.getTime());
}
out.close();
}
public Horse returnNameByNumber(String num){
boolean find = false;
int i = 0;
while(horses !=null && i <horses.size() && find == false){
if (horses.get(i).getNum().equalsIgnoreCase(num)){
find = true;
} else
i++;
}
if (find == true)
return horses.get(i);
else System.out.println("Not found");
return null;
}
private ArrayList<Horse>horses;
private static final File HORSE = new File("Horse.dati");
}
This is just a tester.
Code:
import java****.FileNotFoundException;
public class Tester {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Gestore gest = new Gestore();
gest.newHorse();
}
}
Don't forget to create the text file. Of course there are a lot of bug and error, i know