iT邦幫忙

0

JAVA的ATM操作做不出AccountFile然後也算不出AccountError(使用Apache NetBeans IDE 10.0)

Tzu 2020-09-26 18:58:221187 瀏覽

各位大大好:
事情是這樣的~
我現在有3個CODE在資料夾:

分別如下:
https://ithelp.ithome.com.tw/upload/images/20200926/20119035Bnm7lQtZcf.png
程式碼如下:

 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
/**
 *
 * @author User
 */
class AccountError extends Exception{
    public AccountError(String message){super(message);}
}


class Account implements Serializable{
    private long balance;
    
    public Account(long money){balance=money;}
    
    public void deposite(long money)throws AccountError{
        if(money<0)throw new AccountError("存款金額不可為負值");
        else
            balance+=money;}
        
    public void withdraw (long money)throws AccountError {
        if(money>balance)throw new AccountError("存款不足");
        else
            balance-=money;  
        
    }
    public long ckeckBalance(){return balance;}
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author User
 */
public class WriteAccountObject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] argv) throws IOException{
        // TODO code application logic here
        System.out.print("簡單帳戶模擬計算,");
        System.out.println("開戶需要存多少錢?");
        
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        
        Account myAccount=new Account(Integer.parseInt(br.readLine()));
        
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("AccountFile"));
        
        oos.writeObject(myAccount);
        oos.flush();
        oos.close();
        
         System.out.println("已將帳戶資訊存至檔案 AccountFile!");
    }
    
}


原來我是打算做到這裡先開戶存1000到AccountFile
但是失敗

所以要做的下一個 程式碼:


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author User
 */
public class ReadAccountObject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] argv) throws IOException,ClassNotFoundException{
        // TODO code application logic here
       System.out.println("由檔案讀取帳戶資訊");
       
       ObjectInputStream ois=new  ObjectInputStream(new FileInputStream("AccountFile"));
       Account myAccount=(Account)ois.readObject();
       ois.close();
       
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       try{
           while(true){
               System.out.print("\n您現在要(1)從款(2)提款,");
               int choice=Integer.parseInt(br.readLine());
               System.out.print("請輸入金額,");
               int money=Integer.parseInt(br.readLine());
               
               if(choice==1){
                   myAccount.deposite(money);
                   System.out.print("存了"+money+"元後,帳戶還剩");
                   System.out.println(myAccount.ckeckBalance()+"元");
               }
               else if(choice==2){
                   myAccount.deposite(money);
                   System.out.print("存了"+money+"元後,帳戶還剩");
                   System.out.println(myAccount.ckeckBalance()+"元");
               }
           }
       }
       catch(AccountError e){
           System.out.println(e);
           
       }
    }
    
}


也無法計算出甚麼?

真的怎麼想都想不出要改哪裡~因為程式碼也都沒有反紅??QQ

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
海綿寶寶
iT邦大神 1 級 ‧ 2020-09-26 22:12:55
最佳解答

建議你
拿這裡的Example 2去改成你要的結果比較快

另外,點這裡是我這次鐵人賽唯一的一篇文章,喜歡的話左上角點 Like

最後
如果你還打算繼續問下去的話
請去前一個問題選個最佳解答
做為結案的動作

如果沒打算繼續問的話
就算了

看更多先前的回應...收起先前的回應...
Tzu iT邦新手 1 級 ‧ 2020-09-26 23:52:47 檢舉

我選好了喔/images/emoticon/emoticon25.gif

以防妳不知道
上面那個Example 2是超連結
點下去可以看到一個跟妳要寫的九分類似的程式原始碼
拿來改會比較快

Tzu iT邦新手 1 級 ‧ 2020-09-27 18:11:56 檢舉

其實我這個程式碼基本上能改的應該只有AccountFile的那個地方,後來有用樓上那個例子RUN出來~ 還是很感謝大大

有RUN出來就選個最佳解答結案囉...

2

你要不要先學會看錯誤訊息?

不是很明顯告訴你 readxxxxx.java 23行有問題。
問題的錯誤就是找不到指定檔案。

你上篇找到檔案的問題解決了沒?

看更多先前的回應...收起先前的回應...
wrxue iT邦好手 1 級 ‧ 2020-09-26 21:52:44 檢舉

哈,Debug的捷徑就是看錯誤訊息+GOOGLE錯誤訊息,
偏偏要繞遠路貼上來等人幫忙看、幫忙解答。

Tzu iT邦新手 1 級 ‧ 2020-09-26 22:52:52 檢舉

有阿~ 上一篇解決了~所以我再建一個AccountFile的意思嗎?

那你這篇為何會犯上一篇的錯。
還是只是解決你程式不會出問題。
但你根本不懂怎麼解決。

Tzu iT邦新手 1 級 ‧ 2020-09-27 18:09:50 檢舉

/images/emoticon/emoticon20.gif

我用絕對路徑還是不可以....

但是上面那個怎麼改還是無法.........
用絕對路徑也無法
https://ithelp.ithome.com.tw/upload/images/20200927/20119035YXLN4RDeTh.png

我後來用下面這個例子是成功的~
程式碼1:


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author User
 */
public class Student implements Serializable {
    public Student (String s,short e,short m,short j){
        name=s;
        EScore=e;
        MScore=m;
        JScore=j;
    }
    
    public Student(){}
    
    public String getN(){return name;}
    public Short getE(){return EScore;}
    public Short getM(){return MScore;}
    public Short getJ(){return JScore;}
    
    public double getAvg(){
        return(EScore+MScore+JScore);
    }
    private String name;
    private short EScore;
    private short MScore;
    private short JScore;
    
}````

---

程式碼2:
````import java.io.*;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author User
 */
public class WriteObject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        
        System.out.println("請輸入要建立的學生成績檔檔名");
        System.out.print("➡");
        
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String filename=br.readLine();
        ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream(filename));
        
        String str=new String();
        int counter=0;
        
        do{
            counter++;
            
            System.out.print("請輸入學生姓名:");
            String name=br.readLine();
            
            System.out.print("請輸入英文分數:");
            str=br.readLine();
            short e=Short.parseShort(str);
            
            System.out.print("請輸入數學分數:");
            str=br.readLine();
            short m=Short.parseShort(str);
            
            System.out.print("請輸入Java分數:");
            str=br.readLine();
            short j=Short.parseShort(str);
            
            Student ss=new Student(name,e,m,j);
            
            os.writeObject(ss);
            
            System.out.print("還要輸入另外一筆資料嗎(y/n)");
            
            str=br.readLine();
            
        }while(str.equalsIgnoreCase("Y"));
        
        os.flush();
        os.close();
        
        System.out.println("\n已寫入"+counter+"筆學生資料至檔案"+filename);
        
    }
    
}



ObjectStream File  FileReader/FileWriter

程式碼3:


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author User
 */
public class ReadObject {
    public static void main(String[] args) throws IOException,ClassNotFoundException {
        // TODO code application logic here
        
        System.out.println("要讀取的學生成績檔檔名");
        System.out.print("➡");
        
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String filename=br.readLine();
        
        int counter=0;
        double Esum=0;
        double Msum=0;
        double Jsum=0;
        Student ss=new Student();
        
        System.out.println("姓名\t英文\t數學\tJava\t平均");
        System.out.println("-------------------------------------");
        
        try(ObjectInputStream ois=
                new ObjectInputStream(new FileInputStream(filename))){
         while(true){
                ss=(Student)ois.readObject();
                counter++;
                
                Esum +=ss.getE();
                Msum +=ss.getM();
                Jsum +=ss.getJ();
               System.out.println(ss.getN()+'\t'+ss.getE()+'\t'
                       +ss.getM()+'\t'+ss.getJ()+'\t'
                       +ss.getAvg()); 
                
            }
        }
      catch(EOFException e){
          System.out.println("\n已從檔案"+filename+"讀取"+counter+"筆學生資料");
          System.out.println("\n全員英文平均"+(Esum/counter));
          System.out.println("\n全員數學平均"+(Msum/counter));
          System.out.println("\n全員JAVA平均"+(Jsum/counter));
      }  
}
}

我要發表回答

立即登入回答