《Java语言程序设计》P296
本章是关于对象的思考,主要是在研究面向对象的程序设计时类的设计,作业写得比较杂乱,构造方法时没有严格遵守类的流行设计风格,由于是作业,再加上比较简单,没有添加任何注释。
整套程序分为两个类,一个Account类,用于定义Account()及其方法;一个AccountTest类,用于调用Account类中的方法以实现题目要求的功能。
public class Account { private static int ACOUNT_NUMBER = 5; private int moneyAtAcount = 5000; private boolean isLogin = false; public void Acount(){ } public int getMoneyAtAcount(){ return moneyAtAcount; } public void withdraw(int m){ moneyAtAcount -= m; System.out.println("Withdraw "+m+" succeed, now balance have " + moneyAtAcount); } public void deposite(int n){ moneyAtAcount += n; System.out.println("Deposite "+n+" succeed, now balance have " + moneyAtAcount); } public void login(int p){ if(p == ACOUNT_NUMBER){ isLogin = true; }else System.out.println("Wrroy Account"); } public boolean getLogin(){ return isLogin; } public void logout(){ isLogin = false; } }
import java.util.Scanner; public class AcountTest { public static void main(String[] args) { Account account = new Account(); Scanner input = new Scanner(System.in); for (int i=1;i>0;i++) { //人为无限循环,题目要求如此,实现的比较简陋 System.out.println("Enter an id:"); account.login(input.nextInt()); for (;account.getLogin();) { //对于printf的编辑格式不是很熟悉,为了图方便,选用一打println来输出选单 System.out.println("Main menu"); System.out.println("1:check balance"); System.out.println("2:withdraw"); System.out.println("3:deposit"); System.out.println("4:exit"); System.out.println("Enter a choice: "); int choice = input.nextInt(); switch(choice){ case 1: System.out.println(account.getMoneyAtAcount()); break; case 2: System.out.println("Enter an amount to withdraw: "); account.withdraw(input.nextInt()); break; case 3: System.out.println("Enter an amount to deposit: "); account.deposite(input.nextInt()); break; case 4: account.logout(); break; } } } } }
2016-03-24
时间: 2024-10-10 13:43:33