发布网友 发布时间:2023-12-25 12:02
共3个回答
热心网友 时间:2024-08-28 15:09
//这道提自我认为用到数据库会更好但是不用也可以细节需要楼主注意
package chen.save;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BankSingle
{
//实例化账户类1
static Zhanghu user1=new Zhanghu("1001",2000);
//存款
public static void savemoney()
{
Scanner sc=new Scanner(System.in);
System.out.print("请输入你的账号:");
String id=sc.next();
if(id.equals(user1.getUserid()))
{
System.out.print("\n请输入存款金额");
double savemoney=sc.nextDouble();
//得到余额
double yumoney=user1.getMoney();
user1.setMoney(savemoney+yumoney);
System.out.print("存款成功");
System.out.println("存款后余额为"+user1.getMoney());
}
}
//取款
public static void qumoney()
{
Scanner sc=new Scanner(System.in);
System.out.print("请输入你的账号:");
String id=sc.next();
if(id.equals(user1.getUserid()))
{
System.out.print("\n请输入取款金额:");
double qumoney=sc.nextDouble();
//得到余额
double yumoney=user1.getMoney();
if(qumoney>yumoney)
{
System.out.println("取款不能大于余额请重新输入:"):
qumoney=sc.nextDouble();
}
user1.setMoney(yumoney-qumoney);
System.out.print("取款成功");
System.out.println("取款后余额为"+user1.getMoney());
}
}
//查询余额
public static void select()
{
Scanner sc=new Scanner(System.in);
System.out.print("请输入你的账号:");
String id=sc.next();
if(id.equals(user1.getUserid()))
{
//得到余额
double yumoney=user1.getMoney();
System.out.println("账号:"+id+"\t余额:"+yumoney);
}
}
//显示账号由于没有用到数据库只能这样做
public static void selectall()
{
List<Zhanghu> all=new ArrayList<Zhanghu>();
all.add(user1);
for(Zhanghu use:all)
{
System.out.println("账户:"+use.getUserid());
}
}
public static void main(String[] args)
{
while(true)
{
int i=bankmanger();
switch(i)
{
case 1: savemoney();
break;
case 2:qumoney();
break;
case 3:select();
break;
case 4:selectall();
break;
}
}
}
//"存款"、"取款"、"查询余额"和”显示帐号”。定义主类
public static int bankmanger()
{
Scanner sc=new Scanner(System.in);
System.out.println("1.存款:");
System.out.println("2.取款:");
System.out.println("3.查询余额:");
System.out.println("4.显示账号:");
int i=sc.nextInt();
return i;
}
}
class Zhanghu
{
private String userid;//账号
private double money;//余额
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Zhanghu(String userid, double money) {
super();
this.userid = userid;
this.money = money;
}
}
热心网友 时间:2024-08-28 15:09
太简单了。。。热心网友 时间:2024-08-28 15:17
package com.test;