schema.pre('validate', function preValidate(next) {
if (this.status == 'draft') {
// Make all fields optional
_.each(_.keys(schema.paths), function (attr) {
schema.path(attr).required(false);
});
var draftRequiredMap = ['title']; // Based on condition, our required fields are here.
_.each(draftRequiredMap, function (attr) {
schema.path(attr).required(true);
});
}
}
package accounttest;
/**
* Customer --- A class that stores the customer name and customer usage and provides a method, calcUsage,
* to calculate the price to the customer based on the customer usage
*
* @author s0267955, William Rogers
*/
public class Account {
public static String custName; //Stores the customer name
public static int custUsage; //Stores the customers usage
public static int custAccountNumber; //Stores the customers account number
public Account(){ //No use of a constructor
}
public void setName(String name){ //Sets the name of the customer
custName = name;
}
public String getName(){ //Gets the name of the customer
return custName;
}
public void setUsage(int usage){ //Sets the usage for the customer
custUsage = usage;
}
public int getUsage(){ //Gets the usage of the customer
return custUsage;
}
public void setAccountNumber(int accountNumber){//Sets the account number
custAccountNumber = accountNumber;
}
public int getAccountNumber(){ //Gets the account number of the customer
return custAccountNumber;
}
}
结果:
public class AccountTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final int TOTAL_ACCOUNTS = 2; // The total number of accounts
ArrayList<Account> account= new ArrayList<Account>();
for(int x = 0;x<TOTAL_ACCOUNTS;x++){
Account tempAccount = new Account();
Scanner input = new Scanner( System.in );
System.out.print( "Enter the name for Customer #"+(x+1)+": ");
tempAccount.setName(input.nextLine());
System.out.print( "Enter the account number for "+tempAccount.getName()+":");
tempAccount.setAccountNumber(input.nextInt()); //gets the account number for the temp account
System.out.print( "Enter the internet usage for Customer #"+(x+1)+": ");
tempAccount.setUsage(input.nextInt()); //retrieves customer usage for the temp account object
account.add(tempAccount); //Adds the account object to the array
}
for(Account x:account){
System.out.print(x + " ");
System.out.print(x.getName()+", ");
System.out.print(x.getAccountNumber()+", ");
System.out.print(x.getUsage());
System.out.println();
}
}
}
问题:为什么对象会被覆盖?我需要程序能够添加新对象,但所有地址都被最后添加的对象覆盖
答案 0 :(得分:2)
正如 @BoristheSpider 指出的那样,从变量中删除 static 。
public class Account {
public String custName; //Stores the customer name
public int custUsage; //Stores the customers usage
public int custAccountNumber; //Stores the customers account number
...
注意:将您的扫描仪声明为外循环。