import java.applet.*; import java.lang.*; import java.util.*; import java.awt.*; // 通貨クラス class Money extends Object { public String name; public String country; public double rate; public Money(String n, String c, double r) { name = n; country = c; rate = r; return; } public Money() { name = ""; country = ""; rate = 0; return; } } // 換金クラス public class Exchange extends Applet { int size; Font font; Hashtable tab; Label inlabel, buylabel, outlabel; Label inname, buyname, outname; TextField incountry, buycountry, outcountry; TextField innum, buynum, outnum; TextField message; Money inmoney, buymoney, outmoney; List idlist, idlist2, idlist3; Button calc; Panel in, buy, out, bottom; // 初期化処理 public void init() { size = Integer.parseInt(getParameter("size")); String str = getParameter("data"); StringTokenizer strtok = new StringTokenizer(str, ",", false); // ハッシュテーブル作成 tab = new Hashtable; idlist = new List(3, false); idlist2 = new List(3, false); idlist3 = new List(3, false); for (int i = 0; i < size; i ++){ String sid = strtok.nextToken(); String name = strtok.nextToken(); String country = strtok.nextToken(); String srate = strtok.nextToken(); double rate; try { rate = Double.valueOf(srate).doubleValue(); } catch (NumberFormatException e){ rate = 0; } tab.put(sid, new Money(name, country, rate)); idlist.addItem(sid+":"+country); idlist2.addItem(sid+":"+country); idlist3.addItem(sid+":"+country); } //tab.rehash(); // GUI 部品配置 //setBackground(Color.black); //setForeground(Color.white); font = new Font("TimesRoman", Font.BOLD, 16); inlabel = new Label("Your money"); inlabel.setFont(font); inname = new Label(" "); inname.setFont(font); incountry = new TextField(20); incountry.setEditable(false); incountry.setFont(font); innum = new TextField(10); innum.setEditable(true); innum.setFont(font); buylabel = new Label("A price"); buylabel.setFont(font); buyname = new Label(" "); buyname.setFont(font); buycountry = new TextField(20); buycountry.setEditable(false); buycountry.setFont(font); buynum = new TextField(10); buynum.setEditable(true); buynum.setFont(font); outlabel = new Label("Change"); outlabel.setFont(font); outname = new Label(" "); outname.setFont(font); outcountry = new TextField(20); outcountry.setEditable(false); outcountry.setFont(font); outnum = new TextField(10); outnum.setEditable(false); outnum.setFont(font); in = new Panel(); //in.setLayout(new GridLayout(1, 3)); in.add(idlist); //in.add(incountry); in.add(innum); in.add(inname); buy = new Panel(); buy.add(idlist2); //buy.add(buycountry); buy.add(buynum); buy.add(buyname); out = new Panel(); out.add(idlist3); //out.add(outcountry); out.add(outnum); out.add(outname); message = new TextField(30); message.setEditable(false); message.setFont(font); calc = new Button("Calc"); calc.setFont(new Font("TimesRoman", Font.BOLD, 24)); bottom = new Panel(); bottom.add(message); bottom.add(calc); setLayout(new GridLayout(7, 1)); add(inlabel); add(in); add(buylabel); add(buy); add(outlabel); add(out); add(bottom); } // イベント処理 public boolean action(Event evt, Object what) { if (evt.target instanceof List){ calculate(); } else if (evt.target instanceof Button){ if (((String)what).equals("Calc")) { calculate(); } } return true; } // 数値計算処理 protected void calculate() { String item = idlist.getSelectedItem(); if (item == null){ inname.setText("Please Select"); inmoney = null; } else { inmoney = (Money)tab.get(item.substring(0, 3)); inname.setText(inmoney.name); } item = idlist2.getSelectedItem(); if (item == null){ buyname.setText("Please Select"); buymoney = null; } else { buymoney = (Money)tab.get(item.substring(0, 3)); buyname.setText(buymoney.name); } item = idlist3.getSelectedItem(); if (item == null){ outname.setText("Please Select"); outmoney = null; } else { outmoney = (Money)tab.get(item.substring(0, 3)); outname.setText(outmoney.name); } if (inmoney == null || buymoney == null || outmoney == null){ outnum.setText(""); message.setText(""); return; } double nin, nbuy, nout; try { nin = Double.valueOf(innum.getText()).doubleValue(); } catch (NumberFormatException e) {nin = 0;} try { nbuy = Double.valueOf(buynum.getText()).doubleValue(); } catch (NumberFormatException e) {nbuy = 0;} nout = (nin / inmoney.rate) - (nbuy / buymoney.rate); outnum.setText(String.valueOf(nout * outmoney.rate)); if (nout < 0){ message.setText("Too much shopping!!"); } else if (nout < 5){ message.setText("Nice shopping."); } else { message.setText("You can buy something more."); } return; } }