Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 void printInfo(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());
- }
- }
- class Book {
- private String isbn;
- private double price;
- private int copies;
- 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());
- }
- }
- public Book[] purchase(String isbn, double price, int copies) {
- int itemsSold;
- double totalMade;
- Scanner input = new Scanner(System.in);
- System.out.println("Please enter the ISBN number of the book you would like to purchase: ");
- desiredISBN = input.next(); // not sure if this would work since I think I'd need the whole book object that contains that ISBN?
- // search array for book object containing this ISBN number
- // if (found)
- System.out.println("How many copies would you like to purchase?: ");
- desiredCopies = input.nextInt();
- if(copies == 0)
- System.out.println("I'm sorry, but that book is currently out of stock.");
- if(desiredCopies > copies) // copies is the number of copies in the inventory
- System.out.println("I'm sorry, but we only have" + copies " copies of that book in our inventory. How many would you like to purchase?: ");
- desiredCopies = input.nextInt();
- itemsSold++;
- price +=;
- System.out.println("Enter ISBN");
- isbn = input.next();
- // search array for this ISBN?
- // else if (not found)
- System.out.println("I'm sorry, we do not have that item in our store.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement