Vixe. Vou ver se ponho os mais relevantes (já que alguns se repetem). Só lembrando que a forma com que estou fazendo é a forma pedida pelo trabalho. E as importações estão corrigidas.
Classe Banco:
public class Banco {
Map<String, ContaSimples> tabelaCs;
Map<String, ContaEspecial> tabelaCe;
Map<String, ContaPoupanca> tabelaCp;
private File arquivo;
public void incluirCs (ContaSimples cs) {
tabelaCs.put(cs.getNumConta(), cs);
}
public void incluirCe (ContaEspecial ce) {
tabelaCe.put(ce.getNumConta(), ce);
}
public void incluirCp (ContaPoupanca cp) {
tabelaCp.put(cp.getNumConta(), cp);
}
public ContaSimples consultarCs (String numConta) {
return tabelaCs.get(numConta);
}
public ContaEspecial consultarCe (String numConta) {
return tabelaCe.get(numConta);
}
public ContaPoupanca consultarCp (String numConta) {
return tabelaCp.get(numConta);
}
public Banco() throws Exception{
arquivo = new File("banco.xxx");
if(!arquivo.exists()){
tabelaCs = new HashMap<String, ContaSimples>();
tabelaCe = new HashMap<String, ContaEspecial>();
tabelaCp = new HashMap<String, ContaPoupanca>();
}
else{
FileInputStream fluxoArquivo = new FileInputStream(arquivo);
ObjectInputStream fluxoObjeto = new ObjectInputStream(fluxoArquivo);
tabelaCs = (Map<String, ContaSimples>) fluxoObjeto.readObject();
tabelaCe = (Map<String, ContaEspecial>) fluxoObjeto.readObject();
tabelaCp = (Map<String, ContaPoupanca>) fluxoObjeto.readObject();
fluxoObjeto.close();
}
}
public void fechar() throws Exception{
FileOutputStream fluxoArquivo = new FileOutputStream (arquivo);
ObjectOutputStream fluxoObjeto = new ObjectOutputStream (fluxoArquivo);
fluxoObjeto.writeObject(tabelaCs);
fluxoObjeto.writeObject(tabelaCe);
fluxoObjeto.writeObject(tabelaCp);
fluxoObjeto.close();
}
}
classe controle:
public class Controle {
private Banco banco;
public Controle() throws Exception{
banco = new Banco();
}
public void incluirCs(String nome, String numConta, String saldo, String tempoDeConta) {
ContaSimples cs = new ContaSimples();
cs.setNome(nome);
cs.setNumConta(numConta);
cs.setSaldo(saldo);
cs.setTempoDeConta(tempoDeConta);
banco.incluirCs(cs);
}
public void incluirCe(String nome, String numConta, String saldo, String tempoDeConta, String limite) {
ContaEspecial ce = new ContaEspecial();
ce.setNome(nome);
ce.setNumConta(numConta);
ce.setSaldo(saldo);
ce.setTempoDeConta(tempoDeConta);
ce.setLimite(limite);
banco.incluirCe(ce);
}
public void incluirCp(String nome, String numConta, String saldo, String tempoDeConta, String taxa) {
ContaPoupanca cp = new ContaPoupanca();
cp.setNome(nome);
cp.setNumConta(numConta);
cp.setSaldo(saldo);
cp.setTempoDeConta(tempoDeConta);
cp.setTaxa(taxa);
banco.incluirCp(cp);
}
public List<String> consultarCs (String numConta) {
List<String> campos = new ArrayList<String>();
ContaSimples cs = banco.consultarCs(numConta);
campos.add(cs.getNome());
campos.add(cs.getNumConta());
campos.add(cs.getSaldo());
campos.add(cs.getTempoDeConta());
return campos;
}
public List<String> consultarCe (String numConta) {
List<String> campos = new ArrayList<String>();
ContaEspecial ce = banco.consultarCe(numConta);
campos.add(ce.getNome());
campos.add(ce.getNumConta());
campos.add(ce.getSaldo());
campos.add(ce.getTempoDeConta());
campos.add(ce.getLimite());
return campos;
}
public List<String> consultarCp (String numConta) {
List<String> campos = new ArrayList<String>();
ContaPoupanca cp = banco.consultarCp(numConta);
campos.add(cp.getNome());
campos.add(cp.getNumConta());
campos.add(cp.getSaldo());
campos.add(cp.getTempoDeConta());
campos.add(cp.getTaxa());
return campos;
}
public void sair() throws Exception{
banco.fechar();
}
}
JFrapme TelaContaSimples:
public class TelaContaSimples extends javax.swing.JFrame {
/** Creates new form TelaContaSimples */
public TelaContaSimples() throws Exception{
initComponents();
controle = new Controle();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
txtNome = new javax.swing.JTextField();
txtNumConta = new javax.swing.JTextField();
txtSaldo = new javax.swing.JTextField();
txtTempoDeCasa = new javax.swing.JTextField();
btnInformar = new javax.swing.JButton();
btnIncluir = new javax.swing.JButton();
btnConsultar = new javax.swing.JButton();
btnSair = new javax.swing.JButton();
lblSaldoTotal = new javax.swing.JLabel();
btnLimpar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Nome:");
jLabel2.setText("Número da Conta:");
jLabel3.setText("Saldo Total:");
jLabel4.setText("Saldo:");
jLabel5.setText("Tempo de Casa:");
btnInformar.setText("Informar");
btnInformar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnInformarActionPerformed(evt);
}
});
btnIncluir.setText("Incluir");
btnIncluir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnIncluirActionPerformed(evt);
}
});
btnConsultar.setText("Consultar");
btnConsultar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConsultarActionPerformed(evt);
}
});
btnSair.setText("Sair");
btnSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSairActionPerformed(evt);
}
});
btnLimpar.setText("Limpar");
btnLimpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLimparActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblSaldoTotal)
.addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtNumConta, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtTempoDeCasa, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnLimpar)
.addGroup(layout.createSequentialGroup()
.addComponent(btnInformar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnIncluir)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnConsultar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSair))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtNumConta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(txtTempoDeCasa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(lblSaldoTotal))
.addGap(4, 4, 4)
.addComponent(btnLimpar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnInformar)
.addComponent(btnIncluir)
.addComponent(btnConsultar)
.addComponent(btnSair))
.addContainerGap(35, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
private void btnInformarActionPerformed(java.awt.event.ActionEvent evt) {
lblSaldoTotal.setText(txtSaldo.getText());
}
private void btnIncluirActionPerformed(java.awt.event.ActionEvent evt) {
try{
controle.incluirCs(txtNome.getText(), txtNumConta.getText(),txtSaldo.getText(), txtTempoDeCasa.getText());
}
catch(Exception e){
JOptionPane.showMessageDialog(null, “Erro”);
}
}
private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {
List aux;
String numConta = JOptionPane.showInputDialog(“Entre com o Número da conta”);
aux = controle.consultarCs(numConta);
txtNome.setText(aux.get(0));
txtNumConta.setText(aux.get(1));
txtSaldo.setText(aux.get(2));
txtTempoDeCasa.setText(aux.get(3));
lblSaldoTotal.setText(txtSaldo.getText());
}
private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {
txtNome.setText("");
txtNumConta.setText("");
txtSaldo.setText("");
txtTempoDeCasa.setText("");
lblSaldoTotal.setText("");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws Exception{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnConsultar;
private javax.swing.JButton btnIncluir;
private javax.swing.JButton btnInformar;
private javax.swing.JButton btnLimpar;
private javax.swing.JButton btnSair;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel lblSaldoTotal;
private javax.swing.JTextField txtNome;
private javax.swing.JTextField txtNumConta;
private javax.swing.JTextField txtSaldo;
private javax.swing.JTextField txtTempoDeCasa;
// End of variables declaration
private Controle controle;
}
JFrame Tela (A tela menu):
public class Tela extends javax.swing.JFrame {
/** Creates new form Tela */
public Tela() throws Exception{
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnContaSimples = new javax.swing.JButton();
btnSair = new javax.swing.JButton();
lblErro = new javax.swing.JLabel();
btnContaEspecial = new javax.swing.JButton();
btnContaPoupanca = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnContaSimples.setText("Conta Simples");
btnContaSimples.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnContaSimplesActionPerformed(evt);
}
});
btnSair.setText("Sair");
btnSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSairActionPerformed(evt);
}
});
lblErro.setForeground(new java.awt.Color(255, 0, 0));
btnContaEspecial.setText("Conta Especial");
btnContaEspecial.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnContaEspecialActionPerformed(evt);
}
});
btnContaPoupanca.setText("Conta Poupança");
btnContaPoupanca.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnContaPoupancaActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(157, 157, 157)
.addComponent(lblErro)
.addContainerGap(153, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(btnContaPoupanca)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSair))
.addGroup(layout.createSequentialGroup()
.addComponent(btnContaSimples)
.addGap(36, 36, 36)
.addComponent(btnContaEspecial)))
.addContainerGap(37, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(lblErro)
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnContaSimples)
.addComponent(btnContaEspecial))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnContaPoupanca)
.addComponent(btnSair))
.addGap(60, 60, 60))
);
pack();
}// </editor-fold>
private void btnContaSimplesActionPerformed(java.awt.event.ActionEvent evt) {
[color=red]TelaContaSimples tcs = new TelaContaSimples();[/color]
tcs.setVisible(true);
dispose();
}
private void btnContaEspecialActionPerformed(java.awt.event.ActionEvent evt) {
[color=red]TelaContaEspecial tce = new TelaContaEspecial();[/color]
tce.setVisible(true);
dispose();
}
private void btnContaPoupancaActionPerformed(java.awt.event.ActionEvent evt) {
[color=red]TelaContaPoupanca tcp = new TelaContaPoupanca();[/color]
tcp.setVisible(true);
dispose();
}
private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws Exception{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnContaEspecial;
private javax.swing.JButton btnContaPoupanca;
private javax.swing.JButton btnContaSimples;
private javax.swing.JButton btnSair;
private javax.swing.JLabel lblErro;
// End of variables declaration
}