I have a question with the exceptions, in this exercise we ask that 2 exceptions be considered: One that reports on the problem (and that does not stop the execution of the program, is what I understand), and another that does produce a interruption, and therefore, the execution stops with its corresponding error. I believe that I have correctly created the method and exception for the case of the interruption, but I doubt if they are correctly constructed. I appreciate any response that clarifies how best to use the exceptions, or the correct way to use them.
Exercise Statement
Construct an InvoiceBrowned class that descends from the Invoice class and that includes the issuer (company that issues the invoice) and client attributes and at least one method called printFacture that displays all the attributes (both those inherited from Invoice and InvoiceBased ). The Invoice class will have the following attributes: CIF (alphanumeric), invoice number and total. This class will have a unique constructor to which these attributes will be passed by parameters and will be initialized. The following exceptions should be considered:
- Negative amount: will produce an interruption in the program if the Total attribute is stored with a negative amount.
- Emitter blank: will be informative, and will occur if initialized the emitter with the empty string. All attributes will be private and In addition to the methods indicated in the statement, you must create those that are considered convenient that are needed.
FacturaEmitida.java
public class FacturaEmitida extends Factura {
private String emisor;
private String cliente;
FacturaEmitida(String cif, int numFactura, double total, String emisor, String cliente){
super(cif,numFactura,total);
this.emisor = emisor;
this.cliente = cliente;
}
public void imprimirFactura() {
System.out.println("CIF: " + this.getCif() + "\n" +
"Número de factura: " + this.getNumFactura() + "\n" +
"Emisor: " + this.getEmisor() + "\n" +
"Cliente: " + this.getCliente() + "\n" +
"Total: " + this.getTotal() + "\n");
}
public class ValorNoValido extends Exception{
public ValorNoValido(){ }
public ValorNoValido(String cadena){
super(cadena); //Llama al constructor de Exception y le pasa el contenido de cadena
}
}
public void comprobarImporte() throws ValorNoValido {
if(this.getTotal() < 0){
throw new ValorNoValido("El importe total no puede ser negativo.");
}
}
public String comprobarEmisor(String emisor){
//name.trim().length() == 0
if (this.getEmisor() == "" || this.getEmisor() == null) {
this.emisor = "";
throw new RuntimeException("El campo de Emisor no puede quedar vacío.");
}
return emisor;
}
/**
* @return the emisor
*/
public String getEmisor() {
return emisor;
}
/**
* @param emisor the emisor to set
*/
public void setEmisor(String emisor) {
this.emisor = emisor;
}
/**
* @return the cliente
*/
public String getCliente() {
return cliente;
}
/**
* @param cliente the cliente to set
*/
public void setCliente(String cliente) {
this.cliente = cliente;
}
}
Factura.java
public class Factura {
private String cif;
private int numFactura;
private double total;
Factura(String cif, int numFactura, double total){
this.cif = cif;
this.numFactura = numFactura;
this.total = total;
}
/**
* @return the cif
*/
public String getCif() {
return cif;
}
/**
* @param cif the cif to set
*/
public void setCif(String cif) {
this.cif = cif;
}
/**
* @return the numFactura
*/
public int getNumFactura() {
return numFactura;
}
/**
* @param numFactura the numFactura to set
*/
public void setNumFactura(int numFactura) {
this.numFactura = numFactura;
}
/**
* @return the total
*/
public double getTotal() {
return total;
}
/**
* @param total the total to set
*/
public void setTotal(double total) {
this.total = total;
}
}
Main.java
public class Main {
public static void main(String[] args) {
FacturaEmitida fact = new FacturaEmitida("844571X", 222, 1500, "", "Weist Cheing");
try {
fact.comprobarImporte();
fact.comprobarEmisor(fact.getEmisor());
fact.imprimirFactura();
}catch (FacturaEmitida.ValorNoValido e) {
System.out.println(e.getMessage());
}catch(RuntimeException ex){
System.out.println(ex);
}
}
}