Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bazadanych;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- // Menu
- while(1>0)
- {
- //1. Add a new workout
- System.out.println("\n Press 1 to add a new workout\n press 2 to close\n "
- + "press 3 to load the summary ordered by date of the excersise" );
- Scanner in = new Scanner(System.in);
- int choose = in.nextInt();
- switch(choose){
- case 1: choose = 1;
- System.out.println("\n Date of the excersise (ddmmyyyy without spaces):");
- int date = in.nextInt();
- System.out.println("\n name of the excersise: ");
- String name = in.nextLine();
- System.out.println("\n Total repeats: ");
- int rep = in.nextInt();
- System.out.println("\n Weight: ");
- int weight = in.nextInt();
- DBConnector d = new DBConnector();
- d.addWorkout(name, date, rep, weight );
- continue;
- case 2: choose = 2;
- System.out.println("\nZamykanie programu");
- System.exit(0);
- case 3: choose = 3;
- List<Workout> workouts = d.allWorkouts();
- for (int i=0; i < workouts.size(); i++) {
- System.out.println("---------------------------------");
- System.out.println("The name of the excersise: " + workouts.get(i).getName());
- System.out.println(" Number of reps: " + workouts.get(i).getReps());
- System.out.println(" Weight: " + workouts.get(i).getWeight() + "kg");
- System.out.println("Date: " + workouts.get(i).getDate());
- System.out.println("---------------------------------");
- }
- continue;
- }
- }
- }
- }
- --------------------------------------------------------------------------------------------
- package bazadanych;
- public class Workout extends DBConnector {
- private int workoutId;
- private String name;
- private int reps;
- private int weight;
- private int date;
- public Workout(int workoutId, String name, int weight, int reps, int date)
- {
- setWorkoutId(workoutId);
- setName(name);
- setWeight(weight);
- setReps(reps);
- setDate(date);
- }
- // Getters
- public int getDate()
- {
- return date;
- }
- public int getWorkoutId()
- {
- return workoutId;
- }
- public String getName()
- {
- return name;
- }
- public int getReps()
- {
- return reps;
- }
- public int getWeight()
- {
- return weight;
- }
- //Setters
- public void setDate(int date)
- {
- this.date = date;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public void setReps(int reps)
- {
- this.reps = reps;
- }
- public void setWorkoutId(int workoutId)
- {
- this.workoutId = workoutId;
- }
- public void setWeight(int weight)
- {
- this.weight = weight;
- }
- }
- ---------------------------------------------------------------------------------
- package bazadanych;
- import java.sql.*;
- import java.util.LinkedList;
- import java.util.List;
- public class DBConnector {
- // connection with datebase
- private Connection conn;
- // The object used to execute a static SQL statement and returning the results
- private Statement stat;
- // Construct
- public DBConnector()
- {
- try
- {
- Class.forName("org.sqlite.JDBC");
- }
- catch (ClassNotFoundException e)
- {
- System.err.println("There is no JDBC driver");
- e.printStackTrace();
- }
- try
- {
- conn = DriverManager.getConnection("jdbc:sqlite:GymApp.db"); // GymApp will be the name of the datebase
- stat = conn.createStatement();
- }
- catch (SQLException e)
- {
- System.err.println("I can not connect");
- }
- CreateStructure();
- }
- public boolean CreateStructure()
- {
- // Rule to delete the table and create new, when we want to rework number of columnes etc.
- // String dropFirst = "DROP TABLE IF EXISTS workouts;";
- String sql = "CREATE TABLE IF NOT EXISTS workouts"
- + "("
- + "workoutId INTEGER PRIMARY KEY AUTOINCREMENT,"
- + "name varchar(100),"
- + "reps INTEGER, "
- + " weight INTEGER,"
- + " date INTEGER"
- + ")";
- try
- {
- // stat.execute(dropFirst);
- stat.execute(sql);
- }
- catch (SQLException e)
- {
- System.err.println("There is a problem by Structure creation");
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public boolean addWorkout( String name, int reps, int weight, int date)
- { String sql = " insert into workouts values (Null,?,?,?,?);";
- try
- (PreparedStatement pStmt = conn.prepareStatement(sql)){
- pStmt.setString(1, name);
- pStmt.setInt(2,reps);
- pStmt.setInt(3,weight);
- pStmt.setInt(4, date);
- pStmt.execute();
- }
- catch(SQLException e)
- {
- System.err.println("Can not add a new contact");
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public List<Workout> allWorkouts()
- {
- List<Workout> workouts = new LinkedList<Workout>();
- try {
- ResultSet show = stat.executeQuery("SELECT * FROM workouts ORDER BY date");
- int id;
- String name;
- int reps;
- int weight;
- int date;
- while (show.next())
- {
- id = show.getInt("workoutId");
- name = show.getString("name");
- reps = show.getInt("reps");
- weight = show.getInt("weight");
- date = show.getInt("date");
- workouts.add(new Workout(id, name,reps,weight,date));
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- return null;
- }
- return workouts;
- }
- public void closeConnection() {
- try{
- conn.close();
- }
- catch (SQLException e) {
- System.err.println("There is connection closing error");
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement