Skip to content

Commit

Permalink
data base implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
siqq committed Sep 10, 2014
1 parent f3ea786 commit 321ac3f
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 326 deletions.
1 change: 1 addition & 0 deletions Java-Missle/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="forms-1.3.0.jar" sourcepath="forms-1.3.0-src.zip"/>
<classpathentry kind="lib" path="C:/Users/Andrey/Desktop/mysql-connector-java-5.1.18-bin.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
148 changes: 5 additions & 143 deletions Java-Missle/src/main/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,62 +54,6 @@ public static void main(String[] args) {
/**
* Print menu
*/
public static void menu(final War war) {
int option = 0;

//thread that keep checking the status of war
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(War.TIME_INTERVAL);
// checkEndOfWar(war);
} catch (InterruptedException e) {
}
}
}
}).start();

while (true) {

System.out.println(
"press 1: to add new Missile/Launcher destructor\n"
+ "press 2: to add new lanucher\n"
+ "press 3: to lanuch missile\n"
+ "press 4: to destroy Launcher\n"
+ "press 5: to destroy missile\n"
+ "press 6: to display statistics\n"
+ "press 7: to end war and display statistics\n"
+ "Please choose option");
try {
String stats;
option = input.nextInt();
input.nextLine();
System.out.println();
switch (option) {
case 5:
destructMissile(war);
break;
case 6:
stats = displayStatistics(war);
System.out.println(stats);
break;
case 7:
stats = displayStatistics(war);
logger.log(Level.INFO, "end war\n" + stats);
System.out.println(stats);
System.exit(0);
break;
}

} catch (InputMismatchException e) {
System.err.println("The Input was Invalid... Please try again");
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}

private static void checkEndOfWar(War war) {
int counter = 0;
Expand All @@ -131,103 +75,21 @@ private static void checkEndOfWar(War war) {
}
}
if (counter == size) {
String stats = displayStatistics(war);
/*String stats = displayStatistics(war);
logger.log(Level.INFO, "All launchers destroyed - end of war");
logger.log(Level.INFO, "end war\n" + stats);
System.out.println(stats);
System.exit(0);
System.exit(0);*/
}
}
}

/**
* choose a destructor and select a missile to destruct
* @param war
* @throws Exception
*/
private static void destructMissile(War war) throws Exception {
// first pick a destructor
System.out.println("Choose destructor "
+ "from the following Destructors list:");
Vector<Destructor> destructors = war
.getMissileDestructors();
printDestructors(destructors);

System.out.print("\nEnter your Choise: ");
String destructor_id = input.nextLine();
Destructor selected_destructor = WarUtility.getDestructorById(
destructor_id, war, MISSILE);
if (selected_destructor == null) {
throw new Exception("Destructor does not exist");
}
// now choose it's target
System.out.println("Choose a launcher to destruct "
+ "from the following launchers list:");
Vector<Launcher> launchers = war.getMissileLaunchers();
printMissiles(launchers);

System.out.print("\nEnter your Choise: ");
String target_id = input.nextLine();
Missile target = WarUtility.getMissileById(target_id, war);
if (target == null) {
throw new Exception("Target does not exist");
}
int destruct_time = (int) (TAKES_TIME_MIN + (Math.random() *
(TAKES_TIME_MAX - TAKES_TIME_MIN + 1)));
DestructedMissile assigned_destructor = new DestructedMissile(target, destruct_time,
selected_destructor, selected_destructor.getFileHandler(), war.getListeners());
selected_destructor.addDestructMissile(assigned_destructor);
}

/**
* print all Destructor's id from destructors array
*
* @param destructors
*/
private static void printDestructors(Vector<Destructor> destructors) {
for (Destructor d : destructors) {
System.out.print("[" + d.getType() + " - " + d.getDestructorId() + "] ");
}
}

/**
* print all active launcher's id from launchers array
*
* @param launchers
*/
private static void printLaunchers(Vector<Launcher> launchers) {
for (Launcher l : launchers) {
if (l.isRunning()) {
System.out.print(l.getLauncherId() + " ");
}
}
}

/**
* print all active missiles's id
* @param launchers
*/
private static void printMissiles(Vector<Launcher> launchers) {
Launcher launcher;
Missile missile;
int launcher_size = launchers.size();
for(int i = 0; i < launcher_size; i++) {
launcher = launchers.get(i);
for(int j = 0; j <launcher.getMissiles().size(); j++ ) {
missile = launcher.getMissiles().get(j);
if(missile.getStatus() == Missile.Status.Launched) {
System.out.print(missile.getMissileId() + " ");
}
}
}
}

/**
* The method displays the status of war
* @param war
* @return string of statistic
*/
private static String displayStatistics(War war) {
private static void displayStatistics(War war) {
int total_launched_missiles = 0;
int total_destroyed_missiles = 0;
int total_missiles_hit = 0;
Expand Down Expand Up @@ -259,7 +121,7 @@ private static String displayStatistics(War war) {
}
}
}
statistic = "The statistics of war is: \n"
/* statistic = "The statistics of war is: \n"
+ "The number of missiles launched:\t"
+ total_launched_missiles + "\n"
+ "The number of missiles destroyed:\t"
Expand All @@ -270,6 +132,6 @@ private static String displayStatistics(War war) {
+ total_destroyed_launchers + "\n"
+ "The total value of damage caused:\t"
+ total_damage + "\n";
return statistic;
return statistic;*/
}
}
8 changes: 3 additions & 5 deletions Java-Missle/src/missile/Missile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JProgressBar;

import launcher.Launcher;
import view.MissilePopUpFrame;
import war.War;
import war.controller.WarEventListener;
import war.controller.WarUIEventsListener;
import war.db.WarDBConnection;

public class Missile extends AbstractMissile {

Expand Down Expand Up @@ -120,6 +116,8 @@ public void run() {
String print_log = "Missle "+ this.missileId
+ " was launched from launcher: "
+ this.launcher.getLauncherId();
// java.sql.Date sqlDate = Utils.utilDateToSqlDate(birthdate);
int res = WarDBConnection.addNewMissile(missileId,destination,damage,flyTime,status.toString());
for (WarEventListener l : allListeners) {
l.addedMissileToModelEvent(missileId,destination,damage,flyTime);
}
Expand Down
Loading

0 comments on commit 321ac3f

Please sign in to comment.