简体   繁体   English

@override to.String仅输出默认构造函数java hw

[英]@override to.String printing out only default constructor java hw

At the end of my for loop, Id like to print out all the objects in the array. 在我的for循环结束时,我想打印出数组中的所有对象。 I used a generate toString with string builder from Source, however, after the loop is done executing, it prints out the default values of variables Item: 我在Source中使用了带有字符串生成器的generate toString,但是,在执行完循环后,它会打印出变量Item的默认值:

[Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0], Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0], Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0], Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0], Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0], Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0], null] [项目[getPrice()= 0.0,getName()=还没有名称。,getPriority()=-1.0],项目[getPrice()= 0.0,getName()=还没有名称。,getPriority()=-1.0] ,项目[getPrice()= 0.0,getName()=还没有名称。,getPriority()=-1.0],项目[getPrice()= 0.0,getName()=还没有名称。,getPriority()=-1.0] ,项目[getPrice()= 0.0,getName()=还没有名称。,getPriority()=-1.0],项目[getPrice()= 0.0,getName()=还没有名称。,getPriority()=-1.0] , 空值]

heres my code 这是我的代码

  public class Item {


static Item list[]=new Item [7];
public static  int x = 0;
public static  String setName;
public static double setPrice;
public static int setPrioirty;


private  int priority=-1;
private double price;
private String name;



Item(){

    priority=-1;
    price=0;
    name="No Name yet.";


}// default constructor. 


public Item(int i, double j, String k) {
    setItem(i,j,k);                         //constructor with 3 arguments. 
}

public void setItem (int i, double j, String k){    // setting item with 3 attributes.
    setPriority(i);
    setPrice(j);
    setName(k); 
}

public void setName(String k) { //setting individual attributes in item.

    // TODO Auto-generated method stub //page 378
    name=k;

}


public void setPrice(double j) {//setting individual attributes in item.
    // TODO Auto-generated method stub
    if (j<0||j>100){
        System.out.println("Error: price is too low or high");

    }

    else
        price=j;

    }

public void setPriority(int i) {//setting individual attributes in item.
    // TODO Auto-generated method stub
    priority =((i>=0&&i<7)?i:0);

}


public double getPrice(){
    return price;

}
public String getName(){

    return name;

}
public double getPriority(){
    return priority;

}


 public static void add(Item itemObject) {


    if (x<7)
    {
        list[x]=itemObject;
    System.out.println("Item added at index " + x);

    x++;


    }


 }


@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Item [getPrice()=").append(getPrice()).append(", ");
    if (getName() != null)
        builder.append("getName()=").append(getName()).append(", ");
    builder.append("getPriority()=").append(getPriority()).append("]");
    return builder.toString();
}

   }

main 主要

       import java.util.Arrays;
       import java.util.Scanner;
       import java.util.Set;


     public class homework3main extends Item {



@SuppressWarnings("static-access")
public static void main(String[] args) {

    //item list[]=new item [7]; // array of objects
    Scanner keyboard= new Scanner(System.in);
    for(int x=1; x<7;x++){

        Item itemObject=new Item ();
        //Item itemObject=new Item (setPrioirty,setPrice,setName);
        //creating new object with 3 variables, name, price, priority

        //list[x]=new Item();// is this right?
        System.out.println("Enter an item you want to add to your list "+ x);
        list[x].setName=keyboard.next();

        System.out.println("Enter a price "+x);
        list[x].setPrice=keyboard.nextDouble();

        System.out.println("Enter the priority of the item "+x);
        list[x].setPrioirty=keyboard.nextInt();

        //item itemObject=new item (setPrioirty,setPrice,setName);

        list[x].add(itemObject);

    }   
    System.out.println(Arrays.toString(list));

My conditional statements dont work either in my Set methods. 我的条件语句在我的Set方法中都不起作用。 Cant understand why those dont work, they are pretty straight forward. 不能理解为什么这些都不起作用,他们很简单。

you appear to have several structural issues with the code so here is what i think it should be: 您似乎在代码中存在几个结构性问题,所以我认为这应该是:

import java.util.Arrays;
import java.util.Scanner;

public class Item {
    //the properties of an Item

    private int priority;
    private String name;
    private double price;

    //default constructer
    public Item() {
        priority = -1;   //fill with default values
        price = 0.0;
        name = "No name yet";
    }
    //constructer with all fields given

    public Item(int priority, String name, double price) {
        this.priority = priority; //there are two instances of each variable
        this.name = name;         // use 'this.' to distinguish them
        this.price = price;
    }
    // all getters simply will return the corresponding field

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        //priority must be between 0 and 7
        if (priority >= 0 && priority <= 7) {
            this.priority = priority;
        } else {
            //otherwise default to 0
            this.priority = 0;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        //no constraints on the name so simply assign it
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        //price between 0 and 100 inclusive
        if (price >= 0) {
            if (price <= 100) {
                this.price = price;
            } else {
                //use System.err for errors
                // used nested ifs so you can tell if price is to high or low 
                //otherwise it is a bit ambiguous
                System.err.println("Error: price to high");
            }
        } else {
            System.err.println("Error: price to low");
        }
    }
    //your tostring is fine
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Item [getPrice()=").append(getPrice()).append(", ");
        if (getName() != null) {
            builder.append("getName()=").append(getName()).append(", ");
        }
        builder.append("getPriority()=").append(getPriority()).append("]");
        return builder.toString();
    }

    //just put your main here
    //if you can't then put it in a class but don't sub-class this class
    public static void main(String[] args) {
        //put your list declaration here
        //it doesn't quitemake sense for the Item calss to have a field
        //called list in this instance
        Item[] list = new Item[7];
        Scanner keyboard = new Scanner(System.in);
        //i is the most commonly used variable for 'for' loops
        for (int i = 1; i <= list.length; i++) {
            //create a new item
            Item anItem = new Item();
            //call your methods on that object to set its fields
            System.out.println("Enter an item you want to add to your list " + i);
            anItem.setName(keyboard.next());

            System.out.println("Enter a price " + i);
            anItem.setPrice(keyboard.nextDouble());

            System.out.println("Enter the priority of the item " + i);
            anItem.setPriority(keyboard.nextInt());

            //use the var i for the position
            //remember to subtract 1 since arrays start at 0 but i starts at 1
            list[i-1] = anItem;
        }
        System.out.println(Arrays.toString(list));
    }
}

In your condition 在你的情况下

j < 0 && j > 100

How can j both be smaller than 0 and greater than 100 ? j如何既小于0又大于100 You need || 您需要|| .

In your methods 用你的方法

System.out.println("Enter an item you want to add to your list "+ x);
list[x].setName=keyboard.next();

System.out.println("Enter a price "+x);
list[x].setPrice=keyboard.nextDouble();

System.out.println("Enter the priority of the item "+x);
list[x].setPrioirty=keyboard.nextInt();

you are setting the static fields of the Item class, not the fields of the instance. 您正在设置Item类的static字段,而不是实例的字段。 Either use the setters you have or use the constructor. 使用您拥有的二传手或使用构造函数。 For example 例如

Item itemObject = new Item ();
System.out.println("Enter an item you want to add to your list "+ x);
itemObject.setName(keyboard.next());

System.out.println("Enter a price "+x);
itemObject.setPrice(keyboard.nextDouble());

System.out.println("Enter the priority of the item "+x);
itemObject.setPriority(keyboard.nextInt());

list[x] = itemObject;

You're completely overusing setters by the way. 顺便说一句,您完全滥用了二传手。 Go through this tutorial. 阅读本教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM