-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoop_example
More file actions
126 lines (95 loc) · 3.79 KB
/
oop_example
File metadata and controls
126 lines (95 loc) · 3.79 KB
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
121
122
123
124
125
126
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
class Main {
public static void Main(String[] args) {
User youngerUser = new User("Carlos", "0000-00-00");
// youngerUser.name = "Carlos";
// youngerUser.birthday = LocalDate.parse("0000-00-00");
// these methods were given values without a constructor in the User class
User olderUser = new User("Johnathan Wick", "1964-09-02");
// olderUser.name = "Johnathan Wick";
// olderUser.birthday = LocalDate.parse("1964-09-02");
Book harryPotter = new Book("Harry Potter", "J.K. Rowling", 223);
// book.title = "Harry Potter";
// book.author = "J.K. Rowling";
AudioBook dracula = new AudioBook("Dracula", "Bram Stoker", 30000);
Ebook jeeves = new Ebook("Carry On Jeeves", "P.G. Wodehouse", 280, "PDF");
youngerUser.borrow(harryPotter);
// System.out.printf("%s was born in %s. His age is %d years old", youngerUser.name, youngerUser.birthday.toString(), youngerUser.age());
System.out.printf("%s was born in %s. His age is %d years old", youngerUser.getName(), youngerUser.getBirthday(), youngerUser.age()); // values will be retrieved using the new methods
// example output: Carlos was born in 0000-00-00. His age is (date difference) years old
// System.out.printf("%s was born in %s. His age is %d years old", olderUser.name, olderUser.birthday.toString(), olderUser.age());
System.out.printf("%s was born in %s. His age is %d years old", olderUser.getName(), olderUser.getBirthday(), olderUser.age());
// example output: Johnathan Wick was born in 1964-09-02. His age is 59 years old // 2023 as of this example
// System.out.printf("%s has borrowed these books: %s", youngerUser.name, youngerUser.books.toString());
System.out.printf("%s has borrowed these books: %s", youngerUser.name, youngerUser.borrowedBooks());
// example output: Carlos has borrowed these books: [Harry Potter by J.K. Rowling]
System.out.println(dracula.toString());
// example output: Dracula by Bram Stoker
// AudioBook is inheriting the toString method from Book
System.out.println(jeeves.toString());
// example output: Carry On Jeeves by P.G. Wodehouse
}
}
public class User {
private String name;
// setting this to private requires a getter to retrieve the value
// private also prevents changes to this variable outside the User class
// public String name;
private LocalDate birthday;
private ArrayList<Book> books = new ArrayList<Book>();
User(String name, String birthday) {
this.name = name;
this.birthday = LocalDate.parse(birthday);
}
public String getName() {
return this.name;
}
public String getBirthday() {
return this.birthday.toString();
}
public String borrowedBooks() {
return this.books.toString();
}
public void borrow(Book book) {
this.books.add(book);
}
public int age(){
int age = Period.between(this.birthday, LocalDate.now());
return age.getYears();
}
}
public class Book {
private String title;
private String author;
private int pageCount;
Book(String title, String author, int pageCount) {
this.title = title;
this.author = author;
this.pageCount = pageCount;
}
public String getTitle() {
return this.title;
}
public String getAuthor() {
return this.author;
}
public String toString() {
return String.format("%s by %s", this.title, this.author);
}
}
public class AudioBook extends Book {
private int runTime;
AudioBook(String title, String author, int runTime) {
super(title, author, 0);
this.runTime = runTime;
}
}
public class Ebook extends Book {
private String format;
Ebook(String title, String author, int pageCount, String format) {
super(title, author, pageCount);
this.format = format;
}
}