I'm trying to recreate a printer in Java. I'm fairly new to programming so I'm using huge 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 <= 100) {        this.tonerLevel = tonerLevel;    }    this.ammountOfPaper = ammountOfPaper;    this.isDoubleSided = isDoubleSided;}private boolean isOutOfToner(double numberToPrint) {    if((tonerLevel - (numberToPrint / 2) < 0)) {        return true;    }    else {        return false;    }}private boolean isOutOfPaper(double numberToPrint) {    if(((ammountOfPaper - numberToPrint) < 0)) {        return true;    }    else {        return false;    }}private boolean twoSideNoPaperEven(double numberToPrint) {    if((ammountOfPaper - ((int) numberToPrint / 2)) < 0 ) {        return true;    }    else {        return false;    }}private boolean twoSideNoPaperOdd(double numberToPrint) {    if(((ammountOfPaper - ((int) numberToPrint / 2)) - 1) < 0) {        return true;    }    else {        return false;    }}public void printPages(double numberToPrint) {    if(isDoubleSided == false) {        if(tonerLevel == 0) {            System.out.println(Out of toner);        }        if(ammountOfPaper == 0) {            System.out.println(Out of Paper);        }        if(isOutOfToner(numberToPrint) && (tonerLevel != 0)) {            double difference = tonerLevel * 2;            numberToPrint = difference;            ammountOfPaper -= numberToPrint;            System.out.println(Will run out of toner after this print, able to print  + (int) numberToPrint +                     pages);            tonerLevel = 0;        }        if(isOutOfPaper(numberToPrint) && (ammountOfPaper != 0)) {            double different = ammountOfPaper - numberToPrint;            numberToPrint = numberToPrint + different;            System.out.println(Will run out of paper after this print, printing  + (int) numberToPrint +  pages);            ammountOfPaper = 0;        }        else if(!isOutOfToner(numberToPrint) && (!isOutOfPaper(numberToPrint))) {            ammountOfPaper -= numberToPrint;            tonerLevel = tonerLevel - (numberToPrint / 2);            showPages(numberToPrint);        }    }    else if(isDoubleSided = true) {            if (numberToPrint % 2 == 0) {                if(tonerLevel == 0) {                    System.out.println(Out of Toner);                }                if(ammountOfPaper == 0) {                    System.out.println(Out of Paper);                }                if(twoSideNoPaperEven(numberToPrint) && (ammountOfPaper != 0)) {                    ammountOfPaper -= numberToPrint / 2;                    System.out.println(There is no Paper);                }                else if(!twoSideNoPaperEven(numberToPrint)) {                    tonerLevel = tonerLevel - (numberToPrint / 2);                    ammountOfPaper -= numberToPrint / 2;                    showPages(numberToPrint);                }            } else {                if(tonerLevel == 0) {                    System.out.println(Out of Toner);                }                if(ammountOfPaper == 0) {                    System.out.println(Out of Paper);                }                if(twoSideNoPaperOdd(numberToPrint) && (ammountOfPaper != 0)) {                    System.out.println(There is no paper);                    ammountOfPaper = (ammountOfPaper - ((int) numberToPrint / 2)) - 1;                    ammountOfPaper = 0;                }                else if(!twoSideNoPaperOdd(numberToPrint)) {                    tonerLevel = tonerLevel - (numberToPrint / 2);                    ammountOfPaper = (ammountOfPaper - ((int) numberToPrint / 2)) - 1;                    showPages(numberToPrint);                }            }        }    }public void showPages(double numberToPrint) {    System.out.println(Printing  + (int) numberToPrint +  Pages, paper remaining is:  + this.ammountOfPaper            +  Toner level is:  + this.tonerLevel);}public void refillToner() {    tonerLevel = 100;}public void refillPaper(int paper) {    if(paper > 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;}
Simulating a printer
java;simulation
I'd just like to add something that I haven't seen mentioned by others (or maybe I skipped it), but take for example this piece of code:public void printPages(double numberToPrint) {    // ...    if(tonerLevel == 0) {        System.out.println(Out of toner);    }    if(ammountOfPaper == 0) {        System.out.println(Out of Paper);    }    if(isOutOfToner(numberToPrint) && (tonerLevel != 0)) {        double difference = tonerLevel * 2;        numberToPrint = difference;        ammountOfPaper -= numberToPrint;        System.out.println(Will run out of toner after this print, able to print  + (int) numberToPrint +                 pages);        tonerLevel = 0;    }    if(isOutOfPaper(numberToPrint) && (ammountOfPaper != 0)) {        double different = ammountOfPaper - numberToPrint;        numberToPrint = numberToPrint + different;        System.out.println(Will run out of paper after this print, printing  + (int) numberToPrint +  pages);        ammountOfPaper = 0;    }    else if(!isOutOfToner(numberToPrint) && (!isOutOfPaper(numberToPrint))) {        ammountOfPaper -= numberToPrint;        tonerLevel = tonerLevel - (numberToPrint / 2);        showPages(numberToPrint);    }    // ...}You're checking your parameters twice, when you could return after a faulty state of the printer has been acknowledged (i.e. out of paper; we surely can't print without paper). So you could do something like this:private boolean canPrint(int numberToPrint) {    if (tonerLevel == 0) {        System.out.println(Out of toner);        return false;    } else if (ammountOfPaper == 0) {        System.out.println(Out of Paper);        return false;    } else if (!hasEnoughToner(numberToPrint)) {        // Maybe print a message to let the user know why you're not printing        return false;    } else if (!hasEnoughPaper(numberToPrint)) {        // Maybe print a message to let the user know why you're not printing        return false;    }    return true;}private void print(int numberToPrint) {    if (canPrint(numberToPrint)) {        ammountOfPaper -= numberToPrint;        tonerLevel = tonerLevel - (numberToPrint / 2);        showPages(numberToPrint);    }}I have avoided the parts of code where you mention when you will run out of paper or toner after the print, because I am in a hurry, but you get the point.Basically, once you know the state of something, you shouldn't have to come back to that. If the state is fatal (i.e. no paper), you can return from your method, as there is nothing that you can really print. This way, you can check your conditions only once and be done with them.Hope this helps. :)