1

Noob here,

How would I create groups in my array?

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

I know in javascript you can use brackets like that but i just can't remember how to do in java.

Edit: Here are the rest of my classes. Sorry this will prob be a bit more help:

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

Is this what you mean?

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

As far as I know, in Java, you cannot natively do that. If "receipt" is your own class, you will have to create instances of that. Assuming that it has a constructor taking a String, a double, and int, and two boolean values, that would look something like that:

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 回答
1

by groups i assume you mean a class.

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
}

you can then use it like this

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 回答
0

First off, arrays in Java are different from arrays in JavaScript. Arrays in JavaScript expand as needed but in Java they do not. Also, you cannot 'remove' an element from a Java array.

What you need is an ArrayList<YourClass> which is an expandable array. It also allows you to remove the element you want.

Coming to the topic of grouping the data. You will need your own class with the desired variables and methods defined. Then you put the objects of that class into an ArrayList

So you would have your class:

public class Receipt {
 String item;
 Double price;
 int quantity;
 boolean available; 
 boolean iDoNotKnowWhy; 
 //getters and setters
}  

Then you will have an ArrayList<Receipt> where you add all the data you want.

ArrayList<Receipt> receipts = new ArrayList<>();
receipts.add(new Receipt(....)); // add a new receipt
receipts.add(new Receipts(....)); // another receipt
于 2013-11-05T05:46:21.480 回答
0

Just do like that-

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;
    }

}

Copy Paste above code and enjoy..

于 2013-11-05T06:08:51.363 回答