import java.awt.event.*;
import javax.swing.*;
/******************************************************/
//TestProg 類為公共類,包含main函數
//在其中實現創建兩個重要對象,Toy類的對象toy以及ToyalesFrame類的mine對象。
/*****************************************************/
public class TestProg{
public static void main(String args[]){
Toy toy = new Toy();
ToyalesFrame mine = new ToyalesFrame();
mine.setVisible(true);
}
}
/***********************************************/
/**************************************************/
//此類為實現程序的主界面,包括顯示,布局,容器以及事件監聽,並為其他類的事件處理提供參數
//本身並未實現任何實質功能
/**************************************************/
class ToyalesFrame extends JFrame{
JPanel up; //聲明組件,並且某些組件還包含其他組件
JPanel down;
static JTextArea middle;
JButton buttons[];
JTextField fields[];
String names1[]={
"Show Toy","Show Chart","Save and Exit"
};
private final String names2[]={
"Process Sale","Clear","Staff Name:","Toy Code:","Quantity:"
};
public ToyalesFrame(){ //設置GUI和注冊事件處理
super("Toys Sales");
this.setBounds(0,0,800,600);
Container c= this.getContentPane();
c.setLayout(new BorderLayout(8,8));
up = new JPanel();
down = new JPanel();
up.setOpaque(false);
up.setBackground(Color.gray);
up.setLayout(new GridLayout(1,3,60,0));
Buttonhandler handler1= new Buttonhandler();
buttons = new JButton [5];
for(int count=0;count<names1.length;count++){ //添加局部組件,並且增加事件監聽
buttons[count] = new JButton(names1[count]);
up.add(buttons[count]);
buttons[count].addActionListener(handler1);
}
down.setBackground(Color.gray);
down.setLayout(new FlowLayout(1,50,0));
fields = new JTextField[3];
Box vertical1 = Box.createVerticalBox();
Box vertical2 = Box.createVerticalBox();
Box vertical3 = Box.createVerticalBox();
for(int count=3;count<5;count++)
{ buttons[count]= new JButton(""+names2[count-3]);
buttons[count].addActionListener(handler1);
vertical1.add(buttons[count]);
}
for(int count=2;count<5;count++)
vertical2.add(new JLabel(""+names2[count]));
for(int count=0;count<3;count++){
fields[count]= new JTextField(15);
vertical3 .add(fields[count]);
}
down.add(vertical1);
down.add(vertical2);
down.add(vertical3);
middle = new JTextArea();
middle.setText("序列號 玩具編號 售貨員 單價 售出數量 總銷售額(元)");
middle.append("\n");
middle.setEditable(false);
c.add(up,BorderLayout.NORTH);
c.add(middle,BorderLayout.CENTER);
c.add(down,BorderLayout.SOUTH);
}
private class Buttonhandler implements ActionListener{ //提供事件處理的入口
public void actionPerformed(ActionEvent event){
if(event.getSource()==buttons[0]){
BookFrame stock = new BookFrame();
}
if(event.getSource()==buttons[1]){
ChartFrame chart = new ChartFrame();
}
if(event.getSource()==buttons[2]){
// Salelist list =new Salelist();
//Toylist list = new Toylist();
}
if(event.getSource()==buttons[3]) {
Sale dealsale = new Sale(fields[0].getText(),fields[1].getText(),fields[2].getText());
}
if(event.getSource()==buttons[4]){
fields[0].setText("");
fields[1].setText("");
fields[2].setText("");
}
}
}
}
/******************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*************************************************************/
//Sale 類為處理每一次的銷售活動,包括處理,顯示,記錄,以及清除
/************************************************************/
class Sale {
String name = new String();
String code = new String();
String temcode = new String();
String quantity = new String();
int codenum,quantitynum,temp;
static int serialnum=1;
int exception=0;
public Sale( String name, String code,String quantity){
temcode= code;
if(name.isEmpty())
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"please input the Staff name!");
else{
codenum=Integer.parseInt(temcode);
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"please input a product code!");
quantitynum = Integer.parseInt(quantity);
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"please input a product quantity!");
if(exception==0){
if(codenum<=0||codenum>Toy.kindsum)
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"this product is not available!");
else
{
if (Toy.Toysum[codenum-1]<quantitynum)
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"please input a quantity not above"+Toy.Toysum[codenum-1]);
else {
Toy.Toysum[codenum-1]=Toy.Toysum[codenum-1]-quantitynum;
ToyalesFrame.middle.append(" "+serialnum);
ToyalesFrame.middle.append(" "+Toy.Toycod[codenum-1]);
ToyalesFrame.middle.append(" "+name);
ToyalesFrame.middle.append(" "+Toy.Toyprice[codenum-1]);
ToyalesFrame.middle.append(" "+quantitynum);
java.text.DecimalFormat myformat=new java.text.DecimalFormat("#0.00");
ToyalesFrame.middle.append(" "+Float.parseFloat(myformat.format(Toy.Toyprice[codenum-1]*quantitynum)));
ToyalesFrame.middle.append("\n");
serialnum++;
}
}
}
}
}
}
/***********************************/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/****************************************************/
//Toy 類為從初始化文件toy.txt中讀取玩具初始信息,並作相應的調整,例如存儲,分類,
//以及將字符串元素轉換成可以進行數學運算的int,float等類型
/*****************************************************/
class Toy{
String line;
static String[] Toycode = new String[5];
static String[] Toyname = new String[5];
String[] Toysum1 = new String[5];
static String[] Toyprice1= new String[5];
static int[] Toysum = new int[5];
static Float[] Toyprice= new Float[5];
static int kindsum=0;
public Toy(){
try{
FileInputStream infile= new FileInputStream("toy.txt");
InputStreamReader fis = new InputStreamReader(infile);
BufferedReader bReader= new BufferedReader(fis);
while((line = bReader.readLine()) != null){
int j=0;
String[] lines = line.split("\\ ");
while(lines[j].length()==0) j++;
Toycode[kindsum] = lines[j];
System.out.println("read:"+Toycode[kindsum]);
j++;
while(lines[j].length()==0) j++;
Toyname[kindsum] = lines[j];
System.out.println("read:"+Toyname[kindsum]);
j++;
while(lines[j].length()==0)
j++;
Toyprice1[kindsum] = lines[j];
Toyprice[kindsum] = Float.parseFloat(Toyprice1[kindsum]);
System.out.println("read:"+Toyprice[kindsum]);
j++;
while(lines[j].length()==0) j++;
Toysum1[kindsum] = lines[j];
Toysum[kindsum] = Integer.parseInt(Toysum1[kindsum]);
System.out.println("read:"+Toysum[kindsum]);
kindsum++;
}
fis.close();
}
catch(FileNotFoundException e){
System.out.println("FileStreamTest1:"+e);
}
catch(IOException e){
System.out.println("FileStreamTest1:"+e);
}
}
String[] lines = line.split("\ ");
改成String[] lines = line.split("\t");