error checking not registering
im sure theres a simple answer to this, but when i set my balance to a
negative number, i want the system to print an error, at the moment, it
just prints the negative number to the console, would anyone be able to
help me as to why this may be happening. Any replies appreciated.
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());
}
}
No comments:
Post a Comment