Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class Store {
  5. public static void main(String[] args) throws Exception {
  6. Book[] books = readInventory();
  7.  
  8. for (Book book : books) {
  9. System.out.printf("ISBN: %s, Price: %f, Copies: %d%n", book.getISBN(), book.getPrice(), book.getCopies());
  10. }
  11. }
  12.  
  13. public static Book[] readInventory() throws Exception {
  14. Book[] books = new Book[10];
  15. java.io.File file = new java.io.File("../instr/prog4.dat");
  16. Scanner fin = new Scanner(file);
  17. String isbn;
  18. double price;
  19. int copies;
  20. int i = 0;
  21.  
  22. while (fin.hasNext()) {
  23. isbn = fin.next();
  24. if (fin.hasNextDouble()); {
  25. price = fin.nextDouble();
  26. }
  27. if (fin.hasNextInt()); {
  28. copies = fin.nextInt();
  29. }
  30. Book book = new Book(isbn, price, copies);
  31. books[i] = book;
  32. i++;
  33. }
  34. fin.close();
  35. return books;
  36. }
  37.  
  38. public void printInfo(Book[] books) {
  39. for(int x=0; x<books.length; x++)
  40. System.out.println("ISBN: " + books[x].getISBN() + "n Price: " +
  41. books[x].getPrice() + "n Copies: " + books[x].getCopies());
  42. }
  43. }
  44.  
  45. class Book {
  46. private String isbn;
  47. private double price;
  48. private int copies;
  49.  
  50. public Book(String isbnNum, double priceOfBook, int copiesInStock) {
  51. isbn = isbnNum;
  52. price = priceOfBook;
  53. copies = copiesInStock;
  54. }
  55.  
  56. public String getISBN() {
  57. return isbn;
  58. }
  59.  
  60. public double getPrice() {
  61. return price;
  62. }
  63.  
  64. public int getCopies() {
  65. return copies;
  66. }
  67.  
  68. public void setISBN(String isbn) {
  69. this.isbn = isbn;
  70. }
  71.  
  72. public void setPrice(double price) {
  73. this.price = price;
  74. }
  75.  
  76. public void setCopies(int copies) {
  77. this.copies = copies;
  78. }
  79.  
  80. @Override
  81. public String toString() {
  82. return String.format("ISBN: %s, Price: %f, Copies: %d%n",
  83. this.getISBN(), this.getPrice(), this.getCopies());
  84. }
  85.  
  86. }
  87.  
  88. public Book[] purchase(String isbn, double price, int copies) {
  89. int itemsSold;
  90. double totalMade;
  91. Scanner input = new Scanner(System.in);
  92.  
  93. System.out.println("Please enter the ISBN number of the book you would like to purchase: ");
  94. desiredISBN = input.next(); // not sure if this would work since I think I'd need the whole book object that contains that ISBN?
  95. // search array for book object containing this ISBN number
  96. // if (found)
  97. System.out.println("How many copies would you like to purchase?: ");
  98. desiredCopies = input.nextInt();
  99. if(copies == 0)
  100. System.out.println("I'm sorry, but that book is currently out of stock.");
  101. if(desiredCopies > copies) // copies is the number of copies in the inventory
  102. 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?: ");
  103. desiredCopies = input.nextInt();
  104. itemsSold++;
  105. price +=;
  106.  
  107. System.out.println("Enter ISBN");
  108. isbn = input.next();
  109. // search array for this ISBN?
  110.  
  111. // else if (not found)
  112. 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