2013-05-05 32 views
-1

我有一個類可以測試投入自動售貨機的金額。初始值存儲在一個txt文件中。我正在嘗試使用printWriter獲取用戶輸入並將其存儲在txt文件中。例如,硬幣的起始量是5.用戶放入2枚硬幣並在文本文件中的新的數量是7。在類中的對象上使用PrintWriter

這是所有Money.txt中有:

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

這是我的錢類: 公共類貨幣

{ 
    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!"); 
    } 


    } 
+0

問題是什麼? – 2013-05-05 00:05:17

+0

@MattBall當我運行它並添加錢時,這並不反映在money.txt上。我試圖讓它讀取,存儲和更新可用硬幣的數量。 – 2013-05-05 00:07:33

+0

在您的代碼中沒有對'PrintWritter'(或其他相關類)的引用...... – SJuan76 2013-05-05 00:10:26

回答

1

1)我剛剛測試了你的閱讀文件邏輯,它不起作用。在某些時候,你將字符串視爲int。所以它失敗了。見我如何從文件嚴格值:

 File moneyFile = null; 

     Scanner moneyScan = null; 

     int nickels = 0; 
     int dimes = 0; 
     int quarters = 0; 
     int halfdollars = 0;   

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

     try { 
      moneyScan = new Scanner(moneyFile); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 


     String[] tempAr = new String[2]; 
     String temp = ""; 

     for(int i = 0; i<4; i++) 
     { 
      temp = moneyScan.nextLine(); 
      tempAr = temp.split("//"); 

      //match nickels 
      if(tempAr[1].matches("nickels")) 
      { 
       nickels = Integer.parseInt(tempAr[0].trim()); 
       System.out.println(nickels); 
      } 

      //match dimes 
      if(tempAr[1].matches("dimes")) 
      { 
       dimes = Integer.parseInt(tempAr[0].trim()); 
       System.out.println(dimes); 
      } 


      //match quarters 
      if(tempAr[1].matches("quarters")) 
      { 
       quarters = Integer.parseInt(tempAr[0].trim()); 
       System.out.println(quarters); 
      } 

      //match halfdollars 
      if(tempAr[1].matches("halfdollars")) 
      { 
       halfdollars = Integer.parseInt(tempAr[0].trim()); 
       System.out.println(halfdollars); 
      } 
     } 

2)當你寫你的文件,它保持空白,因爲你不沖水和關閉作家流。在不關閉流的情況下,它不會將字節刷新到文件,並且您的文件將保持空白。

即:mw.close();

+0

無論何時調用printLn,AFAIK PrintWriter都會自動刷新。關閉是一種形式(我認爲),而不是強制性的。這也許是閱讀我能想到的一個小文本文件的最複雜的方式--OP說他只是在學習,沒有理由讓他學習如何進行分裂和匹配。 – Singular1ty 2013-05-05 01:08:44

+0

@ Singular1ty,不,你錯了。它不是。關閉流是必要的。請Google。 – 2013-05-05 01:09:48

+0

@ Singular1ty,除非他關閉流,否則他將永遠無法寫入文件。關閉有兩件事,刷新和關閉流。 – 2013-05-05 01:11:03

1

好吧,我認爲你的主要問題是,你實際上並沒有告訴PrintWriter打印任何東西。

你有mw.moneyObj.increQuarters();和類似的功能,但他們不會自己做任何事情。

moneyObj.increQuarters();很好,但是你需要在別處調用一個單獨的打印函數。

例如,你可能想:

void printAll(){ 
    mw.println(moneyObj.getQuarters()); 
    //Etc for all other printing. 

} 

當用戶完成與自動售貨機打轉轉,讓他們保存結果(或攔截在JFrame的關閉功能)。

然後致電printAll()並按照您希望數字顯示的順序打印到Money.txt

編輯 由於拉維以下提到,你需要調用.close()printAll()方法的末尾,以確保PrintWriter的將寫入該文件正確。

+0

因此添加了一個printAll方法,並在我的驅動程序文件中調用它。出於某種原因,當我運行一切時,我的money.txt變成空白。我究竟做錯了什麼? – 2013-05-05 00:47:31

+0

@TyGivens如果你正在做的是PrintLining,它不應該覆蓋文件。確保你只打開一次文件。當PrintWriters和PrintStreams打開文件時,它們傾向於刪除內容。讓我看看我的一些舊代碼,因爲這個問題似乎很熟悉。 – Singular1ty 2013-05-05 01:03:32

+0

謝謝!你的幫助意味着很多。 – 2013-05-05 01:05:29