在Java中搜索对象数组

时间:2014-07-23 11:22:47

标签: java arrays object

我正在制作一个应该充当书店库存的程序。有两个类(Store和Book),每个类都有多种方法。该程序应该在一个包含10本书的文件中读取(每行是一本书,包含ISBN,价格和库存副本),如下所示:

1234567 31.67 0
1234444 98.50 4
1235555 27.89 2
1235566 102.39 6
1240000 75.65 4
1247761 19.95 12
1248898 155.91 0
1356114 6.95 17
1698304 45.95 3
281982X 31.90 5

该文件将存储在Book []类型的数组中。

在读入文件,存储到数组并关闭文件后,程序将提示用户输入他们想要购买的图书的ISBN号,然后按照以下步骤操作:< / p>

1.Ask the user to enter the ISBN number of the book they'd like to purchase.
2.Search the array for the object that contains that ISBN.
3.If the ISBN isn't found in the array, display a message stating that we don't have that book.
4.If the ISBN is found but the number of copies is 0, display a message saying the book is out of stock.
5.If the ISBN is found and the number of copies is greater than 0, ask the user how many copies they'd like to purchase.
6.If the number they enter is greater than the number of copies of that book in the array, display a message stating that and ask them to enter another quantity.
7.Once the purchase is complete I need to update the array by subtracting the number of copies of that particular book that was purchased.
8.Print the updated array.
9.Display a count of how many books were purchased, and how much money was made from the purchase.

更新 谢谢你们这些家伙&#39;帮助我能够摆脱原始的编译器错误,但是,通常情况下,清除那些带来更多的错误。我能够纠正他们中的大部分,但剩下3个让我难过。以下代码是最新的,反映了自原始帖子以来我所做的更改。

以下是我提出的代码:

import java.util.Scanner; 
import java.io.*; 

public class Store { 
    public static void main(String[] args) throws Exception {
    Book[] books = readInventory();

        for (Book book : books) {
            System.out.printf("ISBN: %s, Price: %f, Copies: %d%n", book.getISBN(), book.getPrice(), book.getCopies()); 
        }
    }

    public static Book[] readInventory() throws Exception {
        Book[] books = new Book[10];
        java.io.File file = new java.io.File("../instr/prog4.dat");
        Scanner fin = new Scanner(file);
        String isbn;
        double price;
        int copies;
        int i = 0;

        while (fin.hasNext()) {
            isbn = fin.next();
                if (fin.hasNextDouble()); {
                    price = fin.nextDouble();
                }
                if (fin.hasNextInt()); {
                    copies = fin.nextInt();
                }
             Book book = new Book(isbn, price, copies);
             books[i] = book;
             i++;
        }
        fin.close();
        return books;
 }

    public Book[] purchase(String isbn, double price, int copies, Book[] books) {
        int itemsSold;
        double totalMade;
        Scanner input = new Scanner(System.in);
        int desiredCopies;

        System.out.println("Please enter the ISBN number of the book you would like to purchase: ");
            String desiredIsbn = input.next();
            for(int index = 0; index < books.length; index++) {
                if(books[index] != desiredIsbn)
                    System.out.println("We do not have that book in our inventory.");
                if(books[index] == desiredIsbn && copies == 0)
                    System.out.println("That book is currently out of stock.");
                if(books[index] == desiredIsbn && copies > 0) {
                    System.out.println("How many copies of this book would you like to purchase?"); 
                        desiredCopies = input.nextInt(); }
                        if(desiredCopies > copies)
                            System.out.println("We only have " + copies + "in stock. Please select another quantity: ");
                            desiredCopies = input.nextInt();
                        // copies = copies - desiredCopies
                        double total = price * desiredCopies;
                    System.out.println("Thank you for your purchase, your order total is: $" + total);
                    itemsSold += desiredCopies;
                    totalMade += total;
                    // update array
                    System.out.print(books[index]);
                    System.out.println("We sold " + itemsSold + " today.");
                    System.out.println("We made $" + totalMade + "today.");
            }   
        return books;
        }

    public void displayInfo(Book[] books) {
        for(int x=0; x<books.length; x++) {
             System.out.println("ISBN: " + books[x].getISBN() + "\n Price: " +
                books[x].getPrice() + "\n Copies: " + books[x].getCopies());
        System.out.print(books[x]);
        }
    }
}

class Book {
 private String isbn;
 private double price;
 private int copies;

 public Book() {
 }

 public Book(String isbnNum, double priceOfBook, int copiesInStock) {
  isbn = isbnNum;
  price = priceOfBook; 
  copies = copiesInStock;
 }

 public String getISBN() {
  return isbn;
 }

 public double getPrice() {
  return price;
 }

 public int getCopies() {
  return copies;
 }

 public void setISBN(String isbn) {
  this.isbn = isbn;
 }

 public void setPrice(double price) {
  this.price = price;
 }

 public void setCopies(int copies) {
  this.copies = copies;
 }

   @Override
    public String toString() {
        return String.format("ISBN: %s, Price: %f, Copies: %d%n",
            this.getISBN(), this.getPrice(), this.getCopies());
    }

}

编写此代码时,我遇到3个编译器错误:

    Store.java:54: incomparable types: Book and java.lang.String
                                if(books[index] != desiredIsbn)
                                                ^
Store.java:56: incomparable types: Book and java.lang.String
                                if(books[index] == desiredIsbn && copies == 0)
                                                ^
Store.java:58: incomparable types: Book and java.lang.String
                                if(books[index] == desiredIsbn && copies > 0) {
                                                ^
3 errors

我无法弄清楚如何纠正这些错误,以便我可以编译它,看看它是否有效。我在过去的几天里一直在研究它,它只需要几个小时。因此,如果有人看到我需要解决的问题,请告诉我,如果可能的话,我希望尽可能贴近我编写的代码,因为就像我说的那样,我已经不在了时间任何建议都非常感谢。

3 个答案:

答案 0 :(得分:6)

您的购买方式中存在语法错误。您缺少结束},这就是无法正确解析后续方法的原因。

我会以更好的缩进格式化您的问题,因此错误可能会变得明显。

与问题更新对应的更新:

您正在将图书books[index]String进行比较,这些图片是不兼容的类型。你可能想要的是books[index].getISBN()。另外,将String与equals进行比较,而不是=,而不是

books[index] != desiredIsbn

你会想写

!books[index].getISBN().equals(desiredIsbn)

答案 1 :(得分:2)

你缺少几个开/关括号..  if(books[index] == desiredIsbn && copies > 0) 此外,for循环应该在返回之前结束。

答案 2 :(得分:1)

你应该检查结束括号。这将导致语法错误。