我正在尝试用java重新创建一台打印机,我对编程很新,所以我在单个函数中使用了巨大的if else块来指示程序的逻辑,我注意到这是创建一个大量的代码在同一个函数里面,我想知道是否有一个更有说服力/更有效的方法,下面的打印机类。打印机的逻辑不是太重要,但只是为了显示,一个是双面打印机,一个不是,并且逻辑负责检查墨粉水平并确保打印的页面与打印机是双面的或不
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;
}
根据尼古拉斯的建议更改If语句:
public void printPages(double numberToPrint) {
if(tonerLevel == 0) {
System.out.println("Out of toner");
return;
}
if(ammountOfPaper == 0) {
System.out.println("Out of Paper");
return;
}
if(isDoubleSided == false) {