Java 打印函数

2023-12-25

我需要帮助在另一个类的 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();
}

如果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.
}

我相信这就是您正在寻找的。

谢谢, 班内特.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java 打印函数 的相关文章

  • 如何为最终用户方便地启动Java GUI程序

    用户想要从以下位置启动 Java GUI 应用程序Windows 以及一些额外的 JVM 参数 例如 javaw Djava util logging config file logging properties jar MyGUI jar
  • Java Swing:从 JOptionPane 获取文本值

    我想创建一个用于 POS 系统的新窗口 用户输入的是客户拥有的金额 并且窗口必须显示兑换金额 我是新来的JOptionPane功能 我一直在使用JAVAFX并且它是不同的 这是我的代码 public static void main Str
  • 如何默认将 Maven 插件附加到阶段?

    我有一个 Maven 插件应该在编译阶段运行 所以在项目中consumes我的插件 我必须做这样的事情
  • 为什么 i++ 不是原子的?

    Why is i Java 中不是原子的 为了更深入地了解 Java 我尝试计算线程中循环的执行频率 所以我用了一个 private static int total 0 在主课中 我有两个线程 主题 1 打印System out prin
  • 如何找到给定字符串的最长重复子串

    我是java新手 我被分配寻找字符串的最长子字符串 我在网上研究 似乎解决这个问题的好方法是实现后缀树 请告诉我如何做到这一点或者您是否有任何其他解决方案 请记住 这应该是在 Java 知识水平较低的情况下完成的 提前致谢 附 测试仪字符串
  • 给定两个 SSH2 密钥,我如何检查它们是否属于 Java 中的同一密钥对?

    我正在尝试找到一种方法来验证两个 SSH2 密钥 一个私有密钥和一个公共密钥 是否属于同一密钥对 我用过JSch http www jcraft com jsch 用于加载和解析私钥 更新 可以显示如何从私钥 SSH2 RSA 重新生成公钥
  • 使用 Android 发送 HTTP Post 请求

    我一直在尝试从 SO 和其他网站上的大量示例中学习 但我无法弄清楚为什么我编写的示例不起作用 我正在构建一个小型概念验证应用程序 它可以识别语音并将其 文本 作为 POST 请求发送到 node js 服务器 我已确认语音识别有效 并且服务
  • Spring Data JPA 应用排序、分页以及 where 子句

    我目前正在使用 Spring JPA 并利用此处所述的排序和分页 如何通过Spring data JPA通过排序和可分页查询数据 https stackoverflow com questions 10527124 how to query
  • 如何在PreferenceActivity中添加工具栏

    我已经使用首选项创建了应用程序设置 但我注意到 我的 PreferenceActivity 中没有工具栏 如何将工具栏添加到我的 PreferenceActivity 中 My code 我的 pref xml
  • 十进制到八进制的转换[重复]

    这个问题在这里已经有答案了 可能的重复 十进制转换错误 https stackoverflow com questions 13142977 decimal conversion error 我正在为一个类编写一个程序 并且在计算如何将八进
  • getResourceAsStream() 可以找到 jar 文件之外的文件吗?

    我正在开发一个应用程序 该应用程序使用一个加载配置文件的库 InputStream in getClass getResourceAsStream resource 然后我的应用程序打包在一个 jar文件 如果resource是在里面 ja
  • 在 Mac 上正确运行基于 SWT 的跨平台 jar

    我一直致力于一个基于 SWT 的项目 该项目旨在部署为 Java Web Start 从而可以在多个平台上使用 到目前为止 我已经成功解决了由于 SWT 依赖的系统特定库而出现的导出问题 请参阅相关thread https stackove
  • Eclipse Java 远程调试器通过 VPN 速度极慢

    我有时被迫离开办公室工作 这意味着我需要通过 VPN 进入我的实验室 我注意到在这种情况下使用 Eclipse 进行远程调试速度非常慢 速度慢到调试器需要 5 7 分钟才能连接到远程 jvm 连接后 每次单步执行断点 行可能需要 20 30
  • 如何从泛型类调用静态方法?

    我有一个包含静态创建方法的类 public class TestClass public static
  • 声明的包“”与预期的包不匹配

    我可以编译并运行我的代码 但 VSCode 中始终显示错误 早些时候有一个弹出窗口 我不记得是什么了 我点击了 全局应用 从那以后一直是这样 Output is there but so is the error The declared
  • 获取 JVM 上所有引导类的列表?

    有一种方法叫做findBootstrapClass对于一个类加载器 如果它是引导的 则返回一个类 有没有办法找到类已经加载了 您可以尝试首先通过例如获取引导类加载器呼叫 ClassLoader bootstrapLoader ClassLo
  • 编译器抱怨“缺少返回语句”,即使不可能达到缺少返回语句的条件

    在下面的方法中 编译器抱怨缺少退货声明即使该方法只有一条路径 并且它包含一个return陈述 抑制错误需要另一个return陈述 public int foo if true return 5 鉴于Java编译器可以识别无限循环 https
  • JGit 检查分支是否已签出

    我正在使用 JGit 开发一个项目 我设法删除了一个分支 但我还想检查该分支是否已签出 我发现了一个变量CheckoutCommand但它是私有的 private boolean isCheckoutIndex return startCo
  • 如何修复 JNLP 应用程序中的“缺少代码库、权限和应用程序名称清单属性”?

    随着最近的 Java 更新 许多人都遇到了缺少 Java Web Start 应用程序的问题Codebase Permissions and Application name体现属性 尽管有资源可以帮助您完成此任务 但我找不到任何资源综合的
  • Spring Boot @ConfigurationProperties 不从环境中检索属性

    我正在使用 Spring Boot 1 2 1 并尝试创建一个 ConfigurationProperties带有验证的bean 如下所示 package com sampleapp import java net URL import j

