0

什么是最简单的方法来打印这个数组分解成每个手机作为产品编号,名称部门等,然后重新打印按产品名称排序的相同信息。我已经尝试了几种不同的方法,并且已经过了作业的交期,但仍然需要为本周末即将到来的作业弄清楚。当我尝试在 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 和所有这些数字的总和?)我这里是否有不必要的代码,或者到目前为止我是否做对了所有事情? 我不得不说,到目前为止,以在线格式学习这种编码语言并不是一次非常愉快的经历。

4

2 回答 2

0

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));
于 2013-04-17T21:19:44.857 回答
0

这是按名称排序的方法

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 + "]";
    }
}
于 2013-04-17T21:24:28.757 回答