2016-09-30 11 views
-4

私のコードのコンパイルに問題があります。オブジェクトを生成しようとしたときにエラーが発生しました - コンストラクタクラス

問題は、メインクラスのCustomerクラスからオブジェクトを生成しようとしているように見えます。メインクラスCareHireのコードの行がある:私は、文字列とのIntを入力し、アレイ内の各エントリの番号い

newCust[i]=new Customer(); 

。これは、私が各インスタンスで使用する予定の行ですが、明らかに正しくはありませんし、進める方法がわかりません。

この行を編集するときは、作成します。 'Enter'ボタンをクリックすると、文字列だけが表示され、int値は表示されません。私はここで何をすべきですか?

メインクラス:CareHire

package CareHire; 

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.Scanner; 


public class CareHire extends JFrame implements ActionListener 
{ 
private JLabel nameLabel=new JLabel("Name"); 
private JLabel daysLabel=new JLabel("Days of Hire"); 
private JTextField nameField=new JTextField(16); 
    private JTextField daysField=new JTextField(11); 
    private JButton enterButton=new JButton("Enter"); 
private JButton displayButton=new JButton("Display"); 
    private JButton searchButton= new JButton("Search"); 
    private JButton displayStatButton=new JButton("Statistics"); 
    private JButton exitButton=new JButton("Exit"); 
    private JTextArea textArea=new JTextArea(16,35); 
    private Customer newCust[]=new Customer[20]; 
    int numProcessed=0; 
    private static final int FRAME_WIDTH = 480; 
    private static final int FRAME_HEIGHT = 430; 

//This is the problematic section 

public CareHire() { 

     super(" Care Hire System "); 
     setLayout(new FlowLayout()); 

//Issues appears to be with this For Loop 

     for(int i=0;i<20;i++) { 
     newCust[i]=new Customer(); 
    } 

//End there. Remainder of code is provided for context. 

     add(nameLabel);    
     add(nameField); 
     add(daysLabel); 
     add(daysField); 

     add(enterButton); 
     add(displayButton); 
     add(searchButton); 
     add(displayStatButton); 
     add(exitButton); 

     add(textArea); 
     enterButton.addActionListener(this); 
     displayButton.addActionListener(this); 
     searchButton.addActionListener(this); 
     displayStatButton.addActionListener(this); 
     exitButton.addActionListener(this); 
    } 
} 

    public void processInput() 
    { 
    textArea.setText(nameField.getText()); 
    newCust[numProcessed].setDoH(Integer.parseInt(daysField.getText()));    
    String title="Customer Days of Hire ($)\n\n"; 
    textArea.setText(title+newCust[numProcessed].getName()+ "\t $"+newCust[numProcessed].getDoH()+"\t "+" ");    
    nameField.setText("");     
    daysField.setText("");     
    numProcessed++; 
} 

    public void display()  { 
       if(numProcessed==0) { //no data entered    
     JOptionPane.showMessageDialog(null,"Need Customers","Display All",JOptionPane.ERROR_MESSAGE);   
     return;  
     }  
     String allItems="";  
     for(int i=0;i<numProcessed;i++) {  
      allItems=allItems+(i+1)+".\t"+newCust[i].getName()+ "\t\t $"+newCust[i].getDoH()+"\t "+" "+"\n";}  

     textArea.setText("No.\t Customer \t Days of Hire \n\n"+allItems); 
    } 

    public void search() 
    { 

    } 



     public void statistics()  { 

if(numProcessed==0){ //no data entered     
JOptionPane.showMessageDialog(null,"Need Customers", "Find Max",JOptionPane.ERROR_MESSAGE);   
return; 
    } 

String maxName=null; 
int maxAmount=-10000; 


for(int i=0;i<numProcessed;i++) { 
if(newCust[i].getDoH()>maxAmount){ 
maxAmount=newCust[i].getDoH(); 
maxName=newCust[i].getName(); 
} 
} 
} 

    public static void main(String[] args) 
    { 
     JFrame frame = new CareHire(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     frame.setVisible(true); 
     frame.setResizable(false); 
    } 
} 

コンテナクラス:カスタマー

package CareHire; 

public class Customer { 

private String name; 
private int doh; 

public Customer(String custName, int totalDays) {  
name = custName; 
doh = totalDays; 
} 

public void setName(String custName) { 
name = custName; 
} 

public String getName() { 
return name; 
} 

public void setDoH(int totalDays) { 
doh = totalDays; 
} 

public int getDoH() { 
return doh; 
} 
} 
+1

's [i] = int new custName();'もコンパイルされません。 –

+0

あなたのタイトルを改善してください**と**質問**を追加してください、それはあなたが何を求めているのか不明です。 –

+0

申し訳ありませんが、私はこの問題を自分自身で調べているうちにこれを見やすくしようとしています。問題は私が何を求めているのか分からないことかもしれないと思う。それは私に困ってしまった。私は、身体の不自由な質問の段落で本体を更新しました。 –

答えて

1

この構文はs[i]=String new custName();

はそれがs[i] = new Customer();

あるべき "S" と宣言されている間違っています顧客の配列private Customer s[]=new Customer[20]

私は、Cusotmerにコンストラクタ、または雇用データの名前と曜日を追加するいくつかのメソッドがあると仮定します。オブジェクトをどのように埋め込むかを正確に見るためには、より多くのコードを共有する必要があります。

関連する問題