这是一个面向对象的程序,它有两个类Catalog和Products,它们读取包含产品名称,数量和价格的文件,将它们添加到列表数组中,并计算项目的总价格。一切都适用于我的程序,除了它有一个toString方法,当我运行程序时,它打印像这样的东西@ 445ea7e我认为这是因为Catalog类上的addProducts()和getProducts(String name)必须做的事情与toString(),我不知道如何将它们连接起来工作。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Catalog{
private static int MAX_ITEMS = 10;
/**
* List of Products objects.
*/
private Products[] list;
private int nextItem;
/**
* Default constructor
*/
public Catalog(){
list = new Products[MAX_ITEMS];
nextItem = 0;
}
/**
* Adds an item to the list
*/
public void addProducts (String name, int quantity, double price){
**//I tried this: list[nextItem] = new Products(name, quantity, price);
//but I'm not sure if that's ok.**
}
/**
* Reads items from an input file.
*/
public void loadList(String fileName)
throws FileNotFoundException {
if ( (fileName != null) && (!fileName.equals("")) ) {
Scanner input = new Scanner(new File(fileName));
String newLine = null;
String name = null;
int quantity = 0;
double price = 0.0;
while (input.hasNextLine() && nextItem < MAX_ITEMS) {
if (input.hasNext()) {
name = input.next();
} else {
System.err.println("ERROR Not a String");
System.exit(2);
}
if (input.hasNextInt()) {
quantity = input.nextInt();
} else {
System.err.println("ERROR Not an integer");
System.exit(2);
}
if (input.hasNextDouble()) {
price = input.nextDouble();
} else {
System.err.println("ERROR Not a double");
System.exit(2);
}
list[nextItem] = new Products(name, quantity, price);
newLine = input.nextLine();
nextItem += 1;
}
}
return;
}
/**
* Calculate the total cost of products.
*/
public double getTotalPrice(){
double total = 0.0;
for(int i=0; i < nextItem; i++){
Products products = list[i];
total+=products.getTotalPrice();
}
return total;
}
/**
* Search Catalog items with a product name
*/
public Products getProducts(String name){
**//search a list for string equality using the name of the product and returns it to the caller**
}
public static void main(String[] args)
throws FileNotFoundException {
Catalog catalog= new Catalog();
catalog.loadList(args[0]);
System.out.println();
//System.out.println(toString()); **I don't know how to call toString**
System.out.println();
System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice());
}
}
这是带有toString()方法的Products类。
public class Products {
private String name;
private int quantity;
private double price;
/**
* Constructor.
*/
public Products(String name, int quantity, double price){
this.name = name;
this.quantity = quantity;
this.price = price;
}
/**
* Gets name of the product.
*/
public String getName(){
return this.name;
}
/**
* Gets the quantity of products.
*/
public int getQuantity(){
return this.quantity;
}
/**
* Gets the cost per product.
*/
public double getPrice(){
return price;
}
/**
* set quantity of the products.
*/
public void setQuantity(int quantity){
this.quantity=quantity;
}
/**
* Calculate the total price.
*/
public double getTotalPrice(){
return quantity * price;
}
/**
* Returns a spaced-separated list of the attributes.
*/
public String toString(){
String s=null;
s+=getName();
s+=s + " ";
s+=getQuantity();
s+=s + " ";
s+=getPrice();
return s;
}
} 这是包含产品信息的文件
足球2 15.50
ChapStick 1 3.87
第4册10.00
答案 0 :(得分:0)
在toString()方法中(在产品类中),更改
String s = null;
到
String s = "";
此外,您必须为Catalog类创建一个toString方法才能调用它。
目录类的toString方法示例可以是:
public String toString(){
String toReturn = "";
for(Products current: list){
toReturn+=current.toString()+"\n";
}
return toReturn;
}
然后如果main只是调用,System.out.println(catalog.toString());
答案 1 :(得分:0)
您将toString()
作为Catalog
课程添加Product
:
public class Catalog {
private static int MAX_ITEMS = 10;
/**
* List of Products objects.
*/
private Products[] list;
private int nextItem;
// your old code here
// new code
@Override
public String toString() {
return "this is a catalog";
}
}
您致电catalog.toString()
:
public static void main(String[] args) throws FileNotFoundException { Catalog catalog= new Catalog(); catalog.loadList(args[0]); System.out.println(); //System.out.println(toString()); **I don't know how to call toString** // use this line System.out.println(catalog.toString()); System.out.println(); System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice()); }
toString()
课程的Product
方法中。您应该将初始化变量从s=null;
更改为s="";
。这是样本:public String toString(){ String s=""; s+=getName(); s+=s + " "; s+=getQuantity(); s+=s + " "; s+=getPrice(); return s; }
希望这有帮助:)
答案 2 :(得分:0)
尝试向您的toString方法添加覆盖,这可能是您当前输出的解决方案。
@Override
public String toString(){
String s=null;
s+=getName();
s+=s + " ";
s+=getQuantity();
s+=s + " ";
s+=getPrice();
return s;
}