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 + " ……"); }}
阅读全文
0 0
- java语言程序设计 第十一章 11.3
- 《java语言程序设计》第十一章学习总结
- java语言程序设计基础 第十一章 继承11.2
- java语言程序设计 第十一章(11.8 11.9)
- java语言程序设计基础篇第十一章编程练习题
- Java语言程序设计(基础篇)原书第十版第十一章答案
- java第十一章
- JAVA第十一章复习
- Java语言程序设计 第13章
- java 语言程序设计 第十三章 13.5
- Java oop 第十一章 CMS
- Java语言程序设计
- Java语言程序设计
- 学习笔记:第12章 Java语言程序设计
- 《java语言程序设计》第二章学习总结
- 《java语言程序设计》第三章学习总结
- 《java语言程序设计》第四章学习总结
- 《java语言程序设计》第五章学习总结
- CorelDRAW 足球制作完你一个自己绘出足球的梦
- css如何不显示元素?
- sweetalert使用
- HDU 3613 Manacher
- httpError: The Http Transport returned a 0 status code
- java语言程序设计 第十一章 11.3
- JAVA 编程规范
- php验证码实现
- 第一章
- 1701H5 范江睿 10月12日 连续第二天总结
- 腾讯云免费SSL与Tomcat8的集成
- 设计模式学习起点 UML类图笔记
- bzoj 4709: [Jsoi2011]柠檬(分段DP+决策单调性)
- spark wordcount