我确定对此有一个简单的答案,但是当我将余额设置为负数时,我希望系统打印一个错误,目前,它只是将负数打印到控制台,有人可以帮助我吗至于为什么会发生这种情况。任何答复表示赞赏。
package Assignment1;
public class Customer
{
private String name;
private String address;
private int balance;
public Customer(String nameParam, String addressParam, int balanceParam)
{
name = nameParam;
address = addressParam;
balance = balanceParam;
}
public void setName(String nameParam)
{
name = nameParam;
}
public String getName()
{
return name;
}
public void setAddress(String addressParam)
{
address = addressParam;
}
public String getAddress()
{
return address;
}
//I want the system to print an error should the balanceParam be less than 0
public void setBalance(int balanceParam)
{
if (balanceParam >= 0)
{
balance = balanceParam;
}
else
{
System.out.println("Error: Invalid balance");
}
}
public int getBalance()
{
return balance;
}
public static void main(String[] args)
{
Customer customer1 = new Customer("Tom", "High Street", 100);
Customer customer2 = new Customer("Mary", "Low Street", 110);
//The balance for customer3 is set to a negative number
Customer customer3 = new Customer("John", "Middle Street", -10);
System.out.print(customer1.getName() + "\t");
System.out.print(customer1.getAddress()+ "\t");
System.out.println(customer1.getBalance());
System.out.print(customer2.getName()+ "\t");
System.out.print(customer2.getAddress()+ "\t");
System.out.println(customer2.getBalance());
System.out.print(customer3.getName()+ "\t");
System.out.print(customer3.getAddress()+ "\t");
System.out.println(customer3.getBalance());
}
}