public class Account { public int id; public String name; public int money; //无参 //alt + insert //C + y public Account(int myId, String name) { this.id = myId; this.name = name; } public Account(int id, String name, int money) { this.id = id; this.name = name; this.money = money; } public void print() { System.out.println("now : " + id + ", " + name + ", " + money); } public void inMoney(int m) { money += m; } public void outMoney(int m) { if(m > money) { System.out.println("余额不足,取款失败"); } else { money -= m; } } }
import java.util.Scanner; public class TestAccount { public static void main(String[] args) { //类名 对象名 = new 类名(); // 类 java提供的类 系统类 JRE = JVM + Java运行支持类库 Scanner input = new Scanner(System.in); int id; String name; int money; System.out.println("请输入账号:"); id = input.nextInt();//对象名.方法名 System.out.println("请输入姓名:"); name = input.next(); System.out.println("请输入金额:"); money = input.nextInt(); //声明对象 Account ac = new Account(id, name, money); ac.print(); while(true) { System.out.println("/****************菜单*******************/"); System.out.println("/* 1 存款 */"); System.out.println("/* 2 取款 */"); System.out.println("/* 3 退出 */"); System.out.println("/**************************************/"); System.out.println("请选择:"); int type = input.nextInt(); if(type == 1) { System.out.println("请输入存款金额:"); int m = input.nextInt(); ac.inMoney(m); ac.print(); } else if(type == 2) { System.out.println("请输入取款金额:"); int m = input.nextInt(); ac.outMoney(m); ac.print(); } else if(type == 3) { System.out.println("再见!"); break; } else { System.out.println("输入错误,请从新选择!"); } } } }