码迷,mamicode.com
首页 > 其他好文 > 详细

文章标题

时间:2016-05-13 01:43:58      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:


这里写代码片
package domain;

/**
* 描述指挥官
*
* @author 尚硅谷-李贺飞
* @date 2015-1-15
* @time 下午8:53:32
*/
public class Commander extends SpecialTroops {

private int commanderRating; // 描述指挥权限等级

public Commander() {
}

public Commander(int id, String name, int age, double fighting,
        Weapon weapon, double field, int commanderRating) {
    super(id, name, age, fighting, weapon, field);
    this.commanderRating = commanderRating;
}

public int getCommanderRating() {
    return commanderRating;
}

public void setCommanderRating(int commanderRating) {
    this.commanderRating = commanderRating;
}

public String getDetailsForTeam() {
    return getMemberId() + "/" + getDetails() + "\t" + "指挥官" + "\t"
            + getField() + "\t\t" + getCommanderRating() + "级\t";
}

@Override
public String toString() {
    return getDetails() + "\t指挥官\t" + getStatus() + "\t" + getField()
            + "\t  " + commanderRating + "级\t" + getWeapon();
}

}
package domain;

/**
* 描述手榴弹
*
* @author 尚硅谷-李贺飞
* @date 2015-1-15
* @time 下午8:49:59
*/
public class Grenade implements Weapon {

private String type;
private String name;

public Grenade(String type, String name) {
    this.type = type;
    this.name = name;
}

public Grenade() {
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public String getDescription() {
    return type + name;
}

@Override
public String toString() {
    return type + "(" + name + ")";
}

}
package domain;

/**
* 描述步兵
*
* @author 尚硅谷-李贺飞
* @date 2015-1-15
* @time 下午8:37:42
*/
public class Infantry extends Soldier {

private int memberId; // 成员ID:当步兵加入特遣队后分配的ID
private Status status = Status.FREE; //

private Weapon weapon;

public Infantry() {
}

public Infantry(int id, String name, int age, double fighting, Weapon weapon) {
    super(id, name, age, fighting);
    this.weapon = weapon;
}

public int getMemberId() {
    return memberId;
}

public void setMemeberId(int memberId) {
    this.memberId = memberId;
}

public Status getStatus() {
    return status;
}

public void setStatus(Status status) {
    this.status = status;
}

public Weapon getWeapon() {
    return weapon;
}

public void setWeapon(Weapon weapon) {
    this.weapon = weapon;
}

public String getDetailsForTeam(){
    return getMemberId() + "/" + getDetails() + "\t" + "步兵";
}

@Override
public String toString() {
    return getDetails() + "\t步兵\t" + status + "\t\t\t" + weapon;
}

}
package domain;

/**
* 描述手枪
*
* @author 尚硅谷-李贺飞
* @date 2015-1-15
* @time 下午8:44:38
*/
public class Pistol implements Weapon {

private String model;
private double force;

public Pistol(String model, double force) {
    this.model = model;
    this.force = force;
}

public Pistol() {
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public double getForce() {
    return force;
}

public void setForce(double force) {
    this.force = force;
}

@Override
public String getDescription() {
    return model + force;
}

@Override
public String toString() {
    return model + "(" + force + ")";
}

}
package domain;

/**
* 描述步枪
*
* @author 尚硅谷-李贺飞
* @date 2015-1-15
* @time 下午8:47:41
*/
public class Rifle implements Weapon {

private String model;
private double size; // 描述步枪子弹尺寸 如:5.58毫米

public Rifle(String model, double size) {
    this.model = model;
    this.size = size;
}

public Rifle() {
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public double getSize() {
    return size;
}

public void setSize(double size) {
    this.size = size;
}

@Override
public String getDescription() {
    return model + size;
}

@Override
public String toString() {
    return model + "(" + size + "毫米)";
}

}
package domain;

/**
* 描述普通士兵
*
* @author 尚硅谷-李贺飞
* @date 2015-1-15
* @time 下午8:35:19
*/
public class Soldier {

private int id;
private String name;
private int age;
private double fighting; // 用于描述士兵的战斗力

public Soldier(int id, String name, int age, double fighting) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.fighting = fighting;
}

public Soldier() {
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public double getFighting() {
    return fighting;
}

public void setFighting(double fighting) {
    this.fighting = fighting;
}

public String getDetails(){
    return id + "\t" + name + "\t" + age + "\t" + fighting;
}

@Override
public String toString() {
    return getDetails();
}

}
package domain;

/**
* 描述特种兵
*
* @author 尚硅谷-李贺飞
* @date 2015-1-15
* @time 下午8:52:13
*/
public class SpecialTroops extends Infantry {

private double field; // 描述野外生存能力指数

public SpecialTroops() {
}

public SpecialTroops(int id, String name, int age, double fighting,
        Weapon weapon, double field) {
    super(id, name, age, fighting, weapon);
    this.field = field;
}

public double getField() {
    return field;
}

public void setField(double field) {
    this.field = field;
}

public String getDetailsForTeam(){
    return getMemberId() + "/" + getDetails() + "\t" + "特种兵" + "\t" + getField();
}

@Override
public String toString() {
    return getDetails() + "\t特种兵\t" + getStatus() + "\t" + getField() + "\t\t" + getWeapon();
}

}
package domain;

public enum Status {

FREE,
BUSY,
VOCATION; //正在休假

}
package domain;

public class TeamException extends Exception {
private static final long serialVersionUID = 11489748451L;

public TeamException() {
}

public TeamException(String message) {
    super(message);
}

}
package domain;

public interface Weapon {

public String getDescription();

}
package Service;

public class Data {
public static final int SOLDIER = 10;
public static final int INFANTRY = 11;
public static final int SPECIALTROOPS = 12;
public static final int COMMANDER = 13;

public static final int PISTOL = 21;
public static final int RIFLE = 22;
public static final int GRENADE = 23;

//Soldier  :  10, id, name, age, fighting
//Infantry:  11, id, name, age, fighting
//SpecialTroops  :  12, id, name, age, fighting, field
//Commander :  13, id, name, age, fighting, field, commanderRating
public static final String[][] SOLDIERS = {
    {"10", "1", "小龙女", "22", "3000"},
    {"13", "2", "诸葛亮", "32", "18000", "15000", "5"},
    {"11", "3", "鲁智深", "23", "7000"},
    {"11", "4", "赵子龙", "24", "7300"},
    {"12", "5", "李师师", "28", "10000", "5000"},
    {"11", "6", "潘金莲", "22", "6800"},
    {"12", "7", "林冲", "29", "10800","5200"},
    {"13", "8", "韦小宝", "30", "19800", "15000", "3"},
    {"12", "9", "杨过", "26", "9800", "5500"},
    {"11", "10", "乔峰", "21", "6600"},
    {"11", "11", "吕布", "25", "7100"},
    {"12", "12", "陈圆圆", "27", "9600", "4800"}
};

//Pistol      :21, model, force
//Rifle       :22, model, size
//Grenade     :23, type, name
public static final String[][] WEAPONS = {
    {},
    {"22", "AK47", "7.62"},
    {"21", "伯莱塔92F ", "8000"},
    {"21", "德国HKP7", "3800"},
    {"23", "杀伤型", "M24型柄式"},
    {"21", "沙漠之鹰", "9000"},
    {"21", "中国QSG92", "10000"},
    {"23", "烟雾", "OTO M35"},
    {"22", "M4A1", "5.56"},
    {"21", "鲁格P85", "5000"},
    {"21", "捷克CZ83","6000"},
    {"22", "95式", "5.8"}
};

}
package Service;

import static Service.Data.GRENADE;
import static Service.Data.INFANTRY;
import static Service.Data.PISTOL;
import static Service.Data.RIFLE;
import static Service.Data.SOLDIERS;
import static Service.Data.*;

import domain.Commander;
import domain.Grenade;
import domain.Infantry;
import domain.Pistol;
import domain.Rifle;
import domain.Soldier;
import domain.SpecialTroops;
import domain.TeamException;
import domain.Weapon;

public class NameListService {

public static void main(String[] args) {
    NameListService nis = new NameListService();
    for (int i = 0; i < nis.soliders.length; i++) {
        System.out.println(nis.soliders[i]);
    }
}

private Soldier[] soliders;

// 获取所有士兵
public Soldier[] getAllSoldiers() {
    return soliders;
}

public Soldier getSoldier(int id) throws TeamException {
    for (int i = 0; i < soliders.length; i++) {
        if (soliders[i].getId() == id) {
            return soliders[i];
        }
    }
    throw new TeamException("沒有該士兵");
}

public NameListService() {
    soliders = new Soldier[SOLDIERS.length];
    for (int i = 0; i < soliders.length; i++) {
        Soldier solider = null;
        int type = Integer.parseInt(SOLDIERS[i][0]);
        int id = Integer.parseInt(SOLDIERS[i][1]);
        String name = SOLDIERS[i][2];
        int age = Integer.parseInt(SOLDIERS[i][3]);
        double fighting = Double.parseDouble(SOLDIERS[i][4]);

        Weapon weapon = null;
        double field;
        int commandeRating;
        switch (type) {
        case INFANTRY:
            weapon = creatWeapon(i);
            solider = new Infantry(id, name, age, fighting, weapon);
            break;
        case SPECIALTROOPS:
            weapon = creatWeapon(i);
            solider = new SpecialTroops(id, name, age, fighting, weapon, Double.parseDouble(SOLDIERS[i][5]));
            break;
        case COMMANDER:
            weapon = creatWeapon(i);
            solider = new Commander(id, name, age, fighting, weapon, Double.parseDouble(SOLDIERS[i][5]),
                    Integer.parseInt(SOLDIERS[i][6]));
            break;
        default:
            solider = new Soldier(id, name, age, fighting);
            break;
        }
        soliders[i] = solider;

    }

}

public Weapon creatWeapon(int i) {
    int type = Integer.parseInt(WEAPONS[i][0]);
    Weapon weapon = null;
    switch (type) {
    case PISTOL:
        weapon = new Pistol(WEAPONS[i][1], Double.parseDouble(WEAPONS[i][2]));
        break;
    case RIFLE:
        weapon = new Rifle(WEAPONS[i][1], Double.parseDouble(WEAPONS[i][2]));
        break;
    case GRENADE:
        weapon = new Grenade(WEAPONS[i][1], WEAPONS[i][2]);
        break;
    }
    return weapon;
}

}
package Service;

import com.sun.org.apache.xerces.internal.util.Status;

import domain.Commander;
import domain.Infantry;
import domain.Soldier;
import domain.SpecialTroops;
import domain.TeamException;

public class TeamService {
private static int count = 1;// 用於生成特遣隊的成員變量
private final int MAX_MEMBER = 5;
private int total = 0;// 记录特遣队中有效的成员个数
private Infantry[] term = new Infantry[MAX_MEMBER];
public static void main(String[] args) {
NameListService ns=new NameListService();
Soldier[] soldier= ns.getAllSoldiers();
TeamService ts=new TeamService();
try {
ts.addMembers(soldier[11]);
ts.addMembers(soldier[10]);
ts.addMembers(soldier[0]);
} catch (Exception e) {
System.out.println(e.getMessage());
}
Infantry[] term=ts.getTerm();
for(Infantry in:term){
System.out.println(in.getDetailsForTeam());
}
}
// 返回特遣队种有效的成员个数
public Infantry[] getTerm() {
Infantry[] infantry = new Infantry[total];
for (int i = 0; i < infantry.length; i++) {
infantry[i] = term[i];
}
return infantry;
}

public void addMembers(Soldier s) throws Exception {
    // 特遣队成员已满,无法添加
    if (total > MAX_MEMBER)
        throw new Exception("特遣队成员已满,无法添加");
    // 该士兵是普通士兵,无法添加
    if (!(s instanceof Infantry))
        throw new Exception("该士兵是普通士兵,无法添加");
    // 该士兵已是特遣队员
    // 该士兵正在休假,无法添加
    Infantry infantry = (Infantry) s;
    domain.Status status = infantry.getStatus();
    switch (status) {
    case BUSY:
        throw new Exception("该士兵已是特遣队员");
    case VOCATION:
        throw new Exception("该士兵正在休假,无法添加");
    }
    // 该士兵已是特遣队员
    for (int i = 0; i < total; i++) {
        if (infantry.getId() == term[i].getId()) {
            throw new Exception("该士兵已是当前特遣队队员");
        }
    }
    // 特遣队中只能有一名指挥官
    // 特遣队中只能有两名特种兵
    // 特遣队中只能有三名步兵
    int numofin = 0, numofsp = 0, numofcom = 0;
    for (int i = 0; i < total; i++) {
        if (term[i] instanceof Commander)
            numofcom++;
        else if (term[i] instanceof SpecialTroops)
            numofsp++;
        else if (term[i] instanceof Infantry)
            numofin++;
    }
    if (infantry instanceof Commander) {
        if (numofcom >= 1)
            throw new Exception("特遣队中只能有一名指挥官");
    } else if (infantry instanceof SpecialTroops) {
        if (numofsp >= 2) {
            throw new Exception("特遣队中只能有两名特种兵");
        }
    } else if (infantry instanceof Infantry) {
        if (numofin >= 3) {
            throw new Exception("特遣队中只能有三名步兵");
        }
    }
    infantry.setMemeberId(count++);
    infantry.setStatus(status.BUSY);
    term[total++] = infantry;
}
public void removeMember(int memberid) throws Exception {
    int i = 0;
    for (; i < total; i++) {
        if (term[i].getMemberId() == memberid) {
            term[i].setStatus(domain.Status.FREE);
            for (int j = i + 1; j < total; j++) {
                term[j - 1] = term[j];
            }
            break;
        }

    }
    if (i == total)
        throw new Exception("特遣队中只没有该成员");
    term[--total] = null;
}

}
package Service;

import domain.Infantry;
import domain.Soldier;
import domain.TeamException;

public class TeamView {
private NameListService listSvc = new NameListService();
private TeamService teamSvc = new TeamService();// 主要用于管理特遣队成员

public static void main(String[] args) {
    TeamView tv = new TeamView();
    tv.enterMainMenu();
}

public void enterMainMenu() {
    boolean loop = true;
    char key = 0;
    do {
        if (key != ‘1‘)
            listSoliders();
        System.out.print("1-特遣队列表  2-添加特遣队成员 3-删除特遣队成员  4-退出     请选择(1-4):");
        key = TSUtility.readMenuSelection();
        switch (key) {
        case ‘1‘:
            getallTerm();
            break;
        case ‘2‘:
            addmember();
            break;
        case ‘3‘:
            deletemember();
            break;
        case ‘4‘:
            System.out.print("确认是否删除(Y/N):");
            char c = TSUtility.readConfirmSelection();
            if (c == ‘Y‘) {
                loop= false;
            }
        }
    } while (loop);
}

private void deletemember() {
    System.out.println("\n--------------------特遣队成员列表---------------------\n");
    int memberid = TSUtility.readInt();
    System.out.print("确认是否删除(Y/N):");
    char key = TSUtility.readConfirmSelection();
    if (key == ‘N‘)
        return;
    else {
        try {
            teamSvc.removeMember(memberid);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // 按回车继续
    TSUtility.readReturn();

}

public void getallTerm() {
    System.out.println("\n--------------------特遣队成员列表---------------------\n");
    Infantry[] infantries = teamSvc.getTerm();
    if (infantries.length == 0) {
        System.out.println("該特遣隊中沒有成員");
    } else {
        System.out.println("TID/ID\t姓名\t年龄\t战斗力\t兵种\t野外生存指数\t指挥等级");

    }
    for (int i = 0; i < infantries.length; i++) {
        System.out.println(infantries[i].getDetailsForTeam());
    }
    System.out.println("----------------------------------------------------");

}

public void addmember() {
    System.out.println("\n--------------------添加特遣队成员---------------------\b");
    System.out.print("请输入添加士兵的ID:");
    int id = TSUtility.readInt();
    try {
        Soldier soldier = listSvc.getSoldier(id);
        try {
            teamSvc.addMembers(soldier);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        System.out.println("添加成功");
    } catch (TeamException e) {
        System.out.println("添加失败,原因:" + e.getMessage());
    }

    // 按回车继续
    TSUtility.readReturn();

}

public void listSoliders() {
    System.out.println("\n--------------------特遣队成员列表---------------------\n");

    Soldier[] soldiers = listSvc.getAllSoldiers();
    if (soldiers.length == 0) {
        System.out.println("暂无人员信息!");

    } else {
        System.out.println("TID/ID\t姓名\t年龄\t战斗力\t兵种\t野外生存指数\t指挥等级");

    }
    for (Soldier in : soldiers) {
        System.out.println(in);
    }
    System.out.println("----------------------------------------------------");

}

}
package Service;

import java.util.*;

public class TSUtility {
private static Scanner scanner = new Scanner(System.in);

public static char readMenuSelection() {
    char c;
    for (; ; ) {
        String str = readKeyBoard(1, false);
        c = str.charAt(0);
        if (c != ‘1‘ && c != ‘2‘ &&
            c != ‘3‘ && c != ‘4‘) {
            System.out.print("选择错误,请重新输入:");
        } else break;
    }
    return c;
}

public static void readReturn() {
    System.out.print("按回车键继续...");
    readKeyBoard(100, true);
}

public static int readInt() {
    int n;
    for (; ; ) {
        String str = readKeyBoard(2, false);
        try {
            n = Integer.parseInt(str);
            break;
        } catch (NumberFormatException e) {
            System.out.print("数字输入错误,请重新输入:");
        }
    }
    return n;
}

public static char readConfirmSelection() {
    char c;
    for (; ; ) {
        String str = readKeyBoard(1, false).toUpperCase();
        c = str.charAt(0);
        if (c == ‘Y‘ || c == ‘N‘) {
            break;
        } else {
            System.out.print("选择错误,请重新输入:");
        }
    }
    return c;
}

private static String readKeyBoard(int limit, boolean blankReturn) {
    String line = "";

    while (scanner.hasNextLine()) {
        line = scanner.nextLine();
        if (line.length() == 0) {
            if (blankReturn) return line;
            else continue;
        }

        if (line.length() < 1 || line.length() > limit) {
            System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
            continue;
        }
        break;
    }

    return line;
}

}

文章标题

标签:

原文地址:http://blog.csdn.net/lt6631164/article/details/51345937

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!