java 如何更新_如何在java中创建和更新方法?

我需要能够在这部分代码中非常简单地更新数量:

import java.util.HashMap;

import java.util.Map;

public class StockData {

private static class Item {

Item(String n, double p, int q) {

name = n;

price = p;

quantity = q;

}

// get methods

public String getName() { return name; }

public double getPrice() { return price; }

public int getQuantity() { return quantity; }

// instance variables

private String name;

private double price;

private int quantity;

}

// with a Map you use put to insert a key, value pair

// and get(key) to retrieve the value associated with a key

// You don't need to understand how this works!

private static Map stock = new HashMap();

static {

// if you want to have extra stock items, put them in here

// use the same style - keys should be Strings

stock.put("00", new Item("Bath towel", 5.50, 10));

stock.put("11", new Item("Plebney light", 20.00, 5));

stock.put("22", new Item("Gorilla suit", 30.00, 7));

stock.put("33", new Item("Whizz games console", 50.00, 8));

stock.put("44", new Item("Oven", 200.00, 4));

}

public static String getName(String key) {

Item item = stock.get(key);

if (item == null) return null; // null means no such item

else return item.getName();

}

public static double getPrice(String key) {

Item item = stock.get(key);

if (item == null) return -1.0; // negative price means no such item

else return item.getPrice();

}

public static int getQuantity(String key) {

Item item = stock.get(key);

if (item == null) return -1; // negative quantity means no such item

else return item.getQuantity();

}

// update stock levels

// extra is +ve if adding stock

// extra is -ve if selling stock

public static void update(String key, int extra) {

Item item = stock.get(key);

if (item != null) item.quantity += extra;

}

}

我为更新页面构建了GUI,只需要添加方法吗?

很抱歉这个琐碎的问题,但你必须从某个地方开始。

感谢您的帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值