11package classroom_21 .controllers ;
22
3-
43import classroom_21 .models .Veiculo ;
54import classroom_21 .services .BancoDados ;
65import classroom_21 .services .Receber ;
76
8-
97import java .sql .*;
108import java .util .ArrayList ;
119
1210public class VeiculoController {
13- public static void imprime (Veiculo v ){
11+
12+ public static void imprime (Veiculo v ) {
1413 System .out .println ("ID: " + v .getId ());
1514 System .out .println ("Modelo: " + v .getModelo ());
1615 System .out .println ("Fabricante: " + v .getFabricante ());
@@ -19,7 +18,7 @@ public static void imprime(Veiculo v){
1918 System .out .println ("Preço: R$ " + v .getPreco ());
2019 }
2120
22- public static Veiculo cadastra (){
21+ public static Veiculo cadastra () {
2322 Veiculo v = new Veiculo ();
2423
2524 System .out .print ("Modelo: " );
@@ -40,21 +39,21 @@ public static Veiculo cadastra(){
4039 return v ;
4140 }
4241
43- public static void addVeiculo (Veiculo v ){
42+ public static void addVeiculo (Veiculo v ) {
4443 Connection conn = BancoDados .conecta ();
45- try {
44+ try {
4645 String sql = "INSERT INTO veiculo (modelo, fabricante, ano, cor, preco) VALUES (?,?,?,?,?)" ;
4746 PreparedStatement statement = conn .prepareStatement (sql );
4847
49- statement .setString (1 ,v .getModelo ());
50- statement .setString (2 ,v .getFabricante ());
51- statement .setInt (3 ,v .getAno ());
52- statement .setString (4 ,v .getCor ());
53- statement .setDouble (5 ,v .getPreco ());
48+ statement .setString (1 , v .getModelo ());
49+ statement .setString (2 , v .getFabricante ());
50+ statement .setInt (3 , v .getAno ());
51+ statement .setString (4 , v .getCor ());
52+ statement .setDouble (5 , v .getPreco ());
5453
5554 int linhas = statement .executeUpdate ();
5655
57- if (linhas > 0 ){
56+ if (linhas > 0 ) {
5857 System .out .println ("\n \n ===============================" );
5958 System .out .println ("Veículo Cadastrado com Sucesso!" );
6059 System .out .println ("===============================\n \n " );
@@ -67,25 +66,26 @@ public static void addVeiculo(Veiculo v){
6766 BancoDados .fecha (conn );
6867 }
6968
70- public static ArrayList <Veiculo > getAll (){
69+ public static ArrayList <Veiculo > getAll () {
7170 Connection conn = BancoDados .conecta ();
7271
73- try {
72+ try {
7473 String sql = "SELECT * FROM veiculo" ;
7574 Statement statement = conn .createStatement ();
7675
7776 ResultSet resultados = statement .executeQuery (sql );
7877
7978 ArrayList <Veiculo > veiculosList = new ArrayList <Veiculo >();
8079
81- while (resultados .next ()){
82- veiculosList .add (new Veiculo (
80+ while (resultados .next ()) {
81+ veiculosList .add (new Veiculo (
8382 resultados .getInt ("id" ),
8483 resultados .getString ("modelo" ),
8584 resultados .getString ("fabricante" ),
8685 resultados .getString ("cor" ),
8786 resultados .getInt ("ano" ),
88- resultados .getDouble ("preco" )
87+ resultados .getDouble ("preco" ),
88+ resultados .getBoolean ("vendido" )
8989 ));
9090 }
9191 BancoDados .fecha (conn );
@@ -98,25 +98,26 @@ public static ArrayList<Veiculo> getAll(){
9898 return null ;
9999 }
100100
101- public static ArrayList <Veiculo > getPorFabricante (String fabricante ){
101+ public static ArrayList <Veiculo > getPorFabricante (String fabricante ) {
102102 Connection conn = BancoDados .conecta ();
103103
104- try {
105- String sql = "SELECT * FROM veiculo WHERE fabricante LIKE '%" + fabricante +"%'" ;
104+ try {
105+ String sql = "SELECT * FROM veiculo WHERE fabricante LIKE '%" + fabricante + "%'" ;
106106 Statement statement = conn .createStatement ();
107107
108108 ResultSet resultados = statement .executeQuery (sql );
109109
110110 ArrayList <Veiculo > veiculosList = new ArrayList <Veiculo >();
111111
112- while (resultados .next ()){
112+ while (resultados .next ()) {
113113 veiculosList .add (new Veiculo (
114114 resultados .getInt ("id" ),
115115 resultados .getString ("modelo" ),
116116 resultados .getString ("fabricante" ),
117117 resultados .getString ("cor" ),
118118 resultados .getInt ("ano" ),
119- resultados .getDouble ("preco" )
119+ resultados .getDouble ("preco" ),
120+ resultados .getBoolean ("vendido" )
120121 ));
121122 }
122123 BancoDados .fecha (conn );
@@ -129,25 +130,26 @@ public static ArrayList<Veiculo> getPorFabricante(String fabricante){
129130 return null ;
130131 }
131132
132- public static ArrayList <Veiculo > getPorPrecoMaximo (double preco ){
133+ public static ArrayList <Veiculo > getPorPrecoMaximo (double preco ) {
133134 Connection conn = BancoDados .conecta ();
134135
135- try {
136- String sql = "SELECT * FROM veiculo WHERE preco <= " + preco +"" ;
136+ try {
137+ String sql = "SELECT * FROM veiculo WHERE preco <= " + preco + "" ;
137138 Statement statement = conn .createStatement ();
138139
139140 ResultSet resultados = statement .executeQuery (sql );
140141
141142 ArrayList <Veiculo > veiculosList = new ArrayList <Veiculo >();
142143
143- while (resultados .next ()){
144+ while (resultados .next ()) {
144145 veiculosList .add (new Veiculo (
145146 resultados .getInt ("id" ),
146147 resultados .getString ("modelo" ),
147148 resultados .getString ("fabricante" ),
148149 resultados .getString ("cor" ),
149150 resultados .getInt ("ano" ),
150- resultados .getDouble ("preco" )
151+ resultados .getDouble ("preco" ),
152+ resultados .getBoolean ("vendido" )
151153 ));
152154 }
153155 BancoDados .fecha (conn );
@@ -159,26 +161,27 @@ public static ArrayList<Veiculo> getPorPrecoMaximo(double preco){
159161 BancoDados .fecha (conn );
160162 return null ;
161163 }
162-
163- public static ArrayList <Veiculo > getPorCor (String cor ){
164+
165+ public static ArrayList <Veiculo > getPorCor (String cor ) {
164166 Connection conn = BancoDados .conecta ();
165167
166- try {
167- String sql = "SELECT * FROM veiculo WHERE cor LIKE '%" + cor +"%'" ;
168+ try {
169+ String sql = "SELECT * FROM veiculo WHERE cor LIKE '%" + cor + "%'" ;
168170 Statement statement = conn .createStatement ();
169171
170172 ResultSet resultados = statement .executeQuery (sql );
171173
172174 ArrayList <Veiculo > veiculosList = new ArrayList <Veiculo >();
173175
174- while (resultados .next ()){
176+ while (resultados .next ()) {
175177 veiculosList .add (new Veiculo (
176178 resultados .getInt ("id" ),
177179 resultados .getString ("modelo" ),
178180 resultados .getString ("fabricante" ),
179181 resultados .getString ("cor" ),
180182 resultados .getInt ("ano" ),
181- resultados .getDouble ("preco" )
183+ resultados .getDouble ("preco" ),
184+ resultados .getBoolean ("vendido" )
182185 ));
183186 }
184187 BancoDados .fecha (conn );
@@ -190,26 +193,27 @@ public static ArrayList<Veiculo> getPorCor(String cor){
190193 BancoDados .fecha (conn );
191194 return null ;
192195 }
193-
194- public static ArrayList <Veiculo > getPorFaixaDePreco (double menor ,double maior ){
196+
197+ public static ArrayList <Veiculo > getPorFaixaDePreco (double menor , double maior ) {
195198 Connection conn = BancoDados .conecta ();
196199
197- try {
198- String sql = "SELECT * FROM veiculo WHERE preco >= " + menor +"AND preco <= " + maior ;
200+ try {
201+ String sql = "SELECT * FROM veiculo WHERE preco >= " + menor + "AND preco <= " + maior ;
199202 Statement statement = conn .createStatement ();
200203
201204 ResultSet resultados = statement .executeQuery (sql );
202205
203206 ArrayList <Veiculo > veiculosList = new ArrayList <Veiculo >();
204207
205- while (resultados .next ()){
208+ while (resultados .next ()) {
206209 veiculosList .add (new Veiculo (
207210 resultados .getInt ("id" ),
208211 resultados .getString ("modelo" ),
209212 resultados .getString ("fabricante" ),
210213 resultados .getString ("cor" ),
211214 resultados .getInt ("ano" ),
212- resultados .getDouble ("preco" )
215+ resultados .getDouble ("preco" ),
216+ resultados .getBoolean ("vendido" )
213217 ));
214218 }
215219 BancoDados .fecha (conn );
@@ -221,26 +225,27 @@ public static ArrayList<Veiculo> getPorFaixaDePreco(double menor,double maior){
221225 BancoDados .fecha (conn );
222226 return null ;
223227 }
224-
225- public static ArrayList <Veiculo > getPorModelo (String modelo ){
228+
229+ public static ArrayList <Veiculo > getPorModelo (String modelo ) {
226230 Connection conn = BancoDados .conecta ();
227231
228- try {
229- String sql = "SELECT * FROM veiculo WHERE modelo LIKE '%" + modelo +"%'" ;
232+ try {
233+ String sql = "SELECT * FROM veiculo WHERE modelo LIKE '%" + modelo + "%'" ;
230234 Statement statement = conn .createStatement ();
231235
232236 ResultSet resultados = statement .executeQuery (sql );
233237
234238 ArrayList <Veiculo > veiculosList = new ArrayList <Veiculo >();
235239
236- while (resultados .next ()){
240+ while (resultados .next ()) {
237241 veiculosList .add (new Veiculo (
238242 resultados .getInt ("id" ),
239243 resultados .getString ("modelo" ),
240244 resultados .getString ("fabricante" ),
241245 resultados .getString ("cor" ),
242246 resultados .getInt ("ano" ),
243- resultados .getDouble ("preco" )
247+ resultados .getDouble ("preco" ),
248+ resultados .getBoolean ("vendido" )
244249 ));
245250 }
246251 BancoDados .fecha (conn );
@@ -252,7 +257,76 @@ public static ArrayList<Veiculo> getPorModelo(String modelo){
252257 BancoDados .fecha (conn );
253258 return null ;
254259 }
255-
256-
257-
258- }
260+
261+ public static Veiculo getPorId (int id ) {
262+ Connection conn = BancoDados .conecta ();
263+
264+ try {
265+ String sql = "SELECT * FROM veiculo WHERE id = " + id ;
266+ Statement statement = conn .createStatement ();
267+
268+ ResultSet resultados = statement .executeQuery (sql );
269+
270+ Veiculo v = null ;
271+
272+ while (resultados .next ()) {
273+ v = new Veiculo (
274+ resultados .getInt ("id" ),
275+ resultados .getString ("modelo" ),
276+ resultados .getString ("fabricante" ),
277+ resultados .getString ("cor" ),
278+ resultados .getInt ("ano" ),
279+ resultados .getDouble ("preco" ),
280+ resultados .getBoolean ("vendido" )
281+ );
282+ }
283+ BancoDados .fecha (conn );
284+ return v ;
285+
286+ } catch (SQLException e ) {
287+ System .err .println (e );
288+ }
289+
290+ BancoDados .fecha (conn );
291+ return null ;
292+ }
293+
294+ public static void updateVeiculo (Veiculo v ) {
295+ Connection conn = BancoDados .conecta ();
296+
297+ try {
298+ String sql = "UPDATE veiculo"
299+ + "SET modelo = ?,"
300+ + "fabricante = ?,"
301+ + "ano = ?,"
302+ + "cor = ?,"
303+ + "preco = ?,"
304+ + "vendido = ?"
305+ + "WHERE id = ?" ;
306+
307+ PreparedStatement statement = conn .prepareStatement (sql );
308+
309+ statement .setString (1 , v .getModelo ());
310+ statement .setString (2 , v .getFabricante ());
311+ statement .setInt (3 , v .getAno ());
312+ statement .setString (4 , v .getCor ());
313+ statement .setDouble (5 , v .getPreco ());
314+ statement .setBoolean (6 , v .isVendido ());
315+ statement .setInt (7 , v .getId ());
316+
317+ int linhas = statement .executeUpdate ();
318+
319+ if (linhas > 0 ) {
320+ System .out .println ("\n \n ===============================" );
321+ System .out .println ("Veículo Vendido com Sucesso!" );
322+ System .out .println ("===============================\n \n " );
323+ } else {
324+ System .out .println ("Erro ao cadastrar!" );
325+ }
326+ } catch (SQLException e ) {
327+ System .err .println (e );
328+ }
329+ BancoDados .fecha (conn );
330+ }
331+
332+ }
0 commit comments