银行系统
package bank;import java.util.InputMismatchException;
import java.util.Scanner;
class OutException extends Exception
{
OutException(){
super("您的余额不足!");
}
}
class OutException2 extends Exception
{
OutException2(){
super("请输入正确的序号!");
}
}
class Bank
{
private String Name;
private double Money;
Bank() {
}
Bank(String Name,double Money) {
this.Name = Name;
this.Money = Money;
}
public void printY() {
System.out.println(Name+"当期账户余额为:"+this.Money);
}
public void printX(int i) {
System.out.println((i+1)+"."+Name);
}
public void Cun(double Money)
{
this.Money+=Money;
}
public void Qu(double Money) throws OutException
{
if (this.Money<Money)
throw new OutException();
this.Money-=Money;
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入创建的账户数量:\n");
int m = sc.nextInt();
sc.nextLine();
Bank [] p = new Bank;
for(int i = 0;i<m;i++)
{
System.out.println("请输入账户"+(i+1)+"的名称和金额:");
String x = sc.nextLine();
Double y =sc.nextDouble();
sc.nextLine();
p = new Bank(x,y);
}
while(true) {
try {
System.out.println("请选择你的功能:");
System.out.println("0.退出程序\t1.存钱\n2.取钱\t3.查询账户\n");
int n = sc.nextInt();
sc.nextLine();
if(n<0||n>3)
{
throw new OutException2();
}
switch(n) {
case 1:
System.out.println("请选择存钱账户:");
for(int i=0;i<m;i++)
{
p.printX(i);
}
int a = sc.nextInt();
sc.nextLine();
if(a<1||a>m)
{
throw new OutException2();
}
System.out.println("请输入存钱的金额:");
int b = sc.nextInt();
sc.nextLine();
p.Cun(b);
p.printY();
break;
case 2:
System.out.println("请选择取钱账户:");
for(int i=0;i<m;i++)
{
p.printX(i);
}
int a1 = sc.nextInt();
sc.nextLine();
if(a1<1||a1>m)
{
throw new OutException2();
}
System.out.println("请输入取钱的金额:");
int b1 = sc.nextInt();
sc.nextLine();
p.Qu(b1);
p.printY();
break;
case 3:
System.out.println("请选择要查询的账户:");
for(int i=0;i<m;i++)
{
p.printX(i);
}
int a2 = sc.nextInt();
sc.nextLine();
if(a2<1||a2>m)
{
throw new OutException2();
}
p.printY();
break;
case 0:
System.out.println("程序已退出!");
System.exit(0);
}
//break;
}
catch (InputMismatchException e1) {
System.out.println("输入类型错误");
sc.next();
}
catch(OutException e1)
{
System.out.println(e1.getMessage());
} catch (OutException2 e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
} good
{:9_232:}
页:
[1]