user2932587
user2932587

Reputation:

Printing Methods

I am trying to print the contents of a method (purchase(String isbn, double price, int copies)), but am having no luck. As the following code is written,

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());
    }

}

I get the compiler error

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

If I comment out the:

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

portion of the main() method, the program prints the array, but nothing else. I need the program to print the purchase method, including the updated array (which I still don't know how to do so any help with that would be appreciated as well).

Any suggestions as to how I can get this to work? I'd like to stick as close to the code I have written if I can, I've been working on this for the last several days but it's due in about an hour and a half so I'm running out of time. Thanks in advance.

Upvotes: 0

Views: 115

Answers (7)

Dragondraikk
Dragondraikk

Reputation: 1679

Your purchase Method uses a different signature than you are calling.

purchase requires String, double, int, Book[] as arguments, but you are trying to call it with String, double, int. Try adding a Book-Array.

On top of that, looking through purchase it looks like most of the arguments are not even being used. You should consider using those arguments or removing them.

PS: You may want to consider using an IDE like Eclipse, Netbeans, or IntelliJ IDEA to help catch such common mistakes :)

Upvotes: 1

Duncan Jones
Duncan Jones

Reputation: 69339

The compiler error is because you passed three arguments, when the method requires four. The following code would compile:

purchase(isbn, price, copies, books);

However, your code looks wrong in other ways, since you've assigned no values to isbn, price or copies. Your Bookclass already contains theses values, so you only need to supply an array of Book objects to your print method.

E.g. change your purchase method to:

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;
}

Some other notes:

  • Try a for-each loop, e.g. for (Book book : books) {
  • Consider using a currency type rather than a double.
  • Don't declare variables before you need them.

Upvotes: 3

Natraj
Natraj

Reputation: 1

You are passing 3 arguments for your purchase method as follows
purchase(isbn, price, copies);

But There are 4 parameters for purchase method as follows: public Book[] purchase(String isbn, double price, int copies, Book[] books)

So you need to pass 4th variable 'books'. Thats why you are getting compiler error.

Upvotes: 0

earthmover
earthmover

Reputation: 4525

You are not passing Book array in your method. Pass Book[] books as the last argument in the purchase() method like :

purchase(isbn, price, copies, books);

However, how do you call purchase() method in your main method, since it is non-static

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

Upvotes: 0

DavidPostill
DavidPostill

Reputation: 7921

You are calling:

purchase(isbn, price, copies);

Purchase() is defined as

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

Perhaps you meant to call:

purchase(isbn, price, copies, books);

Upvotes: 0

Jens
Jens

Reputation: 69440

In this line is a parameter missing:

purchase(isbn, price, copies);

Think you have to add books as parameter

purchase(isbn, price, copies, books);

should work.

Upvotes: 0

nobalG
nobalG

Reputation: 4620

You are passing wrong number of arguments in your purchase() method. You have declared it something like

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

i.e with four arguments,but you are passing only three while calling it i.e

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

Upvotes: 0

Related Questions