【问题标题】:Array sorting printing and summing数组排序打印和求和
【发布时间】:2013-04-17 21:04:37
【问题描述】:

什么是最简单的方法来打印这个数组分解成每个手机作为产品编号,名称部门等,然后重新打印按产品名称排序的相同信息。我尝试了几种不同的方法,并且已经过了作业的交期,但仍然需要为本周末即将到来的作业弄清楚。当我尝试在 MobilePhone 类上实现比较器时,它迫使我将其设为抽象或使用 @override,但我无法弄清楚要覆盖的位置或内容以使其工作,因为抽象类会导致许多其他问题。

package InventoryPro2;

import java.util.*;

class MobilePhone {

    private double productNumber; // Variables
    private String name;
    private String department;
    private double unitsInStock;
    private double unitPrice;

    public MobilePhone() {
        this(0.0, "", "", 0.0, 0.0);
    }

    public MobilePhone(double productNumber, String name, String department,
            double unitsInStock, double unitPrice) { //assign variables
        this.productNumber = productNumber;
        this.name = name;
        this.department = department;
        this.unitsInStock = unitsInStock;
        this.unitPrice = unitPrice;
    }

    public double getproductNumber() { // retrieve values
        return productNumber;
    }

    public String getname() {
        return name;
    }

    public String getdepartment() {
        return department;
    }

    public double getunitPrice() {
        return unitPrice;
    }

    public double getunitsInStock() {
        return unitsInStock;
    }

    public void setproductNumber(double productNumber) {
        this.productNumber = productNumber;
    }

    public void setname(String name) {
        this.name = name;
    }

    public void setdepartment(String department) {
        this.department = department;
    }

    public void setunitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public void setunitsInStock(double unitsInStock) {
        this.unitsInStock = unitsInStock;
    }

    public double gettotalInv() {
        return getunitPrice() * getunitsInStock();
    }
}

public class InventoryPro2 {

    MobilePhone mobilephone = new MobilePhone();

    public static void main(String args[]) {


        System.out.println("Mobile Phone Inventory Program");
        System.out.println();//skips a line

        MobilePhone[] phones = new MobilePhone[5];


        phones[0] = new MobilePhone();
        phones[0].setproductNumber(1);
        phones[0].setname("Motorola");
        phones[0].setdepartment("Electronics");
        phones[0].setunitPrice(150.10);
        phones[0].setunitsInStock(98);

        phones[1] = new MobilePhone();
        phones[1].setproductNumber(2);
        phones[1].setname("Samsung");
        phones[1].setdepartment("Electronics");
        phones[1].setunitPrice(199.99);
        phones[1].setunitsInStock(650);

        phones[2] = new MobilePhone();
        phones[2].setproductNumber(3);
        phones[2].setname("Nokia");
        phones[2].setdepartment("Electronics");
        phones[2].setunitPrice(200.25);
        phones[2].setunitsInStock(125);

        phones[3] = new MobilePhone();
        phones[3].setproductNumber(4);
        phones[3].setname("LG");
        phones[3].setdepartment("Electronics");
        phones[3].setunitPrice(100.05);
        phones[3].setunitsInStock(200);

        phones[4] = new MobilePhone();
        phones[4].setproductNumber(5);
        phones[4].setname("IPhone");
        phones[4].setdepartment("Electronics");
        phones[4].setunitPrice(299.99);
        phones[4].setunitsInStock(150);

        System.out.println("Order of inventory before sorting:");
        System.out.println();

    }
}

(另外,从数组的每个部分中提取一条信息的最佳方法是什么,例如要打印的 totalInv 和所有这些数字的总和?)我这里有不必要的代码还是我做了所有事情到目前为止?我不得不说,到目前为止,以在线格式学习这种编码语言并不是一个非常愉快的经历。

【问题讨论】:

  • 某种地图,也许是 TreeMap

标签: java arrays sorting printing


【解决方案1】:

这是按名称排序的方法

import java.util.Arrays;
import java.util.Comparator;

public class AppInventoryPro2 {

