2017-10-17 95 views
-1

未定義的代碼的其餘部分是好的,由於某種原因,我得到的UserInterface類的錯誤。我是java新手,所以如果它是一個簡單的錯誤,可以在幾秒鐘內解決,我提出這個問題,我提前道歉。我正在使用Java Eclipse Oxygen。如果您可以提供您的答案並解釋您是如何做到的,那麼我也可以在此過程中學習,而不僅僅是解決方案。Java的構造函數中的UserInterface

的錯誤是: 構造Bbat(String, String, int, Store, String, String, int)未定義 - UserInterface.java

下面是Bbat類和用戶界面類的代碼。

package stage02; 

public class Bbat extends SportsGoods { 

    private boolean fullTime; 
    private String studentType; 
    private double currentFees; 


    public Bbat() { 
     super (null,null,0,null); 
     fullTime = true; 
     studentType = null; 
     currentFees = 0.0; 
    } 
    public Bbat (String gN, String fN, int age, Store ei, boolean fT, String sT, double cF) { 
     super (gN, fN, age, ei); 
     fullTime = fT; 
     studentType = sT; 
     currentFees = cF; 
    } 

    public boolean getFullTime() { 
     return fullTime; 
    } 
    public String getStudentType() { 
     return studentType; 
    } 
    public double getCurrentFees() { 
     return currentFees; 
    } 
    public void setFullTime (boolean fT) { 
     fullTime = fT; 
    } 
    public void setStudentType (String sT) { 
     studentType = sT; 
    } 
    public void setCurrentFees (double cF) { 
     currentFees = cF; 
    } 

    public String toString() { 
     return super.toString() + "\nFull Time: " + (getFullTime()?"Yes":"No") + 
        "\nStudent Type: " + getStudentType() + 
        "\nCurrent Fees: $" + getCurrentFees() + " per hour\n"; 
    } 
} 

用戶界面:

package stage02; 
import java.util.*; 
import javax.swing.JOptionPane; 

public class UserInterface { 

public void begin() { 

    ArrayList <SportsGoods> people = new ArrayList <SportsGoods>(); 

    boolean finished = false; 
    while (!finished) { 
    int selection = showMenu(); 
    switch (selection) { 
    case 1: 
    people.add(addCbat()); 
    break; 
    case 2: 
    people.add(addBbat()); 
    break; 
    case 3: 
    JOptionPane.showMessageDialog(null, "Displaying the stock information ...", "Stock List", JOptionPane.PLAIN_MESSAGE); 
    for (int i = 0; i < people.size(); i++) { 
     JOptionPane.showMessageDialog(null, people.get(i), "Product Information", JOptionPane.PLAIN_MESSAGE); 
    } 
    JOptionPane.showMessageDialog(null, "There are " + people.size() + " record(s) in the list", "Total records", JOptionPane.PLAIN_MESSAGE); 
    break; 
    case 4: 
    finished = true; 
    JOptionPane.showMessageDialog(null, "*** Program Ended ***\n" + 
     "*** Thank you for using this program ***"); 
    break; 
    default: 
    JOptionPane.showMessageDialog(null, "\n** Invalid Selection **\n", "ERROR", JOptionPane.ERROR_MESSAGE); 
    } 
    } 
} 

public int showMenu() { 
    int selection = 0; 
    String stringSelection = JOptionPane.showInputDialog(
    "******* MENU *******\n\n" + 
    "1. Add a new Cricket Bat\n" + 
    "2. Add a new BaseBall Bat\n" + 
    "3. Display all Details\n" + 
    "4. Exit Program\n\n" + 
    "Type the number of your selection, and click OK: "); 
    selection = Integer.parseInt(stringSelection.trim()); 
    return selection; 
} 

public Cbat addCbat() { 
     String aName = JOptionPane.showInputDialog(null, "\nWhat is the Bat's model? ").trim(); 
     String bName = JOptionPane.showInputDialog(null, "What is " + aName + "'s grip? ").trim(); 
     int age = 0; 
     do { 
     String stringValidAge = JOptionPane.showInputDialog(null, "What is " + aName + "'s price? (no decimal places) "); 
     double doubleAge = Double.parseDouble(stringValidAge); 
     age = (int) doubleAge; 
     if (age <= 1 && age > 200) { 
     JOptionPane.showMessageDialog(null, "Error - cost must be greater than 1 to be sold by this store", "ERROR", JOptionPane.ERROR_MESSAGE); 
     } 
     } while (age <= 1 && age > 200); 

     String eType = null; 
     do { 
     eType = JOptionPane.showInputDialog(null, "What is " + bName + "'s colour? ".trim()); 
     if (!eType.equals("Red") && !eType.equals("Blue") && !eType.equals("Yellow") && !eType.equals("Black") && !eType.equals("Green")) { 
     JOptionPane.showMessageDialog(null, "\n** Invalid Selection **\nBat grip colour must be Red, Blue, Yellow, Black, or Green", 
     "ERROR", 
     JOptionPane.ERROR_MESSAGE); 
     } 
     } while (!eType.equals("Red") && !eType.equals("Blue") && !eType.equals("Yellow") && !eType.equals("Black") && !eType.equalsIgnoreCase("Green")); 

     String eNum = JOptionPane.showInputDialog(null, "What is " + aName + "'s Product Code? "); 
     double nYE; 
     do { 
     String stringNYE = JOptionPane.showInputDialog(null, "How many " + aName + " are in stock?"); 
     nYE = (int) Double.parseDouble(stringNYE); 
     if (nYE < 0) { 
     JOptionPane.showMessageDialog(null, "\n** Value must be zero or more **\n", "ERROR", JOptionPane.ERROR_MESSAGE); 
     } 
     } while (nYE < 0); 
     String or = null; 
     or = JOptionPane.showInputDialog(null, "Name of store involved ? "); 
     String city = JOptionPane.showInputDialog(null, "City where " + or + " is ? "); 
     Store org = new Store(or, city); 
     Cbat s = new Cbat(aName, bName, age, org, eType, eNum, (int) nYE); 
     JOptionPane.showMessageDialog(null, "Thank you - bat added to the list", "Bat added", JOptionPane.PLAIN_MESSAGE); 
    return s; 
} 

public Bbat addBbat() { 
    String gName = JOptionPane.showInputDialog(null, "\nWhat is the Bat's model? ").trim(); 
    String fName = JOptionPane.showInputDialog(null, "What is " + gName + "'s grip? ").trim(); 
    int age = 0; 
    do { 
    String stringValidAge = JOptionPane.showInputDialog(null, "What is " + gName + "'s price? (no decimal places) "); 
    double doubleAge = Double.parseDouble(stringValidAge); 
    age = (int) doubleAge; 
    if (age <= 1 && age > 200) { 
    JOptionPane.showMessageDialog(null, "Error - cost must be greater than 1 to be sold by this store", "ERROR", JOptionPane.ERROR_MESSAGE); 
    } 
    } while (age <= 1 && age > 200); 

    String eType = null; 
    do { 
    eType = JOptionPane.showInputDialog(null, "What is " + fName + "'s colour? ".trim()); 
    if (!eType.equals("Red") && !eType.equals("Blue") && !eType.equals("Yellow") && !eType.equals("Black") && !eType.equals("Green")) { 
    JOptionPane.showMessageDialog(null, "\n** Invalid Selection **\nBat grip colour must be Red, Blue, Yellow, Black, or Green", 
    "ERROR", 
    JOptionPane.ERROR_MESSAGE); 
    } 
    } while (!eType.equals("Red") && !eType.equals("Blue") && !eType.equals("Yellow") && !eType.equals("Black") && !eType.equalsIgnoreCase("Green")); 

    String eNum = JOptionPane.showInputDialog(null, "What is " + gName + "'s Product Code? "); 
    double nYE; 
    do { 
    String stringNYE = JOptionPane.showInputDialog(null, "How many " + gName + " are in stock?"); 
    nYE = (int) Double.parseDouble(stringNYE); 
    if (nYE < 0) { 
    JOptionPane.showMessageDialog(null, "\n** Value must be zero or more **\n", "ERROR", JOptionPane.ERROR_MESSAGE); 
    } 
    } while (nYE < 0); 
    String or = null; 
    or = JOptionPane.showInputDialog(null, "Name of store involved ? "); 
    String city = JOptionPane.showInputDialog(null, "City where " + or + " is ? "); 
    Store org = new Store(or, city); 
    Bbat e = new Bbat(gName, fName, age, org, eType, eNum, (int) nYE); 
    JOptionPane.showMessageDialog(null, "Thank you - bat added to the list", "Bat added", JOptionPane.PLAIN_MESSAGE); 
    return e; 

} 
} 

您對這個問題的幫助將不勝感激。

回答

0

要調用構造函數不對其中Bbat e = new Bbat(gName, fName, age, org, eType, eNum, (int) nYE);沒有定義,即參數類型不匹配。

所以,你的構造應該有如下定義:

public Bbat (String gN, String fN, int age, Store ei, String fT, String sT, double cF) 

而且fullTime boolean類型。但是你逝去的String作爲參數,以便要麼使fullTimeString或代替eType通過布爾你在哪裏調用(Bbat e = new Bbat(gName, fName, age, org, eType, eNum, (int) nYE);

+0

我有一個構造函數在我Bbat類,所以我需要做什麼來定義它是什麼與用戶界面?我猜可能用戶界面中的代碼是錯誤的? – Saada

+0

首先決定在Bbat構造函數中需要哪些參數並相應地編寫代碼。閱讀信息。我解釋了。 –

+0

因此,無論我在Bbat構造函數中有什麼,我需要在用戶界面中以相同的順序具有完全相同的內容? – Saada