Guest User

Untitled

a guest
May 25th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. import java.awt.event.WindowAdapter;
  2. import java.awt.event.WindowEvent;
  3. import java.text.DecimalFormat;
  4. import java.util.Vector;
  5. import javax.swing.JFrame;
  6. import javax.swing.JOptionPane;
  7.  
  8. public class ControleEstoque {
  9. static Vector<Produto> Produtos = new Vector();
  10. static int codigosCadastrados[];
  11. static DecimalFormat DF = new DecimalFormat("0.00");
  12.  
  13. static boolean cadastrarProduto(int codigo, String nome, int qtd, float preco){
  14. try {
  15. if (checarCodigo(codigo))
  16. throw new Exception("Código existente.");
  17. else if (preco < 0f)
  18. throw new Exception("O preço deve ser maior que R$ 0,00.");
  19. else if (qtd < 0)
  20. throw new Exception("A quantidade inicial de produto deve ser maior que zero.");
  21. else if (nome.equals(""))
  22. throw new Exception("O produto deve ter um nome.");
  23. Produtos.addElement(new Produto(codigo, qtd, nome, preco));
  24. JOptionPane.showMessageDialog(null, "Produto cadastrado com sucesso!", "Sucesso!", JOptionPane.PLAIN_MESSAGE);
  25. adicionarCodigo(codigo);
  26. return true;
  27. }
  28. catch (Exception e) {
  29. JOptionPane.showMessageDialog(null, e.getMessage(), "Erro.", JOptionPane.ERROR_MESSAGE);
  30. return false;
  31. }
  32. }
  33.  
  34. static boolean checarCodigo(int codigo){
  35. try {
  36. for (int i = 0; i < codigosCadastrados.length; i++){
  37. if (codigosCadastrados[i] == codigo)
  38. return true;
  39. }
  40. } catch (Exception e) {
  41. return false;
  42. }
  43. return false;
  44. }
  45.  
  46. static void adicionarCodigo(int codigo){
  47. try {
  48. int temp[] = new int[codigosCadastrados.length];
  49. temp = codigosCadastrados;
  50. codigosCadastrados = new int[temp.length+1];
  51. for (int i = 0; i < temp.length; i++){
  52. codigosCadastrados[i] = temp[i];
  53. }
  54. codigosCadastrados[codigosCadastrados.length-1] = codigo;
  55. } catch (Exception e) {
  56. codigosCadastrados = new int[1];
  57. codigosCadastrados[0] = codigo;
  58. }
  59. }
  60.  
  61. static void imprimirCodigos(){
  62. try {
  63. for (int i = 0; i < codigosCadastrados.length; i++){
  64. System.out.println(codigosCadastrados[i]);
  65. }
  66. } catch (Exception e){
  67. System.out.println("Não há códigos no cadastro.");
  68. }
  69. }
  70.  
  71. static String relatorioProduto(int index){
  72. return ("Código: "+Produtos.elementAt(index).getCodigo()+"\nNome: "+Produtos.elementAt(index).getNome()
  73. +"\nQuantidade em estoque: "+Produtos.elementAt(index).getQuantidade()+"\nPreço:"+DF.format(Produtos.elementAt(index).getPreco()));
  74. }
  75.  
  76. static String gerarRelatorio(){
  77. String temp = "";
  78. for (int i = 0; i < Produtos.size(); i++){
  79. temp += relatorioProduto(i);
  80. temp += "\n\n";
  81. }
  82. return temp;
  83. }
  84. static int procurarProduto(int codigo){
  85. try {
  86. for (int i = 0; i < codigosCadastrados.length; i++){
  87. if (codigosCadastrados[i] == codigo)
  88. return i;
  89. }
  90. return -1;
  91. } catch (Exception e) {
  92. return -1;
  93. }
  94. }
  95. static boolean verificarDisponibilidade(int codigo, int qtd){
  96. if (!Produtos.isEmpty()){
  97. for (int i = 0; i < Produtos.size(); i++){
  98. if (Produtos.elementAt(i).getCodigo() == codigo){
  99. if (Produtos.elementAt(i).getQuantidade() >= 10 && Produtos.elementAt(i).getQuantidade()-qtd >= 0)
  100. return true;
  101. else
  102. return false;
  103. }
  104. }
  105. }
  106. return false;
  107. }
  108. public static void main(String args[]){
  109. final JFrame Frame = new JFrame("Teste cadastro");
  110. Frame.add(new AplicacaoTabajara());
  111. Frame.setSize(380,380);
  112. Frame.addWindowListener(new WindowAdapter() {
  113. @Override
  114. public void windowClosing(WindowEvent e){
  115. new Thread(new Runnable() {
  116. public void run(){
  117. System.exit(0);
  118. }
  119. }).start();
  120. }
  121. });
  122. Frame.setVisible(true);
  123. }
  124. }
Add Comment
Please, Sign In to add comment