随机推荐

  • Python 看门狗重复事件

    我创建了一个修改后的看门狗示例 以便监视已添加到 Windows 中特定目录的 jpg 照片文件 import time from watchdog observers import Observer from watchdog event
  • 如何从 Windows 设置环境变量

    在 windows xp 7 8 vista 10 等中添加环境变量的方法是什么 在 Windows 7 8 Vista 或 XP 中 在桌面或开始菜单中找到 我的电脑 图标 右键单击它 然后从菜单中选择 属性 项 当您看到属性对话框时 单
  • 在 Rails 中渲染 JSON 时包含关联模型

    现在我有这一行 render json programs except gt created at updated at 但是 由于程序属于公司 我想显示公司名称而不是公司 ID 渲染节目时如何包含公司名称 像这样的东西应该有效 rende
  • 如何通过 git pre-receive hook 验证用户身份

    我想写一个pre receivePython 中的 githook 据我了解 没有任何参数被传入pre receive脚本 而不是每个引用都是使用标准输入在单独的行上传递的 我已经能够通过以下方式阅读参考更改 usr bin env pyt
  • 如何创建具有静态返回类型的扩展方法?

    我试图编写一个简单的扩展方法Color返回该颜色的黑色和白色等效值的静态类 问题是扩展方法无法返回Static类型 那么 我该怎么做呢 请帮我 问题是没有方法可以返回静态类型 静态类是无状态的 或仅具有静态状态 因此只有一个 实例 可以从引
  • 对数据库中的所有表启用更改跟踪

    假设在 SQL Server 数据库上启用了更改跟踪 如何在数据库中的所有表上启用更改跟踪 您可以使用以下 T SQL 脚本生成另一个 T SQL 脚本 该脚本启用CHANGE TRACKING所有具有主键的表的功能 Step 1 Exec
  • 如何区分 jQuery 选择器字符串和其他字符串

    我想检查字符串的 类型 特别是 如何区分 jQuery 选择器字符串和其他字符串 也就是说 下面的代码中selectorTest应该如何实现呢 var stringType function value var htmlExpr lt lt
  • C++:#pragma comment(lib, "XXX") 实际上对“XXX”做什么?

    我的背景是 C 但我必须保留一些遗留的 MS C 在那个代码库中我偶然发现 pragma comment lib OtherLib700 lib 其中 700 是一些版本控制 除此之外 该库还有一个同名的 DLL 我首先认为该程序将依赖于
  • 如何从 OHLC 数据计算枢轴值

    我有一个带有 open high low close 和 key 列的 pandas 数据集 现在我想按键对数据集进行分组 并使用公式 最高价 最低价 收盘价 3 计算枢轴 到目前为止我可以做到 但要求是将计算的数据转移到下一组 我无法编码
  • pthread 中的信号处理

    我创建了一个 pthread 并在其中安装了一个信号处理程序 与我们在中所做的方式相同main 功能 线程的信号处理程序是一个单独的函数 令人惊讶的是 它不起作用 即线程的信号处理程序无法捕获信号 这是代码 include
  • NSNumberFormatterStyle.SpellOutStyle 中的错误?

    当我将 NSNumberFormatterStyle SpellOutStyle 用于较大的数字 不是溢出类型的数字 时 它似乎会在四万亿中的某个地方崩溃 let formatter NSNumberFormatter formatter
  • 为什么MySQL在违反唯一键约束时会出现自动增量间隙,但违反主键约束时却不会?

    我明白当使用INSERT ON DUPLICATE KEY UPDATE在 MySQL 中 当插入失败时 会出现自动增量间隙 然而 我注意到 只有在违反唯一键约束时才会出现间隙 如果主键约束失败 则不会出现自增间隙 两者差异的原因是什么 T
  • 当我尝试从表单 A 显示表单 B 时,为什么编译器会说“未声明的标识符”?

    为什么此代码不起作用 procedure TFormNotification Button3Click Sender TObject begin FormB Show end 我越来越未声明的标识符 error 您可能有一个名为的全局变量F
  • django {% if user.groups == 'FK' %} 不起作用[重复]

    这个问题在这里已经有答案了 我正在使用 django 制作一个网站 if user groups FC 在我的模板中不起作用 我有这样的组 For example one of my users username is hong belon
  • 如果查询中没有占位符/动态数据,您可以省略 PDO 准备吗?

    我目前正在开发一个将 PDO 与 MySQL 数据库结合使用的应用程序 我看到一些查询 它们非常简单SELECT声明 例如 SELECT FROM table ORDER BY name ASC 代码确实not use prepare 例如
  • 如何在 Protractor 中执行测试之前恢复数据库

    我在 Protractor 中编写了 E2E 测试 它使用节点通过 webdriver 运行 现在我有一些插入测试 它将插入数据并创建用户 现在 如果我第一次运行该案例 它将通过 但是当我重新运行测试时 它将失败 因为它已经存在 预期 当
  • Azure 可用性集和规模集的差异

    有人可以在 ARM 门户中定义两者之间的区别吗 另外 如果我需要将新的 Azure RM VM 添加到现有 AS 可用性集 PowerShell 会是什么 谢谢 普拉布 可用性集由一组离散的 VM 组成 这些 VM 具有自己的名称和各自的属
  • PowerMock + Robolectric + Dagger2。第一部分

    这个问题是从第一部分创建的PowerMock Robolectric Dagger2 https stackoverflow com questions 34689722 powermock robolectric dagger2 所以我又
  • 如何使用 MonthCalender 在文本框中插入日期?

    我有一个textBox1在我的Windows窗体中 我想用它来从用户那里获取日期 我想展示MonthCalender1一旦用户将光标放在textbox1然后在里面设置日期textbox1自动 然后日历就会消失 我如何使用 C 或 C CLI
  • Java 打印函数

    我需要帮助在另一个类的 java 应用程序中编写打印函数 printAll 的函数我认为是正确的 而其他函数肯定是错误的 public void printAll Iterator