-
Notifications
You must be signed in to change notification settings - Fork 0
/
Book.java
120 lines (99 loc) · 2.54 KB
/
Book.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
*Book.java
*
*/
public class Book{
/* fields for the Book class */
private String title;
private String author;
private String publisher;
private int year;
private int edition;
private String isbn;
private double price;
private int stock;
private String format;
/* constructor function */
Book(String title, String author, String publisher, int year, int
edition, String isbn, double price, String format){
this.title = title;
this.author = author;
this.publisher = publisher;
this.year = year;
this.edition = edition;
this.isbn = isbn;
this.price = price;
this.stock = 0;
this.format = format;
}
/* copy constructor, use another book to initialize a new book (deep copies another Book to this Book) */
public Book(Book another) {
this.title = another.title;
this.author = another.author;
this.publisher = another.publisher;
this.year = another.year;
this.edition = another.edition;
this.isbn = another.isbn;
this.price = another.price;
this.stock = another.stock;
this.format = another.format;
}
/* return book title */
public String getTitle(){
return title;
}
/* return book author */
public String getAuthor(){
return author;
}
/* return book publisher */
public String getPublisher(){
return publisher;
}
/* return book year of publication */
public int getPublishYear(){
return year;
}
/* return book edition */
public int getEdition(){
return edition;
}
/* return book International Standard Book Number (ISBN) */
public String getIsbn(){
return isbn;
}
/* return book price */
public double getPrice(){
return price;
}
/* return book stock on hand */
public int getStock(){
return stock;
}
/* return book format */
public String getFormat(){
return format;
}
/* return book stock value */
public double getStockValue(){
return stock * price;
}
/* return a formated string representation of the book details */
public String toString(){
String s = String.format("%-30s %-30s %-30s %-4d %-10s %5.2f %-2d %-10s",
title, author, publisher, year, isbn, price, stock, format);
return s;
}
/* sets the price of the book to a new price */
public void setPrice(double newPrice){
price = newPrice;
}
/* change the stock level of the book */
public void changeStock(int change){
if( (stock + change) < 0 ){
throw new UnsupportedOperationException("BookStockError");
} else {
stock += change;
}
}
}