印刷方法

时间:2014-07-23 12:31:06

标签: java arrays methods

我正在尝试打印方法的内容(购买(String isbn,double price,int copies)),但我没有运气。如下面的代码所示,

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()); 
        }
    String isbn;
    double price;
    int copies;
    purchase(isbn, price, copies);

    }

    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 static Book[] purchase(String isbn, double price, int copies, Book[] books) {
        int itemsSold = 0;
        double totalMade = 0;
        Scanner input = new Scanner(System.in);
        int desiredCopies = 0;

        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].getISBN().equals(desiredIsbn))
                    System.out.println("We do not have that book in our inventory.");
                if(books[index].getISBN().equals(desiredIsbn) && copies == 0)
                    System.out.println("That book is currently out of stock.");
                if(books[index].getISBN().equals(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());
    }

}

我收到编译错误

Store.java:21: purchase(java.lang.String,double,int,Book[]) in Store cannot be applied to (java.lang.String,double,int)
        purchase(isbn, price, copies);
        ^
1 error

如果我发表评论:

String isbn;
double price;
int copies;
purchase(isbn, price, copies);

main()方法的一部分,程序打印数组,但没有别的。我需要程序来打印购买方法,包括更新的数组(我仍然不知道如何做,所以任何帮助也将受到赞赏)。

有关如何使其工作的任何建议?如果可以的话,我想尽可能贴近我写的代码,过去几天我一直在研究这个问题,但是大约需要一个半小时,所以我已经没时间了。提前致谢。

7 个答案:

答案 0 :(得分:4)

编译器错误是因为您传递了三个参数,当方法需要四个时。以下代码将编译:

purchase(isbn, price, copies, books);

但是,您的代码在其他方面看起来有误,因为您没有为isbnpricecopies分配任何值。您的Book类已经包含这些值,因此您只需要为打印方法提供一组Book个对象。

E.g。将您的购买方式更改为:

public static Book[] purchase(Book[] books) {
  int itemsSold = 0;
  double totalMade = 0;
  Scanner input = new Scanner(System.in);
  int desiredCopies = 0;

  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].getISBN().equals(desiredIsbn))
      System.out.println("We do not have that book in our inventory.");
    if (books[index].getISBN().equals(desiredIsbn) && books[index].getCopies() == 0)
      System.out.println("That book is currently out of stock.");
    if (books[index].getISBN().equals(desiredIsbn) && books[index].getCopies() > 0) {
      System.out
          .println("How many copies of this book would you like to purchase?");
      desiredCopies = input.nextInt();
    }
    if (desiredCopies > books[index].getCopies())
      System.out.println("We only have " + books[index].getCopies()
          + "in stock. Please select another quantity: ");
    desiredCopies = input.nextInt();
    // copies = copies - desiredCopies
    double total = books[index].getPrice() * 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;
}

其他一些说明:

  • 尝试for-each循环,例如for (Book book : books) {
  • 请考虑使用货币类型而不是double
  • 在需要之前不要声明变量。

答案 1 :(得分:2)

您的purchase方法使用的签名与您呼叫的签名不同。

purchase需要String, double, int, Book[]作为参数,但您尝试使用String, double, int调用它。尝试添加Book-Array。

最重要的是,通过purchase查看大多数参数甚至都没有被使用。您应该考虑使用这些参数或删除它们。

PS:你可能想考虑使用像Eclipse,Netbeans或IntelliJ IDEA这样的IDE来帮助解决这些常见错误:)

答案 2 :(得分:1)

您在purchase()方法中传递的参数数量错误。 你宣布它像

  

public Book[] purchase(String isbn, double price, int copies, Book[] books)//taking four arguments

即有四个参数,但你在调用它时只传递了三个,即

  

purchase(isbn, price, copies);//but you are calling it by passing only three parameters reason for the error

答案 3 :(得分:1)

在这一行中缺少一个参数:

purchase(isbn, price, copies);

认为你必须添加书籍作为参数

purchase(isbn, price, copies, books);

应该有用。

答案 4 :(得分:1)

您正在致电:

purchase(isbn, price, copies);

Purchase()定义为

public Book[] purchase(String isbn, double price, int copies, Book[] books)

也许你打算打电话:

purchase(isbn, price, copies, books);

答案 5 :(得分:1)

您没有在方法中传递Book数组。将Book[] books作为purchase()方法中的最后一个参数传递,如:

purchase(isbn, price, copies, books);

但是,如何在主方法中调用purchase()方法,因为它是non-static

public Book[] purchase(String isbn, double price, int copies, Book[] books) {....}

答案 6 :(得分:1)

您正在为您的购买方式传递3个参数,如下所示  购买(isbn,price,copies);

但购买方法有4个参数如下: public Book []购买(String isbn,double price,int copies,Book [] books)

所以你需要传递第四个变量&#39;书籍&#39;。这就是为什么你会遇到编译错误。