Created
January 8, 2019 11:00
-
-
Save EnioRich/e3ae4810f84e6e3482d262c9562a48bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Apartment { | |
private double district; | |
private String address; | |
private double square; | |
private int rooms; | |
private double price; | |
public Apartment(double area, String address, double square, int rooms, double price) { | |
this.district = area; | |
this.address = address; | |
this.square = square; | |
this.rooms = rooms; | |
this.price = price; | |
} | |
public double getDistrict() { | |
return district; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public double getSquare() { | |
return square; | |
} | |
public int getRooms() { | |
return rooms; | |
} | |
public double getPrice() { | |
return price; | |
} | |
@Override | |
public String toString() { | |
return "Apartment{" + | |
"district=" + district + | |
", address='" + address + '\'' + | |
", square=" + square + | |
", rooms=" + rooms + | |
", price=" + price + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ApartmentDAO { | |
private final Connection connection; | |
public ApartmentDAO(Connection connection) { | |
this.connection = connection; | |
} | |
public void init() { | |
try (Statement stmt = connection.createStatement()) { | |
stmt.execute("DROP TABLE IF EXISTS Apartment"); | |
stmt.execute("CREATE TABLE Apartment (" + | |
"district VARCHAR (10) NOT NULL," + | |
" address VARCHAR (50) NOT NULL," + | |
"area VARCHAR (50) NOT NULL," + | |
"rooms VARCHAR (50) NOT NULL," + | |
"price VARCHAR (50) NOT NULL )"); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void addFlat(double district, String address, double area, int rooms, double money) { | |
try (PreparedStatement pstmt = connection.prepareStatement("INSERT INTO Apartment VALUES (?,?,?,?,?)")) { | |
pstmt.setDouble(1, district); | |
pstmt.setString(2, address); | |
pstmt.setDouble(3, area); | |
pstmt.setInt(4, rooms); | |
pstmt.setDouble(5, money); | |
pstmt.executeUpdate(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void removeApartment(String address) { | |
try (PreparedStatement pstm = connection.prepareStatement("DELETE FROM Apartment WHERE address = ?")) { | |
pstm.setString(1, address); | |
pstm.executeUpdate(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public List<Apartment> showApartments() throws SQLException { | |
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Apartment;"); | |
ResultSet resultSet = preparedStatement.executeQuery(); | |
List<Apartment> apartments = new ArrayList<>(); | |
while (resultSet.next()) { | |
double district = resultSet.getDouble(1); | |
String address = resultSet.getString(2); | |
double area = resultSet.getDouble(3); | |
int rooms = resultSet.getInt(4); | |
double price = resultSet.getDouble(5); | |
apartments.add(new Apartment(district, address, area, rooms, price)); | |
} | |
return apartments; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataConnect { | |
// "jdbc:mysql://localhost:3306/hw?serverTimezone=Europe/Kiev" | |
public static final String DB_CONN = "jdbc:mysql://localhost:3306/hw"; | |
public static final String DB_USERNAME = "root"; | |
public static final String DB_PASSWORD = "12345"; | |
private DataConnect(){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import java.util.Scanner; | |
public class MainApartments { | |
// private static final String url = "jdbc:mysql://localhost:3306/hwl?serverTimezone=Europe/Kiev"; | |
public static void main(String[] args) throws SQLException { | |
ApartmentDAO ada = new ApartmentDAO(DriverManager.getConnection(DataConnect.DB_CONN, DataConnect.DB_USERNAME, DataConnect.DB_PASSWORD)); | |
Scanner scanner = new Scanner(System.in); | |
ada.init(); | |
while (true) { | |
System.out.println("Select operation: "); | |
System.out.println("1) Add apartment"); | |
System.out.println("2) Delete apartment"); | |
System.out.println("3) Show apartments"); | |
System.out.println(); | |
System.out.println("Select operation: "); | |
int number = scanner.nextInt(); | |
switch (number) { | |
case 1: { | |
System.out.println("Enter district"); | |
double district = scanner.nextDouble(); | |
System.out.println("Enter address"); | |
String address = scanner.next(); | |
System.out.println("Enter area"); | |
double square = scanner.nextDouble(); | |
System.out.println("Enter rooms"); | |
int rooms = scanner.nextInt(); | |
System.out.println("Enter money"); | |
double money = scanner.nextDouble(); | |
ada.addFlat(district, address, square, rooms, money); | |
break; | |
} | |
case 2: { | |
System.out.println("Enter address: "); | |
String address = scanner.next(); | |
ada.removeApartment(address); | |
break; | |
} | |
case 3: { | |
int count = 1; | |
for (Apartment a : ada.showApartments()) { | |
System.out.print(count + " "); | |
System.out.println(a); | |
count++; | |
} | |
break; | |
} | |
default: | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment