Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.text.DecimalFormat;
- import java.util.Vector;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class ControleEstoque {
- static Vector<Produto> Produtos = new Vector();
- static int codigosCadastrados[];
- static DecimalFormat DF = new DecimalFormat("0.00");
- static boolean cadastrarProduto(int codigo, String nome, int qtd, float preco){
- try {
- if (checarCodigo(codigo))
- throw new Exception("Código existente.");
- else if (preco < 0f)
- throw new Exception("O preço deve ser maior que R$ 0,00.");
- else if (qtd < 0)
- throw new Exception("A quantidade inicial de produto deve ser maior que zero.");
- else if (nome.equals(""))
- throw new Exception("O produto deve ter um nome.");
- Produtos.addElement(new Produto(codigo, qtd, nome, preco));
- JOptionPane.showMessageDialog(null, "Produto cadastrado com sucesso!", "Sucesso!", JOptionPane.PLAIN_MESSAGE);
- adicionarCodigo(codigo);
- return true;
- }
- catch (Exception e) {
- JOptionPane.showMessageDialog(null, e.getMessage(), "Erro.", JOptionPane.ERROR_MESSAGE);
- return false;
- }
- }
- static boolean checarCodigo(int codigo){
- try {
- for (int i = 0; i < codigosCadastrados.length; i++){
- if (codigosCadastrados[i] == codigo)
- return true;
- }
- } catch (Exception e) {
- return false;
- }
- return false;
- }
- static void adicionarCodigo(int codigo){
- try {
- int temp[] = new int[codigosCadastrados.length];
- temp = codigosCadastrados;
- codigosCadastrados = new int[temp.length+1];
- for (int i = 0; i < temp.length; i++){
- codigosCadastrados[i] = temp[i];
- }
- codigosCadastrados[codigosCadastrados.length-1] = codigo;
- } catch (Exception e) {
- codigosCadastrados = new int[1];
- codigosCadastrados[0] = codigo;
- }
- }
- static void imprimirCodigos(){
- try {
- for (int i = 0; i < codigosCadastrados.length; i++){
- System.out.println(codigosCadastrados[i]);
- }
- } catch (Exception e){
- System.out.println("Não há códigos no cadastro.");
- }
- }
- static String relatorioProduto(int index){
- return ("Código: "+Produtos.elementAt(index).getCodigo()+"\nNome: "+Produtos.elementAt(index).getNome()
- +"\nQuantidade em estoque: "+Produtos.elementAt(index).getQuantidade()+"\nPreço:"+DF.format(Produtos.elementAt(index).getPreco()));
- }
- static String gerarRelatorio(){
- String temp = "";
- for (int i = 0; i < Produtos.size(); i++){
- temp += relatorioProduto(i);
- temp += "\n\n";
- }
- return temp;
- }
- static int procurarProduto(int codigo){
- try {
- for (int i = 0; i < codigosCadastrados.length; i++){
- if (codigosCadastrados[i] == codigo)
- return i;
- }
- return -1;
- } catch (Exception e) {
- return -1;
- }
- }
- static boolean verificarDisponibilidade(int codigo, int qtd){
- if (!Produtos.isEmpty()){
- for (int i = 0; i < Produtos.size(); i++){
- if (Produtos.elementAt(i).getCodigo() == codigo){
- if (Produtos.elementAt(i).getQuantidade() >= 10 && Produtos.elementAt(i).getQuantidade()-qtd >= 0)
- return true;
- else
- return false;
- }
- }
- }
- return false;
- }
- public static void main(String args[]){
- final JFrame Frame = new JFrame("Teste cadastro");
- Frame.add(new AplicacaoTabajara());
- Frame.setSize(380,380);
- Frame.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent e){
- new Thread(new Runnable() {
- public void run(){
- System.exit(0);
- }
- }).start();
- }
- });
- Frame.setVisible(true);
- }
- }
Add Comment
Please, Sign In to add comment