patternjavaModerate
Simulating a printer
Viewed 0 times
printersimulatingstackoverflow
Problem
I'm trying to recreate a printer in Java. I'm fairly new to programming so I'm using huge
I was wondering if there was a more eloquent/efficient way of doing this
Logic for the printer isn't too important, but just to show anyway: one is a double sided printer one isn't, and logic is in charge of checking toner levels and making sure pages printed are in line with printer being double sided or not.
if/else blocks inside a single function to dictate the logic of the program. I'm noticing this is creating a mass of code inside the same function.I was wondering if there was a more eloquent/efficient way of doing this
Printer class.Logic for the printer isn't too important, but just to show anyway: one is a double sided printer one isn't, and logic is in charge of checking toner levels and making sure pages printed are in line with printer being double sided or not.
package com.company;
public class Printer {
private String name;
private double tonerLevel = 100;
private int ammountOfPaper;
private int numberOfPagesPrinted;
private boolean isDoubleSided;
public Printer(String name, double tonerLevel, int ammountOfPaper, boolean isDoubleSided) {
this.name = name;
if(tonerLevel >= 0 && tonerLevel 50) {
System.out.println("Cannot put in more paper");
}
else {
this.ammountOfPaper += paper;
}
}
public int getAmmountOfPaper() {
return ammountOfPaper;
}
public double getTonerLevel() {
return tonerLevel;
}
public void setTonerLevel(double tonerLevel) {
this.tonerLevel = tonerLevel;
}
public void setAmmountOfPaper(int ammountOfPaper) {
this.ammountOfPaper = ammountOfPaper;
}Solution
There are several things I would do to increase readability of your code.
The first would be to create methods that query some field. It will allow the code to be more human readable and self documenting.
So for example you would have methods such as:
These would replace
The second would be to move the functionality of how the printer handles each bad event (ie. no paper, no toner) into their own methods.
so for example you would have methods:
The code in your main method will now be much easier to read and understand when it looks like this:
(As I'm writing this I noticed you do have
The 3rd thing you could do is to utilize inheritance and polymorphism by creating subclasses of Printer and making the Printer class abstract.
So for example you would have:
This eliminates some of the checking needed to see if the printer is double sided.
The first would be to create methods that query some field. It will allow the code to be more human readable and self documenting.
So for example you would have methods such as:
private boolean isOutOfPaper() {
return amountOfPaper == 0;
}
private boolean isOutOfToner() {
return tonerLevel == 0;
}These would replace
if(amountOfPaper == 0) with if(isOutOfPaper()) and if(tonerLevel == 0) with if(isOutOfToner())The second would be to move the functionality of how the printer handles each bad event (ie. no paper, no toner) into their own methods.
so for example you would have methods:
private void handleNoPaper() {
System.out.println("Out of Paper");
}
private void handleNoToner() {
System.out.println("Out of Toner");
}The code in your main method will now be much easier to read and understand when it looks like this:
if(isOutofToner()) {
handleNoToner();
}
if(isOutOfPaper()) {
handleNoPaper();
}(As I'm writing this I noticed you do have
isOutOfPaper(int numOfPages) method. However that method name is a little misleading because it is checking if there is enough paper to print the current job rather than if it is out of paper now. I would rename that method to hasEnoughPaperToPrint(int numOfPages) and of course the same applies to your current isOutOfToner(int numOfPages) method)The 3rd thing you could do is to utilize inheritance and polymorphism by creating subclasses of Printer and making the Printer class abstract.
So for example you would have:
public abstract class Printer {
private String name;
private double tonerLevel = 100;
private int amountOfPaper;
private int numberOfPagesPrinted;
public abstract void printPages();
//rest of class ommitted
}
public class DoubleSidedPrinter extends Printer {
public void printPages() {
//print as double sided. No check necessary.
}
}
public class OneSidedPrinter extends Printer {
public void printPages() {
//print as one sided. No check necessary.
}
}This eliminates some of the checking needed to see if the printer is double sided.
Code Snippets
private boolean isOutOfPaper() {
return amountOfPaper == 0;
}
private boolean isOutOfToner() {
return tonerLevel == 0;
}private void handleNoPaper() {
System.out.println("Out of Paper");
}
private void handleNoToner() {
System.out.println("Out of Toner");
}if(isOutofToner()) {
handleNoToner();
}
if(isOutOfPaper()) {
handleNoPaper();
}public abstract class Printer {
private String name;
private double tonerLevel = 100;
private int amountOfPaper;
private int numberOfPagesPrinted;
public abstract void printPages();
//rest of class ommitted
}
public class DoubleSidedPrinter extends Printer {
public void printPages() {
//print as double sided. No check necessary.
}
}
public class OneSidedPrinter extends Printer {
public void printPages() {
//print as one sided. No check necessary.
}
}Context
StackExchange Code Review Q#138774, answer score: 13
Revisions (0)
No revisions yet.