低效无用功...

main
lensfrex 3 years ago
parent 6fd5fe8d0a
commit bea60a3774
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 21
      src/main/java/me/lensfrex/manager/Main.java
  2. 17
      src/main/java/me/lensfrex/manager/data/ManagerTool.java
  3. 40
      src/main/java/me/lensfrex/manager/data/Student.java
  4. 10
      src/main/java/me/lensfrex/manager/data/StudentManager.java
  5. 8
      src/main/java/me/lensfrex/manager/sqlite/DataTypes.java
  6. 99
      src/main/java/me/lensfrex/manager/sqlite/SQLiteTool.java
  7. 6
      src/main/java/me/lensfrex/manager/utils/IOUtil.java
  8. 4
      src/main/resources/me.lensfrex.manager/studentInformationExample.txt
  9. 9
      src/main/resources/me.lensfrex.manager/welcomeMessage.txt

@ -15,7 +15,11 @@ public class Main {
} }
private void run() { private void run() {
while (true) {
if (parseCommand(getUserCommand()) == -1) {
break;
}
}
} }
private String getUserCommand() { private String getUserCommand() {
@ -24,12 +28,15 @@ public class Main {
return command.readLine(); return command.readLine();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return "null";
} }
} }
private void parseCommand(String command) { private int parseCommand(String command) {
switch (command) { switch (command) {
case "null":
case "0":
return -1;
case "1": case "1":
case "2": case "2":
case "3": case "3":
@ -39,7 +46,13 @@ public class Main {
case "7": case "7":
case "8": case "8":
case "9": case "9":
break; case "10":
return 0;
case "11":
System.out.println(welcomeMessage);
return 0;
default:
return 0;
} }
} }
} }

@ -0,0 +1,17 @@
package me.lensfrex.manager.data;
import java.util.ArrayList;
import java.util.Map;
public class ManagerTool {
private final ArrayList<Student> studentData;
public ManagerTool(ArrayList<Student> studentData) {
this.studentData = studentData;
}
public void addStudent(long id, String name, Map<String, Integer> score) {
}
}

@ -0,0 +1,40 @@
package me.lensfrex.manager.data;
import java.util.Map;
public class Student {
private String name;
//private String id;
private long id;
private Map<String, Integer> score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Map<String, Integer> getScore() {
return score;
}
public void setScore(Map<String, Integer> score) {
this.score = score;
}
public Student(String name, long id, Map<String, Integer> score) {
this.name = name;
this.id = id;
this.score = score;
}
}

@ -0,0 +1,10 @@
package me.lensfrex.manager.data;
import java.util.ArrayList;
public class StudentManager {
private ArrayList<Student> studentData = new ArrayList<>();
}

@ -0,0 +1,8 @@
package me.lensfrex.manager.sqlite;
public class DataTypes {
public static final String SQL_DATA_TYPE_NUMBER = "int";
public static final String SQL_DATA_TYPE_TEXT = "text";
public static final String SQL_DATA_TYPE_REAL = "real";
public static final String SQL_DATA_TYPE_NULL = "null";
}

@ -0,0 +1,99 @@
package me.lensfrex.manager.sqlite;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* SQL操作的简单封装...
* 试图手动封装SQL操作...
* 有BUG这就对了
* SQL语句仍在学习中...
* SQL语句拼接怎么这么麻烦...
*/
public class SQLiteTool {
private static final String DATABASE_LOCATION = "student.db";
private Connection dbConnection;
private Statement statement;
/**
* 初始化数据库文件若成功则返回true若数据库初始化出现异常则返回false
* 不保证数据库中存在欲操作的表需手动确认
*
* @return 初始化是否成功若成功则返回true若数据库初始化出现异常则返回false
*/
public boolean initDatabase() {
try {
Class.forName("org.sqlite.JDBC");
this.dbConnection = DriverManager.getConnection("jdbc:sqlite:" + DATABASE_LOCATION);
this.statement = dbConnection.createStatement();
return true;
} catch (Exception e) {
System.out.println("Some errors happens when initialing database.");
e.printStackTrace();
return false;
}
}
/**
* 向数据库中创建一个表
*
* @param tableName 数据库表的名称
* @param columns 数据库表列的参数map中key为列的名称value为其相应的数据类型
* @throws SQLException 如果创建表的过程中发生错误
*/
public void createTable(String tableName, HashMap<String, String> columns) throws SQLException {
StringBuilder stringBuilder = new StringBuilder("create table %s (");
for (Map.Entry<String, String> column : columns.entrySet()) {
stringBuilder.append(String.format("%s %s, ", column.getKey(), column.getValue()));
}
String createStatement = stringBuilder
.replace(stringBuilder.lastIndexOf(","), stringBuilder.lastIndexOf(",") + 2, ")")
.toString();
statement.executeUpdate(createStatement);
}
/**
* 查询表格是否存在
*
* @param tableName 表格名
* @return 存在返回true否则返回false
*/
public boolean isTableExists(String tableName) {
String tryStatement = String.format("select * from %s", tableName);
try {
statement.executeQuery(tryStatement);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 向数据库里插入一条记录
*
* @param table 表格名
* @param values 每一列的数据值文本类型数据应使用单引号 ' 括住
* @throws SQLException 如果执行的过程中发生错误
*/
public void insert(String table, String... values) throws SQLException {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("insert into ").append(table).append(" values ( ");
for (String value : values) {
stringBuilder.append(value).append(' ');
}
stringBuilder.append(")");
statement.executeUpdate(stringBuilder.toString());
}
}

@ -12,7 +12,7 @@ import java.nio.charset.Charset;
public class IOUtil { public class IOUtil {
public static String inputStreamToString(InputStream inputStream, Charset charSet) { public static String inputStreamToString(InputStream inputStream, Charset charSet) {
byte[] bytes = readDataFromInputStream(inputStream); byte[] bytes = readDataFromInputStream(inputStream, 5);
if (bytes != null) { if (bytes != null) {
return new String(bytes, charSet); return new String(bytes, charSet);
} else { } else {
@ -20,10 +20,6 @@ public class IOUtil {
} }
} }
public static byte[] readDataFromInputStream(InputStream inputStream) {
return readDataFromInputStream(inputStream, 5);// read every 5kb in default
}
public static byte[] readDataFromInputStream(InputStream inputStream, int byteAllocation) { public static byte[] readDataFromInputStream(InputStream inputStream, int byteAllocation) {
try { try {
ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream(); ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();

@ -0,0 +1,4 @@
- Please enter the student information following this format:
[id] [name] ...[subjectName,score]
- Example:
23333333 Stud English,100 Math,99 History,98

@ -3,15 +3,16 @@
**************************************************************** ****************************************************************
Do you want to: Do you want to:
1. Input record 1. Input record
2. Caculate total and average score of every course 2. Calculate total and average score of every course
3. Caculate total and average score of every student 3. Calculate total and average score of every student
4. Sort in decending order by total socre of every student 4. Sort in descending order by total score of every student
5. Sort in ascending order by number 5. Sort in ascending order by number
6. Sort in ascending order by name 6. Sort in ascending order by name
7. Search by number 7. Search by number
8. Search by name 8. Search by name
9. Statistic analysis for every course 9. Statistic analysis for every course
10. List reocrd 10. List record
11. Show help again
0. Exit 0. Exit
**************************************************************** ****************************************************************
Please enter your choice: Please enter your choice:
Loading…
Cancel
Save