封装
package day01;
public class Acount {
public long number;
private String name;
private double num;
private long password;
static long numbercount=201733;
public Acount(String name,long password,long number) {
this.name = name;
this.password = password;
this.number = number;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getNum() {
return num;
}
public void setNum(double num) {
this.num = num;
}
public long getPassword() {
return password;
}
public void setPassword(long password) {
this.password = password;
}
public static long setnumber() {
numbercount++;
return numbercount;
}
public String toString() {
return "姓名:"+name+"账号:"+number+"金额"+num;
}
}
package day01;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class bank {
public static void main(String[] args) {
List<Acount> bank=new ArrayList<Acount>();
while(true) {
System.out.println("选择你的业务");
System.out.println("按1:添加账户");
System.out.println("按2:查询业务");
System.out.println("按3:消除账户");
System.out.println("按4:退出服务");
Scanner input=new Scanner(System.in);
int i;
i=input.nextInt();
switch(i) {
case 1:
System.out.println("请输入您的用户名称与密码");
String name3=input.next();
long password3=input.nextLong();
Acount e=new Acount(name3,password3,Acount.setnumber());
bank.add(e); break;
case 2:System.out.println("请输入您的用户名称与密码");
String name=input.next();
long password=input.nextLong();
if(inspect(bank,name,password)!=0) {
int i2=inspect(bank,name,password);
i2--;
System.out.println(bank.get(i2).toString());
}
else {
System.out.println("用户不存在");
}
break;
case 3:
System.out.println("请输入您的用户名称与密码");
String name2=input.next();
long password2=input.nextLong();
if(inspect(bank,name2,password2)!=0) {
int i2=inspect(bank,name2,password2);
i2--;
System.out.println(bank.remove(i2)+"消除成功");
}
else {
System.out.println("用户不存在");
}
break;
case 4:System.exit(0);
}
}
}
public static int inspect(List<Acount> bank,String name,long password) {
int j=0;
for(int i=0;i<bank.size();i++) {
if(bank.get(i).getName().equals(name)&&bank.get(i).getPassword()==password) {
j=(i+1);
break;
}
}
return j;
}
}