package ygx.yang.ecommerce;import java.util.*;public class Item implements Comparable
{
private String id;
private String name;
private double retail;
private int quantity;
private double price; Item(String idIn, String nameIn, String retailIn, String quanIn){
id=idIn;
name=nameIn;
retail=Double.parseDouble(retailIn);
quantity=Integer.parseInt(quanIn); if (quantity>400)
{
price=retail* .5D;
}
else if (quantity>200)
{
price=retail* .6D;
}
else
price=retail* .7D;
price=Math.floor(price*100+.5)/100;
} public int compareTo(Object obj){
Item temp=(Item)obj;
if (this.price<temp.price)
{
return 1;
}
else if (this.price>temp.price)
{
return -1;
}
return 0;
} public String getId(){
return id;
} public String getName(){
return name;
} public double getRetail(){
return retail;
} public int getQuantity(){
return quantity;
} public double getPrice(){
return price;
}
}
------》这是第一个类
package ygx.yang.ecommerce;import java.util.*;public class Storefront
{
private LinkedList catalog=new LinkedList(); public void addItem(String id, String name, String price, String quant){
Item(Integer) it=new Item(Integer)(id, name, price, quant);
catalog.add(it);
} public Item getItem(int i){
return (Item)catalog.get(i);
} public int getSize(){
return catalog.size();
} public void sort(){
Collections.sort(catalog);
}
}
------》这是第二个类
import ygx.yang.ecommerce.*;public class GiftShop
{
public static void main(String[] args)
{
Storefront store=new Storefront();
store.addItem("C01","MUG","9.99","150");
store.addItem("C02","LG MUG","12.99","82");
store.addItem("C03","MOUSEPAD","10.49","800");
store.addItem("D01","T SHIRT","16.99","90");
store.sort(); for (int i=0;i<store.getSize() ;i++ )
{
Item show=(Item)store.getItem(i);
System.out.println("\nItem ID: "+ show.getId()+
"\nName: "+show.getName()+
"\nRetail Price: $"+show.getRetail()+
"\nPrice: $"+show.getPrice()+
"\nQuantity: "+show.getQuantity());
}
}
}
------》这是入口类
我编译第二个类时出现了标题的提示,大家好像都说这只是警告,没关系,不过编译最后一个类时提示“GiftShop.java:7: 无法访问 Storefront
错误的类文件: .\Storefront.class
类文件包含错误的类: ygx.yang.ecommerce.Storefront
请删除该文件或确保该文件位于正确的类路径子目录中。
Storefront store=new Storefront();”
我用的是jdk1.6,而且也是在ygx.yang.ecommerce目录下编译前两个程序,classpath设置也对的。请大家帮帮忙忙啊
{
private String id;
private String name;
private double retail;
private int quantity;
private double price; Item(String idIn, String nameIn, String retailIn, String quanIn){
id=idIn;
name=nameIn;
retail=Double.parseDouble(retailIn);
quantity=Integer.parseInt(quanIn); if (quantity>400)
{
price=retail* .5D;
}
else if (quantity>200)
{
price=retail* .6D;
}
else
price=retail* .7D;
price=Math.floor(price*100+.5)/100;
} public int compareTo(Object obj){
Item temp=(Item)obj;
if (this.price<temp.price)
{
return 1;
}
else if (this.price>temp.price)
{
return -1;
}
return 0;
} public String getId(){
return id;
} public String getName(){
return name;
} public double getRetail(){
return retail;
} public int getQuantity(){
return quantity;
} public double getPrice(){
return price;
}
}
------》这是第一个类
package ygx.yang.ecommerce;import java.util.*;public class Storefront
{
private LinkedList catalog=new LinkedList(); public void addItem(String id, String name, String price, String quant){
Item(Integer) it=new Item(Integer)(id, name, price, quant);
catalog.add(it);
} public Item getItem(int i){
return (Item)catalog.get(i);
} public int getSize(){
return catalog.size();
} public void sort(){
Collections.sort(catalog);
}
}
------》这是第二个类
import ygx.yang.ecommerce.*;public class GiftShop
{
public static void main(String[] args)
{
Storefront store=new Storefront();
store.addItem("C01","MUG","9.99","150");
store.addItem("C02","LG MUG","12.99","82");
store.addItem("C03","MOUSEPAD","10.49","800");
store.addItem("D01","T SHIRT","16.99","90");
store.sort(); for (int i=0;i<store.getSize() ;i++ )
{
Item show=(Item)store.getItem(i);
System.out.println("\nItem ID: "+ show.getId()+
"\nName: "+show.getName()+
"\nRetail Price: $"+show.getRetail()+
"\nPrice: $"+show.getPrice()+
"\nQuantity: "+show.getQuantity());
}
}
}
------》这是入口类
我编译第二个类时出现了标题的提示,大家好像都说这只是警告,没关系,不过编译最后一个类时提示“GiftShop.java:7: 无法访问 Storefront
错误的类文件: .\Storefront.class
类文件包含错误的类: ygx.yang.ecommerce.Storefront
请删除该文件或确保该文件位于正确的类路径子目录中。
Storefront store=new Storefront();”
我用的是jdk1.6,而且也是在ygx.yang.ecommerce目录下编译前两个程序,classpath设置也对的。请大家帮帮忙忙啊
public int compareTo(Object obj) {
Item temp = (Item) obj;
if (this.price < temp.price) {
return 1;
} else if (this.price > temp.price) {
return -1;
}
return 0;
} 改为: public int compareTo(Item obj) {
if (this.price < obj.price) {
return 1;
} else if (this.price > obj.price) {
return -1;
}
return 0;
}3. private LinkedList catalog=new LinkedList(); --> private LinkedList<Item> catalog = new LinkedList<Item>();4. Item(Integer) it=new Item(Integer)(id, name, price, quant); 语法错误,改为:Item it = new Item(id, name, price, quant);更改以上 4 处就可以了。1~3 可以不用改的,警告可以忽略的,但不建议忽略。
仅供楼主参考。
public class GiftShop {
public static void main(String[] args) {
Storefront store=new Storefront();
store.addItem("C01","MUG","9.99","150");
store.addItem("C02","LG MUG","12.99","82");
store.addItem("C03","MOUSEPAD","10.49","800");
store.addItem("D01","T SHIRT","16.99","90");
store.sort();
for (int i=0;i <store.getSize() ;i++ ) {
Item show=(Item)store.getItem(i);
System.out.println("\nItem ID: "+ show.getId()+
"\nName: "+show.getName()+
"\nRetail Price: $"+show.getRetail()+
"\nPrice: $"+show.getPrice()+
"\nQuantity: "+show.getQuantity());
}
}
}
import java.util.*; public class Storefront{
private LinkedList<Item> catalog=new LinkedList<Item>();
public void addItem(String id, String name, String price, String quant){
Item it=new Item(id, name, price, quant);
catalog.add(it);
}
public Item getItem(int i){
return (Item)catalog.get(i);
}
public int getSize(){
return catalog.size();
}
public void sort(){
Collections.sort(catalog);
}
} import java.util.*;
class Item implements Comparable<Item>{
private String id;
private String name;
private double retail;
private int quantity;
private double price; Item(String idIn, String nameIn, String retailIn, String quanIn){
id=idIn;
name=nameIn;
retail=Double.parseDouble(retailIn);
quantity=Integer.parseInt(quanIn); if (quantity> 400) {
price=retail* .5D;
}
else if (quantity> 200) {
price=retail* .6D;
}
else
price=retail* .7D;
price=Math.floor(price*100+.5)/100;
} public int compareTo(Item temp){
//Item temp=(Item)obj;
if (this.price <temp.price) {
return 1;
}
else if (this.price> temp.price) {
return -1;
}
return 0;
} public String getId(){
return id;
} public String getName(){
return name;
} public double getRetail(){
return retail;
} public int getQuantity(){
return quantity;
} public double getPrice(){
return price;
}
}
GiftShop.java:7: 无法访问 Storefront
错误的类文件: E:\J21work\Storefront.java
文件不包含类 Storefront
请删除该文件或确保该文件位于正确的类路径子目录中。
Storefront store = new Storefront();后来我在第三个类进行了修改
将
import (路径).ecommerce.*;改为
import (路径).ecommerce.Item;
import (路径).ecommerce.Storefront;
就没问题了很奇怪为什么用了*不行,而分开导入就可以了。