1

ここのノブ、

アレイにグループを作成するにはどうすればよいですか?

receipt[] re11 = {["Book", 12.49, 1, false, true],["Music CD", 14.99, 1, false, false]};

JavaScript ではそのように括弧を使用できることは知っていますが、Java での方法を思い出せません。

編集:これが残りのクラスです。申し訳ありませんが、これはもう少し助けになるでしょう:

public class receipt {

private double price;
private Integer quantity;
private String itemName;
private boolean imported;
private boolean taxExemption;

private double basicTaxRate = .10;
private double importDuty = .05; 
private double grandTotal;

public receipt(){

}

public receipt(String newItemName, double newPrice, Integer newQuantity, boolean newImported, boolean newTaxExemption){
    itemName = newItemName;
    price = newPrice;
    quantity = newQuantity;
    imported = newImported;
    taxExemption = newTaxExemption;
}

//Accessors
public double getPrice(){
    return price;
}

public Integer getQuantity(){
    return quantity;
}

public String getItemName(){
    return itemName;
}

public boolean getImported(){
    return imported;
}

public boolean getTaxExemption(){
    return taxExemption;
}


//mutators
public void setPrice(double newPrice){
    //JOptionPane.showInputDialog("What is the price of the item?");
    this.price = newPrice;
}

public void setQuantity(Integer newQuantity){
    this.quantity = newQuantity;
}

public void setItemName(String newItemName){
    this.itemName = newItemName;
}

public void setImported(boolean newImported){
    this.imported = newImported;
}

public void setTaxExemption(boolean newTaxExemption){
    this.taxExemption = newTaxExemption;
}

public double computeTax(){
    if(imported == true){
        return price*(1+(basicTaxRate+importDuty));
    }
    else{
        return price*(1+basicTaxRate);
    }
}

public double computeTotal(){
    if (taxExemption = false){
        return computeTax()*quantity;
    }
    else
        return price*quantity;

}

public void computeGrandTotal(){
    grandTotal =+ computeTotal();
    //return grandTotal;        
}
4

5 に答える 5

1

グループとは、クラスを意味すると思います。

class Receipt {
 String item;
 Double price;
 int quantity;
 boolean a; //unsure what the true and false are in your context
 boolean b; //unsure what the true and false are in your context
}

その後、このように使用できます

Receipt[] rel1 = {new Receipt("Book", 12.49, 1, false, true),new Receipt("Music CD", 14.99, 1, false, false)};
于 2013-11-05T05:44:56.230 に答える
1

これはあなたが意味するものですか?

Receipt[] re11 = {new Receipt("Book", 12.49, 1, false, true),new Receipt("Music CD", 14.99, 1, false, false)};
于 2013-11-05T05:42:30.993 に答える
1

私の知る限り、Java ではそれをネイティブに行うことはできません。「領収書」が独自のクラスである場合は、そのインスタンスを作成する必要があります。String、double、int、および 2 つのブール値を取るコンストラクターがあると仮定すると、次のようになります。

receipt[] re11 = {new receipt("Book", 12.49, 1, false, true),new receipt("Music CD", 14.99, 1, false, false)};
于 2013-11-05T05:43:01.993 に答える
0

そのようにしてください-

import java.util.ArrayList;
/**
 * 
 * @author manish
 *
 */

public class CustomArrayList {

    public static void main(String args[]){
        ArrayList<reciept> custReciept=new ArrayList<reciept>();
        custReciept.add(new reciept("Book", 12.55, true, false));
        custReciept.add(new reciept("Book2", 15.55, true, true));

        for(int i=0;i<custReciept.size();i++){
    System.out.println( custReciept.get(i).name);
    System.out.println( custReciept.get(i).price);
    System.out.println( custReciept.get(i).first);

    }

    }


}
class reciept{
    String name;
    double price;
    boolean first,second;

    public reciept(String name, double price, boolean first, boolean second) {
        super();
        this.name = name;
        this.price = price;
        this.first = first;
        this.second = second;
    }

}

上記のコードをコピーして貼り付けて楽しんでください..

于 2013-11-05T06:08:51.363 に答える