Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. package bazadanych;
  2.  
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. // Menu
  11. while(1>0)
  12. {
  13. //1. Add a new workout
  14. System.out.println("\n Press 1 to add a new workout\n press 2 to close\n "
  15. + "press 3 to load the summary ordered by date of the excersise" );
  16.  
  17.  
  18. Scanner in = new Scanner(System.in);
  19. int choose = in.nextInt();
  20.  
  21.  
  22. switch(choose){
  23.  
  24. case 1: choose = 1;
  25.  
  26. System.out.println("\n Date of the excersise (ddmmyyyy without spaces):");
  27. int date = in.nextInt();
  28.  
  29.  
  30. System.out.println("\n name of the excersise: ");
  31. String name = in.nextLine();
  32.  
  33. System.out.println("\n Total repeats: ");
  34. int rep = in.nextInt();
  35.  
  36.  
  37.  
  38. System.out.println("\n Weight: ");
  39. int weight = in.nextInt();
  40.  
  41. DBConnector d = new DBConnector();
  42.  
  43.  
  44.  
  45. d.addWorkout(name, date, rep, weight );
  46.  
  47. continue;
  48.  
  49. case 2: choose = 2;
  50. System.out.println("\nZamykanie programu");
  51. System.exit(0);
  52.  
  53. case 3: choose = 3;
  54. List<Workout> workouts = d.allWorkouts();
  55.  
  56. for (int i=0; i < workouts.size(); i++) {
  57. System.out.println("---------------------------------");
  58. System.out.println("The name of the excersise: " + workouts.get(i).getName());
  59. System.out.println(" Number of reps: " + workouts.get(i).getReps());
  60. System.out.println(" Weight: " + workouts.get(i).getWeight() + "kg");
  61. System.out.println("Date: " + workouts.get(i).getDate());
  62. System.out.println("---------------------------------");
  63. }
  64. continue;
  65.  
  66. }
  67. }
  68. }
  69. }
  70.  
  71. --------------------------------------------------------------------------------------------
  72.  
  73.  
  74. package bazadanych;
  75.  
  76. public class Workout extends DBConnector {
  77.  
  78. private int workoutId;
  79. private String name;
  80. private int reps;
  81. private int weight;
  82. private int date;
  83.  
  84. public Workout(int workoutId, String name, int weight, int reps, int date)
  85. {
  86. setWorkoutId(workoutId);
  87. setName(name);
  88. setWeight(weight);
  89. setReps(reps);
  90. setDate(date);
  91. }
  92.  
  93. // Getters
  94. public int getDate()
  95. {
  96. return date;
  97. }
  98. public int getWorkoutId()
  99. {
  100. return workoutId;
  101. }
  102. public String getName()
  103. {
  104. return name;
  105. }
  106.  
  107. public int getReps()
  108. {
  109. return reps;
  110. }
  111. public int getWeight()
  112. {
  113. return weight;
  114. }
  115.  
  116.  
  117. //Setters
  118. public void setDate(int date)
  119. {
  120. this.date = date;
  121. }
  122. public void setName(String name)
  123. {
  124. this.name = name;
  125. }
  126. public void setReps(int reps)
  127. {
  128. this.reps = reps;
  129. }
  130. public void setWorkoutId(int workoutId)
  131. {
  132. this.workoutId = workoutId;
  133. }
  134. public void setWeight(int weight)
  135. {
  136. this.weight = weight;
  137. }
  138.  
  139.  
  140. }
  141.  
  142.  
  143. ---------------------------------------------------------------------------------
  144.  
  145.  
  146. package bazadanych;
  147.  
  148.  
  149. import java.sql.*;
  150. import java.util.LinkedList;
  151. import java.util.List;
  152. public class DBConnector {
  153.  
  154. // connection with datebase
  155.  
  156. private Connection conn;
  157.  
  158. // The object used to execute a static SQL statement and returning the results
  159. private Statement stat;
  160.  
  161.  
  162. // Construct
  163.  
  164. public DBConnector()
  165. {
  166.  
  167. try
  168. {
  169. Class.forName("org.sqlite.JDBC");
  170. }
  171. catch (ClassNotFoundException e)
  172. {
  173. System.err.println("There is no JDBC driver");
  174. e.printStackTrace();
  175. }
  176. try
  177. {
  178.  
  179. conn = DriverManager.getConnection("jdbc:sqlite:GymApp.db"); // GymApp will be the name of the datebase
  180. stat = conn.createStatement();
  181. }
  182. catch (SQLException e)
  183. {
  184. System.err.println("I can not connect");
  185. }
  186.  
  187. CreateStructure();
  188.  
  189. }
  190.  
  191. public boolean CreateStructure()
  192. {
  193.  
  194. // Rule to delete the table and create new, when we want to rework number of columnes etc.
  195. // String dropFirst = "DROP TABLE IF EXISTS workouts;";
  196.  
  197.  
  198. String sql = "CREATE TABLE IF NOT EXISTS workouts"
  199. + "("
  200. + "workoutId INTEGER PRIMARY KEY AUTOINCREMENT,"
  201. + "name varchar(100),"
  202. + "reps INTEGER, "
  203. + " weight INTEGER,"
  204. + " date INTEGER"
  205. + ")";
  206. try
  207. {
  208. // stat.execute(dropFirst);
  209.  
  210. stat.execute(sql);
  211.  
  212. }
  213. catch (SQLException e)
  214. {
  215. System.err.println("There is a problem by Structure creation");
  216. e.printStackTrace();
  217. return false;
  218. }
  219. return true;
  220. }
  221.  
  222.  
  223. public boolean addWorkout( String name, int reps, int weight, int date)
  224. { String sql = " insert into workouts values (Null,?,?,?,?);";
  225. try
  226. (PreparedStatement pStmt = conn.prepareStatement(sql)){
  227. pStmt.setString(1, name);
  228. pStmt.setInt(2,reps);
  229. pStmt.setInt(3,weight);
  230. pStmt.setInt(4, date);
  231. pStmt.execute();
  232. }
  233. catch(SQLException e)
  234. {
  235. System.err.println("Can not add a new contact");
  236. e.printStackTrace();
  237. return false;
  238. }
  239. return true;
  240. }
  241.  
  242. public List<Workout> allWorkouts()
  243. {
  244. List<Workout> workouts = new LinkedList<Workout>();
  245.  
  246. try {
  247. ResultSet show = stat.executeQuery("SELECT * FROM workouts ORDER BY date");
  248. int id;
  249. String name;
  250. int reps;
  251. int weight;
  252. int date;
  253.  
  254. while (show.next())
  255. {
  256. id = show.getInt("workoutId");
  257. name = show.getString("name");
  258. reps = show.getInt("reps");
  259. weight = show.getInt("weight");
  260. date = show.getInt("date");
  261.  
  262. workouts.add(new Workout(id, name,reps,weight,date));
  263. }
  264. }
  265. catch (SQLException e)
  266. {
  267. e.printStackTrace();
  268. return null;
  269.  
  270. }
  271. return workouts;
  272. }
  273.  
  274. public void closeConnection() {
  275. try{
  276. conn.close();
  277. }
  278. catch (SQLException e) {
  279. System.err.println("There is connection closing error");
  280. e.printStackTrace();
  281. }
  282. }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement