2009-08-20 20 views
1

这是作业,但我需要一个“微调”。我无法找到如何按名称排序。试图理解排序方法。 Java作业

我的问题:(请将答案初学型)

  1. 做什么都看的权利这么远?
  2. 如何排序?
  3. 有没有人有优化/清洁这个建议?

这里是分配:

的库存计划第2部分检查点在6个星期,由于有以下要求:

  1. 修改清单方案,以便应用程序能处理多个项目。使用数组来存储项目。

  2. 输出应该一次显示一个产品的信息,包括产品编号,产品名称,库存单位数量,每个单位的价格以及该产品库存的价值。

  3. 此外,输出应显示整个库存的值。

  4. 创建一个方法来计算整个库存的价值。

  5. 创建另一种按产品名称对数组项进行排序的方法。

为了满足这些需求,您需要将以下添加到您的库存类(非产品类):

1)声明类型产品(私有的实例变量)

数组

2)内,您的主要while循环,执行以下操作:

a. Instantiate a product object 
b. Populate the product object with the input from the console (like Inventory Part 1  does) 
c. Add the product object reference to the next element in your array 
d. Destroy the reference to the product object variable (note this object was added to your array so you can set the local variable that refers to the object to null) 

3)你的主while循环之外,做到以下几点:

a. Call a method in your Inventory class to sort the array of Product objects. Note you need to sort an array of Product objects by the Product Name instance variable. To do this, you will need to create a separate .java file that implements the Comparator interface. You will pass the Product array and implemented Comparator interface class as arguments to the sort method of the Arrays class (part of the Java API). 
b. Create a For Loop that iterates through the array of Product objects (similar to the one I have below). Invoke the get method on each instance variable and format the output by calling printf. Sample For Loop to use: 
for (Product product : productArray) 
    {   Add statements here 
    } 
c. Call a method in your Inventory class to calculate the total inventory value of all the Product objects in your array. 

这里是我的代码

public class InvTest2{// main method begins 
     public static void main(String args[]) { 

     int version = 2;// Version number 
     final int invLeng = 5;// Declare Inv length 

     // Welcome message 
     System.out.printf("\n%s%d\n" , 
     "Welcome to the Inventory Program v.", version); 

     Inv[] DVDs = new Inv[invLeng]; 
     DVDs[0] = new Inv("The Invisible Man", 0, 8.50); // new DVD constructor 
     DVDs[1] = new Inv("The Matrix", 1, 17.99); 
     DVDs[2] = new Inv("Se7en", 7, 12.99); 
     DVDs[3] = new Inv("Oceans Eleven", 11, 9.99); 
     DVDs[4] = new Inv("Hitch Hikers Guide to the Galaxy", 42, 18.69); 

     // Display formatted results 
     int c = 0; 
     double runningValue = 0; 
     System.out.printf("\n%s\n", "Inventory of DVD movies"); 
     while(c != DVDs.length){ 
     System.out.printf("\n\n%s%d\n%s%s\n%s%d\n%s%,.2f\n%s%,.2f\n", 
     "Item Number:  ",c,  
     "DVD Title:  ",DVDs[c].getdvdTitle(), 
     "Copies in stock: ",DVDs[c].getdvdInStock(), 
     "Price each disk: $",DVDs[c].getdvdValue(), 
     "Value of copies: $",DVDs[c].getdvdStockValue());//End print 
     runningValue += DVDs[c].getdvdStockValue(); 
     c++; 
     } 
     System.out.printf("\n%s%,.2f\n", 
     "Collection Value: $",runningValue); 

     }// end method main 

}//end class Inventory1 

这里是库存

public class Inv {//Begin DVD class 

    // Declare instance variables 
    String dvdTitle;// Declare title as string 
    int dvdInStock;// Declare dvdInStock as float 
    double dvdValue;// Declare dvdValue as float 

    // constructor initializes DVD information 
    public Inv(String title, int inStock, double value) { // Initialize (clear) instance variables here 
     dvdTitle = title; 
     dvdInStock = inStock; 
     dvdValue = value; 
     } // end constructor 

    public String getdvdTitle() {// public method to get the DVD name 
     return dvdTitle;}// end method getdvdTitle 

    public int getdvdInStock() {// public method to retrieve the dvdInStock 
     return dvdInStock;} // end method get dvdInStock 

    public double getdvdValue() {// public method to retrieve the dvdValue 
     return dvdValue;} // end method get dvdValue 

    public double getdvdStockValue() {// public method to dvdStockValue 
     return (dvdValue * dvdInStock);}// end method get dvdStockValue 

    } // end class Inv 
+1

你的代码不符合你的老师提供的要求。你可能希望从那里开始。 – doomspork 2009-08-20 18:03:16

+0

对不起,编辑clobber。如果你觉得自己做得不好,请将我的身后卷起来。 – Welbog 2009-08-20 18:06:29

+1

寻求家庭作业帮助的问题是,没有人再也不必再做家庭作业,他们想要做。你最好问一些关于你遇到的实际问题的更具体的问题 – mkoryak 2009-08-20 18:08:25

回答

2

使用Comparator很容易,可以用这样的事情来完成:

public class InvSortByName implements Comparator<Inv>{ 
    public int compare(Inv o1, Inv o2) { 
     return o1.getdvdTitle().compareTo(o2.getdvdTitle()); 
    } 
} 

我相信,根据所提供的说明和代码,你可能误解了指导方针。似乎应该有一个主要(...),一个库存类和一个DVD /产品类。你目前只有一部分。

你可以用一个更清洁和更易于使用的foreach替换主循环。说明提及并提供了for-each的示例。根据提供的说明,该循环应从主体中移除。

您还应该查看一些基本的Java最佳实践和命名约定。

+0

非常有帮助。谢谢先生! – gooddadmike 2009-08-20 19:53:34

+0

这就是我看到它的方式;一个主程序循环,其中有一个Inventory对象,其中可以放置各种Product对象(此处为DVD)。 然后库存类可以有一个调用Arrays.sort(T [],比较器)的排序方法。 (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator) – extraneon 2009-08-20 20:00:05

+2

或者,您可以让您的DVD可比较只是打电话给Arrays.sort(T []) – extraneon 2009-08-20 20:01:33

6

分配告诉你什么,以便实现按名称排序做。

获得一些睡眠,然后重新读取“主循环外部”下的第一个项目符号点,执行以下操作:“。

+0

touche'我想比较器部分不是真的在据我所知,阅读。这是主要问题,并且缺乏睡眠。 – gooddadmike 2009-08-20 19:51:54

1

你确定你应该使用Arrays.sort方法吗?出于某种原因,要求5在我看来,它要求您编写自己的排序方法。我可能是错误的,但你可能想在使用Arrays.sort之前进行验证。