Post by Hauskaz on Nov 5, 2012 18:32:32 GMT -5
I wrote this a long time ago but have since forgotten too much about Java to interpret it.
package malaska;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableColumn;
/**
* Malaska, the XF Administrative Toolkit
* Account Value Automation (AVA)
*
* @author Hauskaz
* @version 1.0.0 BETA, 2009-04-30
*/
public class AVA extends Malaska {
static Scanner scanner = new Scanner(System.in);
static String name;
static int posts;
static double oldValue;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean dbExists = Malaska("Account Value Automation (AVA)",
"1.0.0 BETA",
"2009-04-28 Hauskaz");
if (!dbExists) {
firstTime();
}
System.out.print("Bringing up interface... ");
gui();
System.out.println("Done.");
}
static JButton addButton, editButton, deleteButton, ubbcButton;
/**
* Brings up the table window.
*/
static void gui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
JFrame tableFrame = new JFrame();
tableFrame.setTitle("Account Table");
tableFrame.setSize(480, 480);
String[] tableLabels = {
"#", "Account", "Posts", "Age", "VC", "Value", "Change",
"Rate of Change", "Value Per Post"};
JTable accountTable = new JTable(accountDB, tableLabels);
TableColumn column = accountTable.getColumnModel().getColumn(0);
column.setPreferredWidth(10);
column = accountTable.getColumnModel().getColumn(1);
column.setPreferredWidth(100);
column = accountTable.getColumnModel().getColumn(2);
column.setPreferredWidth(30);
column = accountTable.getColumnModel().getColumn(3);
column.setPreferredWidth(30);
column = accountTable.getColumnModel().getColumn(4);
column.setPreferredWidth(30);
column = accountTable.getColumnModel().getColumn(5);
column.setPreferredWidth(75);
column = accountTable.getColumnModel().getColumn(6);
column.setPreferredWidth(60);
column = accountTable.getColumnModel().getColumn(7);
column.setPreferredWidth(30);
column = accountTable.getColumnModel().getColumn(8);
column.setPreferredWidth(30);
JScrollPane tableScroll = new JScrollPane(accountTable);
accountTable.setFillsViewportHeight(true);
addButton = new JButton("Add");
editButton = new JButton("Edit");
deleteButton = new JButton("Delete");
ubbcButton = new JButton("UBBC");
ButtonListener bl = new ButtonListener();
addButton.addActionListener(bl);
editButton.addActionListener(bl);
deleteButton.addActionListener(bl);
ubbcButton.addActionListener(bl);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 4));
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
buttonPanel.add(ubbcButton);
JPanel masterPanel = (JPanel) tableFrame.getContentPane();
masterPanel.setLayout(new BorderLayout());
masterPanel.add(tableScroll, BorderLayout.CENTER);
masterPanel.add(buttonPanel, BorderLayout.SOUTH);
JFrame addDialog = new JFrame();
// TODO complete this dialog
tableFrame.setVisible(true);
}
static void firstTime() {
System.out.println("An initial database needs to be established.");
System.out.println("Please provide the following information.");
while (true) {
System.out.print("XF total post count: ");
posts = scanner.nextInt();
if ((int) posts > 0) {
break;
}
System.out.println("Invalid input.");
}
System.out.println("Date of last update:");
System.out.print("Year: ");
int year = scanner.nextInt();
System.out.print("Month: ");
int month = scanner.nextInt();
System.out.print("Day: ");
int day = scanner.nextInt();
cal.set(year, month - 1, day);
lastUpdate = cal.getTime();
System.out.print("Last recorded value of XF: ");
oldValue = scanner.nextDouble();
System.out.print("Processing initial database... ");
process("XF", posts, oldValue);
System.out.println("Done.");
}
static void addAccount() {
System.out.println("ADD AN ACCOUNT");
System.out.print("Account name: ");
name = scanner.next();
System.out.print("This account have a record on XF: ");
scanner.nextBoolean();
}
static class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
addAccount();
}
}
}
}
package malaska;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
/**
* Malaska, the XF Administrative Toolkit
* Main Library
*
* accountDB is a two dimensional array storing the entire database of accounts.
* It uses the following index:
* [i][0] Rank (int)
* [i][1] Account (String)
* [i][2] Posts (int)
* [i][3] Age (int)
* [i][4] VC (double)
* [i][5] Value (double)
* [i][6] Change (double)
* [i][7] Rate of Change (double)
* [i][8] Value Per Post (double)
*
* Malaska doesn't do anything on its own. To utilize Malaska, inherit it into
* another program.
*
* @author Hauskaz
* @version 1.0, 2009-04-30
*/
public class Malaska {
static BufferedReader dbFile;
static Object[][] accountDB = new Object[200][9];
static Calendar cal = Calendar.getInstance();
static Date rightNow = new Date();
static Date xfAge, lastUpdate;
static int updateDelta;
/**
* Main setup method.
*
* @param app the name of the application using the Malaska library
* @param versionID the version of the application using the library
* @param author the author of the application using the library
* @return whether or not a database exists
*/
static boolean Malaska(String app, String versionID, String author) {
System.out.println("Malaska, the XF Administrative Toolkit");
System.out.println(app);
System.out.println(versionID);
System.out.println(author);
System.out.print("Configurating date calculation... ");
cal.set(2004, 5, 16);
xfAge = cal.getTime();
System.out.println("Done.");
System.out.print("Loading database... ");
try {
// TODO implement record saving
dbFile = new BufferedReader(new FileReader("records.mdb"));
return true;
} catch (IOException ex) {
System.out.println("No record found!");
System.out.println();
return false;
}
}
static void process(String account, int postCount, double lastValue) {
accountDB[0][1] = account;
accountDB[0][2] = postCount;
accountDB[0][3] = deltaDays(xfAge, rightNow);
accountDB[0][5] = value();
accountDB[0][6] = (Double) accountDB[0][5] - lastValue;
double delta = (Integer) accountDB[0][3];
accountDB[0][8] = (Double) delta / 100.0;
}
/**
* Returns the number of days between two different Dates.
*
* @param a The first Date.
* @param b The second Date to be subtracted from the first.
* @return The number of days between the two Dates.
*/
static int deltaDays(Date a, Date b) { // TODO returns incorrect ages
int tempDifference = 0;
int difference = 0;
Calendar earlier = Calendar.getInstance();
Calendar later = Calendar.getInstance();
if (a.compareTo(b) < 0) {
earlier.setTime(a);
later.setTime(b);
} else {
earlier.setTime(b);
later.setTime(a);
}
while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR)) {
tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));
difference += tempDifference;
earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
}
if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR)) {
tempDifference = later.get(Calendar.DAY_OF_YEAR) - earlier.get(Calendar.DAY_OF_YEAR);
difference += tempDifference;
}
return difference;
}
static double value() {
int postCount = (Integer) accountDB[0][2];
int age = (Integer) accountDB[0][3];
return (double) (postCount * age) / 100;
}
}