小编典典

Java打印功能

java

我需要在另一个类的此Java应用程序中编写打印功能的帮助。

这些功能与printAll我认为是正确的,而其他功能肯定是错误的。

public void printAll() {
    Iterator<StockItem> iterator = values();
    while (iterator.hasNext())
        System.out.println(iterator.next().toString());
}

// Prints a directory of all StockItems from the given vendor, 
// in sorted order (ordered by SKU).
public void print(String vendor) {
    Iterator<StockItem> iterator = values();
    if (dictionary.getItem(SKU).getVendor() == vendor)
        System.out.println(tmp.toString());
}

我将在下面写下此问题所需部分的全部功能。

import data_structures.*;
import java.util.Iterator;

public class ProductLookup {


DictionaryADT<String,StockItem> dictionary;
private int maxSize;

public ProductLookup(int maxSize, DictionaryADT<String,StockItem> dictionary) {
    this(maxSize);
    this.dictionary = dictionary;
}

// Constructor.  There is no argument-less constructor, or default size
public ProductLookup(int maxSize) {
    this.maxSize = maxSize;
}

// Adds a new StockItem to the dictionary
public void addItem(String SKU, StockItem item) {
    dictionary.insert(SKU,item);
}

// Returns the StockItem associated with the given SKU, if it is
// in the ProductLookup, null if it is not.
public StockItem getItem(String SKU) {
    if (SKU == null)
        return null;
    return dictionary.getValue(SKU);
}

// Returns the retail price associated with the given SKU value.
// -.01 if the item is not in the dictionary
public float getRetail(String SKU) {
    if (!dictionary.contains(SKU))
        return (float) -.01;
    return getItem(SKU).getRetail();
}

public float getCost(String SKU) {
    if (!dictionary.contains(SKU))
        return (float) -.01;
    return getItem(SKU).getCost();
}

// Returns the description of the item, null if not in the dictionary.
public String getDescription(String SKU) {
    if (!dictionary.contains(SKU))
        return null;
    return getItem(SKU).getDescription();
}

// Deletes the StockItem associated with the SKU if it is
// in the ProductLookup.  Returns true if it was found and
// deleted, otherwise false.  
public boolean deleteItem(String SKU) {
    if (SKU == null)
        return false;
    return dictionary.remove(SKU);
}

// Prints a directory of all StockItems with their associated
// price, in sorted order (ordered by SKU).
public void printAll() {
    Iterator<StockItem> iterator = values();
    while (iterator.hasNext())
        System.out.println(iterator.next().toString());
}

// Prints a directory of all StockItems from the given vendor, 
// in sorted order (ordered by SKU).
public void print(String vendor) {
    Iterator<StockItem> iterator = values();
    if (dictionary.getItem(SKU).getVendor() == vendor)
        System.out.println(tmp.toString());
}

// An iterator of the SKU keys.
public Iterator<String> keys() {
    return dictionary.keys();
}

// An iterator of the StockItem values.    
public Iterator<StockItem> values() {
     return dictionary.values();
}
}

由于实际上没有看到DictionaryADT令人困惑,因此我将其包含在此处。

package data_structures;

import java.util.Iterator;
import java.util.NoSuchElementException;


public interface DictionaryADT<K,V> {

// Returns true if the dictionary has an object identified by
// key in it, otherwise false.
public boolean contains(K key);

// Adds the given key/value pair to the dictionary.  Returns
// false if the dictionary is full, or if the key is a duplicate.
// Returns true if addition succeeded.
public boolean insert(K key, V value);

// Deletes the key/value pair identified by the key parameter.
// Returns true if the key/value pair was found and removed,
// otherwise false.
public boolean remove(K key);

// Returns the value associated with the parameter key.  Returns
// null if the key is not found or the dictionary is empty.
public V getValue(K key);

// Returns the key associated with the parameter value.  Returns
// null if the value is not found in the dictionary.  If more
// than one key exists that matches the given value, returns the
// first one found.
public K getKey(V value);

// Returns the number of key/value pairs currently stored
// in the dictionary
public int size();

// Returns true if the dictionary is at max capacity
public boolean isFull();

// Returns true if the dictionary is empty
public boolean isEmpty();

// Returns the Dictionary object to an empty state.
public void clear();

// Returns an Iterator of the keys in the dictionary, in ascending
// sorted order.  The iterator must be fail-fast.
public Iterator<K> keys();

// Returns an Iterator of the values in the dictionary.  The
// order of the values must match the order of the keys.
// The iterator must be fail-fast.
public Iterator<V> values();
}

阅读 217

收藏
2020-11-30

共1个答案

小编典典

如果DictionaryADT是具有所有实际实现的类,则需要调用

我相信您然后在DictionaryADT中有Map,类似

public Collection<StockItem> values() {
    return dictionary.values(); 
}

要获取密钥,将Iterator更改为Set

public Set<String> keys() {
    return dictionary.keySet(); // return Set, Please perform all the set opetations.
}

我相信这是您想要的。

谢谢,贝内特。

2020-11-30