📄 data.java
字号:
/* Derby - Class org.apache.derbyDemo.scores.data.Data Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derbyDemo.scores.data;import java.sql.*;import org.apache.derbyDemo.scores.util.*;/** * <p> * Data used by Scores application * </p> * */public class Data{ //////////////////////////////////////////////////////// // // CONSTANTS // //////////////////////////////////////////////////////// private static final int QUESTIONS_PER_TEMPLATE = 10; private static final double IMPROVEMENT_PER_TAKING = 2.0; private static final double GRAMMAR_SCHOOL_PENALTY = 20.0; private static final double PER_STUDENT_FLUCTUATION = 3.0; private static final double FLUCTATION_MULTIPLIER = 2.0; public static final School LincolnGrammar = new School( "Lincoln Grammar", true ); public static final School WashingtonHS = new School( "Washington HS", false ); public static final Student AliceToklas = new Student ( "Alice", "Toklas", "1998-03-21", LincolnGrammar ); public static final Student GertrudeStein = new Student ( "Gertrude", "Stein", "1999-07-15", LincolnGrammar ); public static final Student HenryJames = new Student ( "Henry", "James", "2000-09-03", LincolnGrammar ); public static final Student GoreVidal = new Student ( "Gore", "Vidal", "2008-12-25", LincolnGrammar ); public static final Student TrumanCapote = new Student ( "Truman", "Capote", "1992-08-05", WashingtonHS ); public static final Student WaltWhitman = new Student ( "Walt", "Whitman", "1991-12-28", WashingtonHS ); public static final Student DorothyParker = new Student ( "Dorothy", "Parker", "1990-01-09", WashingtonHS ); public static final Student AndrewSullivan = new Student ( "Andrew", "Sullivan", "1989-02-20", WashingtonHS ); public static final Test GradeSchoolMath = new Test( "Grade School Math", true, 90.0 ); public static final Test GradeSchoolEnglish = new Test( "Grade School English", true, 85.0 ); public static final Test HighSchoolMath = new Test( "High School Math", false, 80.0 ); public static final Test HighSchoolEnglish = new Test( "High School English", false, 75.0 ); public static final Question GSM_1 = new Question( "GSM_1", 1, 5, 2, GradeSchoolMath ); public static final Question GSM_2 = new Question( "GSM_2", 2, 3, 1, GradeSchoolMath ); public static final Question GSM_3 = new Question( "GSM_3", 3, 4, 3, GradeSchoolMath ); public static final Question GSE_1 = new Question( "GSE_1", 3, 5, 2, GradeSchoolEnglish ); public static final Question GSE_2 = new Question( "GSE_2", 2, 3, 1, GradeSchoolEnglish ); public static final Question GSE_3 = new Question( "GSE_3", 1, 4, 3, GradeSchoolEnglish ); public static final Question HSM_1 = new Question( "HSM_1", 1, 5, 2, HighSchoolMath ); public static final Question HSM_2 = new Question( "HSM_2", 2, 3, 1, HighSchoolMath ); public static final Question HSM_3 = new Question( "HSM_3", 3, 4, 3, HighSchoolMath ); public static final Question HSE_1 = new Question( "HSE_1", 3, 5, 2, HighSchoolEnglish ); public static final Question HSE_2 = new Question( "HSE_2", 2, 3, 1, HighSchoolEnglish ); public static final Question HSE_3 = new Question( "HSE_3", 1, 4, 3, HighSchoolEnglish ); public static final Question[] QUESTIONS = { GSM_1, GSM_2, GSM_3, GSE_1, GSE_2, GSE_3, HSM_1, HSM_2, HSM_3, HSE_1, HSE_2, HSE_3, }; public static final School[] SCHOOLS = { LincolnGrammar, WashingtonHS, }; public static final Student[] STUDENTS = { AliceToklas, GertrudeStein, TrumanCapote, WaltWhitman, HenryJames, GoreVidal, DorothyParker, AndrewSullivan, }; public static final Test[] TESTS = { GradeSchoolMath, GradeSchoolEnglish, HighSchoolMath, HighSchoolEnglish, }; //////////////////////////////////////////////////////// // // STATE // //////////////////////////////////////////////////////// //////////////////////////////////////////////////////// // // INNER CLASSES // //////////////////////////////////////////////////////// public abstract static class KeyedObject { private Integer _key; public void create( Database database ) throws SQLException { Logger log = Logger.getLogger(); boolean loggingEnabled = log.isLoggingEnabled(); // don't print out the chatter from creating these rows. try { log.enableLogging( false ); createMinion( database ); } finally { log.enableLogging( loggingEnabled ); } } protected abstract void createMinion( Database database ) throws SQLException; protected abstract PreparedStatement getKeyFinder ( Database database ) throws SQLException; public int getPrimaryKey( Database database ) throws SQLException { if ( _key == null ) { PreparedStatement ps = getKeyFinder( database ); setPrimaryKey( Utils.getScalarValue( ps ) ); } return _key.intValue(); } private void setPrimaryKey( int key ) { _key = new Integer( key ); } public SQLException notImplemented() { return new SQLException( "Not implemented." ); } } public static class School extends KeyedObject { private String _schoolName; private boolean _isGrammarSchool; public School( String schoolName, boolean isGrammarSchool ) { _schoolName = schoolName; _isGrammarSchool = isGrammarSchool; } public String getSchoolName() { return _schoolName; } public boolean isGrammarSchool() { return _isGrammarSchool; } protected void createMinion( Database database ) throws SQLException { Connection conn = database.getConnection(); PreparedStatement ps = Utils.prepare ( conn, "insert into School( schoolName ) values ( ? )" ); ps.setString( 1, _schoolName ); ps.execute(); Utils.close( ps ); } protected PreparedStatement getKeyFinder( Database database ) throws SQLException { Connection conn = database.getConnection(); PreparedStatement ps = Utils.prepare ( conn, "select schoolID from School where schoolName = ?" ); ps.setString( 1, _schoolName ); return ps; } } public static class Student extends KeyedObject { private String _firstName; private String _lastName; private String _birthday; private School _school; public Student ( String firstName, String lastName, String birthday, School school ) { _firstName = firstName; _lastName = lastName; _birthday = birthday; _school = school; } public String getFirstName() { return _firstName; } public String getLastName() { return _lastName; } public String getBirthday() { return _birthday; } public School getSchool() { return _school; } protected void createMinion( Database database ) throws SQLException { int param = 1; Connection conn = database.getConnection(); java.sql.Date sqlDate = java.sql.Date.valueOf( _birthday ); PreparedStatement ps = Utils.prepare ( conn, "insert into Student\n" + "( schoolID, lastName, firstName, birthday )\n" + "values( ?, ?, ?, ? )" ); ps.setInt( param++, _school.getPrimaryKey( database ) ); ps.setString( param++, _lastName ); ps.setString( param++, _firstName ); ps.setDate( param++, sqlDate ); ps.execute(); Utils.close( ps ); } protected PreparedStatement getKeyFinder( Database database ) throws SQLException { Connection conn = database.getConnection(); PreparedStatement ps = Utils.prepare ( conn, "select studentID from Student\n" + "where lastName = ? and firstName = ?" ); ps.setString( 1, _lastName ); ps.setString( 2, _firstName ); return ps; } } public static class Test extends KeyedObject { private String _testName; private boolean _grammarSchoolTest; private double _highSchoolScore; public Test ( String testName, boolean grammarSchoolTest, double highSchoolScore ) { _testName = testName; _grammarSchoolTest = grammarSchoolTest; _highSchoolScore = highSchoolScore; } public String getTestName() { return _testName; } public boolean isGrammarSchoolTest() { return _grammarSchoolTest; } public double getHighSchoolScore() { return _highSchoolScore; } protected void createMinion( Database database ) throws SQLException { Connection conn = database.getConnection(); PreparedStatement ps = Utils.prepare ( conn, "insert into Test( testName ) values ( ? )" ); ps.setString( 1, _testName ); ps.execute(); Utils.close( ps ); } protected PreparedStatement getKeyFinder( Database database ) throws SQLException { Connection conn = database.getConnection(); PreparedStatement ps = Utils.prepare ( conn, "select testID from Test where testName = ?" ); ps.setString( 1, _testName ); return ps; } } public static class TestTaking extends KeyedObject { private Student _student; private Test _test; public TestTaking( Student student, Test test ) { _student = student; _test = test; } public Student getStudent() { return _student; } public Test getTest() { return _test; } protected void createMinion( Database database ) throws SQLException { Connection conn = database.getConnection(); int param = 1; PreparedStatement ps = Utils.prepare ( conn, "insert into TestTaking\n" + "( studentID, testID, score ) values ( ?, ?, ? )" ); ps.setInt( param++, _student.getPrimaryKey( database ) ); ps.setInt( param++, _test.getPrimaryKey( database ) ); ps.setInt( param++, -1 ); ps.execute(); Utils.close( ps ); } protected PreparedStatement getKeyFinder( Database database ) throws SQLException { Connection conn = database.getConnection(); int param = 1; PreparedStatement ps = Utils.prepare ( conn, "select max( takingID ) from TestTaking\n" + "where studentID = ? and testID = ?" ); ps.setInt( param++, _student.getPrimaryKey( database ) ); ps.setInt( param++, _test.getPrimaryKey( database ) ); return ps; } } public static class Question extends KeyedObject { private String _questionName; private int _difficulty; private int _numberOfChoices; private int _correctChoice; private Test _test; public Question ( String questionName, int difficulty, int numberOfChoices, int correctChoice, Test test ) { _questionName = questionName; _difficulty = difficulty; _numberOfChoices = numberOfChoices; _correctChoice = correctChoice; _test = test; } public String getQuestionName() { return _questionName; } public int getDifficulty() { return _difficulty; } public int getNumberOfChoices() { return _numberOfChoices; } public int getCorrectChoice() { return _correctChoice; } public Test getTest() { return _test; } protected void createMinion( Database database )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -