[技術討論] java學藝未精,求指教

本帖最後由 henrietta 於 2015-1-31 11:14 編輯

今日我個老細又加左野比我e個打雜IT狗做,叫我去賣飯飛
為左準確計數(實質玩野),我寫鬼左個java program同佢慢慢計
可是我想按save把資料做落一個txt檔
但學藝未精,求指教(未學string如何處理)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class program extends JFrame implements ActionListener{
    private int couponM;
     public void setCouponM(int aCouponM){
        this.couponM=aCouponM;
    }
    public int getCouponM(){
        return couponM;
    }
    char[] toSave={};
    private static final int EOF=-1;
    private JButton calculate=new JButton("Calculate");
    private JButton reset=new JButton("Reset");
    private JButton save=new JButton("Save");
    private JTextField giveMoney=new JTextField(10);
    private JTextField coupon=new JTextField(5);
    private JLabel Money=new JLabel("Pay:");
    private JLabel couponl=new JLabel("Total Coupon:");
    private JLabel totalMoney=new JLabel("Total Price:");
    private JLabel exchange=new JLabel("Change:");
    private JTextField couponFrom=new JTextField(5);
    private JTextField couponTo=new JTextField(5);
    private JTextField name=new JTextField(20);
    private JLabel namel=new JLabel("Name:");
    private JLabel froml=new JLabel("No. From");
    private JLabel Tol=new JLabel("No. To");
    public program(){
        super("Meal Coupon");
    JPanel labelPanel = new JPanel();
    labelPanel.setLayout(new GridLayout(3,2));
    labelPanel.add(Money);
    labelPanel.add(giveMoney);
    labelPanel.add(couponl);
    labelPanel.add(coupon);
    labelPanel.add(totalMoney);
    labelPanel.add(exchange);
    JPanel labelPanel2 = new JPanel();
    labelPanel2.setLayout(new GridLayout(3,2));
    labelPanel2.add(froml);
    labelPanel2.add(Tol);
    labelPanel2.add(couponFrom);
    labelPanel2.add(couponTo);   
    labelPanel2.add(calculate);
    labelPanel2.add(reset);
    JPanel p3=new JPanel();
    p3.setLayout(new GridLayout(1,2));
    p3.add(namel);
    p3.add(name);
    add(p3,BorderLayout.NORTH);
    add(labelPanel2,BorderLayout.CENTER);
    add(save,BorderLayout.EAST);
    add(labelPanel,BorderLayout.WEST);   
    calculate.addActionListener(this);
    reset.addActionListener(this);
    save.addActionListener(this);   
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    }
    public void actionPerformed(ActionEvent e){
        int total;
        Object source=e.getSource();
        if(calculate==source){
            String agiveMoney=giveMoney.getText();
            int bgiveMoney=Integer.parseInt(agiveMoney);
            String acoupon=coupon.getText();
            int bcoupon=Integer.parseInt(acoupon);
            totalMoney.setText("Total Money:"+bcoupon*couponM+"");
            int exchangea=bgiveMoney-bcoupon*couponM;
            if(exchangea<0){
                JOptionPane.showMessageDialog(null,"Not enough money!!");
            }
            exchange.setText("Change:"+(exchangea)+"");
        }
        else if(reset==source){
            giveMoney.setText("");
            coupon.setText("");
            couponFrom.setText("");
            couponTo.setText("");
            name.setText("");
            exchange.setText("Exchange:");
            totalMoney.setText("Total Money:");
        }
        else if(save==source){
            program ae=new program();
            try {  
                ae.outputFile();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null,"Error!!!");
            }
        }
    }
    public void outputFile() throws IOException{
        program c=new program();
        int byteRead;
        InputStream in=new FileInputStream("Meal.txt");
        OutputStream out=new FileOutputStream("Meala.txt");
        Writer writer = new OutputStreamWriter(out);
        while((byteRead=in.read())!=EOF){
            String sname=name.getText();
            System.out.println(sname);
            writer.write(giveMoney.getText()+"\n");
            writer.write(coupon.getText()+"\n");
            writer.write(totalMoney.getText()+"\n");
            writer.write(exchange.getText()+"\n");
            writer.write(couponFrom.getText()+"\n");
            writer.write(couponTo.getText()+"\n");         
        }
        in.close();
        out.close();
        writer.close();
    }
}






public class Drive {
    public static void main(String[] args){
    program a=new program();
    a.setCouponM(12);
    a.setVisible(true);
    }
}

import java.io.*;

public class File {
    BufferedWriter out;

    public File(){
        try {
            //replace helloworld.txt with the name of the file CHANGE after
            //you declare which file to write to add the boolean true which
            // will stop it from replacing the helloworld.txt with a new one.
            out = new BufferedWriter(new FileWriter("helloworld.txt",true));

            //Write out a string to the file
            out.write("Hello you stupid duck");

            //write a new line to the file so the next time you write
            //to the file it does it on the next line
            out.newLine();

            //flushes and closes the stream
            out.close();
        }catch(IOException e){
            System.out.println("There was a problem:" + e);

        }
    }

    public static void main(String[] args){
        File File = new File();
    }
}

放工再試!!

TOP

你究竟要做乜野呢

TOP

你究竟要做乜野呢
Guillo 發表於 2015-1-30 17:43


做一個program計收錢找錢
然後再save去一個file,記低時間日期飯票號碼員工姓名

TOP

Why don't use database(mariadb)?

TOP

提示: 作者被禁止或刪除 內容自動屏蔽

TOP

基本上你係唔明如何寫 java database application. 講就有排講啦。不如你照跟啦

   ...
Databases 發表於 2015-1-30 19:57

好,謝謝

TOP

Why don't use database(mariadb)?
hongkong_netcop 發表於 2015-1-30 19:27

2月考java試,順手當溫習

TOP

提示: 作者被禁止或刪除 內容自動屏蔽

TOP

請住記考java 同寫 java appliction係兩回事。。e.g. 出面唔會用JDBC..而會用hibernate / JPA..唔多 ...
Databases 發表於 2015-1-30 20:28

現在最緊要過左考先

TOP