import java.util.*;
成都网络公司-成都网站建设公司创新互联建站10年经验成就非凡,专业从事网站建设、成都网站建设,成都网页设计,成都网页制作,软文平台,一元广告等。10年来已成功提供全面的成都网站建设方案,打造行业特色的成都网站建设案例,建站热线:18980820575,我们期待您的来电!
public class ATM {
static int people=123456;
static int mima=123456;
static int yue=10000;
static Scanner in=null;
public ATM(int people,int mima){
this.mima=mima;
}
public static void main(String[] args) {
chongxindenglu();
int count=1;
while(count!=0){
System.out.println("1、查询余额");
System.out.println("2、取款");
System.out.println("3、存款");
System.out.println("4、修改密码");
System.out.println("5、重新登录");
count=in.nextInt();
switch(count){
case 1: System.out.println(10000); System.out.println(); break;
case 2: quKuan(); break;
case 3: cunKuan(); break;
case 4: xiugai(); break;
case 5: chongxindenglu();
}
}
}
public static void quKuan(){
System.out.println("请输入取款金额:");
int qukuanjine=in.nextInt();
if(qukuanjine5000){
System.out.println("取款金额不能大于5000"+"\n");
}else{
if(qukuanjine%100!=0){
System.out.println("取款金额必须为100的整数倍"+"\n");
}else{
yue=yue-qukuanjine;
System.out.println("您取出了"+qukuanjine+"元,您现在的余额为:"+yue+"元"+"\n");
}
}
}
public static void cunKuan(){
System.out.println("请输入存款金额");
int cunkuanjine=in.nextInt();
if(cunkuanjine0){
System.out.println("存款金额不能小于0");
}else{
yue=yue+cunkuanjine;
System.out.println("您已成功存入:"+cunkuanjine+"元,您现在总余额为:"+yue+"元"+"\n");
}
}
public static void xiugai(){
System.out.println("请输入您的密码:");
int mima1=in.nextInt();
if(mima1==mima){
System.out.println("请输入新密码:");
mima=in.nextInt();
System.out.println("您的密码已修改为:"+mima+"\n");
}else{
System.out.println("密码输入错误:"+"\n");
}
}
public static void chongxindenglu(){
in=new Scanner(System.in);
System.out.println("请输入登录帐号:");
int denglupeople=in.nextInt();
System.out.println("请输入登录密码:");
int denglumima=in.nextInt();
if(denglupeople==people denglumima==mima)
System.out.println("帐号登录成功"+"\n");
else
System.out.println("密码输入错误,登录失败"+"\n");
}
}
ok,稍等
呵呵,已经给你拆分成了两个独立的类了。
我再吧注释加起吧。
哪儿不清楚的可以给我留言嘛,刚开始学习的时候就要多看看别人写的代码,然后从中学习。这里写的用到了简单的封装面向对象静态类,你可以在多了解下,不难的。
import java.util.Scanner;
public class Atm {
//显示菜单
static void showMenu() {
System.out.println();
System.out.print("1.查询账户余额\n");
System.out.print("2.存款\n");
System.out.print("3.取款\n");
System.out.print("0.退出\n");
System.out.print("请选择操作:");
}
public static void main(String[] arg) {
//创建一个account的对象
Account account = new Account();
System.out.println("*******欢迎使用**********");
//循环操作提示
while (true) {
showMenu();//调用显示菜单的方法
//得到用户的输入
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
switch (input) {
case 1:
account.query();
break;
case 2:
System.out.print("请输入存款额:");
float in = scanner.nextFloat();
account.in(in);
account.query();
break;
case 3:
System.out.print("请输入取款额:");
float out = scanner.nextFloat();
account.out(out);
account.query();
break;
case 0:
System.out.println("谢谢使用");
System.exit(0); //终止程序
break;
default:
System.out.println("输入有误");
}
}
}
}
//帐号类
class Account {
private float money = 8000;
// 查询账户余额
public void query() {
System.out.println("账户余额:" + money);
}
// 取出,out是取出的存款数
public void out(float out) {
if (money out) {
System.out.println("账户余额不足");
}
this.money -= money;
}
// 存入,in是输入的存款数
public void in(float in) {
this.money += in;
}
}
public class Card {
public String type; //卡的类型
public String cardCode; //卡号
public double money; //钱
/**
* 重写toString()方法
*/
public String toString(){
return "卡的类型:"+type+"\t卡号:"+cardCode+"\t余额:"+money;
}
}public class Person {
Card card;
/**
* 构造方法,一般用来初始化信息
* 此方法是创建对象时自动调用的
*
*/
public Person(){
card=new Card();
card.type="招商银行";
card.money=1000D;
}
/**
*
* @param money 你要取的钱
* @return 返回的是取到的钱
*/
public double getMoney(double money){
if( (card.money-money) =0){
card.money=card.money-money;//从卡中减去要取的钱,并重新改变卡中的余额
return money;
}else{
System.out.println("余额不足:请存款!");
return 0;
}
}
/**
* 存款
* @param money
*/
public void setMoney(double money){
if(money=0){
System.out.println("不能为0!");
}else
card.money=card.money+money;
}
/**
* 这个方法是输出客户卡的信息
*/
public void print(){
System.out.println(card);
}
}public class Text {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p=new Person();
p.card.cardCode="111100022222";
p.setMoney(1000);
double money=p.getMoney(2000);
System.out.println(p.card);
}
}
package arraylist;
import java.util.Scanner;
public class AtmDemo
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Boolean flag = true;
int times = 0;
while(flag){
times++;
if(times == 4){
System.out.println("密码错误,请取卡");
break;
}
System.out.println("请输入你的密码");
String password = sc.next();
if(password.equals("111111")){
Boolean moneyflag = true;
while(moneyflag){
System.out.println("请输入金额");
int number = sc.nextInt();
if(number = 0 number = 1000 number % 100 == 0){
System.out.println("用户取了" + number + "元。交易完成");
moneyflag = false;
}else{
System.out.println("请重新输入金额");
}
}
break;
}else{
continue;
}
}
}
}
AccountTest.java class BankAccount //定义银行账户类BankAccount{private static int amount =2000; //账户余额最初为2000public void despoit(int m) //定义存款的方法{amount=amount+m;System.out.println("晓明存入["+m+"元]");}public void withdraw(int m) //定义取款的方法{amount=amount-m;System.out.println("张新取走["+m+"元]");if(amount0)System.out.println("***余额不足!***);public int balance() //定义得到账户余额的方法{return amount;}}classicCustomerextendsThread {String name;BankAccount bs; //定义一个具体的账户对象public Customer(BankAccount b,String s){name=s;bs=b;}public static void cus(String name,BankAccount bs) //具体的账户操作方法{if(name.equals("小明")) //判断用户是不是小明{try{for(int i=0;i6;i++) //用户晓明则向银行存款6次,每次1000元 {Thread.currentThread().sleep((int)(Math.random()*300));bs.despoit(1000);}}catch(InterruptedException e){}}else{try{for(int i=0;i6;i++) //用户不是小明则从银行取款6次,每次1000元{Thread.currentThread().sleep((int)(Math.random()*300));bs.withdraw(1000); }}catch(InterruptedException e){} }}public void run() //定义run方法}cus(name,bs); }}public classAccountTest{public static void main(String [] args) throws InterruptedException{BankAccount bs=new BankAccount();Customer customer1=new Customer(bs,"小明");Customer customer2=new Customer(bs,"张新");Thread t1=new Thread(customer1);Thread t2=new Thread(customer2);t1.Start();t2.start();Thread.currentThread().sleep(500);}}
public static void main(String[] args) {
String realPwd = "111111";
Integer errorCount = 0;
Integer account;
Scanner sc = new Scanner(System.in);
while(true){
System.out.print("请输入密码:");
String pwd = sc.next();
if(!pwd.equals(realPwd)){
errorCount++;
System.out.println("第"+errorCount+"次输入密码不正确");
if(errorCount=3){
System.out.println("密码错误,请取卡");
break;
}
}else{
while(true){
System.out.print("请输入取款金额:");
account = sc.nextInt();
if(account1000 || account0){
System.out.println("取款金额最低0元,最高1000元");
}else if(account%100!=0){
System.out.println("只有100的纸币");
}else{
System.out.println("取款金额"+account+"元");
System.out.println("交易完成,请取卡");
break;
}
}
break;
}
}
}