    public static void main(String... args) {

        System.out.println("Mobile Phone Inventory Program");
        System.out.println();// skips a line

        MobilePhone[] phones = new MobilePhone[5];

        phones[0] = new MobilePhone();
        phones[0].setproductNumber(1);
        phones[0].setname("Motorola");
        phones[0].setdepartment("Electronics");
        phones[0].setunitPrice(150.10);
        phones[0].setunitsInStock(98);

        phones[1] = new MobilePhone();
        phones[1].setproductNumber(2);
        phones[1].setname("Samsung");
        phones[1].setdepartment("Electronics");
        phones[1].setunitPrice(199.99);
        phones[1].setunitsInStock(650);

        phones[2] = new MobilePhone();
        phones[2].setproductNumber(3);
        phones[2].setname("Nokia");
        phones[2].setdepartment("Electronics");
        phones[2].setunitPrice(200.25);
        phones[2].setunitsInStock(125);

        phones[3] = new MobilePhone();
        phones[3].setproductNumber(4);
        phones[3].setname("LG");
        phones[3].setdepartment("Electronics");
        phones[3].setunitPrice(100.05);
        phones[3].setunitsInStock(200);

        phones[4] = new MobilePhone();
        phones[4].setproductNumber(5);
        phones[4].setname("IPhone");
        phones[4].setdepartment("Electronics");
        phones[4].setunitPrice(299.99);
        phones[4].setunitsInStock(150);

        System.out.println("Order of inventory before sorting:");
        System.out.println(Arrays.toString(phones));

        Arrays.sort(phones, new Comparator<MobilePhone>() {
            @Override
            public int compare(MobilePhone mp1, MobilePhone mp2) {
                return mp1.getname().compareTo(mp2.getname());
            }
        });

        System.out.println("Order of inventory after sorting by name:");
        System.out.println(Arrays.toString(phones));
    }
}

class MobilePhone {

    private double productNumber; // Variables
    private String name;
    private String department;
    private double unitsInStock;
    private double unitPrice;

    public MobilePhone() {
        this(0.0, "", "", 0.0, 0.0);
    }

    public MobilePhone(double productNumber, String name, String department,
            double unitsInStock, double unitPrice) { // assign variables
        this.productNumber = productNumber;
        this.name = name;
        this.department = department;
        this.unitsInStock = unitsInStock;
        this.unitPrice = unitPrice;
    }

    public double getproductNumber() { // retrieve values
        return productNumber;
    }

    public String getname() {
        return name;
    }

    public String getdepartment() {
        return department;
    }

    public double getunitPrice() {
        return unitPrice;
    }

    public double getunitsInStock() {
        return unitsInStock;
    }

    public void setproductNumber(double productNumber) {
        this.productNumber = productNumber;
    }

    public void setname(String name) {
        this.name = name;
    }

    public void setdepartment(String department) {
        this.department = department;
    }

    public void setunitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public void setunitsInStock(double unitsInStock) {
        this.unitsInStock = unitsInStock;
    }

    public double gettotalInv() {
        return getunitPrice() * getunitsInStock();
    }

    @Override
    public String toString() {
        return "MobilePhone [productNumber=" + productNumber + ", name=" + name
                + ", department=" + department + ", unitsInStock="
                + unitsInStock + ", unitPrice=" + unitPrice + "]";
    }
}

【讨论】:

  • 仍然希望我将类抽象化以实现比较器。这会导致 InventoryPro2 类出现问题,因为它不允许我将 MobilePhone 类中的信息用于数组
  • 我不确定你的意思.. 此代码适用于 jdk-7。创建 AppInventoryPro2 类,将代码复制粘贴到其中并运行。
  • 确实如此。我唯一需要做的就是将它们打印在单独的行上。 \n 我想,但是该代码在哪里可以工作?我还在类名上加上了“实现比较器”,这就是问题所在。不知道不这样做也可以使用它。
  • 1.要在单独的行上打印,作为快速修复将 [return "MobilePhone] 替换为 [return "\n MobilePhone],否则您需要在此处迭代 [System.out.println(Arrays.toString(phones));] 并添加行结束; 2.“实现比较器”也可以,但它允许唯一的比较规则(可以如代码所示覆盖)。
【解决方案2】:

1 - 打印MobilePhone 类的内容:覆盖默认的toString 方法,如下所示:

@Override
public String toString() {
    return "MobilePhone [productNumber=" + productNumber +
    ", name=" + name + ']'; // add more info if needed
}

2 - 允许按名称排序:让 MobilePhone 类实现 Comparable 接口,如 这个:

class MobilePhone implements Comparable {
  ...

    @Override 
    public int compareTo(Object o) {
        MobilePhone m = (MobilePhone) o; 
        return (this.name.compareTo(o.name));
    }
}

编辑:要打印您的 MobilePhone 对象数组,您可以这样做:

System.out.printf("Phones: %s%n", Arrays.toString(phones));

【讨论】:

  • 我是否会使用 for 循环进行打印以使用数组的所有 5 个部分并从每个部分打印一条信息以使 5 部手机打印?
  • @SadNoob:编辑了我的答案以表明这一点。基本上你需要:System.out.printf("Phones: %s%n", Arrays.toString(phones)); 来打印你的 MobilePhone 对象数组的所有元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2016-12-29
  • 2017-12-25
  • 2012-11-27
  • 2021-12-09
  • 1970-01-01
相关资源
最近更新 更多