怎样用Java创建银行账号Account类,实现存款(balance)的存(deposit)、取(withdraw)和查询(getbalan

如题所述

我也刚开始学,这是我自己写的
import java.util.Scanner;

/**
* 实现取款机功能,存款balance、存钱deposit、取withdraw、查询getbalan
*
*/

public class Account {

public static void main(String[] args) {

double balance = 0;
double deposit = 0;
double withdraw = 0;

for (;;) {
System.out.println("请输入数字:\t1.存钱 \t2.取钱\t3.查询余额\n");
Scanner input = new Scanner(System.in);
String num = input.next();
double s1 = 0, s2= 0 ;
if (num.equals("1")) {
System.out.println("输入存入金额:");
deposit = input.nextDouble();
System.out.println("存钱数目为:" + deposit);
s1 += deposit;
}
if (num.equals("2")) {
System.out.println("输入取钱金额:");
withdraw = input.nextDouble();
System.out.println("取走金额为:" + withdraw);
s2 += withdraw;
}
if (true | num.equals("3")) {
balance += s1 - s2;
System.out.println("账户余额为:" + balance);

}

}
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-17
package bam;

public abstract class Account {

private static long id = 100000;

private String password;

private String name;

private String personId;

private String email;

private double balance;

public Account() {
}

public Account(String password, String name, String personId, String email) {
this.setId(++id);
this.setPassword(password);
this.setName(name);
this.setPersonId(personId);
this.setEmail(email);
}

public String toString() {
return "id:" + id + ", name:" + name + ", personId:" + personId
+ ", email:" + email + ", balance:" + balance;
}

public long getId() {
return id;
}

public void setId(long id) {
id++;
this.id = id;

}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPersonId() {
return personId;
}

public void setPersonId(String personId) {
this.personId = personId;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public final double deposit(double d) {
System.out.println("您存入的现金为 :" + d);
balance = balance + d;
System.out.println("您账户的余额为 :" + balance);
return balance;
}

public abstract double withdraw(double w);
}本回答被网友采纳
第2个回答  2011-11-09
不会,赚点经验,楼主勿怪