オブジェクト指向基本問題。
Q1
財布を表すクラスWallet、自動販売機を表すクラスVendingMachineを作成し、各操作ができるプログラムを作成する。
■一般クラス:Wallet
<フィールド変数(field)>
private int money 現在の財布の中身
<コンストラクタ(constructor)>
public Wallet(int money)
… 財布の中身の初期値を指定してインスタンスを生成
ただし引数moneyの値がマイナスの場合は0円とする
<メソッド(method)>
insertMoney
… 引数に指定された金額を財布に入れる
takeOutMoney
… 引数に指定された金額を財布から取り出す
ただし金額が不足する場合は財布内の全額を取り出す
display
… 現在の財布の中身を表示する
■一般クラス:VendingMachine
<フィールド変数(field)>
private int money 投入された金額
private int num 商品の個数
<コンストラクタ(constructor)>
public VendingMachine(int n)
… 商品の個数を指定してインスタンスを生成
投入金額の初期値は0円とする
<一般メソッド(method)>
insertCoins 機能
… 引数に指定された金額だけ自動販売機に投入する
cancel
… 現在の投入額を釣り銭として返却する
purchase
… 100円の商品を1個購入する
投入金額不足や売切れの場合はその旨を表示する。
insertGoods
… 引数に指定された個数だけ商品を投入する。
■アプリケーションクラス
・中身の初期値が1,000円の財布を1個生成する
・商品の個数が3個の自動販売機を1個生成する
各機能
(1) 硬貨投入 財布から指定金額を取り出して自動販売機に投入する
(2) 商品購入 自動販売機で商品1個を購入して釣り銭を財布に戻す
(3) 硬貨取出 自動販売機に投入した金額を取り出して財布に戻す
(4) 商品投入 自動販売機に指定した個数だけ商品を補充する
(5) 所持金表示 現在の財布の中身を表示する
[実行例]
現在の入金額:0円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>5 現在の財布の中身:1000円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>1 投入する金額を入力して下さい >100 財布から100円取り出しました。 現在の入金額:100円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>5 現在の財布の中身:900円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>2 現在の入金額:0円 財布に0円入れました。 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>5 現在の財布の中身:900円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>1 投入する金額を入力して下さい >150 財布から150円取り出しました。 現在の入金額:150円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>2 現在の入金額:0円 財布に50円入れました。 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>1 投入する金額を入力して下さい >100 財布から100円取り出しました。 現在の入金額:100円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>2 現在の入金額:0円 財布に0円入れました。 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>1 投入する金額を入力して下さい >100 財布から100円取り出しました。 現在の入金額:100円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>2 商品売切れ 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>4 投入する個数を入力して下さい >3 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>2 現在の入金額:0円 財布に0円入れました。 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>5 現在の財布の中身:600円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>1 投入する金額を入力して下さい >50 財布から50円取り出しました。 現在の入金額:50円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>2 投入金額が足りません 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>5 現在の財布の中身:550円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>1 投入する金額を入力して下さい >50 財布から50円取り出しました。 現在の入金額:100円 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>2 現在の入金額:0円 財布に0円入れました。 1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>6 アプリケーションを終了します。
Walletクラス
public class Wallet {
private int money;
public Wallet(int money){
this.money = money > 0 ? money: 0;
}
public void insertMoney(int money){
this.money += money;
System.out.printf("財布に%d円入れました。%n",money);
}
public int takeOutMoney(int money){
int m=this.money > money ? money : this.money;
this.money -= m;
System.out.printf("財布から%d円取り出しました。%n",m);
return m;
}
public void display(){
System.out.printf("現在の財布の中身:%d円%n", this.money);
}
}
VendingMachineクラス
public class VendingMachine {
private int money;
private int num;
public VendingMachine(int n){
this.insertGoods(n);
this.insertCoins(0);
}
public void insertCoins(int money){
this.money += money;
System.out.printf("現在の入金額:%d円%n", this.money);
}
public int cancel(){
int m=this.money;
this.money=0;
System.out.printf("現在の入金額:%d円%n", this.money);
return m;
}
public int purchase(){
if(this.money < 100){
System.out.println("投入金額が足りません");
return -1;
}
if(this.num < 1){
System.out.println("商品売切れ");
return -1;
}
this.money -= 100;
this.num--;
return cancel();
}
public void insertGoods(int num){
this.num+=num;
}
}
アプリケーションクラス
public class VendingMachineApp {
public static void main(String[] args) {
Wallet wallet=new Wallet(1000);
VendingMachine vm=new VendingMachine(3);
Scanner s=new Scanner(System.in);
while(true){
System.out.print("1. 硬貨投入 / 2. 商品購入 / 3. 硬貨取出 / 4.商品投入 / 5.所持金表示 / 6.終了>");
int select=s.nextInt();
switch(select){
case 1:
System.out.print("投入する金額を入力して下さい >");
int money=s.nextInt();
vm.insertCoins(wallet.takeOutMoney(money));
break;
case 2:
int ret=vm.purchase();
if(ret != -1){
wallet.insertMoney(ret);
}
break;
case 3:
ret=vm.cancel();
wallet.insertMoney(ret);
break;
case 4:
System.out.print("投入する個数を入力して下さい >");
int num=s.nextInt();
vm.insertGoods(num);
break;
case 5:
wallet.display();
break;
case 6:
System.out.println("アプリケーションを終了します。");
return;
}
}
}
}

コメント