XML+仿cmd
BeSuper
思维导图:
BeSuper思维导图.pngBeSuper.java 代码
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement(name = "besuper")
@XmlAccessorType(XmlAccessType.FIELD)
public class BeSuper {
@XmlElementWrapper(name = "users")
@XmlElement(name = "user")
private List<User> users;
@XmlElementWrapper(name = "questions")
@XmlElement(name = "question")
private List<Question> questions;
@XmlElementWrapper(name = "answers")
@XmlElement(name = "answer")
private List<Answer> answers;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
class User {
@XmlElement(name = "name")
private String name;
@XmlElement(name = "score")
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
class Question {
@XmlAttribute(name = "id")
private String id;
@XmlAttribute(name = "from")
private String from;
@XmlAttribute(name = "solved")
private boolean solved;
@XmlValue
private String content;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isSolved() {
return solved;
}
public void setSolved(boolean solved) {
this.solved = solved;
}
public String toString() {
String template = "[%s] %s. [%s] from %s";
String solvedLabel = isSolved() ? "SOLVED" : "UNSOLVED";
return String.format(template, solvedLabel, id, content, from);
}
}
@XmlAccessorType(XmlAccessType.FIELD)
class Answer {
@XmlAttribute(name = "id")
private String id;
@XmlAttribute(name = "from")
private String from;
@XmlValue
private String content;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String toString() {
return id + ": [ANSWER: " + content + ", from " + from + "]";
}
}
image01.png
QingkeConsole.java 代码
import java.io.InputStream;
import java.util.Scanner;
public class QingkeConsole {
// private
private static Scanner scanner;
static {
scanner = new Scanner(System.in);
}
private QingkeConsole(InputStream is) {
scanner = new Scanner(is);
}
public static String NEW_LINE = "\n";
public static void print(String line) {
System.out.print(line);
}
public static void println(Object object) {
System.out.println(object);
}
public static void println(String line) {
System.out.println(line);
}
public static String askUserInput(String prompt) {
while (true) {
System.out.print(prompt);
String input = scanner.nextLine().trim();
if (!"".equals(input)) {
return input;
}
System.out.println("Invalid input. Empty value is not allowed!");
}
}
}
image02.png
BeSuperLauncher.java 代码
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
public class BeSuperLauncher {
private final static String EXIT = "exit";
private final static String HELP = "help";
private final static String ASK = "ask";
private final static String ANSWER = "answer";
private final static String LIST = "list";
private final static String GRADE = "grade";
private static String command;
private static BeSuper beSuper;
private static User player = null;
public static void main(String[] args) {
// initialize fields
loadBeSuper();
// entrance
QingkeConsole.print("Welcome to play this game, ");
while (true) {
String name = QingkeConsole.askUserInput("please enter your name: ");
if ("".equals(name.trim())) {
QingkeConsole.println("Name is not allowed to be empty!");
continue;
}
for (User user : beSuper.getUsers()) {
if (name.equals(user.getName())) {
player = user;
}
}
if (player == null) {
player = new User();
player.setName(name);
beSuper.getUsers().add(player);
saveBeSuper();
}
break;
}
QingkeConsole.println("Nice to meet you, " + player.getName() + ". You can type 'help' for usage.");
while (true) {
command = QingkeConsole.askUserInput("cmd> ");
if (HELP.equalsIgnoreCase(command)) {
printUsage();
continue;
} else if (EXIT.equalsIgnoreCase(command)) {
System.out.println("Bye Bye");
break;
} else if (LIST.equalsIgnoreCase(command)){
for (Question question : beSuper.getQuestions()) {
QingkeConsole.println(question);
}
} else if (GRADE.equalsIgnoreCase(command)) {
QingkeConsole.println("Your score: " + player.getScore());
continue;
} else if (ASK.equalsIgnoreCase(command)) {
String content = QingkeConsole.askUserInput("Please enter your question: ");
Question question = new Question();
question.setContent(content);
question.setFrom(player.getName());
question.setSolved(false);
question.setId(System.currentTimeMillis() + "");
beSuper.getQuestions().add(question);
QingkeConsole.println("Your question has been created!");
saveBeSuper();
continue;
} else if (ANSWER.equalsIgnoreCase(command)) {
String qid = QingkeConsole.askUserInput("Enter the question id: ");
Question q = null;
Answer a = null;
for (Question question : beSuper.getQuestions()) {
if (qid.equals(question.getId())) {
q = question;
break;
}
}
if (q == null) {
QingkeConsole.println("Question for id: " + qid + " is not exist.");
continue;
} else if (q.isSolved()) {
for (Answer answer : beSuper.getAnswers()) {
if (qid.equalsIgnoreCase(answer.getId())) {
a = answer;
break;
}
}
QingkeConsole.println("Qusestion has been solved. See anwser below: ");
QingkeConsole.println(a);
continue;
}
String content = QingkeConsole.askUserInput("Enter your answer for the question");
a = new Answer();
a.setId(qid);
a.setFrom(player.getName());
a.setContent(content);
q.setSolved(true);
player.setScore(player.getScore() + 10);
beSuper.getAnswers().add(a);
QingkeConsole.println("Thanks for your answer.");
saveBeSuper();
continue;
} else {
QingkeConsole.println("Invalid command. See the user as below:");
printUsage();
}
}
}
private static void loadBeSuper() {
JAXBContext ctx;
try {
ctx = JAXBContext.newInstance(BeSuper.class);
beSuper = (BeSuper) ctx.createUnmarshaller().unmarshal(new FileInputStream("besuper.xml"));
Collections.sort(beSuper.getQuestions(), new Comparator<Question>() {
@Override
public int compare(Question o1, Question o2) {
boolean isSovled1 = o1.isSolved();
boolean isSovled2 = o2.isSolved();
if (isSovled1 == isSovled2) {
String id1 = o1.getId();
String id2 = o2.getId();
return id1.compareTo(id2);
} else if (isSovled1) {
return 1;
} else if (isSovled2) {
return -1;
}
return 0;
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
private static void saveBeSuper() {
JAXBContext ctx;
try {
ctx = JAXBContext.newInstance(BeSuper.class);
ctx.createMarshaller().marshal(beSuper, new File("besuper.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
}
private static void printUsage() {
StringBuilder sb = new StringBuilder("This is the usage for BeSuper game:\n");
sb.append("exit - Exit").append("\n");
sb.append("help - Print usage").append("\n");
sb.append("list - List the opening question").append("\n");
sb.append("ask - Create a question").append("\n");
sb.append("answer - Answer a question").append("\n");
sb.append("grade - Get your grade").append("\n");
QingkeConsole.println(sb.toString());
}
}
image03.png
运行实现结果:
image04.png image05.png
具体详见思维导图
作者: 聪聪工作室
业务合作: 18758171751
微信: 18758171751
Email: 1099749430@qq.com聪聪工作室---Java---独家制作
版权所有,盗版必究!
网友评论