-1

自動販売機に投入された金額をテストするクラスがあります。初期値は txt ファイルに保存されます。printWriter を使用してユーザー入力を取得し、txt ファイルに保存しようとしています。たとえば、1 セント硬貨の開始額は 5 です。ユーザーが 2 セント硬貨を入れると、テキスト ファイルの新しい額は 7 になります。

Money.txt に含まれる内容は次のとおりです。

5 //nickels
5 //dimes
5 //quarters
0 //halfdollars

これは私のお金のクラスです: public class Money

{
    private int nickels;
    private int quarters;
    private int dimes;
    private int halfs;


    public void increNickels() {
        nickels ++;
    }

    public int getNickels(){
        return nickels;
    }

    public void increQuarters(){
        quarters ++;
    }

    public int getQuarters(){
        return quarters;
    }

    public void increDimes(){
        dimes ++;
    }

    public int getDimes(){
        return dimes;
    }

    public void increHalfs(){
        halfs ++;
    }

    public int getHalfs(){
        return halfs;
    }
}

これは私の自動販売クラスです:

    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import java.util.ArrayList;

    /**
     *
     * @author Mira and Ty
     */
    public class VendingClass {

            Money moneyObj;
            public Item [] itemList;
            public Money [] moneyList;
            Integer noCode = null;

            int itemChoiceNum;

        public VendingClass(){

            moneyObj = new Money();
            itemList = new Item [4];
            moneyList = new Money [4];
        }
        public void setItemList() throws IOException {

        String welcome = "Welcome to the \n \t\tPolemon Distribution Center \n \t\t\tPlease note that if there is not enough \nmoney in "
                    + "machine correct change \nwill not be given."; // creates welcome message

            JOptionPane.showMessageDialog(null, welcome);
            int itemQuantity; //variable that will display item code

            String items; //creates the String variable items which elements from itemNames array will be stored into
            int itemCost; //creates the integer variable itemCost whic elements from itemCosts array will be stored into

            File itemFile = new File("items.txt");
            Scanner itemScan = new Scanner(itemFile);

            int code = -1;
            for(int x = 0; x < itemList.length; x++){      //for loop addes the elements of each array and itemCode to itemList object
                items = itemScan.nextLine();
                itemCost = itemScan.nextInt();
                itemQuantity = itemScan.nextInt();
                itemScan.nextLine();
                code++;
                itemList[x] = new Item(code, items, itemCost, itemQuantity);
            }
            itemScan.close();
        }

    public void setMoney() throws IOException {

            File moneyFile = new File("Money.txt");
            Scanner moneyScan = new Scanner(moneyFile);

             for(int x = 0; x < moneyList.length; x++){
                int nickels = moneyScan.nextInt();
                int dimes = moneyScan.nextInt();
                int quarters = moneyScan.nextInt();
                int halfs = moneyScan.nextInt();
            }

            moneyScan.close();
    }



        public void displayVend(){

            String itemChoice = JOptionPane.showInputDialog(null, itemList);
                itemChoiceNum = Integer.parseInt(itemChoice);

            if(itemChoice == null){

                JOptionPane.showMessageDialog(null, "Thank you for your service. Goodbye!");
            }

                  JOptionPane.showMessageDialog(null, "You've caught a " + itemList[itemChoiceNum].getItem());


          }

        public void insertMoney() throws IOException{

            int moneyNeeded = itemList[itemChoiceNum].getCost();//price of item will be decremented

            while(moneyNeeded > 0){ //while loop testing the amount of money added to vending machine
            String message = JOptionPane.showInputDialog(null, "Pokemon: " + itemList[itemChoiceNum].getItem() + "\n You owe" + moneyNeeded + "\nEnter 50, 25, 10, or 5");

PrintWriter mw = new PrintWriter("Money.txt");            
int userMoney = Integer.parseInt(message);
            moneyNeeded = moneyNeeded - userMoney;

            if(userMoney == 5){
                mw.moneyObj.increNickels();
            }

            if(userMoney == 10){
                mw.moneyObj.increDimes();
            }

            if(userMoney == 25){
                mw.moneyObj.increQuarters();
            }

            if(userMoney == 50){
                mw.moneyObj.increHalfs();
            }

            if(message == null){
                JOptionPane.showMessageDialog(null, "Thank you for your service. Goodbye!");
                System.exit(0);
            }


           }
            JOptionPane.showMessageDialog(null, "Thank you for coming to the Pokemon Distribution Center! \nWe hope you and your " + itemList[itemChoiceNum].getItem() + " catch them all!");
    }


    }
4

2 に答える 2