我在确定哪些方法应该从我的VendingMachine类中取出并放入我的TesterClass来测试我的程序时遇到问题。我试图通过在我的ArrayList中构建Candy对象来测试VendingMachine和Candy Classes,但我不确定如何最好地实现这一点。 Candy对象的ArrayList包含直板字符串和双倍价格。我希望我的测试人员运行自动售货机方法,提示用户提供println方法中列出的问题。
Tester Class
public class Tester {
private static Object candyBarList;
public static void main(String args[])
{
WHAT METHODS SHOULD I PLACE HERE
}
糖果类
public class Candy{
private double price;
private String candyBarName;
Candy()
{
price = 1.50;
candyBarName = "Pluto Bar";
//default constructor
}
Candy(double price1, String str){
price = price1;
candyBarName = str;
//constructor with parameters
}
public double getPrice(){
return price;
}
public void setPrice(double price){
this.price = price;
}
public String getCandyBarName(){
return candyBarName;
}
public void setCandyBarName(String candyBarName){
this.candyBarName = candyBarName;
}
自动售货机类
}
import java.util.ArrayList;
import java.util.Scanner;
import vendingmachine.Candy;
package vendingmachine;
/**
*
* @author admin
*/
public class VendingMachine {
double money = 0.0;
Candy candyBar = new Candy();
ArrayList<Candy> candyBarList = new ArrayList<>();
double cashBox = 0.0;
public VendingMachine(){
candyBarList.add(candyBar);
candyBarList.add(new Candy(2.00, "Snickers Bar"));
candyBarList.add(new Candy(2.00, "Snickers Bar"));
candyBarList.add(new Candy(2.00, "Snickers Bar"));
candyBarList.add(new Candy(2.00, "Snickers Bar"));
candyBarList.add(new Candy(2.00, "Snickers Bar"));
}
/**
* @return
*/
public double paymentIn(){
Scanner input = new Scanner(System.in);
//double payAmount=0.0;
System.out.println("Insert Money: ");
double payAmount = input.nextDouble();
return payAmount;
}
public static boolean isPaymentEnough(double depositMoney, double price){
if (depositMoney >= price)
{
return true;
}
else{
return false;
}
}
public void dispenseCandy (double depositMoney, Candy selectedCandy){
cashBox+= depositMoney - selectedCandy.getPrice();
candyBarList.remove(selectedCandy);
}
public String candySelector(){
for (int i=0; candyBarList.size() >= i; i++){
System.out.println(candyBarList.get(i));
}
Scanner input = new Scanner(System.in);
String candyBarSelected = input.next();
System.out.println("Please type in the Candy Bar you would like");
candyBarSelected = input.next();
return candyBarSelected;
}
public Candy findCandy(){
String candyName = candySelector();
for (int i=0; candyBarList.size() >= i; i++){
if (candyBarList.get(i).getCandyBarName().equals(candyName)){
return candyBarList.get(i);
}
}
System.out.println("Your selection is invalid");
return null;
}