为何Customer.java中ATM的getServiceStatus()返回值为false?
调试问题记录
在Customer.java中查询ATM服务状态的返回值为false,目前其他功能运行正常,已在该问题上调试许久仍未找到原因。
相关业务代码
ATM.java
public class ATM { int cash; boolean inService; public ATM() { cash = 0; inService = false; } public ATM(int x, boolean y) { cash = x; inService = y; } public int queryCash() { return cash; } public void increaseCash(int x) { cash = cash + x; } public void reduceCash(int x) { cash = cash - x; } public boolean getServiceStatus() { return inService; } public void changeServiceStatus() { if (inService) { inService = false; System.out.println("inService is now false"); } else { System.out.println("inService is now true"); } } public class CashDispenser { public void dispenseCash(int x) { reduceCash(x); System.out.println(x + " dollars has been dispensed."); } } CashDispenser dispenser = new CashDispenser(); public class ReceiptPrinter { public void printReceipt() { System.out.println("Receipt has been printed."); } } ReceiptPrinter printer = new ReceiptPrinter(); public class CardReader{ public void readCard() { System.out.println("Card has been read."); } } CardReader reader = new CardReader(); public class KeypadDisplay{ public void displayPINverification() { System.out.println("PIN has been verified."); } } KeypadDisplay display = new KeypadDisplay(); }
Person.java
public class Person { String name; public Person(){ name = "default"; } public String getName() { return name; } public void setName(String nameString) { name = nameString; System.out.println("The name is set to "+ name); } }
Operator.java
public class Operator extends Person { public void topUpATM(ATM atm) { System.out.println("Current inService is "+ atm.inService); System.out.println("Current cash is "+ atm.cash); if (atm.getServiceStatus() == true){ atm.changeServiceStatus(); System.out.println("ATM now has "+ atm.queryCash()+" dollars."); atm.changeServiceStatus(); } if (atm.queryCash() < 5000){ atm.increaseCash(5000); System.out.println("ATM now" + atm.queryCash()+ " dollars."); atm.changeServiceStatus(); } } }
Customer.java
问题描述:不清楚代码哪里出了问题,按逻辑此处getServiceStatus()应该返回true,但实际返回的是false。目前其他功能都能正常运行,已经在这个问题上卡了很久,始终找不到问题原因。
public class Customer extends Person { public void withdrawCash(ATM atm, int amount) { //atm.changeServiceStatus(); System.out.println("Current inService is "+atm.getServiceStatus()); if (!atm.getServiceStatus()) { System.out.println("ATM is not in service."); } else if (atm.queryCash()<amount) { System.out.println("ATM has insufficient cash"); } else { atm.reader.readCard(); atm.display.displayPINverification(); atm.dispenser.dispenseCash(amount); atm.printer.printReceipt(); System.out.println(amount+" successfully withdrawn from ATM"); } } }
A00.java(主程序入口)
import java.util.Scanner; public class A00 { public static void main (String [] args) { Scanner input = new Scanner(System.in); int Number; System.out.println("Please enter between 0 and 10,000"); Number = input.nextInt(); ATM atm_1 = new ATM (0,false); ATM atm_2 = new ATM (Number,true); input.nextLine(); System.out.println("Please enter a name for the operator"); String name = input.nextLine(); Operator operate = new Operator(); operate.setName(name); System.out.println("Processing ATM 1"); operate.topUpATM(atm_1); System.out.println("Processing ATM 2"); operate.topUpATM(atm_2); Customer cust = new Customer(); String customer; System.out.println("Enter the name of a customer:"); customer = input.nextLine(); cust.setName(customer); System.out.println("Please enter the amount you want to withdraw: "); int withdraw; withdraw = input.nextInt(); cust.withdrawCash(atm_1, withdraw); input.close(); } }
内容的提问来源于stack exchange,提问作者Nisarg Shah
相关产品推荐
相关产品推荐

