Skip to content

Instantly share code, notes, and snippets.

@kohbo
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohbo/9a17d540506fb66d7135 to your computer and use it in GitHub Desktop.
Save kohbo/9a17d540506fb66d7135 to your computer and use it in GitHub Desktop.
Regiments
import java.util.ArrayList;
import java.util.Iterator;
public class Army {
ArrayList<Regiment> army;
Army(){
army = new ArrayList<Regiment>(20);
}
public void addRegiment(Regiment r){
army.add(r);
}
public void updateList(int index, int men){
Regiment temp = army.get(index);
temp.addStrength(men);
}
public void printReport(int week){
int biggest = findBiggest();
Iterator<Regiment> iterator = army.listIterator();
System.out.printf("Week Number %d\n%-7s%-20s%-15s\n", week,"Reg #", "Name", "Strength");
while(iterator.hasNext()){
Regiment temp = iterator.next();
System.out.printf("%-7d%-20s%-15d", temp.getRegnum(), temp.getName(), temp.getStrength());
if(biggest == temp.getRegnum()-1){
temp.deploy();
System.out.println("\tDeployed this week");
} else {
System.out.print("\n");
}
}
System.out.println();
}
public int findBiggest(){
int num = 0;
int biggest = 0;
for(int i = 0; i < army.size(); i++){
if(army.get(i).getStrength() > num){
num = army.get(i).getStrength();
biggest = i;
}
}
return biggest;
}
public void addStrength(){
for(Regiment r : army){
if(r.getRegnum() == 5){
r.addStrength(30);
} else {
r.addStrength(100);
}
}
}
}
public class Regiment {
private int regnum; //Regiment Number
private String name;
private int strength; //number of men
Regiment(int r, String s, int str){
regnum = r;
name = s;
strength = str;
}
void addStrength(int men){
strength += men;
}
public int getRegnum() {
return regnum;
}
public String getName() {
return name;
}
public int getStrength() {
return strength;
}
public void deploy(){
strength = 0;
}
}
1 Aardvarks
2 Begonias
3 Chrysanthemums
4 Dhalias
5 Elephants
6 Ferrets
7 GilaMonsters
8 Hyraxes
9 Ibex
10 Jackyls
11 KimodoDragons
12 Lemurs
13 Marigolds
14 Nonames
15 Opossums
16 Porcupines
17 Quahogs
18 Rhododendrons
19 Swordfish
20 Tapirs
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Test {
FileInputStream input;
public static void main(String[] args){
int week;
int men = 1000;
Army army = new Army();
try {
Scanner scan = new Scanner(new File("regiments.txt"));
while(scan.hasNextLine()){
String line[] = scan.nextLine().split(" ");
Regiment r = new Regiment(Integer.valueOf(line[0]),line[1],men);
army.addRegiment(r);
men -= 50;
}
} catch (FileNotFoundException e) {
System.out.println("Error opening file...");
System.exit(0);
}
for(week = 1; week <= 20; week++){
army.printReport(week);
army.addStrength();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment