#Beginner - How to read an infile?

4 messages · Page 1 of 1 (latest)

untold mulch
#
import java.util.Objects;
public class AvocadoEntry {
    private String type;
    private double averagePrice;
    private double totalSold;
    private String region;
    private int year;
    public AvocadoEntry(String type, double averagePrice, double totalSold, String region, int year) {
        this.type = type;
        this.averagePrice = averagePrice;
        this.totalSold = totalSold;
        this.region = region;
        this.year = year;
    }
    public String getType() {
        return type;
    }
    public double getAveragePrice() {
        return averagePrice;
    }
    public double getTotalSold() {
        return totalSold;
    }
    public String getRegion() {
        return region;
    }
    public int getYear() {
        return year;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AvocadoEntry)) return false;
        AvocadoEntry that = (AvocadoEntry) o;
        return Double.compare(that.getAveragePrice(), getAveragePrice()) == 0 &&
                Double.compare(that.getTotalSold(), getTotalSold()) == 0 &&
                getYear() == that.getYear() &&
                Objects.equals(getType(), that.getType()) &&
                Objects.equals(getRegion(), that.getRegion());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getType(), getAveragePrice(), getTotalSold(), getRegion(), getYear());
    }
}

input:13,2017-10-01,1.59,3423.95,31.2,150.24,0.0,3242.51,3242.51,0.0,0.0,organic,2017,Albany

Reader

public List<AvocadoEntry> read() {
        Path path = Path.of(contentRootPath);
        try {
            return Files.lines(path)
                    .map(line -> line.split(","))
                    .map(List::of)
                    .map(toAvocadoEntry())
                    .collect(toList());
        } catch (IOException e) {
            return new ArrayList<>();
        }
    }
#

i dont like it when you just copy paste it so here is a different thing so you can learn from it

real shuttle
#

this is quite confusing to me as im very much a beginner

#

could you simplify it more in anyway?