简体   繁体   中英

ArrayList reading lines from files and assigning them

I have created a code that reads from a file, every line is a different book, and each line contains a title and a price. The books file is:

    The Hobbit,$15.00
    Purple Cow,$10.00
    Death by Black Hole,$35.00

The methods work. But I'm trying to create a method that works like: Enter a budget: 15 and then shsows all the boks under 15 dollars. I've done a part of the searchForPrice method but It doesn't really work out. How could I do it? Thanks.

import java.util.*;
import java.io.*;
import javax.swing.*;
public class BookStore {
    private ArrayList<Book> library;
    String Book;

    public BookStore() throws FileNotFoundException {
        library = readBooks("books1.txt");
    }
    public ArrayList<Book> readBooks(String path) {
        ArrayList<Book> books = new ArrayList<Book>();

        try{
            BufferedReader br = new BufferedReader(new FileReader(path));
            String line;
            while ((line = br.readLine()) != null) {
                String[] arr = line.split(",");
                String title = arr[0];
                double price = Double.parseDouble(arr[1].trim().replace("$",""));//in case has a $
                books.add(new Book(title,price));

            }
            br.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return books;
    }

    public String toString() {
        String result = "";
        for (Book tempBook : library) {
            result += tempBook.toString() + "\n";
        }
        return result;
    }

    public ArrayList<Book> searchForTitle(String searchString) {
        ArrayList<Book> searchResult = new ArrayList<Book>();
        for (Book currentBook : library) {
            if ((currentBook.getTitle()).indexOf(searchString) != -1)
                searchResult.add(currentBook);
        }
        searchResult.trimToSize();
        return searchResult;
    }

    //get lowest price book
    public Book lowestPrice() {
        Book lowestPrice = library.get(0);
        for (Book book : library) {
            if (book.getPrice() < lowestPrice.getPrice()) {
                lowestPrice = book;
            }
        }
        return lowestPrice;
    }

    //bookslessthanthenumbertheyenter
    public ArrayList<Book> searchForPrice(double searchdouble) {
        ArrayList<Book> searchPrice = new ArrayList<Book>();
        for (Book currentBook : library) {
            if ((currentBook.getPrice()).Double.parseDouble.indexOf(searchdouble) != -1)
                searchPrice.add(currentBook);
        }
        searchPrice.trimToSize();
        return searchPrice;
    }
}

Does this loop even compile:

for (Book currentBook : library) {
    if ((currentBook.getPrice()).Double.parseDouble.indexOf(searchdouble) != -1)
        searchPrice.add(currentBook);
}

In the Book class you store the price as double , so no need for conversion, just compare the price of each book with searchdouble :

for (Book currentBook : library) {
    if (currentBook.getPrice() < searchdouble)
        searchPrice.add(currentBook);
}

Also why do you need:

searchPrice.trimToSize();

since you did not declare any initial capacity?

In Java the best way to compare Doubles would be with the function provided by the Double class compare .

So the code would look like this.

if (Double.compare(currentBook.getPrice(), searchdouble) <= 0)

The function compare will return 0 if they are both equal, -1 if the first is lower and 1 if it's higher.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM