I haven't worked a lot with conversions, maybe I have some flaws in my logic.
Here is my logic:
1. First I create a method that verifies if the input ins correct. It tries to parse the number to base 10 using Long.parseLong(value,radix). If the input is not correct it will throw an exception.
2.If the convertNumberToOtherNumberSystem method, I convert the number.getDigit() to a base 10 long using Long.parseLong(number.getDigit(), number.getNumberSystem().getNumberSystemIntValue())
3. I create a string representation of the required base number from the decimal number that I created at step 2 using the method Long.ToString(decimalNumber, required base)
4. I return the new Nubmer.
package com.codegym.task.task30.task3001;
/*
Numeral system converter
*/
public class Solution {
public static void main(String[] args) {
Number number = new Number(NumberSystemType._10, "6");
Number result = convertNumberToOtherNumberSystem(number, NumberSystemType._2);
System.out.println(result); // 110 expected
number = new Number(NumberSystemType._16, "6df");
result = convertNumberToOtherNumberSystem(number, NumberSystemType._8);
System.out.println(result); // 3337 expected
number = new Number(NumberSystemType._16, "abcdefabcdef");
result = convertNumberToOtherNumberSystem(number, NumberSystemType._16);
System.out.println(result); // abcdefabcdef expected
number = new Number(NumberSystemType._12, "7ab");
result = convertNumberToOtherNumberSystem(number, NumberSystemType._10);
System.out.println(result); // 1139 expected
}
//I used this logic. First I convert the number given to a base 10 number, then I convert the number from base 10 to the required base.
public static Number convertNumberToOtherNumberSystem(Number number, NumberSystem expectedNumberSystem) {
//write your code here
checkIfNumberFormatIsCorrect(number);
long decimalNumber=Long.parseLong(number.getDigit(),number.getNumberSystem().getNumberSystemIntValue());
String convertedNumber = Long.toString(decimalNumber, expectedNumberSystem.getNumberSystemIntValue());
return new Number(expectedNumberSystem, convertedNumber);
}
//This methode checks if the number format is ok. If it is not, it throws and exception
public static void checkIfNumberFormatIsCorrect (Number number){
switch (number.getNumberSystem().getNumberSystemIntValue()) {
case 16:try{
long number1=Long.parseLong(number.getDigit(),16);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 12:
try{
long number2=Long.parseLong(number.getDigit(),12);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 10:
try{
long number3=Long.parseLong(number.getDigit(),10);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 9:
try{
long number4=Long.parseLong(number.getDigit(),9);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 8:
try{
long number5=Long.parseLong(number.getDigit(),8);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 7:
try{
long number6=Long.parseLong(number.getDigit(),7);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 6:
try{
long number7=Long.parseLong(number.getDigit(),6);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 5:
try{
long number8=Long.parseLong(number.getDigit(),5);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 4:
try{
long number1=Long.parseLong(number.getDigit(),4);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 3:
try{
long number1=Long.parseLong(number.getDigit(),3);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
case 2:
try{
long number1=Long.parseLong(number.getDigit(),2);
}
catch (NumberFormatException e){
throw new NumberFormatException("Not a good number format");
}
break;
}
}
}