java语言程序设计 第十一章 11.3

来源:互联网 发布:刮骨疗伤是真的吗 知乎 编辑:程序博客网 时间:2024/06/05 16:30

程序小白,希望和大家多交流,共同学习
简单写了一个运行GUI
等学到第14章的JavaFX后会陆续写点好看的
这里写图片描述

//主类

import java.util.Scanner;public class CreatAccount{    public static AccoutGUI gui = new AccoutGUI();    public static void main(String [] args)    {        start();    }    public static void start()    {        CheckingAccount checkingaccount = null;        SavingAccount savingaccount = null;        gui.accountHead();        System.out.print("输入你的选择:");        int chooseAccount = succInput(1, 2);        if (chooseAccount == 1)        {            checkingaccount= creatCheckingAccount();        }        else if (chooseAccount == 2)        {            savingaccount = creatSavingAccount();        }        gui.chooseList();        System.out.print("输入你的选择(0-3):");        int choose = succInput(0, 3);        while (choose != 0)        {            if (chooseAccount == 1)            {                judgeChoose(choose, checkingaccount);            }            else if (chooseAccount == 2)            {                judgeChoose(choose, savingaccount);            }            System.out.print("输入你的选择(0 - 3):");            choose = succInput(0, 3);        }    }    public static int succInput(int start, int end)    {        Scanner input = new Scanner(System.in);        int choose = input.nextInt();        while ((choose < start) || (choose > end))        {            System.out.print("无效输入,请重新输入: ");            choose = input.nextInt();        }        return choose;    }    public static CheckingAccount creatCheckingAccount()    {        Scanner input = new Scanner(System.in);        System.out.print("输入 Id: ");        int id = input.nextInt();        return new CheckingAccount(id, 0.0);    }    public static SavingAccount creatSavingAccount()    {        Scanner input = new Scanner(System.in);        System.out.print("输入 Id: ");        int id = input.nextInt();        return new SavingAccount(id, 0.0);    }    public static void judgeChoose(int choose, CheckingAccount checkingAccount)    {        System.out.println("**********");        switch (choose)        {        case 0:            break;        case 1:            System.out.println(gui.operation("存款"));            checkingAccount.deposit(enterMoney());            break;        case 2:            System.out.println(gui.operation("取款"));            checkingAccount.withdraw(enterMoney());            break;        case 3:            System.out.println(gui.operation("显示用户信息"));            System.out.println(checkingAccount.toString());            break;        }    }    public static void judgeChoose(int choose, SavingAccount savingAccount)    {        System.out.println("**********");        switch (choose)        {        case 0:            break;        case 1:            System.out.println(gui.operation("存款"));            double money = enterMoney();            savingAccount.deposit(money);            break;        case 2:            System.out.println(gui.operation("取款"));            money = enterMoney();            savingAccount.withdraw(money);            break;        case 3:            System.out.println(gui.operation("显示用户信息"));            System.out.println(savingAccount.toString());            break;        }    }    public static double enterMoney()    {        Scanner input = new Scanner(System.in);        System.out.print("输入钱:");        double money = input.nextDouble();        while (money <= 0)        {            System.out.print("无效输入,重新输入: ");            money = input.nextDouble();        }        return money;    }}

//Account父类

import java.util.Date;public class Account{    private int id;    private double balance;    private double annualInterestRate;    private Date dateCreated;    public Account()    {        dateCreated = new Date();    }    public Account(int newId, double newBalance)    {        id = newId;        balance = newBalance;        dateCreated = new Date();    }    public void setId(int newId)    {        id = newId;    }    public int getId()    {        return id;    }    public void setBalance(double newBalance)    {        balance = newBalance;    }    public double getBalance()    {        return balance;    }    public String getDateCreated()    {        return dateCreated.toString();    }    public void setAnnualInterestRate(double newAnnualInterestRate)    {        annualInterestRate = newAnnualInterestRate;    }    public double getAnnualInterestRate()    {        return annualInterestRate;    }    public double getMonthlyIbterestRate()    {        return annualInterestRate / 12;    }    public void withdraw(double money)    {        balance -= money;    }    public void deposit(double money)    {        balance += money;    }}

// CheckingAccount继承类

public class  CheckingAccount extends Account{    public static final double OVERDRAFTLIMIT = 600;    public CheckingAccount()    {    }    public CheckingAccount(int id, double balance)    {        super(id, balance);    }    public void withdraw(double money)    {        double balance = getBalance();        if (balance - money < 0)        {            if (money - balance < OVERDRAFTLIMIT)            {                setBalance(balance - money);            }            else                System.out.println("Exceed the overdraft limit after spending");        }        else            setBalance(balance - money);    }    public String toString()    {        return "Checking account: " + "\nId: " + getId() + "\nBalance: " + getBalance() +             "\nDateCreated: " + getDateCreated();    }}

//SavingAccount继承类

public class SavingAccount extends Account{    public SavingAccount()    {    }    public SavingAccount(int id, double balance)    {        super(id, balance);    }    public void withdraw(double money)    {        if (getBalance() - money < 0)        {            System.out.println("Saving account overdraft is not allowed");        }        else            setBalance(getBalance() - money);    }    public String toString()    {        return "Saving account: " + "\nId: " + getId() + "\nBalance: " + getBalance() +             "\nDateCreated: " + getDateCreated();    }}

//运行页面辅助类

public class AccoutGUI{    public void accountHead()    {        System.out.println("\t\tAccount");        System.out.println("1.创建 checking account");        System.out.println("2.创建 Saving account");    }    public void chooseList()    {        System.out.println("1.存钱");        System.out.println("2.取款");        System.out.println("3.显示用户信息");        System.out.println("0.退出");    }    public String operation(String operate)    {        return ("正在进行 " + operate + " ……");    }}