【发布时间】:2019-11-28 15:20:34
【问题描述】:
如何使用另一个类中的对象验证一个类中的数组。 我正在为一个钓鱼比赛做项目,我有一个类别允许的物种,但必须在另一个类别中进行捕获。
#MAIN CLASS#
Fish bass = new Fish("Bass", 14, 20);
Fish catfish = new Fish("Catfish", 12, 30);
Fish trout = new Fish("Trout", 15, 12);
Fish walleye = new Fish("Walleye", 10, 15);
fishList.add(bass);
fishList.add(catfish);
fishList.add(trout);
fishList.add(walleye);
##class Fish##
{
//attributes:
String speciesAllowed;
double sizeLimit;
int bagLimit;
//constructors:
Fish()
{
speciesAllowed = " ";
sizeLimit = 0.0;
bagLimit = 0;
}// end constructor w/o argument
Fish(String speciesAllowed, double sizeLimit, int bagLimit)
{
this.speciesAllowed = speciesAllowed;
this.sizeLimit = sizeLimit;
this.bagLimit = bagLimit;
}//end constructor w/ argument
###class Tournament extends Fish###
{
//attributes:
String ssn;
String species;
double size;
int bag;
double weight;
//------------------------------------------------------------------------
//constructors
Tournament()
{
super();
ssn = " ";
species = " ";
size = 0.0;
bag = 0;
weight = 0.0;
}//end constructor w/0 argument
Tournament(String speciesAllowed, double sizeLimit, int bagLimit, String ssn, String species, double
size, int bag, double weight)
{
super(speciesAllowed, sizeLimit, bagLimit);
this.ssn = ssn;
this.species = species;
this.size = size;
this.bag = bag;
this.weight = weight;
}//end constructor w/ argument
//-------------------------------------------------------------------------
void getCatch()
{
String input = " ";
boolean valid = false;
input = JOptionPane.showInputDialog("Enter SSN: ");
valid = checkSSN(input);
while(!valid)
{
input = JOptionPane.showInputDialog("Invalid SSN...Please re-enter SSN: ");
valid = checkSSN(input);
}
ssn = input;
input = JOptionPane.showInputDialog("Enter species of the catch: ");
//valid = checkSpecies(input);
while(!valid)
{
input = JOptionPane.showInputDialog("Sorry that fish is not allowed ");
//valid = checkSpecies(input);
}
species = input;
//engHours = Integer.parseInt(input);
}//end getBOAT()
//-------------------------------------------------------------------------
void dispTournament()
{
JOptionPane.showMessageDialog(null, "SSN : " + ssn + "\n" +
"Species : " + species + "\n" +
"Size : " + size + "\n" +
"Bag : " + bag + "\n" +
"Weight : " + weight + "\n");
}//end dispTournament
//-------------------------------------------------------------------------
boolean checkSSN(String input)
{
if(input.length() != 9 )
return false;
else
for(int i = 0; i < input.length(); i++)
{
if(!Character.isDigit(input.charAt(i)))
return false;
}
return true;
boolean checkSpecies(String input)
{
String allowed1 = "bass";
String allowed2 = "trout";
String allowed3 = "walleye";
String allowed4 = "catfish";
for(int i = 0; i < input.length(); i++)
{
if(input(i).equalsignorecase(<Fish>speciesAllowed))
return false;
}
return true;
}//end checkSpecies
不确定需要多少帮助我的问题与布尔 checkSpecies 相关。我如何将它与 Fish 类中的 speciesAllowed 进行比较。因此,这些是用户可以输入的唯一 Fish。
不,这不是整个程序,这只是我需要帮助的 sn-ps。谢谢。
【问题讨论】:
标签: java string validation arraylist