Javaããã°ã©ãã³ã°è½åèªå®è©¦é¨èª²é¡ããã°ã©ã ã®ãªãã¡ã¯ã¿ãªã³ã°ã¬ãã¼ãï¼ãã®ï¼ï¼
ååã®Javaプログラミング能力認定試験課題プログラムのリファクタリングレポート(その1)ã«å¼ãç¶ãã試é¨åé¡ã®ãªãã¡ã¯ã¿ãªã³ã°ã«ã¤ãã¦èãã¾ãã
ç»é¢å ¥åºåå¦çã®æ½è±¡åã¨ã«ãã»ã«å
ååã¯ãã®è©¦é¨åé¡ã§ãã£ã¨ãã³ã¼ãã£ã³ã°ãé¢åãªãã¡ã¤ã«å ¥åºåé¢é£ã®å¦çãRepositoryã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹åã§æ½è±¡åãããã¨ã§ãå¦çãå ±éåãããã ãã§ãªããå ¨ä½ã®ããã°ã©ã ã®è¨è¿°ãç°¡æåããããã¨ã説æãã¾ããããã®è©¦é¨åé¡ã®ããã°ã©ã ã§ãã¡ã¤ã«å ¥åºåã¨ãªããã§ããä¸ã¤é¢åãªã®ã¯ãã³ã³ã½ã¼ã«ã使ã£ãã¦ã¼ã¶ã¼ã¨ã®å¯¾è©±å¦çãè¡ãé¨åã§ãããã®å¦çã¯ä¸è¦åç´ããã§ãããä¸æ£ãªå ¥åå¤ããã£ãå ´åã«å¦çãç¹°ãè¿ããªã©ãCOBOLç´ã®åå²ã¨ã«ã¼ãæ§é ã ãã§è¡¨ãã¨å¦çãçµæ§è¤éåãã¦ãã¾ãã¾ããå®éããªãªã¸ãã«ã®ã½ã¼ã¹ã§ã¯ã以ä¸ã®ãããªãããã¦è¤éãªå¤éã«ã¼ãæ§é ããããã¨ããã«åºç¾ãã¾ãã
ï¼Javaããã°ã©ãã³ã°è½åèªå®è©¦é¨1ç´åé¡ããå¼ç¨ï¼ //ãã¼ã¿ã®ãã人æIDãå ¥åãããã¾ã§ã«ã¼ã DELETEKID: while( true ) { String jID; //人æID while( true ) { //人æIDãæ£ããå ¥åãããã¾ã§ã«ã¼ã jID = displayMessage( KD_MESS.GETID ); //人æIDã®åå¾ if( (new JDisplay(jID).getjData()) != null ) { break; } } KDisplay kdisplay = new KDisplay(jID); while( true ) { //人æIDã®åå¾ this.kData = kdisplay.startThis(); //稼åç¶æ³ã表示 if( kData != null ) { No = displayMessage( KD_MESS.GETKID ); //åé¤ãã稼åç¶æ³IDã®åå¾ if (chkKData(No)) break DELETEKID; } else { continue DELETEKID; } } } String YorN = displayMessage( KD_MESS.GETYN ); while( true ) { if( YorN.equals("Y") ) { if( deleteData(No) ) System.out.println( "åé¤ãã¾ããã" ); //åé¤æ¥ä»ã®æ ¼ç´ else System.out.println( "åé¤ã§ãã¾ããã§ããã" ); break; } else if( YorN.equals("N") ) break; else YorN = displayMessage( KD_MESS.R_GETYN ); }
以ä¸ã®ã³ã¼ãã¯å¤éã®ç¡éã«ã¼ãã¨ãªã£ã¦ãããå¤éã«ã¼ããæããããã«ã©ãã«ä»ãbreakæï¼gotoæã«ç¸å½ï¼ã使ããã¦ãã¾ããCOBOLæ代ã¯ããããè¤éãªã³ã¼ããä¸ç¬ã§ä½æãç解ã§ããããç¸å½ã®é人ããã°ã©ãã¼ã¨ããã¦ããã®ããããã¾ãããããããå°ãªãã¨ãJavaã¯ãªãã¸ã§ã¯ãæåè¨èªã§ãããã®ãããªè¤éãªã³ã¼ãã®ä½æã¨ä¿å®ã«ä½åãªç¥çµãããæ¸ããã®ã§ã¯ãªããé©åãªã¤ã³ã¿ã¼ãã§ã¼ã¹ãæ½åºãããã¨ã§ã¯ããã«æ¥½ãã§ãã¾ãã
ååãã¡ã¤ã«å
¥åºåã«å¯¾ãã¦Repositoryã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ãèããã®ã¨åãè¦é ã§ãã³ã³ã½ã¼ã«ã使ã£ãã¦ã¼ã¶ã¼ã¨ã®å¯¾è©±å¦çãæ½è±¡åããConsoleã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ãä½æãããã¨ãèãã¾ããæ¢åã®ã½ã¼ã¹ã³ã¼ããèªãã¨ãããã¾ããããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ã¯ä»¥ä¸ã®ãããªæ©è½ãæããã¾ãã
- åã«æååãã¡ãã»ã¼ã¸ã¨ãã¦è¡¨ç¤ºããã
- æååãå ¥åããã
- æååãå ¥åããããæ£ããå ¥åãå¾ãããã¾ã§å ¥åãç¹°ãè¿ãã
- æ°å¤ãå ¥åããã
- æ¥ä»ãå ¥åããã
- é¸æè¢ã表示ãããã®ä¸ããä¸ã¤ã®é ç®ãé¸æããã
- ã¯ããããããé¸æããã
Javaã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ãã¦ã¯å ·ä½çã«ä»¥ä¸ã®ãããªãã®ãä½æãããã¨ã«ãªãã¾ãã
package sample.common.console; import java.util.Date; import java.util.List; import sample.common.entity.Identifiable; import sample.common.entity.NameId; public interface Console { /** * ã¡ãã»ã¼ã¸ã表示ããã * @param message 表示対象ã¡ãã»ã¼ã¸ */ void display(String message); /** * YesNoã®é¸æã¡ãã»ã¼ã¸ã表示ããã * @param message 表示対象ã¡ãã»ã¼ã¸ * @return Yesãé¸æãããå ´åã¯true */ boolean confirm(String message, String yes, String no); /** * ã¡ãã»ã¼ã¸ã¨ã¨ãã«å ¥åããã³ããã表示ããæ¨æºå ¥åããã®å ¥åãåãä»ãã * * @param 表示ã¡ãã»ã¼ã¸ * @return å ¥åæåå */ String accept(String message); /** * ã¡ãã»ã¼ã¸ã¨ã¨ãã«å ¥åããã³ããã表示ããæ¨æºå ¥åããã®å ¥åãåãä»ããã * æ£ããå ¥åå¤ãå¾ãããã¾ã§ãååº¦å ¥åãç¹°ãè¿ãã * * @param 表示ã¡ãã»ã¼ã¸ * @return å ¥åæåå */ String accept(String message, ValidInput<String> validInput); /** * ã¡ãã»ã¼ã¸ã¨ã¨ãã«å ¥åããã³ããã表示ããæ¨æºå ¥åããã®æ´æ°å ¥åãåãä»ãã * * @param 表示ã¡ãã»ã¼ã¸ * @return å ¥åå¤ */ int acceptInt(String message); int acceptInt(String message, ValidInput<Integer> validInput); /** * ã¡ãã»ã¼ã¸ã¨ã¨ãã«å ¥åããã³ããã表示ããæ¨æºå ¥åããã®é·æ´æ°å ¥åãåãä»ãã * * @param 表示ã¡ãã»ã¼ã¸ * @return å ¥åå¤ */ long acceptLong(String message); long acceptLong(String message, ValidInput<Long> validInput); /** * yyyyMMddæ¸å¼ã§æ¥ä»ãå ¥åãããæ£ããæ¥ä»ãå ¥åãããã¾ã§å¦çãç¹°ãè¿ãã * * @param message 表示ã¡ãã»ã¼ã¸ * @return å ¥åãããæ¥ä» */ Date acceptDate(String message); /** * æ¥ä»ãå ¥åãããæ£ããæ¥ä»ãå ¥åãããã¾ã§å¦çãç¹°ãè¿ãã * * @param message 表示ã¡ãã»ã¼ã¸ * @param format æ¥ä»ãã©ã¼ããã * @return å ¥åãããæ¥ä» */ Date acceptDate(String message, String format); /** * ã¡ãã»ã¼ã¸ã¨ã¨ãã«é¸æè¢ã®ãªã¹ãã表示ããã * é¸æçµæãè¿ããæ£ããé¸æçµæãå ¥åãããã¾ã§ãå é¨ã§åå ¥åãä¿ãã * * @param selectList * @param message * @return é¸æçµæ */ String acceptFromNameIdList(List<? extends NameId<?>> selectList, String message); String acceptFromIdList(List<? extends Identifiable<?>> selectList, String message); String acceptFromList(List<String> selectList, String message); }
æååã®åºåãdisplayãå ¥åãacceptã¨ããã®ã¯ãããã¦COBOL風ã®ããã£ãã©ãªã¼ãå©ç¨ããã¡ãã£ã¨ããéã³å¿ããã§ãããã®ã¯ã©ã¹ãå®è£ ãã¦ãã¾ãã°ãåºæ¬çã«ããããæ©è½ããåå©ç¨ãããã¨ãã§ãã¾ããå®éãååã®Repositoryã«ãããã¡ã¤ã«å ¥åºåã®æ½è±¡åã¨åããããã¨ã§ãã稼åãã®ç»é²å¦çã¯ã以ä¸ã®ããã«ãããã¦ç°¡åã«è¨è¿°ã§ãã¾ãã
package sample.app.work_management; import java.io.File; import sample.common.console.Console; import sample.common.console.ConsoleImpl; import sample.common.console.ValidInput; import sample.common.io.CharSeparatedFileRepository; import sample.common.program.Function; import sample.domain.HumanResource; import sample.domain.Partner; import sample.domain.Work; /** * 稼åç¶æ³å ¥å */ public class InputWorkFunction implements Function { // TODO DIå private CharSeparatedFileRepository<Work> workRepository = new CharSeparatedFileRepository<Work>(); private CharSeparatedFileRepository<HumanResource> hrRepository = new CharSeparatedFileRepository<HumanResource>(); private CharSeparatedFileRepository<Partner> partnerRepository = new CharSeparatedFileRepository<Partner>(); private Console console = new ConsoleImpl(); public InputWorkFunction() { workRepository.setMasterFile(new File("kadou.txt")); workRepository.setWorkFile(new File("kadou.tmp")); partnerRepository.setMasterFile(new File("torihiki.txt")); partnerRepository.setWorkFile(new File("torihiki.tmp")); } /** * 稼åç¶æ³ç®¡ç(追å )ã®å®è¡ */ public void run() { Work work = inputData(); doCreate(work); } /** * 稼åç¶æ³ã®å ¥å */ private Work inputData() { Work work = new Work(); long hrId = console.acceptLong("人æIDãå ¥åãã¦ãã ããã", new ValidInput<Long>() { @Override public boolean isValid(Long input) { // 人æIDåå¨ãã§ã㯠return hrRepository.findById(input) != null; } }); work.setHrId(hrId); work.setPartnerId(console.acceptFromNameIdList(partnerRepository.findAll(), "åå¼å ãé¸æãã¦ãã ããã")); work.setStartDate(console.accept("稼åéå§æ¥ãå ¥åãã¦ãã ããã")); work.setEndDate(console.accept("稼åçµäºæ¥ãå ¥åãã¦ãã ããã")); work.setContractSalary(console.accept("å¥ç´å価ãå ¥åãã¦ãã ããã")); return work; } /** * 稼åç¶æ³ã®ãã¡ã¤ã«ã¸ã®ç»é² */ private void doCreate(Work work) { workRepository.create(work); console.display("ç»é²ããã¾ããã"); } }
ãã®ããã°ã©ã ãªãããã»ã©ã³ã¼ãã®èªè§£ãé
ã人ã§ãæ°åãããã°å®å
¨ã«å
容ãææ¡ã§ããã¨æãã¾ããè¤éãªå¦çãæ½è±¡åãããã¨ã§ããå
¥åãåãåããããã¡ã¤ã«ã«ç»é²ããããªã©å¤é¨ä»æ§æ¸ã«è¿ãé常ã«é«ãã¬ãã«ã§ããã°ã©ã ãè¨è¿°ããã¦ãããã¨ã«æ³¨ç®ãã¦ãã ãããCOBOLæ代ã§ã¯ããã«å¤§éã®ã³ã¼ããéãè¨è¿°ããç解ã§ãããã¨ããã¨ãããããã°ã©ãã¼ã®è½åã ã£ãã®ããããã¾ãããããªãã¸ã§ã¯ãæåè¨èªã®å ´åã¯ãããã«ã³ã¼ããåç´ã«ããããããè¨è¿°ã§ãããã¨ããã¨ãããããã°ã©ãã¼ã«ãã£ã¨ãè¦æ±ãããè½åã«ãªãã®ã§ãã
åèã¾ã§ã«ãä¸è¨ã¨åãæ©è½ãå®ç¾ãã¦ãããªãªã¸ãã«ã®ã½ã¼ã¹ã以ä¸ã«å¼ç¨ãã¦ããã¾ãã
ï¼Javaããã°ã©ãã³ã°è½åèªå®è©¦é¨1ç´åé¡ããå¼ç¨ï¼ /* KInput.java */ import java.io.BufferedReader; import java.io.FileReader; import java.io.File; import java.io.IOException; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.FileNotFoundException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.ArrayList; /** 稼åç¶æ³å ¥å */ class KInput extends GChars { /** ã¡ãã»ã¼ã¸é¸æç¨åæåå®æ° */ enum KI_MESS { GETID } /** 稼åç¶æ³ç®¡ç(追å )ã®å®è¡ */ void startThis() { String jID; //人æID while( true ) { //人æIDãæ£ããå ¥åãããã¾ã§ã«ã¼ã jID = displayMessage( KI_MESS.GETID ); //人æIDã®åå¾ if( (new JDisplay(jID).getjData()) != null ) { break; } } if( writeData(jID,getData(getTList())) ) { System.out.println( "ç»é²ããã¾ããã" ); } } /** 人æIDã¨ç¨¼åç¶æ³ãã¹ã¿ãç §åï¼æ ¼ç´ããã¦ããæ大ã®ç¨¼åç¶æ³çªå·ãæ¤ç´¢ãï¼ããã« * 1ãå ããå¤ãå ¥åããã稼åç¶æ³ã«å²ãå½ã¦ã¦ç¨¼åãã¹ã¿ã«åºå * @param jID 人æIDã表ãæåå * @param inData å ¥åããã稼åç¶æ³ã表ãæååé å * @return æ£å¸¸çµäºãã©ã° */ boolean writeData( String jID,String[] inData ) { File textFile = new File( "kadou.txt" ); File tempFile = new File( "kadou.tmp" ); try { BufferedReader br = new BufferedReader( new FileReader(textFile) ); //稼åç¶æ³ãã¹ã¿ãéã BufferedWriter bw = new BufferedWriter( new FileWriter(tempFile) ); //ãã³ãã©ãªã¼ãã¡ã¤ã«ãéã String instr, ID, strtmp; int No = 0, mNo = 0; //稼åç¶æ³ãã¹ã¿ãã1ã¬ã³ã¼ããã¤èªè¾¼ã¿ while( (instr=br.readLine()) != null ) { ID = instr.substring( 0, instr.indexOf('\t') ); //人æIDã®ååºã if( ID.equals(jID) ) { //人æIDãä¸è´ strtmp = instr.substring( instr.indexOf('\t')+1, instr.length() ); No = Integer.parseInt( strtmp.substring( 0, strtmp.indexOf('\t') ) ); //稼åç¶æ³çªå·ã®ååºã if( No > mNo ) { mNo = No; //æ大ã®ç¨¼åç¶æ³çªå·ã®åå¾ } } bw.write( instr ); //人ææ å ±ã®è»¢è¨ bw.newLine(); //æ¹è¡ } bw.write( jID+"\t"+(++mNo)+listtoStr_t(inData) ); //æ°ãããã¼ã¿ã®æ¸è¾¼ã¿ bw.newLine(); //æ¹è¡ br.close(); bw.close(); textFile.delete(); tempFile.renameTo( textFile ); //ãã³ãã©ãªã¼ãã¡ã¤ã«ã稼åç¶æ³ãã¹ã¿ã«ç½®æã return true; } catch( IOException e ) { //稼åç¶æ³ãã¹ã¿ã¸ã®ã¢ã¯ã»ã¹ã¨ã©ã¼ System.err.println( e.getMessage() ); } return false; } /** 稼åç¶æ³ã®å ¥å * @param TList åå¼å ãªã¹ãã表ãæååé å * @return å ¥åããã稼åç¶æ³æ å ± */ String[] getData( String[][] TList ) { String inData[][] = { { "åå¼å ID", "稼åéå§æ¥", "稼åçµäºæ¥", "å¥ç´å価" }, { "", "", "", "" } }; for( int i = 0; i < inData[0].length; i++ ) { if( inData[0][i].equals("åå¼å ID") ) { RIGHTTID: while( true ) { //æå¹ãªåå¼å IDãå ¥åãããã¾ã§ã«ã¼ã System.out.println( "åå¼å ãé¸æãã¦ãã ããã" ); System.out.print( listtoStr_t(TList) ); inData[1][i] = getChars(); for( String TL : TList[0] ) { if( inData[1][i].equals( TL ) ) { break RIGHTTID; } } } } else { System.out.print( inData[0][i]+"ãå ¥åãã¦ãã ããã\n>" ); inData[1][i] = getChars(); } } return inData[1]; } /** å ¥åããã稼åç¶æ³ãã¿ãåºåãã®æååã«å¤æãï¼æ¥ä»ãä»å ãã * @param inData å ¥åããã稼åç¶æ³ * @return æåå */ String listtoStr_t( String[] inData ) { StringBuffer buff = new StringBuffer(); for( String inD : inData ) buff.append( "\t"+inD ); //稼åç¶æ³ãã¼ã¿ SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd" ); String today = dateFormat.format( new Date() ); buff.append( "\t"+today+"\t"+today+"\t" ); //æ¥ä» return buff.toString(); } /** åå¼å ãªã¹ãã®é ç®ãæååã«å¤æãã * @param TList åå¼å ãªã¹ãã表ãæååé å * @return æåå */ String listtoStr_t( String[][] TList ) { StringBuffer buff = new StringBuffer(); for( int i = 0; i < TList[0].length; i++ ) { buff.append( TList[0][i]+" "+TList[1][i] ); buff.append( "\n" ); } buff.append( " [" ); //åå¼å IDãªã¹ãã®è¡¨ç¤º for( String TL : TList[0] ) { buff.append( TL ); buff.append( "," ); } buff.deleteCharAt( buff.length()-1 ); //æ«å°¾ã®","ãåé¤ buff.append( "]>" ); return buff.toString(); } /** åå¼å ãªã¹ãããã¡ã¤ã«ããèªã¿è¾¼ã * @return åå¼å ãªã¹ã */ String[][] getTList() { String instr; ArrayList<String> a_ID = new ArrayList<String>(); ArrayList<String> a_Name = new ArrayList<String>(); try { BufferedReader br = new BufferedReader( new FileReader( "torihiki.txt" ) ); //åå¼å ãã¹ã¿ãéã //åå¼å ãã¹ã¿ãã1ã¬ã³ã¼ããã¤èªè¾¼ã¿ while( (instr = br.readLine()) != null ) { if( (instr.length()-1) == instr.lastIndexOf( '\t' ) ) { //åé¤æ¥ä»ãªã String[] s = instr.split( "\t" ); a_ID.add( s[0] ); a_Name.add( s[1] ); } } br.close(); //åå¼å ãã¹ã¿ãéãã } catch( FileNotFoundException e ) { //åå¼å ãã¹ã¿ããªã } catch( IOException e ) { //åå¼å ãã¹ã¿ã¸ã®ã¢ã¯ã»ã¹ã¨ã©ã¼ } String[][] TList = new String[2][a_ID.size()]; for( int i = 0; i < a_ID.size(); i++ ) { TList[0][i] = a_ID.get( i ); //åå¼å IDã®ã»ãã TList[1][i] = a_Name.get( i ); //ä¼ç¤¾åã®ã»ãã } return TList; } /** ã¡ãã»ã¼ã¸çªå·ã«åããã¦ã¡ãã»ã¼ã¸ãé¸æãï¼æ¨æºå ¥åããã®å ¥åãåãä»ãã * @param mID ã¡ãã»ã¼ã¸çªå·ã表ãåæåå®æ° * @return å ¥åæåå */ String displayMessage( KI_MESS mID ) { String mess = ""; switch( mID ) { case GETID: //人æIDã®åå¾ mess = "人æIDãå ¥åãã¦ãã ããã\n>"; break; default: //ã¨ã©ã¼ } System.out.print( mess ); return getChars(); } }
ãªãªã¸ãã«ã½ã¼ã¹ã§ã¯RepositoryãConsoleãªã©ã®æ½è±¡åãæ½åºããã¦ããªããããåå²ãã«ã¼ããé åãåºæ¬åã®ãããªã¯ããã«ä½æ°´æºã®æ©è½ã使ã£ã¦ãã¸ãã¯ãè¨è¿°ããã¦ãã¾ãããã®ãããããã°ã©ã ã§å®ç¾ããããã¨=å¤é¨ä»æ§ã¨ã®ã¤ãªãããã¯ããã«è¦ãã«ãããªã£ã¦ãã¾ãã両è ãæ¯è¼ãã¦ãããã§ããªãã¸ã§ã¯ãæåã®æ¹ãè¤éã§ä¿å®ã大å¤ã¨æããã¾ããï¼åç´ã«ã³ã¼ããæ°åãã¼ã»ã³ãçããªãã¨ããããããã¬ãã«ã§ã®éãã§ã¯ããã¾ãããã¾ãã«ãæ¡éãã§åç´åãããä¿å®ã³ã¹ããä¸ãã£ã¦ãã¾ããããã«ããªãã¡ã¯ã¿ãªã³ã°ã®çµææ½åºãããRepositoryãConsoleã¨ãã£ãã¯ã©ã¹ã¯ã人æ管çã¨ããåºæã®æ©è½ã«ã¾ã£ããä¾åããªããããå¥ã®ã¢ããªã±ã¼ã·ã§ã³ã§ãåå©ç¨ãã§ããç¹ãè¦éããªãã§ãã ããã
ããªã¢ã¼ãã£ãºã ã®æ´»ç¨ã«ããã¡ã¤ã³ããã°ã©ã ã®åç´åã¨æ¡å¼µæ§ã®åä¸
ãªãªã¸ãã«ã®ã¡ã¤ã³ããã°ã©ã ã§ã¯ã¡ãã¥ã¼ã®é¸æé ç®ã«ãããã£ã¦ããµãæ©è½ã«switchæã§åå²ãã¦ãã¾ãã
ï¼Javaããã°ã©ãã³ã°è½åèªå®è©¦é¨1ç´åé¡ããå¼ç¨ï¼ /** åæ©è½ã®å¼åºãï¼åæ©è½ããå¶å¾¡ãæ»ã£ããï¼ãã¼å ¥åãåãä»ãã * @param contentsNO æ©è½ã³ã¼ãã表ãæ´æ°å¤ * @return çµäºãã©ã°ããçµäºãã®æ©è½ã³ã¼ãã渡ãããã¨ãã®ã¿True */ boolean functionStart( int contentsNO ) { switch( contentsNO ) { case 0: // 人ææ¤ç´¢ SInput sinput = new SInput(); sinput.startThis(); break; case 1: // 人æ管ç(追å ) JInput jinput = new JInput(); jinput.startThis(); break; case 2: // 人æ管ç(æ´æ°) JUpdate jupdate = new JUpdate(); jupdate.startThis(); break; case 3: // 人æ管ç(åé¤) JDelete jdelete = new JDelete(); jdelete.startThis(); break; case 4: // 稼åç¶æ³ç®¡ç(追å ) KInput kinput = new KInput(); kinput.startThis(); break; case 5: // 稼åç¶æ³ç®¡ç(åé¤) KDelete kdelete = new KDelete(); kdelete.startThis(); break; case 6: // çµäº return true; default: // å ¥åã¨ã©ã¼ return false; } if( contentsNO > 0 ) { //人æ管çã¨ç¨¼åç¶æ³ç®¡çã®ã¿ System.out.print( "ã¨ã³ã¿ã¼ãã¼ãæ¼ãã¨ã¡ãã¥ã¼ã«æ»ãã¾ãã\n>" ); getChars(); } return false; }
ãã®è©¦é¨åé¡ç¨åº¦ã®ããã°ã©ã ã§ã¯ããã§ãä½ã¨ãç解ã§ããã¬ãã«ã§ã¯ããã¾ãããä»å¾æ©è½ã追å ãããé½åº¦ããã®switchæãä¿®æ£ãã¦ãããªãã¦ã¯ãªãã¾ãããããã§æ³¨ç®ãã¹ããã¨ã¯ãå¼ã³åºãã®å¯¾è±¡ã¨ãªã£ã¦ããåãµãæ©è½ã®ã¨ã³ããªã¼ãã¤ã³ãã¯å
¨ã¦startThis()ã¨ããå
±éã®ã¡ã½ããã§å§ã¾ã£ã¦ãããã¨ã§ãããã®ãããªå ´åãããç¶æ¿ã¨ããªã¢ã¼ãã£ãºã ã使ãã¹ãæã§ããJavaè¨èªã§ãã¡ã½ãããããªã¢ã¼ãã£ãã¯ã«å¼ã³åºãããã«ã¯ãåã«å
±éã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãåã¯ã©ã¹ã«å®è£
ãããã°ããã ããªã®ã§ããã®å ´åã®ãªãã¡ã¯ã¿ãªã³ã°ã¯ç°¡åã§ãã
å®éã«ãã¡ã¤ã³ã¯ã©ã¹ã®ä¸ã§ãµãæ©è½ãèµ·åãã¦ããé¨åã¯ä»¥ä¸ã®ããã«é常ã«ç°¡åã«ãªãã¾ãã
protected Map<String, Function> functionMap = new HashMap<String, Function>(); /** * æ©è½ã³ã¼ãã«è©²å½ããæ©è½ãå¼ã³åºã */ private void runFunction(String inputCode) { Function subFunction = functionMap.get(inputCode); if (subFunction == null) return; try { subFunction.run(); } catch (Exception ex) { // TODO é©åãªä¾å¤å¦ç ex.printStackTrace(); } }
ããªã¢ã¼ãã£ãºã ï¼å¤æ
æ§ï¼ã¨ããç¨èªãé£ããèãããã®ã§ããã以ä¸ã®ããã«åä¸ã®ã¡ã½ãããå¼ã³åºãå¯è½ãªä»»æã®åã®ãªãã¸ã§ã¯ããå¼ã³åºãå
ã§åºå¥ããä¸æ§ã«æ±ããã¨ãããã¨ã«éãã¾ããããããã®ä¸ããã³ã¼ãã«å¯¾å¿ãããµãæ©è½ãªãã¸ã§ã¯ããåãåºãã¦åã«run()ã¡ã½ãããèµ·åãã¦ãã¾ãããã®ãªãã¸ã§ã¯ããã©ã®æ©è½ã§ããããåºå¥ããå¿
è¦ã¯ã¾ã£ããããã¾ãããããã¯ã¡ããã©çç®±ã®ä¸ã«ãã¼ã«ãã³ãéçãªã©ãã¾ãã¾ãªçè¨ç¨å
·ãå
¥ã£ã¦ããå ´åã«ãããããä»»æã®çè¨ç¨å
·ãåãåºãã¦æåãæ¸ãã¨ãããã¨ã¨åãã§ãããAãã¨ããæåãæ¸ãéã«é常ã¯çè¨ç¨å
·ã®éããããããæèããå¿
è¦ã¯ããã¾ããããããªãã¨ãèãã¦ããããã¤ãã¼ã¼ã«ãªã£ã¦ãã¾ãããã§ãã*1ãã®ããã«ãªãã¸ã§ã¯ãæåè¨èªã§ã¯æ¥å¸¸ç¡æèã«è¡ã£ã¦ããæ½è±¡åãåç´åã®èãæ¹ãåãå
¥ãããã¦ãã¾ãã決ãã¦ãç 究è
ãããã°ã©ãã³ã°ããã¢å°ç¨ã®æ©è½ã¨ããããã§ã¯ããã¾ããã
â»è©¦é¨åé¡ã®ã½ã¼ã¹ãä¸é¨æ²è¼ãã¦ããé¨åã«ã¤ãã¦ã¯ãèä½æ¨©æ³ç¬¬32æ¡ã®å¼ç¨ã«ãããããåæ³ã§ããã¨ç解ãã¦ãã¾ãã引用 - Wikipedia
ï¼ãã®ï¼ã«ã¤ã¥ãäºå®ï¼
*1:å³å¯ã«è¨ãã°ãçå§ãªã©å¾®å¦ãªèª¿æ´ãå¿ è¦ãªãã¨ã¯è¨ãã¾ã§ãããã¾ãããã