import java.util.ArrayList;
public class Lan{
private ArrayList<Member> members;
public Lan()
{
members = new ArrayList<Member>();
}
public void addMember(Member newMember){
if(newMember.getBalance() >= 200)
{
this.members.add(newMember);
}
else
{
System.out.println("You dont have enough money to enter");
}
}
public void printMembers(){
System.out.println("People attending");
for(Member member : members)
{
System.out.println(member);
}
}
}
public class Member{
private String name;
private String alias;
private int balance;
public Member(String fullName, String nickname){
this.name = fullName;
this.alias = nickname;
this.balance = 0;
}
public void setBalance(int account)
{
if(account > 0)
{
balance = balance + account;
}
else
{
System.out.println("You have not entered a valid number");
}
}
public String getName()
{
return name;
}
public String getAlias()
{
return alias;
}
public int getBalance()
{
return balance;
}
@Override
public String toString() {
return "Name: " + this.name + " / Alias: " + this.alias;
}
}
这些是我的班级Lan
和Member
,要让这些成员进入局域网,他们需要有200个余额。一切正常,但我似乎无法弄清楚如何在Lan数组中添加(Balance - 200)
&lt; - 输入价格。我是否在Lan的if
声明中执行此操作?或者我是否需要创建一个名为cost
的新字段?
答案 0 :(得分:0)
问题在于,只有在有足够资金进入的情况下,您才希望将余额减少200。您可以通过在正确的if语句中将其余额设置为当前余额 - 200来实现此目的。像这样:
public void addMember(Member newMember){
if(newMember.getBalance() >= 200)
{
this.members.add(newMember);
newMember.setBalance(newMember.getBalance()-200);
}
else
{
System.out.println("You dont have enough money to enter");
}
}