ユーザー名:パスワード:Windowsでの実行ユーザー名
maruno::Administrator suzuki::Administrator yamada::Administrator sato::Administrator
ホスト: CVSNTを起動しているサーバーのIPアドレス(ホスト名) リポジトリー・パス: c:\cvsrep ユーザー: passwdファイルに設定したユーザー名 パスワード: パスワード 接続型: pserver デフォルト・ポートの使用: チェック 終了時の接続の妥当性検査: チェック
- /*
- * 作成日: 2004/11/08
- *
- * TODO この生成されたファイルのテンプレートを変更するには次へジャンプ:
- * ウィンドウ - 設定 - Java - コード・スタイル - コード・テンプレート
- */
- /**
- * @author administrator
- *
- * TODO この生成された型コメントのテンプレートを変更するには次へジャンプ:
- * ウィンドウ - 設定 - Java - コード・スタイル - コード・テンプレート
- */
- public class HelloMaruno {
- public static void main(String[] args) {
- System.out.println("Hello Maruno!");
- }
- }
Hello Maruno!
"AbstractProduct.java" | 抽象商品クラス | 変更なし |
"Product.java" | 商品クラス | 変更なし |
"DiscountInterface.java" | 割引商品インターフェース | 変更なし |
"DiscountProduct.java" | 割引商品クラス | 変更なし |
"SetProduct.java" | セット商品クラス | サンプル解答ソース |
"AdminProduct.java" | 商品管理クラス | 変更なし |
"ProductServer.java" | 商品管理サーバー・クラス | サンプル解答ソース |
"GUIProduct.java" | GUIテスト・クラス | サンプル解答ソース |
"TestProduct.java" | CUIテスト・クラス | サンプル解答ソース |
"priducts.txt" | テスト・データ | 変更なし |
- // 1903 = 1401
- // package product;
- // 抽象商品クラスです。
- // 商品を扱うクラスが持っていなければならないメソッドを提供しています。
- public abstract class AbstractProduct implements Comparable {
- private String code; // 商品コード
- private String name; // 商品名
- private int price; // 商品定価
- // 商品オブジェクトのハッシュ・コードを返します。
- public int hashCode() {
- return code.hashCode();
- }
- // 商品オブジェクトを文字列化します。
- public String toString() {
- return "[" + getCode() + ", " + getName() + ", " + getPrice() + "]";
- }
- // 商品オブジェクトが等しいか否かを判定します。
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj instanceof Product) {
- if (code.equals(((Product)obj).getCode())) {
- return true;
- }
- }
- return false;
- }
- // 商品オブジェクトの大小関係を判定します。
- public int compareTo(Object obj) {
- return this.getCode().compareTo(((AbstractProduct)obj).getCode());
- }
- // 商品コードに値を設定します。
- public void setCode(String code) {
- this.code = code;
- }
- // 商品コードの値を取得します。
- public String getCode() {
- return this.code;
- }
- // 商品名に値を設定します。
- public void setName(String name) {
- this.name = name;
- }
- // 商品名の値を取得します。
- public String getName() {
- return this.name;
- }
- // 商品定価に値を設定します。
- public void setPrice(int price) {
- if (price < 0) {
- this.price = 0;
- return;
- }
- this.price = price;
- }
- // 商品定価に値を設定します。
- public void setPrice(String price) {
- try {
- setPrice(Integer.parseInt(price));
- } catch (NumberFormatException e) {
- setPrice(0);
- e.printStackTrace();
- } catch (Exception e) {
- setPrice(0);
- e.printStackTrace();
- }
- }
- // 商品定価の値を取得します。
- public int getPrice() {
- return this.price;
- }
- }
- // 1903 = 1404
- // 商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- // package product;
- public class Product extends AbstractProduct {
- // 商品クラスのコンストラクター。
- public Product(String code, String name, int price) {
- setCode(code);
- setName(name);
- setPrice(price);
- }
- // 商品クラスのコンストラクター。
- public Product(String code, String name, String price) {
- setCode(code);
- setName(name);
- setPrice(price);
- }
- }
- // 1903 = 1106
- // package product;
- // 割引インターフェースです。
- // 割引される商品情報が持っていなければならないメソッドの実装を
- // 強制しています。
- interface DiscountInterface {
- // 商品割引率の値を設定します。
- void setDiscount(int discount);
- // 商品割引率の値を取得します。
- int getDiscount();
- }
- // 1903 = 1401
- // package product;
- // 割引商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class DiscountProduct extends Product implements DiscountInterface {
- private int discount; // 商品割引率
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, int price) {
- this(code, name, price, 0);
- }
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, String price) {
- this(code, name, price, "0");
- }
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, int price, int discount) {
- super(code, name, price);
- setDiscount(discount);
- }
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, String price, String discount) {
- super(code, name, price);
- setDiscount(discount);
- }
- // 商品オブジェクトを文字列化します。
- public String toString() {
- return "[" + getCode() + ", " + getName() + ", " + getPrice() + ", " + discount + "%]";
- }
- // 商品割引率に値を設定します。
- public void setDiscount(int discount) {
- if (discount < 0 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- public void setDiscount(String discount) {
- setDiscount(Integer.parseInt(discount));
- }
- // 商品割引率の値を取得します。
- public int getDiscount() {
- return this.discount;
- }
- // 商品の売値の値を取得します。
- public int getPrice() {
- return (int)(super.getPrice() * (1 - discount / 100.0));
- }
- }
- // 1903 = 1702
- // package product;
- import java.util.*;
- // セット商品クラスです。
- // ひとつまたは複数の商品を集めて、割引したものをセット商品として扱います。
- // セット商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class SetProduct extends AbstractProduct implements DiscountInterface {
- private int discount; // セット商品割引率
- private Map products; // セット商品リスト
- // セット商品クラスのコンストラクター。
- public SetProduct(String code, String name, int discount, Map products) {
- setCode(code);
- setName(name);
- setDiscount(discount);
- setProducts(products);
- }
- // セット商品オブジェクトを文字列化します。
- public synchronized String toString() {
- return "[" + getCode() + ", " + getName() + ", " + getDiscount() + ", " + getPrice() + "]";
- }
- // 商品の売値の値を取得します。
- public synchronized int getPrice() {
- int price = 0;
- Iterator itr = products.values().iterator();
- while (itr.hasNext()) {
- price += ((Product)itr.next()).getPrice();
- }
- return (int)(price * (1 - discount / 100.0));
- }
- // 商品割引率に値を設定します。
- public synchronized void setDiscount(int discount) {
- if (discount < 10 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- public synchronized void setDiscount(String discount) {
- try {
- setDiscount(Integer.parseInt(discount));
- } catch (NumberFormatException e) {
- setDiscount(0);
- e.printStackTrace();
- } catch (Exception e) {
- setDiscount(0);
- e.printStackTrace();
- }
- }
- // 商品割引率の値を取得します。
- public synchronized int getDiscount() {
- return discount;
- }
- // セット商品に値を設定します。
- public synchronized void setProducts(Map products) {
- this.products = products;
- }
- // セット商品を取得します。
- public synchronized Map getProducts() {
- return products;
- }
- // 商品を追加します。
- public synchronized void add(Product product) {
- products.put(product.getCode(), product);
- }
- // 商品を更新します。
- public synchronized void update(Product product) {
- products.put(product.getCode(), product);
- }
- // 商品を削除します。
- public synchronized void remove(String code) {
- products.remove(code);
- }
- // 商品を照会します。
- public synchronized String query(String code) {
- if (!products.containsKey(code)) {
- return "[Not Found]";
- }
- return ((Product)(products.get(code))).toString();
- }
- }
- // 1903 = 1616
- // package server;
- // import product.*;
- import java.util.*;
- import java.io.*;
- // セット商品クラスをファイルによって管理するクラスです。
- public class AdminProduct {
- // 標準入力から商品データを読んで、セット商品クラスに商品を設定します。
- public void read(SetProduct sp) {
- BufferedReader br = null;
- try {
- br = new BufferedReader(new InputStreamReader(System.in));
- String s = null;
- while ((s = br.readLine()) != null) {
- String[] sa = s.split("\t");
- if (sa.length >= 2 && sa[0].toUpperCase().equals("D")) {
- sp.remove(sa[1]);
- } else if (sa.length >= 4 && sa[0].toUpperCase().equals("A")) {
- sp.add(new Product(sa[1], sa[2], sa[3]));
- } else if (sa.length >= 4 && sa[0].toUpperCase().equals("U")) {
- sp.update(new Product(sa[1], sa[2], sa[3]));
- } else {
- continue;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- // 標準出力にセット商品データの商品を出力します。
- public void write(SetProduct sp) {
- Map p = (Map)sp.getProducts();
- Product product = null;
- Iterator itr = p.values().iterator();
- while (itr.hasNext()) {
- product = (Product)itr.next();
- System.out.print(product.getCode() + "\t" + product.getName() + "\t" + product.getPrice());
- System.out.println();
- }
- }
- // ファイルから商品データを読んで、セット商品クラスに商品を設定します。
- public void read(SetProduct sp, String fileName) {
- BufferedReader br = null;
- try {
- br = new BufferedReader(new FileReader(fileName));
- String s = null;
- while ((s = br.readLine()) != null) {
- String[] sa = s.split("\t");
- if (sa.length >= 2 && sa[0].toUpperCase().equals("D")) {
- sp.remove(sa[1]);
- } else if (sa.length >= 4 && sa[0].toUpperCase().equals("A")) {
- sp.add(new Product(sa[1], sa[2], sa[3]));
- } else if (sa.length >= 4 && sa[0].toUpperCase().equals("U")) {
- sp.update(new Product(sa[1], sa[2], sa[3]));
- } else {
- continue;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- // ファイルにセット商品データの商品を設定します。
- public void write(SetProduct sp, String fileName) {
- Map p = (Map)sp.getProducts();
- BufferedWriter bw = null;
- try {
- bw = new BufferedWriter(new FileWriter(fileName));
- Product product = null;
- Iterator itr = p.values().iterator();
- while (itr.hasNext()) {
- product = (Product)itr.next();
- bw.write(product.getCode() + "\t" + product.getName() + "\t" + product.getPrice());
- bw.newLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- bw.flush();
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- // 1903 = 1701
- // package server;
- // import product.*;
- import java.util.*;
- import java.net.*;
- import java.io.*;
- // 商品サーバーです。
- public class ProductServer {
- public static void main(String[] args) throws Exception {
- final int SERVER_PORT = 54321;
- TreeMap p = new TreeMap();
- SetProduct s = new SetProduct("SA01", "パソコン・セット", 10, p);
- System.out.println("セット商品のインスタンスを生成しました。 s = " + s);
- System.out.println();
- AdminProduct ap = new AdminProduct();
- ap.read(s, args[0]);
- System.out.println("セット商品のデータを設定しました。 s = " + s);
- // サーバ用ソケットの作成
- ServerSocket serversock = new ServerSocket(SERVER_PORT);
- // 非同期制御データ・オブジェクトの作成
- while (true) {
- // クライアントからの接続要求待ち
- Socket sock = serversock.accept();
- new ProductServerThread(sock, s).start();
- }
- }
- }
- //クライアントと対応するサーバーのスレッドです。
- public class ProductServerThread extends Thread {
- private Socket sock;
- private SetProduct s;
- ProductServerThread(Socket sock, SetProduct s) {
- this.sock = sock;
- this.s = s;
- }
- public void run() {
- System.out.println("socket = " + sock);
- DataInputStream dis = null;
- DataOutputStream dos = null;
- try {
- // クライアントと対話処理するストリーム、ソケットを生成
- dis = new DataInputStream(sock.getInputStream());
- dos = new DataOutputStream(sock.getOutputStream());
- // 商品コードを受け取って、対応する商品を返す
- String code = dis.readUTF();
- String result = s.query(code);
- System.out.println(" [" + code + " = " + result + "]");
- dos.writeUTF(result);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- // ストリーム、ソケットをクローズ
- dos.close();
- dis.close();
- sock.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- // 1903
- // package application;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- // 商品システムGUIクライアント・アプリケーションです。
- public class GUIProduct {
- private static final int SERVER_PORT = 54321;
- private static String serverName = null;
- private static Socket sock = null;
- private Frame frame;
- private Panel northPanel, westPanel, centerPanel, eastPanel, southPanel;
- private Label codeLabel, nameLabel, priceLabel, discountLabel, messageLabel;
- private TextField codeField, nameField, priceField, discountField;
- private TextArea messageArea;
- private Button searchButton, updateButton, deleteButton, resetButton, exitButton;
- private String code;
- private String name;
- private int price;
- private String message;
- public GUIProduct() {
- frame = new Frame("商品情報");
- northPanel = new Panel();
- westPanel = new Panel();
- centerPanel = new Panel();
- eastPanel = new Panel();
- southPanel = new Panel();
- westPanel.setLayout(new GridLayout(0, 1));
- centerPanel.setLayout(new GridLayout(0, 1));
- searchButton = new Button("検索");
- searchButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("clecked search button");
- System.out.println(" product code: " + codeField.getText());
- messageArea.setText("clecked search button"
- + System.getProperty("line.separator")
- + " product code: " + codeField.getText()
- + System.getProperty("line.separator")
- );
- query(codeField.getText());
- }
- } );
- northPanel.add(searchButton);
- updateButton = new Button("新規/更新");
- updateButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println(" product code: " + codeField.getText());
- System.out.println(" product name: " + nameField.getText());
- System.out.println(" price: " + Integer.parseInt(priceField.getText()));
- System.out.println(" message: " + messageArea.getText());
- System.out.println();
- }
- } );
- northPanel.add(updateButton);
- deleteButton = new Button("削除");
- deleteButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("clecked delete button");
- System.out.println(" product code: " + codeField.getText());
- System.out.println();
- }
- } );
- northPanel.add(deleteButton);
- resetButton = new Button("取消");
- resetButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- codeField.setText("");
- nameField.setText("");
- priceField.setText("");
- messageArea.setText("");
- System.out.println("clecked reset button");
- System.out.println();
- }
- } );
- northPanel.add(resetButton);
- exitButton = new Button("終了");
- exitButton.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- }
- );
- northPanel.add(exitButton);
- codeLabel = new Label("商品コード:");
- codeField = new TextField(8);
- westPanel.add(codeLabel);
- centerPanel.add(codeField);
- nameLabel = new Label("商品名:");
- nameField = new TextField(30);
- westPanel.add(nameLabel);
- centerPanel.add(nameField);
- priceLabel = new Label("定価:");
- priceField = new TextField(8);
- westPanel.add(priceLabel);
- centerPanel.add(priceField);
- discountLabel = new Label("割引率:");
- discountField = new TextField(8);
- westPanel.add(discountLabel);
- centerPanel.add(discountField);
- messageLabel = new Label("メッセージ:");
- messageArea = new TextArea();
- southPanel.add(messageArea);
- frame.add(northPanel, BorderLayout.NORTH);
- frame.add(westPanel, BorderLayout.WEST);
- frame.add(centerPanel, BorderLayout.CENTER);
- frame.add(eastPanel, BorderLayout.EAST);
- frame.add(southPanel, BorderLayout.SOUTH);
- frame.setSize(480, 320);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- serverName = args[0];
- GUIProduct gp = new GUIProduct();
- }
- void query(String code) {
- try {
- // クライアントと対話処理するストリーム、ソケットを生成
- sock = new Socket(serverName, SERVER_PORT);
- DataInputStream dis =
- new DataInputStream(sock.getInputStream());
- DataOutputStream dos =
- new DataOutputStream(sock.getOutputStream());
- dos.writeUTF(code);
- String str = dis.readUTF();
- System.out.println("商品コード " + code + " の商品は " + str + " です。");
- if (str.equals("[Not Found]")) {
- return;
- }
- String prods[] = str.substring(1, str.length()-1).split(",");
- nameField.setText(prods[1].trim());
- priceField.setText(prods[2].trim());
- // ストリーム、ソケットをクローズ
- dos.close();
- dis.close();
- sock.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- sock.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- // 1903 = 1702
- // package application;
- import java.io.*;
- import java.net.*;
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) throws Exception {
- final int SERVER_PORT = 54321;
- final String serverName = args[0];
- Socket sock = null;
- BufferedReader br = null;
- try {
- br = new BufferedReader(new InputStreamReader(System.in));
- String str = null;
- while ((str = br.readLine()) != null && !str.toUpperCase().equals("X")) {
- // クライアントと対話処理するストリーム、ソケットを生成
- sock = new Socket(serverName, SERVER_PORT);
- DataInputStream dis =
- new DataInputStream(sock.getInputStream());
- DataOutputStream dos =
- new DataOutputStream(sock.getOutputStream());
- dos.writeUTF(str);
- System.out.println("商品コード " + str + " の商品は " + dis.readUTF() + " です。");
- // ストリーム、ソケットをクローズ
- dos.close();
- dis.close();
- sock.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- br.close();
- sock.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
□実行結果 (サーバー)
- A A001 パソコン 188000
- A P001 プリンター 24000
- A C001 CD-ROM 6500
- A M001 USBメモリー 9800
- A H001 ハードディスク 9200
- U C001 DVD-ROM 9200
- D M001
$ java ProductServer products.txt セット商品のインスタンスを生成しました。 s = [SA01, パソコン・セット, 10, 0] セット商品のデータを設定しました。 s = [SA01, パソコン・セット, 10, 207360] socket = Socket[addr=/127.0.0.1,port=4104,localport=54321] [A001 = [A001, パソコン, 188000]] socket = Socket[addr=/127.0.0.1,port=4105,localport=54321] [B001 = [Not Found]] socket = Socket[addr=/127.0.0.1,port=4106,localport=54321] [C001 = [C001, DVD-ROM, 9200]]□実行結果 (CUI クライアント)
$ java TestProduct localhost A001 商品コード A001 の商品は [A001, パソコン, 188000] です。 B001 商品コード B001 の商品は [Not Found] です。 C001 商品コード C001 の商品は [C001, DVD-ROM, 9200] です。□実行結果 (GUI クライアント)
$ java GUIProduct localhost