ENCAPSULATION CHALLENGE

package com.cnc;

public class Main {

public static void main(String[] args) {
Printer printer = new Printer(50,true);
System.out.println("Initial Page count is "+ printer.getNumberOfPages());

int pagesPrinted = printer.getNumberOfPages(4);
}
}

package com.cnc;

public class Printer {
private int tonerLevel = 100;
private int numberOfPages;
private boolean isDuplex;

public Printer(int tonerLevel, boolean isDuplex) {
if(tonerLevel>=-1 && tonerLevel<=100) {
this.tonerLevel = tonerLevel;
}else{
this.tonerLevel=-1;
}
this.isDuplex = isDuplex;
this.numberOfPages = 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.isDuplex){
pagesToPrint=(pages/2)+(pages%2);
System.out.println("Printing in duplex mode");
}
this.numberOfPages += pagesToPrint;
return pagesToPrint;
}


public Printer(int numberOfPages) {
this.numberOfPages = numberOfPages;

}
public int getNumberOfPages() {
return numberOfPages;
}

}

} 

Popular posts from this blog

NUMBER PALINDROME CHALLENGE

LARGEST PRIME FACTOR OF A NUMBER