我收到此错误;二进制运算符'-'的错误操作数类型。它发生在网上;
- p2.getProductNum();
如果有人可以帮助解决此异常,将不胜感激。
BetterBasket类
package catalogue;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
/**
* @version 1.0
*/
public class BetterBasket extends Basket implements Serializable
{
private static final long serialVersionUID = 1L;
@Override
public boolean add( Product pr )
{
super.add(pr);
Collections.sort(this, new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
return p1.getProductNum() - p2.getProductNum();
}
});
}
}
产品类别
package catalogue;
import java.io.Serializable;
/**
* Used to hold the following information about
* a product: Product number, Description, Price and
* Stock level.
*/
public class Product implements Serializable
{
private static final long serialVersionUID = 20092506;
private String theProductNum; // Product number
private String theDescription; // Description of product
private double thePrice; // Price of product
private int theQuantity; // Quantity involved
/**
* Construct a product details
* @param aProductNum Product number
* @param aDescription Description of product
* @param aPrice The price of the product
* @param aQuantity The Quantity of the product involved
*/
public Product( String aProductNum, String aDescription,
double aPrice, int aQuantity )
{
theProductNum = aProductNum; // Product number
theDescription = aDescription; // Description of product
thePrice = aPrice; // Price of product
theQuantity = aQuantity; // Quantity involved
}
public String getProductNum() { return theProductNum; }
public String getDescription() { return theDescription; }
public double getPrice() { return thePrice; }
public int getQuantity() { return theQuantity; }
public void setProductNum( String aProductNum )
{
theProductNum = aProductNum;
}
public void setDescription( String aDescription )
{
theDescription = aDescription;
}
public void setPrice( double aPrice )
{
thePrice = aPrice;
}
public void setQuantity( int aQuantity )
{
theQuantity = aQuantity;
}
}
篮子课
package catalogue;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Currency;
import java.util.Formatter;
import java.util.Locale;
/**
* A collection of products from the CatShop.
* used to record the products that are to be/
* wished to be purchased.
*
*/
public class Basket extends ArrayList<Product> implements Serializable
{
private static final long serialVersionUID = 1;
private int theOrderNum = 0; // Order number
/**
* Constructor for a basket which is
* used to represent a customer order/ wish list
*/
public Basket()
{
theOrderNum = 0;
}
/**
* Set the customers unique order number
* Valid order Numbers 1 .. N
* @param anOrderNum A unique order number
*/
public void setOrderNum( int anOrderNum )
{
theOrderNum = anOrderNum;
}
/**
* Returns the customers unique order number
* @return the customers order number
*/
public int getOrderNum()
{
return theOrderNum;
}
/**
* Add a product to the Basket.
* Product is appended to the end of the existing products
* in the basket.
* @param pr A product to be added to the basket
* @return true if successfully adds the product
*/
// Will be in the Java doc for Basket
@Override
public boolean add( Product pr )
{
return super.add( pr ); // Call add in ArrayList
}
/**
* Returns a description of the products in the basket suitable for printing.
* @return a string description of the basket products
*/
public String getDetails()
{
Locale uk = Locale.UK;
StringBuilder sb = new StringBuilder(256);
Formatter fr = new Formatter(sb, uk);
String csign = (Currency.getInstance( uk )).getSymbol();
double total = 0.00;
if ( theOrderNum != 0 )
fr.format( "Order number: %03d\n", theOrderNum );
if ( this.size() > 0 )
{
for ( Product pr: this )
{
int number = pr.getQuantity();
fr.format("%-7s", pr.getProductNum() );
fr.format("%-14.14s ", pr.getDescription() );
fr.format("(%3d) ", number );
fr.format("%s%7.2f", csign, pr.getPrice() * number );
fr.format("\n");
total += pr.getPrice() * number;
}
fr.format("----------------------------\n");
fr.format("Total ");
fr.format("%s%7.2f\n", csign, total );
fr.close();
}
return sb.toString();
}
}
答案 0 :(得分:1)
替换此行:
return p1.getProductNum() - p2.getProductNum();
使用
p1.getProductNum().compareTo(p2.getProductNum());
p1.getProductNum()
和p2.getProductNum()
都是字符串,您不能将它们相减。
您想比较它们,因为您需要创建一个Comperator
用于对Collection
个对象中的Product
进行排序。
答案 1 :(得分:0)
getProductNum()
返回一个字符串。
如果出于任何原因想要将此字符串保留为字符串,则可以使用:
p1.getProductNum().compareTo(p2.getProductNum());
如果不需要字符串,请更改:
private String theProductNum;
收件人:
`private int theProductNum;` and make sure the method returns an int :)