1

Just to let you know: I know how to use Scanner od BufferedReader, just dont know where to use it in this case.

I am working on my first bigger app in Java. (I had to use SQLite as a DB) That's some kind of gym app, where I will add my workouts (4 simple variables) And then it will be saved in DB and sorted to read out.

My question is... How should I add an Input from the user? I have setters and getters and no Idea where this input should be added. In main class? Should I build a new method?

 package bazadanych;

import java.util.List;
import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {

     DBConnector d = new DBConnector();



     d.addWorkout( "bicek", 12, 5,22052019);

     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("---------------------------------");
     }
    }



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();
        }
    }
}
  • Possible duplicate of [How can I get the user input in Java?](https://stackoverflow.com/questions/5287538/how-can-i-get-the-user-input-in-java) – achAmháin Jun 21 '18 at 10:40
  • I know how to use Scanner or Buffered reader. Just dont know how to combine it in this case – Maja Pastusiak Jun 21 '18 at 10:42

1 Answers1

1

To answer your main question, you should add the input from the user in the main method. You'd use an instance of Scanner to read the values of workout name, reps and weight. Date you could simply pick up the current date, code sample below. A few other recommendations:

  • 1 - Change the workout date to long, that's a standard in the industry.
  • 2 - The method CreateStructure does not follow Java coding standards, rename it to createStructure.

  • 3 - You are storing the workout ID as NULL, that could cause you trouble later when trying to retrieve the data from the database.

Code sample:

public static void main(String[] args) {
  DBConnector d = new DBConnector();

  // Retrieve input from the user
  Scanner sc = new Scanner(System.in);
  String name = sc.nextLine();
  int reps = sc.nextInt();
  int weight = sc.nextInt();
  // create the workout with the data
  d.addWorkout( name, reps, weight, LocalDate.now().toEpochDay());
  List<Workout> workouts = d.allWorkouts();
  // print workouts
}
Paolo
  • 21,270
  • 6
  • 38
  • 69
fpezzini
  • 774
  • 1
  • 8
  • 17