Commit 94a68ae6 by Patryk Czarnik

Formatowanie kodu IDEA

parent fd341077
package ogloszenia; package ogloszenia;
/** W tej klasie trzeba wpisać swoje ustawienia ścieżek. */ /**
* W tej klasie trzeba wpisać swoje ustawienia ścieżek.
*/
public class Ustawienia { public class Ustawienia {
public static final String SQLITE_PATH = "/home/patryk/ogloszenia/ogloszenia.db"; public static final String SQLITE_PATH = "/home/patryk/ogloszenia/ogloszenia.db";
public static final String PHOTOS_PATH = "/home/patryk/ogloszenia/foto"; public static final String PHOTOS_PATH = "/home/patryk/ogloszenia/foto";
} }
...@@ -10,69 +10,69 @@ import ogloszenia.baza.sqlite.SprzedawcaDAO; ...@@ -10,69 +10,69 @@ import ogloszenia.baza.sqlite.SprzedawcaDAO;
import ogloszenia.exn.BladBazyDanych; import ogloszenia.exn.BladBazyDanych;
public class DostepDoBazySqlite implements AutoCloseable { public class DostepDoBazySqlite implements AutoCloseable {
private Connection c; private Connection c;
DostepDoBazySqlite(Connection con) {
c = con;
}
public static DostepDoBazySqlite newSQLite(String sqliteFile) throws BladBazyDanych {
try {
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:" + sqliteFile);
return new DostepDoBazySqlite(con);
} catch (Exception e) {
throw new BladBazyDanych("Błąd podczas otwierania bazy danych ("+e.getMessage()+")", e);
}
}
public static DostepDoBazySqlite newSQLite() throws BladBazyDanych {
return newSQLite(Ustawienia.SQLITE_PATH);
}
@Override
public void close() {
try {
if(c != null)
c.close();
} catch (SQLException e) {
System.err.println("Błąd podczas close: " + e.getMessage());
}
}
Connection c() {
return c;
}
public void beginTransaction() throws BladBazyDanych {
try {
c.setAutoCommit(false);
//c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
} catch (SQLException e) {
throw new BladBazyDanych("beginTransaction: " + e.getMessage(), e);
}
}
public void endTransaction(boolean commit) throws BladBazyDanych { DostepDoBazySqlite(Connection con) {
try { c = con;
if(commit) { }
c.commit();
} else { public static DostepDoBazySqlite newSQLite(String sqliteFile) throws BladBazyDanych {
c.rollback(); try {
} Class.forName("org.sqlite.JDBC");
c.setAutoCommit(true);
} catch (SQLException e) { Connection con = DriverManager.getConnection("jdbc:sqlite:" + sqliteFile);
throw new BladBazyDanych("endTransaction: " + e.getMessage(), e); return new DostepDoBazySqlite(con);
} } catch (Exception e) {
} throw new BladBazyDanych("Błąd podczas otwierania bazy danych (" + e.getMessage() + ")", e);
}
}
public static DostepDoBazySqlite newSQLite() throws BladBazyDanych {
return newSQLite(Ustawienia.SQLITE_PATH);
}
@Override
public void close() {
try {
if (c != null)
c.close();
} catch (SQLException e) {
System.err.println("Błąd podczas close: " + e.getMessage());
}
}
Connection c() {
return c;
}
public void beginTransaction() throws BladBazyDanych {
try {
c.setAutoCommit(false);
//c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
} catch (SQLException e) {
throw new BladBazyDanych("beginTransaction: " + e.getMessage(), e);
}
}
public void endTransaction(boolean commit) throws BladBazyDanych {
try {
if (commit) {
c.commit();
} else {
c.rollback();
}
c.setAutoCommit(true);
} catch (SQLException e) {
throw new BladBazyDanych("endTransaction: " + e.getMessage(), e);
}
}
public OgloszenieDAO newOgloszenieDAO() {
return new OgloszenieDAO(this);
}
public SprzedawcaDAO newSprzedawcaDAO() {
return new SprzedawcaDAO(this);
}
public OgloszenieDAO newOgloszenieDAO() {
return new OgloszenieDAO(this);
}
public SprzedawcaDAO newSprzedawcaDAO() {
return new SprzedawcaDAO(this);
}
} }
...@@ -6,19 +6,19 @@ import ogloszenia.model.Paliwo; ...@@ -6,19 +6,19 @@ import ogloszenia.model.Paliwo;
public class Konwersje { public class Konwersje {
public static LocalDateTime dateFromString(String s) { public static LocalDateTime dateFromString(String s) {
return s == null ? null : LocalDateTime.parse(s.replace(' ', 'T')); return s == null ? null : LocalDateTime.parse(s.replace(' ', 'T'));
} }
public static String dateToString(LocalDateTime localDateTime) { public static String dateToString(LocalDateTime localDateTime) {
return localDateTime == null ? null : localDateTime.toString().replace('T', ' '); return localDateTime == null ? null : localDateTime.toString().replace('T', ' ');
} }
public static Paliwo paliwoFromString(String s) {
return s == null ? null : Paliwo.valueOf(s.trim().toUpperCase());
}
public static String paliwoToString(Paliwo paliwo) { public static Paliwo paliwoFromString(String s) {
return paliwo == null ? null : paliwo.toString(); return s == null ? null : Paliwo.valueOf(s.trim().toUpperCase());
} }
public static String paliwoToString(Paliwo paliwo) {
return paliwo == null ? null : paliwo.toString();
}
} }
...@@ -18,342 +18,343 @@ import ogloszenia.model.Silnik; ...@@ -18,342 +18,343 @@ import ogloszenia.model.Silnik;
import ogloszenia.model.Sprzedawca; import ogloszenia.model.Sprzedawca;
public class OgloszenieDAO { public class OgloszenieDAO {
private DostepDoBazySqlite db; private DostepDoBazySqlite db;
OgloszenieDAO(DostepDoBazySqlite db) { OgloszenieDAO(DostepDoBazySqlite db) {
this.db = db; this.db = db;
} }
public DostepDoBazySqlite getDBHandler() { public DostepDoBazySqlite getDBHandler() {
return db; return db;
} }
public List<Integer> idList() throws BladBazyDanych { public List<Integer> idList() throws BladBazyDanych {
final String sql = "SELECT id_ogloszenia FROM ogloszenia ORDER BY id_ogloszenia"; final String sql = "SELECT id_ogloszenia FROM ogloszenia ORDER BY id_ogloszenia";
List<Integer> lista = new LinkedList<>(); List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
lista.add(rs.getInt(1)); lista.add(rs.getInt(1));
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("listaIdOgloszen: "+e.getMessage(), e); throw new BladBazyDanych("listaIdOgloszen: " + e.getMessage(), e);
} }
return lista; return lista;
} }
public OgloszenieSamochodowe findById(int idOgloszenia, boolean full) throws BladBazyDanych, NieznanyRekord { public OgloszenieSamochodowe findById(int idOgloszenia, boolean full) throws BladBazyDanych, NieznanyRekord {
final String sql = "SELECT * FROM ogloszenia WHERE id_ogloszenia = ?"; final String sql = "SELECT * FROM ogloszenia WHERE id_ogloszenia = ?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idOgloszenia); stmt.setInt(1, idOgloszenia);
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
if(rs.next()) { if (rs.next()) {
OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs); OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs);
if(full) { if (full) {
doczytajSprzedawce(ogl); doczytajSprzedawce(ogl);
} }
return ogl; return ogl;
} else { } else {
throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia); throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie byIdFull: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie byIdFull: " + e.getMessage(), e);
} }
} }
public OgloszenieSamochodowe findById(int idOgloszenia) throws BladBazyDanych, NieznanyRekord { public OgloszenieSamochodowe findById(int idOgloszenia) throws BladBazyDanych, NieznanyRekord {
return findById(idOgloszenia, false); return findById(idOgloszenia, false);
} }
public OgloszenieSamochodowe findByIdFull(int idOgloszenia) throws BladBazyDanych, NieznanyRekord { public OgloszenieSamochodowe findByIdFull(int idOgloszenia) throws BladBazyDanych, NieznanyRekord {
return findById(idOgloszenia, true); return findById(idOgloszenia, true);
} }
public List<OgloszenieSamochodowe> readAll(boolean full) throws BladBazyDanych { public List<OgloszenieSamochodowe> readAll(boolean full) throws BladBazyDanych {
final String sql = "SELECT * FROM ogloszenia"; final String sql = "SELECT * FROM ogloszenia";
List<OgloszenieSamochodowe> lista = new LinkedList<>(); List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs); OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs);
if(full) try { if (full) try {
doczytajSprzedawce(ogl); doczytajSprzedawce(ogl);
} catch (NieznanyRekord e) { } catch (NieznanyRekord e) {
System.err.println(e); System.err.println(e);
} }
lista.add(ogl); lista.add(ogl);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie readAll: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie readAll: " + e.getMessage(), e);
} }
return lista; return lista;
} }
public List<OgloszenieSamochodowe> readAll() throws BladBazyDanych { public List<OgloszenieSamochodowe> readAll() throws BladBazyDanych {
return readAll(false); return readAll(false);
} }
public List<OgloszenieSamochodowe> readAllFull() throws BladBazyDanych { public List<OgloszenieSamochodowe> readAllFull() throws BladBazyDanych {
return readAll(true); return readAll(true);
} }
public List<OgloszenieSamochodowe> ogloszeniaWedlugCeny(BigDecimal cenaOd, BigDecimal cenaDo) throws BladBazyDanych { public List<OgloszenieSamochodowe> ogloszeniaWedlugCeny(BigDecimal cenaOd, BigDecimal cenaDo) throws BladBazyDanych {
final String sql = "SELECT * FROM ogloszenia" final String sql = "SELECT * FROM ogloszenia"
+ " WHERE cena BETWEEN ? AND ?" + " WHERE cena BETWEEN ? AND ?"
+ " ORDER BY id_ogloszenia"; + " ORDER BY id_ogloszenia";
if(cenaOd == null) if (cenaOd == null)
cenaOd = BigDecimal.ZERO; cenaOd = BigDecimal.ZERO;
if(cenaDo == null) if (cenaDo == null)
cenaDo = new BigDecimal(100000000); cenaDo = new BigDecimal(100000000);
List<OgloszenieSamochodowe> lista = new LinkedList<>(); List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setBigDecimal(1, cenaOd); stmt.setBigDecimal(1, cenaOd);
stmt.setBigDecimal(2, cenaDo); stmt.setBigDecimal(2, cenaDo);
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs); OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs);
try { try {
doczytajSprzedawce(ogl); doczytajSprzedawce(ogl);
} catch (NieznanyRekord e) { } catch (NieznanyRekord e) {
e.printStackTrace(); e.printStackTrace();
} }
lista.add(ogl); lista.add(ogl);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Błąd bazy w metodzie ogloszeniaWedlugCeny: " + e.getMessage(), e); throw new BladBazyDanych("Błąd bazy w metodzie ogloszeniaWedlugCeny: " + e.getMessage(), e);
} }
return lista; return lista;
} }
public List<OgloszenieSamochodowe> ogloszeniaSprzedawcy(int idSprzedawcy) throws BladBazyDanych { public List<OgloszenieSamochodowe> ogloszeniaSprzedawcy(int idSprzedawcy) throws BladBazyDanych {
final String sql = "SELECT * FROM ogloszenia WHERE id_sprzedawcy = ?"; final String sql = "SELECT * FROM ogloszenia WHERE id_sprzedawcy = ?";
List<OgloszenieSamochodowe> lista = new LinkedList<>(); List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idSprzedawcy); stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
lista.add(ogloszenieZResultSet(rs)); lista.add(ogloszenieZResultSet(rs));
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("ogloszeniaSprzedawcy: "+e.getMessage(), e); throw new BladBazyDanych("ogloszeniaSprzedawcy: " + e.getMessage(), e);
} }
return lista; return lista;
} }
public List<Integer> listaIdOgloszenSprzedawcy(int idSprzedawcy) throws BladBazyDanych { public List<Integer> listaIdOgloszenSprzedawcy(int idSprzedawcy) throws BladBazyDanych {
final String sql = "SELECT id_ogloszenia FROM ogloszenia WHERE id_sprzedawcy = ?"; final String sql = "SELECT id_ogloszenia FROM ogloszenia WHERE id_sprzedawcy = ?";
List<Integer> lista = new LinkedList<>(); List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idSprzedawcy); stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
lista.add(rs.getInt(1)); lista.add(rs.getInt(1));
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("listaIdOgloszenSprzedawcy: "+e.getMessage(), e); throw new BladBazyDanych("listaIdOgloszenSprzedawcy: " + e.getMessage(), e);
} }
return lista; return lista;
} }
public void save(OgloszenieSamochodowe ogl) throws BladBazyDanych { public void save(OgloszenieSamochodowe ogl) throws BladBazyDanych {
if(ogl.getIdOgloszenia() == null) { if (ogl.getIdOgloszenia() == null) {
// wstawiamy nowy rekord korzystajac z autoincrement // wstawiamy nowy rekord korzystajac z autoincrement
insertNew(ogl); insertNew(ogl);
} else if(! update(ogl)) { } else if (!update(ogl)) {
insert(ogl); insert(ogl);
} }
} }
public boolean insert(OgloszenieSamochodowe ogl) throws BladBazyDanych { public boolean insert(OgloszenieSamochodowe ogl) throws BladBazyDanych {
uzupelnijDate(ogl); uzupelnijDate(ogl);
final String sql = "INSERT INTO ogloszenia(" final String sql = "INSERT INTO ogloszenia("
+ " id_ogloszenia, id_sprzedawcy, data_wystawienia, cena, tytul, opis," + " id_ogloszenia, id_sprzedawcy, data_wystawienia, cena, tytul, opis,"
+ " marka, model, generacja, kolor, rocznik, przebieg, pojemnosc, moc, paliwo)" + " marka, model, generacja, kolor, rocznik, przebieg, pojemnosc, moc, paliwo)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, ogl.getIdOgloszenia()); stmt.setInt(1, ogl.getIdOgloszenia());
stmt.setInt(2, ogl.getIdSprzedawcy()); stmt.setInt(2, ogl.getIdSprzedawcy());
stmt.setString(3, Konwersje.dateToString(ogl.getDataWystawienia())); stmt.setString(3, Konwersje.dateToString(ogl.getDataWystawienia()));
stmt.setBigDecimal(4, ogl.getCena()); stmt.setBigDecimal(4, ogl.getCena());
stmt.setString(5, ogl.getTytul()); stmt.setString(5, ogl.getTytul());
stmt.setString(6, ogl.getOpis()); stmt.setString(6, ogl.getOpis());
stmt.setString(7, ogl.getMarka()); stmt.setString(7, ogl.getMarka());
stmt.setString(8, ogl.getModel()); stmt.setString(8, ogl.getModel());
stmt.setString(9, ogl.getGeneracja()); stmt.setString(9, ogl.getGeneracja());
stmt.setString(10, ogl.getKolor()); stmt.setString(10, ogl.getKolor());
stmt.setInt(11, ogl.getRocznik()); stmt.setInt(11, ogl.getRocznik());
stmt.setInt(12, ogl.getPrzebieg()); stmt.setInt(12, ogl.getPrzebieg());
Silnik silnik = ogl.getSilnik(); Silnik silnik = ogl.getSilnik();
if(silnik != null) { if (silnik != null) {
stmt.setFloat(13, silnik.getMoc()); stmt.setFloat(13, silnik.getMoc());
stmt.setFloat(14, silnik.getPojemnosc()); stmt.setFloat(14, silnik.getPojemnosc());
stmt.setString(15, Konwersje.paliwoToString(silnik.getPaliwo())); stmt.setString(15, Konwersje.paliwoToString(silnik.getPaliwo()));
} }
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
return (ile > 0); return (ile > 0);
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie insert: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie insert: " + e.getMessage(), e);
} }
} }
public int insertNew(OgloszenieSamochodowe ogl) throws BladBazyDanych { public int insertNew(OgloszenieSamochodowe ogl) throws BladBazyDanych {
uzupelnijDate(ogl); uzupelnijDate(ogl);
final String sql = "INSERT INTO ogloszenia(" final String sql = "INSERT INTO ogloszenia("
+ " id_sprzedawcy, data_wystawienia, cena, tytul, opis, marka, model, generacja, kolor, rocznik, przebieg, pojemnosc, moc, paliwo)" + " id_sprzedawcy, data_wystawienia, cena, tytul, opis, marka, model, generacja, kolor, rocznik, przebieg, pojemnosc, moc, paliwo)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try { try {
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, ogl.getIdSprzedawcy()); stmt.setInt(1, ogl.getIdSprzedawcy());
stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia())); stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia()));
stmt.setBigDecimal(3, ogl.getCena()); stmt.setBigDecimal(3, ogl.getCena());
stmt.setString(4, ogl.getTytul()); stmt.setString(4, ogl.getTytul());
stmt.setString(5, ogl.getOpis()); stmt.setString(5, ogl.getOpis());
stmt.setString(6, ogl.getMarka()); stmt.setString(6, ogl.getMarka());
stmt.setString(7, ogl.getModel()); stmt.setString(7, ogl.getModel());
stmt.setString(8, ogl.getGeneracja()); stmt.setString(8, ogl.getGeneracja());
stmt.setString(9, ogl.getKolor()); stmt.setString(9, ogl.getKolor());
stmt.setInt(10, ogl.getRocznik()); stmt.setInt(10, ogl.getRocznik());
stmt.setInt(11, ogl.getPrzebieg()); stmt.setInt(11, ogl.getPrzebieg());
Silnik silnik = ogl.getSilnik(); Silnik silnik = ogl.getSilnik();
if(silnik != null) { if (silnik != null) {
stmt.setFloat(12, silnik.getPojemnosc()); stmt.setFloat(12, silnik.getPojemnosc());
stmt.setFloat(13, silnik.getMoc()); stmt.setFloat(13, silnik.getMoc());
stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo())); stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo()));
} }
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
if(ile == 0) { if (ile == 0) {
throw new SQLException("Nie mogę wstawić nowego rekordu ogloszenie"); throw new SQLException("Nie mogę wstawić nowego rekordu ogloszenie");
} }
} }
try(Statement stmt = db.c().createStatement(); try (Statement stmt = db.c().createStatement();
ResultSet rs = stmt.executeQuery("SELECT last_insert_rowid()")) { ResultSet rs = stmt.executeQuery("SELECT last_insert_rowid()")) {
if(rs.next()) { if (rs.next()) {
int noweId = rs.getInt(1); int noweId = rs.getInt(1);
ogl.setIdOgloszenia(noweId); ogl.setIdOgloszenia(noweId);
return noweId; return noweId;
} else { } else {
throw new SQLException("Nie mogę pobrać id nowego rekordu ogloszenie"); throw new SQLException("Nie mogę pobrać id nowego rekordu ogloszenie");
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie insertNew: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie insertNew: " + e.getMessage(), e);
} }
} }
public boolean update(OgloszenieSamochodowe ogl) throws BladBazyDanych { public boolean update(OgloszenieSamochodowe ogl) throws BladBazyDanych {
final String sql = "UPDATE ogloszenia SET id_sprzedawcy=?, data_wystawienia=?, cena=?," final String sql = "UPDATE ogloszenia SET id_sprzedawcy=?, data_wystawienia=?, cena=?,"
+ " tytul=?, opis=?, marka=?, model=?, generacja=?," + " tytul=?, opis=?, marka=?, model=?, generacja=?,"
+ " kolor=?, rocznik=?, przebieg=?, pojemnosc=?, moc=?, paliwo=?" + " kolor=?, rocznik=?, przebieg=?, pojemnosc=?, moc=?, paliwo=?"
+ " WHERE id_ogloszenia=?"; + " WHERE id_ogloszenia=?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, ogl.getIdSprzedawcy()); stmt.setInt(1, ogl.getIdSprzedawcy());
stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia())); stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia()));
stmt.setBigDecimal(3, ogl.getCena()); stmt.setBigDecimal(3, ogl.getCena());
stmt.setString(4, ogl.getTytul()); stmt.setString(4, ogl.getTytul());
stmt.setString(5, ogl.getOpis()); stmt.setString(5, ogl.getOpis());
stmt.setString(6, ogl.getMarka()); stmt.setString(6, ogl.getMarka());
stmt.setString(7, ogl.getModel()); stmt.setString(7, ogl.getModel());
stmt.setString(8, ogl.getGeneracja()); stmt.setString(8, ogl.getGeneracja());
stmt.setString(9, ogl.getKolor()); stmt.setString(9, ogl.getKolor());
stmt.setInt(10, ogl.getRocznik()); stmt.setInt(10, ogl.getRocznik());
stmt.setInt(11, ogl.getPrzebieg()); stmt.setInt(11, ogl.getPrzebieg());
Silnik silnik = ogl.getSilnik(); Silnik silnik = ogl.getSilnik();
if(silnik != null) { if (silnik != null) {
stmt.setFloat(12, silnik.getPojemnosc()); stmt.setFloat(12, silnik.getPojemnosc());
stmt.setFloat(13, silnik.getMoc()); stmt.setFloat(13, silnik.getMoc());
stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo())); stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo()));
} }
stmt.setInt(15, ogl.getIdOgloszenia()); stmt.setInt(15, ogl.getIdOgloszenia());
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
return (ile > 0); return (ile > 0);
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie update: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie update: " + e.getMessage(), e);
} }
} }
public void zmienCene(int idOgloszenia, BigDecimal nowaCena) public void zmienCene(int idOgloszenia, BigDecimal nowaCena)
throws BladBazyDanych, NieznanyRekord { throws BladBazyDanych, NieznanyRekord {
final String sql = "UPDATE ogloszenia SET cena=?" final String sql = "UPDATE ogloszenia SET cena=?"
+ " WHERE id_ogloszenia=?"; + " WHERE id_ogloszenia=?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setBigDecimal(1, nowaCena); stmt.setBigDecimal(1, nowaCena);
stmt.setInt(2, idOgloszenia); stmt.setInt(2, idOgloszenia);
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
if(ile == 0) { if (ile == 0) {
throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia); throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie zmienCene: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie zmienCene: " + e.getMessage(), e);
} }
} }
public void delete(int idOgloszenia) public void delete(int idOgloszenia)
throws BladBazyDanych, NieznanyRekord { throws BladBazyDanych, NieznanyRekord {
final String sql = "DELETE FROM ogloszenia WHERE id_ogloszenia=?"; final String sql = "DELETE FROM ogloszenia WHERE id_ogloszenia=?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idOgloszenia); stmt.setInt(1, idOgloszenia);
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
if(ile == 0) { if (ile == 0) {
throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia); throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie delete: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie delete: " + e.getMessage(), e);
} }
} }
private OgloszenieSamochodowe ogloszenieZResultSet(ResultSet rs) throws SQLException {
Silnik silnik = new Silnik(rs.getFloat("moc"), private OgloszenieSamochodowe ogloszenieZResultSet(ResultSet rs) throws SQLException {
rs.getFloat("pojemnosc"), Silnik silnik = new Silnik(rs.getFloat("moc"),
Konwersje.paliwoFromString(rs.getString("paliwo"))); rs.getFloat("pojemnosc"),
Konwersje.paliwoFromString(rs.getString("paliwo")));
return new OgloszenieSamochodowe(
rs.getInt("id_ogloszenia"), return new OgloszenieSamochodowe(
rs.getInt("id_sprzedawcy"), rs.getInt("id_ogloszenia"),
Konwersje.dateFromString(rs.getString("data_wystawienia")), rs.getInt("id_sprzedawcy"),
rs.getBigDecimal("cena"), Konwersje.dateFromString(rs.getString("data_wystawienia")),
rs.getString("tytul"), rs.getBigDecimal("cena"),
rs.getString("opis"), rs.getString("tytul"),
rs.getString("marka"), rs.getString("opis"),
rs.getString("model"), rs.getString("marka"),
rs.getString("generacja"), rs.getString("model"),
rs.getString("kolor"), rs.getString("generacja"),
rs.getInt("rocznik"), rs.getString("kolor"),
rs.getInt("przebieg"), rs.getInt("rocznik"),
silnik rs.getInt("przebieg"),
); silnik
} );
}
void doczytajSprzedawce(Ogloszenie ogl) throws BladBazyDanych, NieznanyRekord {
SprzedawcaDAO sDao = db.newSprzedawcaDAO(); void doczytajSprzedawce(Ogloszenie ogl) throws BladBazyDanych, NieznanyRekord {
Sprzedawca sprzedawca = sDao.findById(ogl.getIdSprzedawcy()); SprzedawcaDAO sDao = db.newSprzedawcaDAO();
ogl.setSprzedawca(sprzedawca); Sprzedawca sprzedawca = sDao.findById(ogl.getIdSprzedawcy());
} ogl.setSprzedawca(sprzedawca);
}
private void uzupelnijDate(OgloszenieSamochodowe ogl) {
if(ogl.getDataWystawienia() == null) { private void uzupelnijDate(OgloszenieSamochodowe ogl) {
ogl.setDataWystawienia(LocalDateTime.now()); if (ogl.getDataWystawienia() == null) {
} ogl.setDataWystawienia(LocalDateTime.now());
} }
}
} }
...@@ -12,69 +12,69 @@ import ogloszenia.model.Adres; ...@@ -12,69 +12,69 @@ import ogloszenia.model.Adres;
import ogloszenia.model.Sprzedawca; import ogloszenia.model.Sprzedawca;
public class SprzedawcaDAO { public class SprzedawcaDAO {
private DostepDoBazySqlite db; private DostepDoBazySqlite db;
SprzedawcaDAO(DostepDoBazySqlite db) {
this.db = db;
}
public DostepDoBazySqlite getDBHandler() {
return db;
}
public List<Integer> idList() throws BladBazyDanych {
final String sql = "SELECT id_sprzedawcy FROM sprzedawcy";
List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
lista.add(rs.getInt(1));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("listaIdSprzedawcow: "+e.getMessage(), e);
}
return lista;
}
public Sprzedawca findById(int idSprzedawcy) throws BladBazyDanych, NieznanyRekord { SprzedawcaDAO(DostepDoBazySqlite db) {
final String sql = "SELECT * FROM sprzedawcy WHERE id_sprzedawcy = ?"; this.db = db;
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { }
stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) {
if(rs.next()) {
return sprzedawcaZResultSet(rs);
} else {
throw new NieznanyRekord("Nieznany sprzedawca " + idSprzedawcy);
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Sprzedawca byId: "+e.getMessage(), e);
}
}
public List<Sprzedawca> readAll() throws BladBazyDanych { public DostepDoBazySqlite getDBHandler() {
final String sql = "SELECT * FROM sprzedawcy"; return db;
List<Sprzedawca> lista = new LinkedList<>(); }
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) { public List<Integer> idList() throws BladBazyDanych {
while(rs.next()) { final String sql = "SELECT id_sprzedawcy FROM sprzedawcy";
lista.add(sprzedawcaZResultSet(rs)); List<Integer> lista = new LinkedList<>();
} try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
} try (ResultSet rs = stmt.executeQuery()) {
} catch (SQLException e) { while (rs.next()) {
throw new BladBazyDanych("Sprzedawca readAll: "+e.getMessage(), e); lista.add(rs.getInt(1));
} }
return lista; }
} } catch (SQLException e) {
throw new BladBazyDanych("listaIdSprzedawcow: " + e.getMessage(), e);
private Sprzedawca sprzedawcaZResultSet(ResultSet rs) throws SQLException { }
Adres adres = new Adres(rs.getString("ulica"), return lista;
rs.getString("kod_pocztowy"), rs.getString("miasto")); }
return new Sprzedawca( public Sprzedawca findById(int idSprzedawcy) throws BladBazyDanych, NieznanyRekord {
rs.getInt("id_sprzedawcy"), rs.getString("nazwa"), final String sql = "SELECT * FROM sprzedawcy WHERE id_sprzedawcy = ?";
adres, rs.getString("telefon"), rs.getString("email") try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
); stmt.setInt(1, idSprzedawcy);
} try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return sprzedawcaZResultSet(rs);
} else {
throw new NieznanyRekord("Nieznany sprzedawca " + idSprzedawcy);
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Sprzedawca byId: " + e.getMessage(), e);
}
}
public List<Sprzedawca> readAll() throws BladBazyDanych {
final String sql = "SELECT * FROM sprzedawcy";
List<Sprzedawca> lista = new LinkedList<>();
try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
lista.add(sprzedawcaZResultSet(rs));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Sprzedawca readAll: " + e.getMessage(), e);
}
return lista;
}
private Sprzedawca sprzedawcaZResultSet(ResultSet rs) throws SQLException {
Adres adres = new Adres(rs.getString("ulica"),
rs.getString("kod_pocztowy"), rs.getString("miasto"));
return new Sprzedawca(
rs.getInt("id_sprzedawcy"), rs.getString("nazwa"),
adres, rs.getString("telefon"), rs.getString("email")
);
}
} }
...@@ -12,35 +12,35 @@ import ogloszenia.Ustawienia; ...@@ -12,35 +12,35 @@ import ogloszenia.Ustawienia;
public class UtworzBaze { public class UtworzBaze {
public static void main(String[] args) { public static void main(String[] args) {
String skrypt = ""; String skrypt = "";
try { try {
skrypt = String.join("\n", Files.readAllLines(Paths.get("ogloszenia_sqlite.sql"))); skrypt = String.join("\n", Files.readAllLines(Paths.get("ogloszenia_sqlite.sql")));
if(Files.deleteIfExists(Paths.get(Ustawienia.SQLITE_PATH))) { if (Files.deleteIfExists(Paths.get(Ustawienia.SQLITE_PATH))) {
System.out.println("Usunięto plik " + Ustawienia.SQLITE_PATH); System.out.println("Usunięto plik " + Ustawienia.SQLITE_PATH);
} }
} catch (IOException e) { } catch (IOException e) {
System.err.println("Błąd podczas przygotowania plików: " + e); System.err.println("Błąd podczas przygotowania plików: " + e);
} }
String[] poleceniaSkryptu = skrypt.split("\\s*;\\s*"); String[] poleceniaSkryptu = skrypt.split("\\s*;\\s*");
System.out.println("Otwarcie pliku z bazą..."); System.out.println("Otwarcie pliku z bazą...");
try(Connection c = DriverManager.getConnection("jdbc:sqlite:" + Ustawienia.SQLITE_PATH); try (Connection c = DriverManager.getConnection("jdbc:sqlite:" + Ustawienia.SQLITE_PATH);
Statement stmt = c.createStatement()) { Statement stmt = c.createStatement()) {
c.setAutoCommit(false); c.setAutoCommit(false);
System.out.println("Wgrywam dane"); System.out.println("Wgrywam dane");
for(String sql : poleceniaSkryptu) { for (String sql : poleceniaSkryptu) {
stmt.addBatch(sql); stmt.addBatch(sql);
System.out.print("."); System.out.print(".");
System.out.flush(); System.out.flush();
} }
stmt.executeBatch(); stmt.executeBatch();
c.commit(); c.commit();
System.out.println("\nGotowe"); System.out.println("\nGotowe");
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("Błąd podczas wgrywania danych: " + e); System.err.println("Błąd podczas wgrywania danych: " + e);
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
...@@ -2,19 +2,19 @@ package ogloszenia.exn; ...@@ -2,19 +2,19 @@ package ogloszenia.exn;
public class BladAplikacji extends Exception { public class BladAplikacji extends Exception {
public BladAplikacji() { public BladAplikacji() {
super(); super();
} }
public BladAplikacji(String message, Throwable cause) { public BladAplikacji(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
public BladAplikacji(String message) { public BladAplikacji(String message) {
super(message); super(message);
} }
public BladAplikacji(Throwable cause) { public BladAplikacji(Throwable cause) {
super(cause); super(cause);
} }
} }
...@@ -2,20 +2,20 @@ package ogloszenia.exn; ...@@ -2,20 +2,20 @@ package ogloszenia.exn;
public class BladBazyDanych extends BladAplikacji { public class BladBazyDanych extends BladAplikacji {
public BladBazyDanych() { public BladBazyDanych() {
super(); super();
} }
public BladBazyDanych(String message, Throwable cause) { public BladBazyDanych(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
public BladBazyDanych(String message) { public BladBazyDanych(String message) {
super(message); super(message);
} }
public BladBazyDanych(Throwable cause) { public BladBazyDanych(Throwable cause) {
super(cause); super(cause);
} }
} }
...@@ -2,12 +2,12 @@ package ogloszenia.exn; ...@@ -2,12 +2,12 @@ package ogloszenia.exn;
public class NieznanyRekord extends BladAplikacji { public class NieznanyRekord extends BladAplikacji {
public NieznanyRekord() { public NieznanyRekord() {
super(); super();
} }
public NieznanyRekord(String message) { public NieznanyRekord(String message) {
super(message); super(message);
} }
} }
package ogloszenia.model; package ogloszenia.model;
public class Adres { public class Adres {
private String ulica; private String ulica;
private String kodPocztowy; private String kodPocztowy;
private String miasto; private String miasto;
public Adres() { public Adres() {
} }
public Adres(String ulica, String kodPocztowy, String miasto) { public Adres(String ulica, String kodPocztowy, String miasto) {
this.ulica = ulica; this.ulica = ulica;
this.kodPocztowy = kodPocztowy; this.kodPocztowy = kodPocztowy;
this.miasto = miasto; this.miasto = miasto;
} }
public String getUlica() { public String getUlica() {
return ulica; return ulica;
} }
public String getKodPocztowy() { public String getKodPocztowy() {
return kodPocztowy; return kodPocztowy;
} }
public String getMiasto() { public String getMiasto() {
return miasto; return miasto;
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + ((kodPocztowy == null) ? 0 : kodPocztowy.hashCode()); result = prime * result + ((kodPocztowy == null) ? 0 : kodPocztowy.hashCode());
result = prime * result + ((miasto == null) ? 0 : miasto.hashCode()); result = prime * result + ((miasto == null) ? 0 : miasto.hashCode());
result = prime * result + ((ulica == null) ? 0 : ulica.hashCode()); result = prime * result + ((ulica == null) ? 0 : ulica.hashCode());
return result; return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)
return true; return true;
if (obj == null) if (obj == null)
return false; return false;
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
Adres other = (Adres) obj; Adres other = (Adres) obj;
if (kodPocztowy == null) { if (kodPocztowy == null) {
if (other.kodPocztowy != null) if (other.kodPocztowy != null)
return false; return false;
} else if (!kodPocztowy.equals(other.kodPocztowy)) } else if (!kodPocztowy.equals(other.kodPocztowy))
return false; return false;
if (miasto == null) { if (miasto == null) {
if (other.miasto != null) if (other.miasto != null)
return false; return false;
} else if (!miasto.equals(other.miasto)) } else if (!miasto.equals(other.miasto))
return false; return false;
if (ulica == null) { if (ulica == null) {
if (other.ulica != null) if (other.ulica != null)
return false; return false;
} else if (!ulica.equals(other.ulica)) } else if (!ulica.equals(other.ulica))
return false; return false;
return true; return true;
} }
@Override @Override
public String toString() { public String toString() {
return String.format("%s, %s %s", ulica, kodPocztowy, miasto); return String.format("%s, %s %s", ulica, kodPocztowy, miasto);
} }
} }
...@@ -3,11 +3,11 @@ package ogloszenia.model; ...@@ -3,11 +3,11 @@ package ogloszenia.model;
import java.util.List; import java.util.List;
public class ListaOgloszen { public class ListaOgloszen {
public List<OgloszenieSamochodowe> ogloszenia; public List<OgloszenieSamochodowe> ogloszenia;
public static ListaOgloszen nowa(List<OgloszenieSamochodowe> ogloszenia) { public static ListaOgloszen nowa(List<OgloszenieSamochodowe> ogloszenia) {
ListaOgloszen lista = new ListaOgloszen(); ListaOgloszen lista = new ListaOgloszen();
lista.ogloszenia = ogloszenia; lista.ogloszenia = ogloszenia;
return lista; return lista;
} }
} }
...@@ -4,156 +4,160 @@ import java.math.BigDecimal; ...@@ -4,156 +4,160 @@ import java.math.BigDecimal;
import java.time.LocalDateTime; import java.time.LocalDateTime;
public class Ogloszenie { public class Ogloszenie {
private Integer idOgloszenia; private Integer idOgloszenia;
private Integer idSprzedawcy; private Integer idSprzedawcy;
private LocalDateTime dataWystawienia; private LocalDateTime dataWystawienia;
private BigDecimal cena; private BigDecimal cena;
private String tytul; private String tytul;
private String opis; private String opis;
private Sprzedawca sprzedawca; private Sprzedawca sprzedawca;
public Ogloszenie() { public Ogloszenie() {
} }
public Ogloszenie(Integer idOgloszenia, Integer idSprzedawcy, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) { public Ogloszenie(Integer idOgloszenia, Integer idSprzedawcy, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia; this.idOgloszenia = idOgloszenia;
this.idSprzedawcy = idSprzedawcy; this.idSprzedawcy = idSprzedawcy;
this.dataWystawienia = dataWystawienia; this.dataWystawienia = dataWystawienia;
this.cena = cena; this.cena = cena;
this.tytul = tytul; this.tytul = tytul;
this.opis = opis; this.opis = opis;
} }
/** Pozostawia pole sprzedawca równe null. */ /**
public Ogloszenie(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) { * Pozostawia pole sprzedawca równe null.
this.idOgloszenia = idOgloszenia; */
this.dataWystawienia = dataWystawienia; public Ogloszenie(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.cena = cena; this.idOgloszenia = idOgloszenia;
this.tytul = tytul; this.dataWystawienia = dataWystawienia;
this.opis = opis; this.cena = cena;
this.idSprzedawcy = null; this.tytul = tytul;
} this.opis = opis;
this.idSprzedawcy = null;
/** Wartość idSprzedawcy pobiera z obiektu sprzedawca. */ }
public Ogloszenie(Integer idOgloszenia, Sprzedawca sprzedawca, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia; /**
this.dataWystawienia = dataWystawienia; * Wartość idSprzedawcy pobiera z obiektu sprzedawca.
this.cena = cena; */
this.tytul = tytul; public Ogloszenie(Integer idOgloszenia, Sprzedawca sprzedawca, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.opis = opis; this.idOgloszenia = idOgloszenia;
this.sprzedawca = sprzedawca; this.dataWystawienia = dataWystawienia;
if(sprzedawca != null) { this.cena = cena;
this.idSprzedawcy = sprzedawca.getIdSprzedawcy(); this.tytul = tytul;
} this.opis = opis;
} this.sprzedawca = sprzedawca;
if (sprzedawca != null) {
public Integer getIdOgloszenia() { this.idSprzedawcy = sprzedawca.getIdSprzedawcy();
return idOgloszenia; }
} }
public void setIdOgloszenia(Integer noweId) { public Integer getIdOgloszenia() {
this.idOgloszenia = noweId; return idOgloszenia;
} }
public String getTytul() { public void setIdOgloszenia(Integer noweId) {
return tytul; this.idOgloszenia = noweId;
} }
public BigDecimal getCena() { public String getTytul() {
return cena; return tytul;
} }
public Integer getIdSprzedawcy() { public BigDecimal getCena() {
return idSprzedawcy; return cena;
} }
public Sprzedawca getSprzedawca() { public Integer getIdSprzedawcy() {
return sprzedawca; return idSprzedawcy;
} }
public void setSprzedawca(Sprzedawca sprzedawca) { public Sprzedawca getSprzedawca() {
this.sprzedawca = sprzedawca; return sprzedawca;
} }
public String getOpis() { public void setSprzedawca(Sprzedawca sprzedawca) {
return opis; this.sprzedawca = sprzedawca;
} }
public void setOpis(String opis) { public String getOpis() {
this.opis = opis; return opis;
} }
public void setCena(BigDecimal cena) { public void setOpis(String opis) {
this.cena = cena; this.opis = opis;
} }
public void setCena(BigDecimal cena) {
public LocalDateTime getDataWystawienia() { this.cena = cena;
return dataWystawienia; }
}
public void setDataWystawienia(LocalDateTime dataWystawienia) { public LocalDateTime getDataWystawienia() {
this.dataWystawienia = dataWystawienia; return dataWystawienia;
} }
@Override public void setDataWystawienia(LocalDateTime dataWystawienia) {
public int hashCode() { this.dataWystawienia = dataWystawienia;
final int prime = 31; }
int result = 1;
result = prime * result + ((cena == null) ? 0 : cena.hashCode()); @Override
result = prime * result + ((dataWystawienia == null) ? 0 : dataWystawienia.hashCode()); public int hashCode() {
result = prime * result + ((idOgloszenia == null) ? 0 : idOgloszenia.hashCode()); final int prime = 31;
result = prime * result + ((idSprzedawcy == null) ? 0 : idSprzedawcy.hashCode()); int result = 1;
result = prime * result + ((opis == null) ? 0 : opis.hashCode()); result = prime * result + ((cena == null) ? 0 : cena.hashCode());
result = prime * result + ((sprzedawca == null) ? 0 : sprzedawca.hashCode()); result = prime * result + ((dataWystawienia == null) ? 0 : dataWystawienia.hashCode());
return result; result = prime * result + ((idOgloszenia == null) ? 0 : idOgloszenia.hashCode());
} result = prime * result + ((idSprzedawcy == null) ? 0 : idSprzedawcy.hashCode());
result = prime * result + ((opis == null) ? 0 : opis.hashCode());
@Override result = prime * result + ((sprzedawca == null) ? 0 : sprzedawca.hashCode());
public boolean equals(Object obj) { return result;
if (this == obj) }
return true;
if (obj == null) @Override
return false; public boolean equals(Object obj) {
if (getClass() != obj.getClass()) if (this == obj)
return false; return true;
Ogloszenie other = (Ogloszenie) obj; if (obj == null)
if (cena == null) { return false;
if (other.cena != null) if (getClass() != obj.getClass())
return false; return false;
} else if (!cena.equals(other.cena)) Ogloszenie other = (Ogloszenie) obj;
return false; if (cena == null) {
if (dataWystawienia == null) { if (other.cena != null)
if (other.dataWystawienia != null) return false;
return false; } else if (!cena.equals(other.cena))
} else if (!dataWystawienia.equals(other.dataWystawienia)) return false;
return false; if (dataWystawienia == null) {
if (idOgloszenia == null) { if (other.dataWystawienia != null)
if (other.idOgloszenia != null) return false;
return false; } else if (!dataWystawienia.equals(other.dataWystawienia))
} else if (!idOgloszenia.equals(other.idOgloszenia)) return false;
return false; if (idOgloszenia == null) {
if (idSprzedawcy == null) { if (other.idOgloszenia != null)
if (other.idSprzedawcy != null) return false;
return false; } else if (!idOgloszenia.equals(other.idOgloszenia))
} else if (!idSprzedawcy.equals(other.idSprzedawcy)) return false;
return false; if (idSprzedawcy == null) {
if (opis == null) { if (other.idSprzedawcy != null)
if (other.opis != null) return false;
return false; } else if (!idSprzedawcy.equals(other.idSprzedawcy))
} else if (!opis.equals(other.opis)) return false;
return false; if (opis == null) {
if (sprzedawca == null) { if (other.opis != null)
if (other.sprzedawca != null) return false;
return false; } else if (!opis.equals(other.opis))
} else if (!sprzedawca.equals(other.sprzedawca)) return false;
return false; if (sprzedawca == null) {
return true; if (other.sprzedawca != null)
} return false;
} else if (!sprzedawca.equals(other.sprzedawca))
@Override return false;
public String toString() { return true;
return String.format("Ogłoszenie #%d: sprzedawca #%s, cena: %.2f\n %s", }
idOgloszenia, idSprzedawcy, cena, tytul);
} @Override
public String toString() {
return String.format("Ogłoszenie #%d: sprzedawca #%s, cena: %.2f\n %s",
idOgloszenia, idSprzedawcy, cena, tytul);
}
} }
...@@ -4,146 +4,150 @@ import java.math.BigDecimal; ...@@ -4,146 +4,150 @@ import java.math.BigDecimal;
import java.time.LocalDateTime; import java.time.LocalDateTime;
public class OgloszenieSamochodowe extends Ogloszenie { public class OgloszenieSamochodowe extends Ogloszenie {
private String marka; private String marka;
private String model; private String model;
private String generacja; private String generacja;
public int rocznik; public int rocznik;
private int przebieg; private int przebieg;
private String kolor; private String kolor;
private Silnik silnik; private Silnik silnik;
public OgloszenieSamochodowe() { public OgloszenieSamochodowe() {
} }
public OgloszenieSamochodowe(Integer idOgloszenia, Integer idSprzedawcy, public OgloszenieSamochodowe(Integer idOgloszenia, Integer idSprzedawcy,
LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis,
String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) { String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) {
super(idOgloszenia, idSprzedawcy, dataWystawienia, cena, tytul, opis); super(idOgloszenia, idSprzedawcy, dataWystawienia, cena, tytul, opis);
this.marka = marka; this.marka = marka;
this.model = model; this.model = model;
this.generacja = generacja; this.generacja = generacja;
this.kolor = kolor; this.kolor = kolor;
this.rocznik = rocznik; this.rocznik = rocznik;
this.przebieg = przebieg; this.przebieg = przebieg;
this.silnik = silnik; this.silnik = silnik;
} }
/** Pozostawia pole sprzedawca równe null. */ /**
public OgloszenieSamochodowe(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis, * Pozostawia pole sprzedawca równe null.
String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) { */
super(idOgloszenia, dataWystawienia, cena, tytul, opis); public OgloszenieSamochodowe(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis,
this.marka = marka; String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) {
this.model = model; super(idOgloszenia, dataWystawienia, cena, tytul, opis);
this.generacja = generacja; this.marka = marka;
this.kolor = kolor; this.model = model;
this.rocznik = rocznik; this.generacja = generacja;
this.przebieg = przebieg; this.kolor = kolor;
this.silnik = silnik; this.rocznik = rocznik;
} this.przebieg = przebieg;
this.silnik = silnik;
/** Wartość idSprzedawcy pobiera z obiektu sprzedawca. */ }
public OgloszenieSamochodowe(Integer idOgloszenia, Sprzedawca sprzedawca,
LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis, /**
String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) { * Wartość idSprzedawcy pobiera z obiektu sprzedawca.
super(idOgloszenia, sprzedawca, dataWystawienia, cena, tytul, opis); */
this.marka = marka; public OgloszenieSamochodowe(Integer idOgloszenia, Sprzedawca sprzedawca,
this.model = model; LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis,
this.generacja = generacja; String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) {
this.kolor = kolor; super(idOgloszenia, sprzedawca, dataWystawienia, cena, tytul, opis);
this.rocznik = rocznik; this.marka = marka;
this.przebieg = przebieg; this.model = model;
this.silnik = silnik; this.generacja = generacja;
} this.kolor = kolor;
this.rocznik = rocznik;
this.przebieg = przebieg;
public String getMarka() { this.silnik = silnik;
return marka.toUpperCase(); }
}
public void setMarka(String marka) { public String getMarka() {
this.marka = marka; return marka.toUpperCase();
} }
public String getModel() { public void setMarka(String marka) {
return model; this.marka = marka;
} }
public String getGeneracja() { public String getModel() {
return generacja; return model;
} }
public String getKolor() { public String getGeneracja() {
return kolor; return generacja;
} }
public int getRocznik() { public String getKolor() {
return rocznik; return kolor;
} }
public int getPrzebieg() { public int getRocznik() {
return przebieg; return rocznik;
} }
public Silnik getSilnik() { public int getPrzebieg() {
return silnik; return przebieg;
} }
@Override public Silnik getSilnik() {
public int hashCode() { return silnik;
final int prime = 31; }
int result = super.hashCode();
result = prime * result + ((generacja == null) ? 0 : generacja.hashCode()); @Override
result = prime * result + ((kolor == null) ? 0 : kolor.hashCode()); public int hashCode() {
result = prime * result + ((marka == null) ? 0 : marka.hashCode()); final int prime = 31;
result = prime * result + ((model == null) ? 0 : model.hashCode()); int result = super.hashCode();
result = prime * result + rocznik; result = prime * result + ((generacja == null) ? 0 : generacja.hashCode());
result = prime * result + ((silnik == null) ? 0 : silnik.hashCode()); result = prime * result + ((kolor == null) ? 0 : kolor.hashCode());
return result; result = prime * result + ((marka == null) ? 0 : marka.hashCode());
} result = prime * result + ((model == null) ? 0 : model.hashCode());
result = prime * result + rocznik;
@Override result = prime * result + ((silnik == null) ? 0 : silnik.hashCode());
public boolean equals(Object obj) { return result;
if (this == obj) }
return true;
if (!super.equals(obj)) @Override
return false; public boolean equals(Object obj) {
if (getClass() != obj.getClass()) if (this == obj)
return false; return true;
OgloszenieSamochodowe other = (OgloszenieSamochodowe) obj; if (!super.equals(obj))
if (generacja == null) { return false;
if (other.generacja != null) if (getClass() != obj.getClass())
return false; return false;
} else if (!generacja.equals(other.generacja)) OgloszenieSamochodowe other = (OgloszenieSamochodowe) obj;
return false; if (generacja == null) {
if (kolor == null) { if (other.generacja != null)
if (other.kolor != null) return false;
return false; } else if (!generacja.equals(other.generacja))
} else if (!kolor.equals(other.kolor)) return false;
return false; if (kolor == null) {
if (marka == null) { if (other.kolor != null)
if (other.marka != null) return false;
return false; } else if (!kolor.equals(other.kolor))
} else if (!marka.equals(other.marka)) return false;
return false; if (marka == null) {
if (model == null) { if (other.marka != null)
if (other.model != null) return false;
return false; } else if (!marka.equals(other.marka))
} else if (!model.equals(other.model)) return false;
return false; if (model == null) {
if (rocznik != other.rocznik) if (other.model != null)
return false; return false;
if (silnik == null) { } else if (!model.equals(other.model))
if (other.silnik != null) return false;
return false; if (rocznik != other.rocznik)
} else if (!silnik.equals(other.silnik)) return false;
return false; if (silnik == null) {
return true; if (other.silnik != null)
} return false;
} else if (!silnik.equals(other.silnik))
@Override return false;
public String toString() { return true;
return super.toString() + "\n" + }
String.format(" Samochód: %s %s %s (%s), rok %d, silnik: %s",
marka, model, generacja, kolor, rocznik, silnik); @Override
} public String toString() {
return super.toString() + "\n" +
String.format(" Samochód: %s %s %s (%s), rok %d, silnik: %s",
marka, model, generacja, kolor, rocznik, silnik);
}
} }
package ogloszenia.model; package ogloszenia.model;
public enum Paliwo { public enum Paliwo {
BENZYNA, BENZYNA,
OLEJ, OLEJ,
GAZ, GAZ,
HYBRYDA, HYBRYDA,
ELEKTRYCZNY; ELEKTRYCZNY;
} }
package ogloszenia.model; package ogloszenia.model;
public class Silnik { public class Silnik {
private Float moc; private Float moc;
private Float pojemnosc; private Float pojemnosc;
private Paliwo paliwo; private Paliwo paliwo;
public Silnik() {
}
public Silnik(Float moc, Float pojemnosc, Paliwo paliwo) { public Silnik() {
this.moc = moc; }
this.pojemnosc = pojemnosc;
this.paliwo = paliwo;
}
public Float getMoc() { public Silnik(Float moc, Float pojemnosc, Paliwo paliwo) {
return moc; this.moc = moc;
} this.pojemnosc = pojemnosc;
this.paliwo = paliwo;
}
public Float getPojemnosc() { public Float getMoc() {
return pojemnosc; return moc;
} }
public Paliwo getPaliwo() { public Float getPojemnosc() {
return paliwo; return pojemnosc;
} }
@Override public Paliwo getPaliwo() {
public int hashCode() { return paliwo;
final int prime = 31; }
int result = 1;
result = prime * result + ((moc == null) ? 0 : moc.hashCode());
result = prime * result + ((paliwo == null) ? 0 : paliwo.hashCode());
result = prime * result + ((pojemnosc == null) ? 0 : pojemnosc.hashCode());
return result;
}
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) final int prime = 31;
return true; int result = 1;
if (obj == null) result = prime * result + ((moc == null) ? 0 : moc.hashCode());
return false; result = prime * result + ((paliwo == null) ? 0 : paliwo.hashCode());
if (getClass() != obj.getClass()) result = prime * result + ((pojemnosc == null) ? 0 : pojemnosc.hashCode());
return false; return result;
Silnik other = (Silnik) obj; }
if (moc == null) {
if (other.moc != null)
return false;
} else if (!moc.equals(other.moc))
return false;
if (paliwo != other.paliwo)
return false;
if (pojemnosc == null) {
if (other.pojemnosc != null)
return false;
} else if (!pojemnosc.equals(other.pojemnosc))
return false;
return true;
}
@Override @Override
public String toString() { public boolean equals(Object obj) {
return String.format("%.1f %s, %.0f KM", pojemnosc, paliwo, moc); if (this == obj)
} return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Silnik other = (Silnik) obj;
if (moc == null) {
if (other.moc != null)
return false;
} else if (!moc.equals(other.moc))
return false;
if (paliwo != other.paliwo)
return false;
if (pojemnosc == null) {
if (other.pojemnosc != null)
return false;
} else if (!pojemnosc.equals(other.pojemnosc))
return false;
return true;
}
@Override
public String toString() {
return String.format("%.1f %s, %.0f KM", pojemnosc, paliwo, moc);
}
} }
package ogloszenia.model; package ogloszenia.model;
public class Sprzedawca { public class Sprzedawca {
private Integer idSprzedawcy; private Integer idSprzedawcy;
private String nazwa; private String nazwa;
private Adres adres; private Adres adres;
private String telefon; private String telefon;
private String email; private String email;
public Sprzedawca() {
}
public Sprzedawca(Integer idSprzedawcy, String nazwa, Adres adres, String telefon, String email) { public Sprzedawca() {
this.idSprzedawcy = idSprzedawcy; }
this.nazwa = nazwa;
this.adres = adres;
this.telefon = telefon;
this.email = email;
}
public Integer getIdSprzedawcy() { public Sprzedawca(Integer idSprzedawcy, String nazwa, Adres adres, String telefon, String email) {
return idSprzedawcy; this.idSprzedawcy = idSprzedawcy;
} this.nazwa = nazwa;
this.adres = adres;
this.telefon = telefon;
this.email = email;
}
public String getNazwa() { public Integer getIdSprzedawcy() {
return nazwa; return idSprzedawcy;
} }
public Adres getAdres() { public String getNazwa() {
return adres; return nazwa;
} }
public String getTelefon() { public Adres getAdres() {
return telefon; return adres;
} }
public String getEmail() { public String getTelefon() {
return email; return telefon;
} }
@Override public String getEmail() {
public int hashCode() { return email;
final int prime = 31; }
int result = 1;
result = prime * result + ((adres == null) ? 0 : adres.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((idSprzedawcy == null) ? 0 : idSprzedawcy.hashCode());
result = prime * result + ((nazwa == null) ? 0 : nazwa.hashCode());
result = prime * result + ((telefon == null) ? 0 : telefon.hashCode());
return result;
}
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) final int prime = 31;
return true; int result = 1;
if (obj == null) result = prime * result + ((adres == null) ? 0 : adres.hashCode());
return false; result = prime * result + ((email == null) ? 0 : email.hashCode());
if (getClass() != obj.getClass()) result = prime * result + ((idSprzedawcy == null) ? 0 : idSprzedawcy.hashCode());
return false; result = prime * result + ((nazwa == null) ? 0 : nazwa.hashCode());
Sprzedawca other = (Sprzedawca) obj; result = prime * result + ((telefon == null) ? 0 : telefon.hashCode());
if (adres == null) { return result;
if (other.adres != null) }
return false;
} else if (!adres.equals(other.adres))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (idSprzedawcy == null) {
if (other.idSprzedawcy != null)
return false;
} else if (!idSprzedawcy.equals(other.idSprzedawcy))
return false;
if (nazwa == null) {
if (other.nazwa != null)
return false;
} else if (!nazwa.equals(other.nazwa))
return false;
if (telefon == null) {
if (other.telefon != null)
return false;
} else if (!telefon.equals(other.telefon))
return false;
return true;
}
@Override @Override
public String toString() { public boolean equals(Object obj) {
return String.format("Sprzedawca #%d: %s adres: %s, tel.: %s, email: %s", if (this == obj)
idSprzedawcy, nazwa, adres, telefon, email); return true;
} if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Sprzedawca other = (Sprzedawca) obj;
if (adres == null) {
if (other.adres != null)
return false;
} else if (!adres.equals(other.adres))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (idSprzedawcy == null) {
if (other.idSprzedawcy != null)
return false;
} else if (!idSprzedawcy.equals(other.idSprzedawcy))
return false;
if (nazwa == null) {
if (other.nazwa != null)
return false;
} else if (!nazwa.equals(other.nazwa))
return false;
if (telefon == null) {
if (other.telefon != null)
return false;
} else if (!telefon.equals(other.telefon))
return false;
return true;
}
@Override
public String toString() {
return String.format("Sprzedawca #%d: %s adres: %s, tel.: %s, email: %s",
idSprzedawcy, nazwa, adres, telefon, email);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment