I am trying to calculate the print cost base on different paper size, single side or double side. So here is the detail:
A4, job type single sided:
*15 cents - black and white page
*25 cents - color page.
A4, job type double sided:
*10 cents - black and white page
*20 cents - color page.
Also need to support for other paper sizes will be added in the future.
And according to my design, developer can just create a A5 class for example to support other paper size, and add other condition in the factory class.
Could someone review my code and help me on whether I have to use interface instead of abstract class?
Here is my code:
PageBase:
public abstract class PageBase {
abstract double GetCost(int total, int color, boolean isSingleSide);
abstract void CalculateUnitPrice(boolean isSingleSide);
}
A4Page Class:
public class A4Page extends PageBase {
public double blackAndWhitePrintUnitCost;
public double colorPrintUniCost;
@Override
public double GetCost(int total, int color, boolean isSingleSide) {
CalculateUnitPrice(isSingleSide);
return color* colorPrintUniCost + (total-color)* blackAndWhitePrintUnitCost;
}
@Override
public void CalculateUnitPrice(boolean isSingleSide) {
if (isSingleSide) {
this.blackAndWhitePrintUnitCost = 0.15;
this.colorPrintUniCost = 0.25;
}
else {
this.blackAndWhitePrintUnitCost = 0.10;
this.colorPrintUniCost = 0.20;
}
}
}
PageFactory:
public class PageFactory {
public PageBase GetPage(String pageType) {
switch (pageType.toUpperCase()) {
case "A4":
return new A4Page();
default:
return new A4Page();
}
}
}
Main:
public class Main {
public static void Main() {
//read
PageFactory pageFactory = new PageFactory();
PageBase page = pageFactory.GetPage("A4");
page.GetCost(0,0,false);
}
}
Aucun commentaire:
Enregistrer un commentaire