- 小教板
-
import java.util.*;
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(qukuanjine>5000){
System.out.println("取款金额不能大于5000"+" ");
}else{
if(qukuanjine%100!=0){
System.out.println("取款金额必须为100的整数倍"+" ");
}else{
yue=yue-qukuanjine;
System.out.println("您取出了"+qukuanjine+"元,您现在的余额为:"+yue+"元"+" ");
}
}
}
public static void cunKuan(){
System.out.println("请输入存款金额");
int cunkuanjine=in.nextInt();
if(cunkuanjine<0){
System.out.println("存款金额不能小于0");
}else{
yue=yue+cunkuanjine;
System.out.println("您已成功存入:"+cunkuanjine+"元,您现在总余额为:"+yue+"元"+" ");
}
}
public static void xiugai(){
System.out.println("请输入您的密码:");
int mima1=in.nextInt();
if(mima1==mima){
System.out.println("请输入新密码:");
mima=in.nextInt();
System.out.println("您的密码已修改为:"+mima+" ");
}else{
System.out.println("密码输入错误:"+" ");
}
}
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("帐号登录成功"+" ");
else
System.out.println("密码输入错误,登录失败"+" ");
}
}
- max笔记
-
/*
要求:使用字符用户界面。当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:
1、查询余额:初始余额为10000元
2、ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。
3、ATM存款:不能出现负存款。
4、修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码。
(卡号密码余额放到文件中)
*/
publicclassATM{
privateAccountacc;
privateFiledataFile;
privateFileWriterfw;
privateBufferedWriterbw;
privateStringfilePath="./data.txt";
publicATM(){
this.acc=newAccount();
try{
this.dataFile=newFile(this.filePath);
if(!this.dataFile.exists()){
this.dataFile.createNewFile();
}
this.fw=newFileWriter(this.filePath);
this.bw=newBufferedWriter(this.fw);
}catch(IOExceptionio){
System.err.println("Cannotopenfile");
io.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
newATM().interact();
}
publicvoidinteract(){
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
System.out.println("Account#:");
Stringtemp=br.readLine();
System.out.println("Password:");
Stringtemp2=br.readLine();
if(!this.acc.isValid(Long.parseLong(temp.trim()),temp2.trim()){
System.err.println("Wrongpassword");
return;
}
System.out.println("1.AccountInquery.");
System.out.println("2.Withdraw");
System.out.println("3.Deposit.");
System.out.println("4.ChangePassword.");
System.out.println("5.ExporttoFile.");
System.out.println("0.Exit.");
intc=100;
while(c!=0){
Stringstr=br.readLine();
try{
intc=Integer.parseInt(str.trim());
}catch(NumberFormatExceptionnfe){
System.err.println("Invalidchoice");
continue;
}
switch(c){
case0:
System.out.println("Thankyou");
break;
case1:
System.out.println("Balance:"+this.acc.balanceInquery());
break;
case2:
System.out.println("Howmuch?");
Stringtemp=br.readLine();
try{
longammount=Long.parseLong(temp.trim());
this.acc.withdraw(ammount);
break;
}catch(NumberFormatExceptionnfe){
System.err.println("Invalidamount");
continue;
}
case3:
System.out.println("Howmuch?");
Stringtemp=br.readLine();
try{
longammount=Long.parseLong(temp.trim());
this.acc.deposit(ammount);
break;
}catch(NumberFormatExceptionnfe){
System.err.println("Invalidamount");
continue;
}
case4:
System.out.println("Oldpassword:");
Stringtemp=br.readLine();
System.out.println("Newpassword:");
Stringtemp2=br.readLine();
this.acc.changePassword(temp,temp2);
break;
case5:
this.bw.write(this.acc.toString());
break;
default:
break;
}
}
}
}
classAccount{
privatelongaccNo=123456;
privateStringpass="123456";
privatelongbalance=10000;
publicAccount(){
}
publicbooleanisValid(longaccNo,Stringpass){
return(this.accNo==accNo)&&(pass.equals(this.pass));
}
publicvoidchangePassword(StringoldPass,Stringpassword){
if(!oldPass.equals(this.pass)){
System.err.println("Wrongpassword.");
return;
}
if(password.length<6){
System.err.println("Passwordtooshort");
return;
}
if(password.equals(this.pass)){
System.err.println("Passwordcannotbethesame.");
return;
}
this.pass=password;
}
publiclongbalanceInquery(){
returnthis.balance;
}
publicvoidwithdraw(longamount){
if(amount>5000||amount<0){
System.err.println("Withdrawlimit:$0-$5000");
return;
}
if((amount%100)!=0){
System.err.println("Theamounthastobeaproductof100");
return;
}
longnewBalance=this.balance-amount;
if(newBalance<0){
System.err.println("Notenoughmoneyintheaccount");
return;
}
this.balance=newBalance;
}
publicvoiddeposit(longamount){
if(amount<0){
System.err.println("Cannotdepositnegativeamount");
return;
}
this.balance+=amount;
}
publicStringtoString(){
return("Account#:"+this.accNo+" "+"Password:"+this.pass+" "+"Balance:"+this.balance);
}
}
- 康康map
-
/*
要求:使用字符用户界面。当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:
1、查询余额:初始余额为10000元
2、ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。
3、ATM存款:不能出现负存款。
4、修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码。
(卡号密码余额放到文件中)
*/
public class ATM {
private Account acc;
private File dataFile;
private FileWriter fw;
private BufferedWriter bw;
private String filePath = "./data.txt";
public ATM() {
this.acc = new Account();
try {
this.dataFile = new File(this.filePath);
if (!this.dataFile.exists()) {
this.dataFile.createNewFile();
}
this.fw = new FileWriter(this.filePath);
this.bw = new BufferedWriter(this.fw);
} catch (IOException io) {
System.err.println("Cannot open file");
io.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ATM().interact();
}
public void interact() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Account #: ");
String temp = br.readLine();
System.out.println("Password: ");
String temp2 = br.readLine();
if (!this.acc.isValid(Long.parseLong(temp.trim()), temp2.trim()) {
System.err.println("Wrong password");
return;
}
System.out.println("1. Account Inquery.");
System.out.println("2. Withdraw");
System.out.println("3. Deposit.");
System.out.println("4. Change Password.");
System.out.println("5. Export to File.");
System.out.println("0. Exit.");
int c = 100;
while (c != 0) {
String str = br.readLine();
try {
int c = Integer.parseInt(str.trim());
} catch (NumberFormatException nfe) {
System.err.println("Invalid choice");
continue;
}
switch (c) {
case 0:
System.out.println("Thank you");
break;
case 1:
System.out.println("Balance: " + this.acc.balanceInquery());
break;
case 2:
System.out.println("How much? ");
String temp = br.readLine();
try {
long ammount = Long.parseLong(temp.trim());
this.acc.withdraw(ammount);
break;
} catch (NumberFormatException nfe) {
System.err.println("Invalid amount");
continue;
}
case 3:
System.out.println("How much? ");
String temp = br.readLine();
try {
long ammount = Long.parseLong(temp.trim());
this.acc.deposit(ammount);
break;
} catch (NumberFormatException nfe) {
System.err.println("Invalid amount");
continue;
}
case 4:
System.out.println("Old password: ");
String temp = br.readLine();
System.out.println("New password: ");
String temp2 = br.readLine();
this.acc.changePassword(temp, temp2);
break;
case 5:
this.bw.write(this.acc.toString());
break;
default:
break;
}
}
}
}
class Account {
private long accNo = 123456;
private String pass = "123456";
private long balance = 10000;
public Account() {
}
public boolean isValid(long accNo, String pass) {
return (this.accNo == accNo) && (pass.equals(this.pass));
}
public void changePassword(String oldPass, String password) {
if (!oldPass.equals(this.pass)) {
System.err.println("Wrong password.");
return;
}
if (password.length < 6) {
System.err.println("Password too short");
return;
}
if (password.equals(this.pass)) {
System.err.println("Password cannot be the same.");
return;
}
this.pass = password;
}
public long balanceInquery() {
return this.balance;
}
public void withdraw(long amount) {
if (amount > 5000 || amount < 0) {
System.err.println("Withdraw limit: $0-$5000");
return;
}
if ((amount % 100) != 0) {
System.err.println("The amount has to be a product of 100");
return;
}
long newBalance = this.balance - amount;
if (newBalance < 0) {
System.err.println("Not enough money in the account");
return;
}
this.balance = newBalance;
}
public void deposit(long amount) {
if (amount < 0) {
System.err.println("Cannot deposit negative amount");
return;
}
this.balance += amount;
}
public String toString() {
return ("Account #: " + this.accNo + " " + "Password: " + this.pass + " " + "Balance: " + this.balance);
}
}