java - 从数组中减去不起作用

标签 java arrays oop object while-loop

我的代码执行初始 if 语句

if(choice.equalsIgnoreCase("Coke")){
    itemid = 1;
    sp.setItemnum(1);
}
else if(choice.equalsIgnoreCase("Water")){
    itemid = 2;
    sp.setItemnum(2);
}
else if(choice.equalsIgnoreCase("Gatorade")){
    itemid = 3;
    sp.setItemnum(3);
}
else if(choice.equalsIgnoreCase("Sprite")){
    itemid = 0;
    sp.setItemnum(0);
}

完成 while 循环后,从库存中减去 1

if(sp.isLeft(itemid)){
    double f = sp.getItemPrice(itemid);
    Customer ct = new Customer(f);

    Scanner sv = new Scanner(System.in);
    System.out.println("Your total is: $" + f +"\nPlease insert money (type amount of money to pay.)");
    double t = sv.nextDouble();
    ct.markPayment(t);
    boolean paid = ct.checkIfPaid();

    while(paid != true){
        System.out.println("Please pay " + ct.getPayment() + " more");
        double k = sv.nextDouble();
        ct.markPayment(k);
        paid = ct.checkIfPaid();
    }
    sp.reduceStock(itemid);

由于某种原因,这并没有减去我的数组的索引。

public void reduceStock(int itemn){
    stock[itemn] -= 1;
}

这是我要从中减去的数组

private int[] stock = {2, 4, 0, 2};

这是我的“isLeft”方法

public boolean isLeft(int itemnum){
    boolean i;
    if(stock[this.itemnum] > 0){
        i = true;
    }
    else{
        i = false;
        System.out.println("We're restocking this item. Please come another time");
        setStock(itemnum);
    }
    return i;
}

这是所有代码

Controller

import java.util.Scanner;
public class Controller {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Supplier sp = new Supplier();
    Vending vd = new Vending();
    int itemid = 4;
    System.out.println("I am a vending machine. What would you like? \nHere are your choices:");
    sp.getItems();
    String choice = sc.nextLine();
    if(choice.equalsIgnoreCase("Coke")){
        itemid = 1;
        sp.setItemnum(1);

    }
    else if(choice.equalsIgnoreCase("Water")){
        itemid = 2;
        sp.setItemnum(2);

    }
    else if(choice.equalsIgnoreCase("Gatorade")){
        itemid = 3;
        sp.setItemnum(3);
    }
    else if(choice.equalsIgnoreCase("Sprite")){
        itemid = 0;
        sp.setItemnum(0);

    }
    else{
        System.out.println("I didn't get that. Please try again");
        main(args);
    }

    if(sp.isLeft(itemid)){
        double f = sp.getItemPrice(itemid);
        Customer ct = new Customer(f);

        Scanner sv = new Scanner(System.in);
        System.out.println("Your total is: $" + f +"\nPlease insert money (type amount of money to pay.)");
        double t = sv.nextDouble();
        ct.markPayment(t);
        boolean paid = ct.checkIfPaid();

        while(paid != true){
            System.out.println("Please pay " + ct.getPayment() + " more");
            double k = sv.nextDouble();
            ct.markPayment(k);
            paid = ct.checkIfPaid();
        }
        sp.reduceStock(itemid);
    }
    else{
        System.out.println("Sorry! that item is out of stock. Please chose another item.\n-------------------------------------------\n");
        vd.addStock(itemid);
        main(args);   

    }
    main(args);





   }


  }

供应商

   import java.util.ArrayList;
 public class Supplier {
private int[] stock = {2, 4, 0, 2};
private double[] price = {2.00, 2.00, 1.00, 2.5};
private String[] items = {"Sprite", "Coke", "Water", "Gatorade"};
int itemnum;
public void setItemnum(int num){
    itemnum = num;
}

public Supplier(){

}
public Supplier(String[] itemStock, String[] itemPrices){

}
public int getItemStock(int c){
    return stock[c];
}
public double getItemPrice(int i){
    return price[i];
}
public void getItems(){
    for(int i = 0; i < items.length; i++){
        System.out.println(items[i] + " ");
    }
}
public int getStock(int d){
    return stock[d];
}
public void setStock(int f){
    stock[f] = 5;
}
public void reduceStock(int itemn){
    stock[itemn] -= 1;
}
public int getNum(){
    return itemnum;
}
public boolean isLeft(int itemnum){
    boolean i;
    if(stock[itemnum] > 0){
        i = true;
    }
    else{
        i = false;
        System.out.println("We're restocking this item. Please come another time");
        setStock(itemnum);
    }
    return i;
}

}

客户

public class Customer {
private double payment;
private boolean paid;

public Customer(){
}
public Customer(double f){
    payment = f;
    // Setting the payment equal to the amount needed to pay
}


public void markPayment(double i){
    payment = payment - i;
}
boolean temp1;


public boolean checkIfPaid(){
    if(payment <= 0.0){
        System.out.println("Here you go. Enjoy");
        temp1 = true;
    }
    else{
        temp1 = false;
    }
    return temp1;
}
// make another check if paid to use!
public double getPayment(){
    return payment;
}

}

自动售货机

public class Vending {
  Supplier sp = new Supplier();
public Vending(){
}

public boolean isLeft(int itemnum){
    boolean i;
    if(sp.getItemStock(itemnum) > 0){
        i = true;
    }
    else{
        i = false;
        System.out.println("We're restocking this item. Please come another time");
        sp.setStock(itemnum);
    }
    return i;
}
public void addStock(int item){
    sp.setStock(item);
}

}

最佳答案

// int itemnum is a local variable:
public boolean isLeft(int itemnum){
    boolean i;
    // but you use this.itemnum instead:
    if(stock[this.itemnum] > 0){

只需删除“这个”即可。

除了这3行还需要有static:

public class Supplier {
    static private int[] stock = {2, 4, 0, 2};
    static private double[] price = {2.00, 2.00, 1.00, 2.5};
    static private String[] items = {"Sprite", "Coke", "Water", "Gatorade"};

关于java - 从数组中减去不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35142740/

相关文章:

java - 无法修复 java.lang.ArrayIndexOutOfBoundsException

php类函数包装器

javascript - Javascript 中的 Array.prototype.reverse 和 Array.reverse 有什么区别?

java - 在Java中,当一个接口(interface) "extends"另一个接口(interface)时到底会发生什么?

java - 无法使用 wget 下载 jdk-7u71-linux-x64.rpm,尝试了论坛中的几乎所有内容

javascript - 为什么用变量调用数组索引是不好的做法?

java - 使用 Flying Saucer 解析 protected 资源(ITextRenderer)

C++ - 静态数组的性能,启动时大小可变

javamail 不工作。出现消息异常

java - 如何在 Spring 的过滤器中排除特定 URL?