Your question is SOLID Principles and Code Smells. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
You're a Consultant at MATRIX Resources reviewing a client's invoicing service before recommending a refactor. Look at this class and identify which SOLID principles it violates.
class MySQLDatabase {
void save(String table, long id, double value) {
// writes directly to a MySQL connection
}
}
interface InvoiceService {
double process(Invoice invoice);
void refund(Invoice invoice);
void generateTaxReport(Invoice invoice);
}
class InvoiceProcessor implements InvoiceService {
private MySQLDatabase db = new MySQLDatabase();
private EmailClient emailClient = new EmailClient();
private PdfRenderer pdfRenderer = new PdfRenderer();
public double process(Invoice invoice) {
double total = 0;
if (invoice.getType() == "STANDARD") {
total = invoice.getSubtotal() * 1.08;
} else if (invoice.getType() == "CONTRACTOR") {
total = invoice.getSubtotal() * 1.08 + 15.0;
} else if (invoice.getType() == "NONPROFIT") {
total = invoice.getSubtotal();
}
db.save("invoices", invoice.getId(), total);
emailClient.send(invoice.getClientEmail(), "Your invoice total is " + total);
pdfRenderer.render(invoice, total);
return total;
}
public void refund(Invoice invoice) { /* ... */ }
public void generateTaxReport(Invoice invoice) { /* ... */ }
}
class DiscountedInvoiceProcessor extends InvoiceProcessor {
@Override
public double process(Invoice invoice) {
throw new UnsupportedOperationException("Discounted invoices use a different pipeline");
}
}
Name each SOLID principle this violates, point to the exact line or method responsible, and explain the consequence in plain terms as if you were advising the client.