Adicionando uma Interface com o Usuário

codificacao.gif (9448 bytes)

Interface com o usuário

ui.gif (5508 bytes)

Alguns trechos de código

A classe principal

public class Tpdv {
  public Tpdv() {
    TpdvFrame frame = new TpdvFrame();
    // ... um monte de coisinhas
    frame.setVisible(true);
  }
  public static void main(String[] args) {
    try  {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
    }
    new Tpdv();
  }
}

A classe TpdvFrame

public class TpdvFrame extends JFrame {
  Loja loja = new Loja();
  ITPDV tpdv = loja.getTPDV();
  // ...
}

Atendimento ao clique do botão Entra Item

public class TpdvFrame extends JFrame {
  // ...
  void entraItem_actionPerformed(ActionEvent e) {
    if(upc.getText().equals("")) {
        JOptionPane.showMessageDialog(this, "UPC deve ser informado");
        return;
    }
    if(quantidade.getText().equals("")) {
        JOptionPane.showMessageDialog(this, "Quantidade deve ser informada");
        return;
    }
    int upcInt = 0;
    int quant = 0;
    try {
        upcInt = Integer.parseInt(upc.getText());
    } catch(NumberFormatException ex) {
        JOptionPane.showMessageDialog(this, "Produto " + upc.getText() + " inexistente");
    }
    try {
        quant = Integer.parseInt(quantidade.getText());
    } catch(NumberFormatException ex) {
        JOptionPane.showMessageDialog(this, "Quantidade deve ser numérica");
    }
    try {
        tpdv.entraItem(upcInt, quant);
    } catch(ProdutoInexistenteException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage());
        return;
    }
    upc.setText("");
    quantidade.setText("");
    total.setText("");
    valorEntregue.setText("");
    troco.setText("");
  }
  // ...
}

Atendimento ao clique do botão Fim Venda

public class TpdvFrame extends JFrame {
  // ...
  void fimVenda_actionPerformed(ActionEvent e) {
    try {
        tpdv.fimDeVenda();
    } catch(NaoHaVendaException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage());
        return;
    }
    total.setText(String.valueOf(tpdv.getVenda().total()));
  }
}

Atendimento ao clique do botão Faça Pagamento

public class TpdvFrame extends JFrame {
  // ...
  void façaPagamento_actionPerformed(ActionEvent e) {
    if(valorEntregue.getText().equals("")) {
        JOptionPane.showMessageDialog(this, "Qual é o valor entregue?");
        return;
    }
    try {
        tpdv.façaPagamento((float)Double.parseDouble(valorEntregue.getText()));
    } catch(PagamentoInsuficienteException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage());
        return;
    }
    troco.setText(String.valueOf(tpdv.getVenda().getTroco()));
  }
}

impl-4 programa anterior