Repository navigation
Expand file tree
/
Copy pathPrinter.java
More file actions
112 lines (100 loc) · 2.96 KB
/
Copy pathPrinter.java
File metadata and controls
112 lines (100 loc) · 2.96 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
import java.util.Scanner;
public class Printer{
/* Fields ********************************************************************/
private int tonerLevel;
private int pagesPrinted;
private boolean isDuplex;
private Scanner myScanner;
/* Constructors **************************************************************/
public Printer(int tonerLevel, boolean isDuplex){
if(tonerLevel > -1){
this.tonerLevel = tonerLevel;
}
else{
this.tonerLevel = -1;
}
this.pagesPrinted = 0;
this. isDuplex = isDuplex;
this.myScanner = new Scanner(System.in);
}
public Printer(){
this(0, false);
}
/* Getters *******************************************************************/
public int getTonerLevel(){
return tonerLevel;
}
public int getPagesPrinted(){
return pagesPrinted;
}
public boolean getIsDuplex(){
return isDuplex;
}
/* Setters *******************************************************************/
public void setTonerLevel(int tonerLevel){
if(tonerLevel > -1 && tonerLevel <= 100 )
this.tonerLevel = tonerLevel;
else
this.tonerLevel = -1;
}
public void setPagesPrinted(int pagesPrinted){
if(pagesPrinted >= 0)
this.pagesPrinted = pagesPrinted;
else
this.pagesPrinted = 0;
}
public void setIsDuplex(boolean isDuplex){
this.isDuplex = isDuplex;
}
/* Methods *******************************************************************/
public int fillToner(int fillAmount){
if(fillAmount < 0){ //try to add negative amount of toner
System.out.println("Sorry, you cannot add a negative amount of toner to the printer.");
return -1;
}
if(tonerLevel == 100){ //toner already full
System.out.println("Toner already full.");
return 0;
}
else{ //adding toner amount between 0 and 100
tonerLevel += fillAmount;
if(tonerLevel >= 100){
tonerLevel = 100;
System.out.println("Toner full.");
}
return 0;
}
}
/* For the sake of simplicity I have assumed that each page requires 1% of the toner to print. *************************************************************/
public int printPages(int pages){
if(pages < 1){
System.out.println("No pages printed.");
return -1;
}
else if( (tonerLevel - pages) >= 0){
char answer = 'n';
if(isDuplex){
System.out.println("Would you like to print double sided: y/n ");
answer = myScanner.next().charAt(0);
}
if(answer == 'y'){
System.out.println("Printing double sided.");
pagesPrinted += ( (pages/2) + (pages%2) ); //since printing on both sides
tonerLevel -= pages;
System.out.println("Printing " + pages + " pages on " + (pages/2) + " sheets of paper.");
return ( (pages/2) + (pages%2) );
}
else{
System.out.println("Printing single sided.");
pagesPrinted += pages;
tonerLevel -= pages;
System.out.println("Printing " + pages + " pages on " + pages + " sheets of paper.");
return pages;
}
}
else{
System.out.println("Toner low, please refill and try again.");
return -1;
}
}
}