在Maven / Netbeans中创建罗马数字转换器作为服务

时间:2017-11-06 15:07:52

标签: java web-services maven netbeans glassfish

我试图基本上创建一个罗马数字翻译器并根据我讲师的一些教程将它放到Maven上,问题是教程不是很有用。我有翻译器的代码,它构建和服务器运行得很好,但我还没有能够弄清楚如何进行获取请求或发布请求或在哪里甚至将它们放在代码中。我有点厌倦,所以我想我会在这里询问它是如何完成的。第一次真正发布在这里,所以希望我没有做错任何事,我的翻译代码如下。而且我100%确定我的网络文件和pom文件已经完成。

package com.mycompany.roman;


import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
/**
 *
 * @author Alex Wong
 */
@Path("roman")
@Produces("application/json")
public class RomanArabicConverter {


    //    Checks whether the entered Arabic numeral is valid

public boolean isValidArabic(int x) {
    String num = String.valueOf(x);

   //      Checks each character if it is a digit.

    for (int k = 0; k < num.length(); k++) {
        if (Character.isDigit(num.charAt(k)) == false) {
            return false;
        }
    }

    //  Checking if the number is bigger than 3999 or smaller than 1
    if (x > 3999 ||x < 1) {
        return false;
    }
    return true;
}


//    Checks whether the entered Roman numeral is valid

public boolean isValidRoman(String num) {

//    Checks each character if it is one of I, V, , L, C, D, M (Roman 
characters)

for (int k = 0; k < num.length(); k++) {
    if (num.charAt(k) != 'I' &&

            num.charAt(k) != 'V' &&

            num.charAt(k) != 'X' &&

            num.charAt(k) != 'L' &&

            num.charAt(k) != 'C' &&

            num.charAt(k) != 'D' &&

            num.charAt(k) != 'M') {

        return false;
    }
}
return true;
}

 //   Returns a string containing the entered Arabic numeral in Roman 
numeral form.
public String toRoman(int num) {

if (isValidArabic(num)) {  //      Checking if the number is a valid Arabic number

    String Roman = ""; //      This will be our result string.

    //    Declare and Initiate our Arrays
    String onesArray[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
    String tensArray[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    String hundredsArray[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

    //    Get the ones in the number
    int ones = num % 10;

    //    Get the tens
    num = (num - ones) / 10;
    int tens = num % 10;

    //    Get the hundreds
    num = (num - tens) / 10;
    int hundreds = num % 10;

    //    Get and write the thousands in the number to our string
    num = (num - hundreds) / 10;
    for (int i = 0; i < num; i++) {
        Roman += "M";
    }

    //    Write the hundreds
    if (hundreds >= 1) {
        Roman += hundredsArray[hundreds - 1];
    }

    //    Write the tens
    if (tens >= 1) {
        Roman += tensArray[tens - 1];
    }

    //    And finally, write the ones
    if (ones >= 1) {
        Roman += onesArray[ones - 1];
    }

    //    Return our string.
    return String.valueOf(Roman);

}else{
return null; //     If the number isn't a valid Arabic number, return null.
}
}
  //      Returns a string containing the entered Roman numeral in Arabic 
numeral form.
public String toArabic(String s) {

    if (isValidRoman(s)) { //      Check if the number is a valid Roman Number

        int Arabic = 0; //    This will be our result.
        int last_digit = 0;
        int current_digit = 0;

        for (int i = 0; i < s.length(); i++) {

            if (s.charAt(i) == 'I') {
                current_digit = 1;
            }
            if (s.charAt(i) == 'V') {
                current_digit = 5;
            }
            if (s.charAt(i) == 'X') {
                current_digit = 10;
            }
            if (s.charAt(i) == 'L') {
                current_digit = 50;
            }
            if (s.charAt(i) == 'C') {
                current_digit = 100;
            }
            if (s.charAt(i) == 'D') {
                current_digit = 500;
            }
            if (s.charAt(i) == 'M') {
                current_digit = 1000;
            }

            if (last_digit < current_digit && last_digit != 0) {
                current_digit -= last_digit;
                Arabic -= last_digit;
                Arabic += current_digit;
                last_digit = current_digit;
                current_digit = 0;
            } else {
                last_digit = current_digit;
                Arabic += current_digit;
                current_digit = 0;
            }
        }

        //    Return our string.
        return String.valueOf(Arabic);

    }else{
    return null;        //    Return null if the number entered is not a proper roman number.
    }
}

}                       

0 个答案:

没有答案