-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38_Encapsulation.java
More file actions
42 lines (37 loc) · 1.1 KB
/
38_Encapsulation.java
File metadata and controls
42 lines (37 loc) · 1.1 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
public class Printer {
private int tonerLevel;
private int pagesPrinted;
private boolean duplex;
public Printer(int tonerLevel, boolean duplex) {
if(tonerLevel >-1 && tonerLevel <=100) {
this.tonerLevel = tonerLevel;
} else {
this.tonerLevel = -1;
}
this.duplex = duplex;
this.pagesPrinted = 0;
}
public int addToner(int tonerAmount) {
if(tonerAmount >0 && tonerAmount <=100) {
if(this.tonerLevel + tonerAmount >100) {
return -1;
}
this.tonerLevel += tonerAmount;
return this.tonerLevel;
} else {
return -1;
}
}
public int printPages(int pages) {
int pagesToPrint = pages;
if(this.duplex) {
pagesToPrint = (pages / 2) + (pages % 2);
System.out.println("Printing in duplex mode");
}
this.pagesPrinted += pagesToPrint;
return pagesToPrint;
}
public int getPagesPrinted() {
return pagesPrinted;
}
}