JAVA简单案例之ATM系统
注意:代码中的package univerisity.shanxi.atm;这一行是我自己新建的包名,每个人建的包名不一样的话这个也是不一样的,在自己新建的包下新建三个类Account、ATM、Test,然后将除了package univerisity.shanxi.atm;这一行以下的代码复制上去即可。
学习了B站上黑马程序员的JAVA入门基础视频的基础阶段写的案例:
案例分为三个类:Account:账户类,定义账户的一些基本信息,有哪些属性以及有参构造器,无参构造器,以及每个属性的get,set方法
ATM:核心部分,定义对账户的具体操作以及欢迎界面,登陆操作界面等
Test:测试类,用来测试功能能否实现
此案例适用于本科阶段的JAVA课程的课程设计
Account类:
package univerisity.shanxi.atm;
public class Account {
private String cardId;
private String name;
private String sex;
private String password;
private double money;
private double edu;
public Account() {
}
public Account(String cardId, String name, String sex, String password, double money, double edu) {
this.cardId = cardId;
this.name = name;
this.sex = sex;
this.password = password;
this.money = money;
this.edu = edu;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return (sex.equals("男")?"先生":"女士");
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getEdu() {
return edu;
}
public void setEdu(double edu) {
this.edu = edu;
}
}
ATM类:
package univerisity.shanxi.atm;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATM {
private ArrayList<Account> accounts=new ArrayList<>();
private Scanner sc=new Scanner(System.in);
private Account acc;
private void createAccount(){
Account acc=new Account();
System.out.println("欢迎进入开户界面:");
System.out.println("请输入姓名:");
String name=sc.next();
acc.setName(name);
while (true) {
System.out.println("请输入性别:");
String sex=sc.next();
if(sex.equals("男") || sex.equals("女")){
acc.setSex(sex);
break;
}else{
System.out.println("您输入的性别不对,请重新输入");
}
}
while (true) {
System.out.println("请输入密码:");
String password=sc.next();
System.out.println("请输入确认密码:");
String okpassword=sc.next();
if(password.equals(okpassword)){
acc.setPassword(password);
break;
}else{
System.out.println("两次输入的密码不一致,请重新输入");
}
}
System.out.println("请输入取现额度:");
double limit=sc.nextDouble();
acc.setEdu(limit);
//卡号
while (true) {
String cardId=createCardId();
if(getAccountByCardId(cardId)==null){
acc.setCardId(cardId);
System.out.println(acc.getName()+acc.getSex()+"您的卡号为:"+cardId);
break;
}
}
accounts.add(acc);
}
private String createCardId(){
String code="";
Random r=new Random();
for (int i = 0; i < 8; i++) {
code+=r.nextInt(10);
}
return code;
}
private Account getAccountByCardId(String cardId){
for (int i = 0; i < accounts.size(); i++) {
Account acc=accounts.get(i);
if(acc.getCardId().equals(cardId)){
return acc;
}
}
return null;
}
private void loginUser(){
if(accounts.size()==0){
System.out.println("系统中无任何账户,请先开户后再登录");
return;
}
while (true) {
System.out.println("请输入卡号:");
String id=sc.next();
Account acc=getAccountByCardId(id);
if(acc!=null){
while (true) {
System.out.println("请输入密码:");
String pass=sc.next();
if(pass.equals(acc.getPassword())){
this.acc=acc;
System.out.println("恭喜您"+this.acc.getName()+"您已登录成功,您的卡号为:"+acc.getCardId());
userOperator();
return;
}else{
System.out.println("您输入的密码不正确,请重新输入:");
}
}
}else{
System.out.println("卡号不正确,请重新输入:");
}
}
}
private void userOperator(){
while (true) {
System.out.println("---------------登录操作界面----------------");
System.out.println("1.查询账户");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.转账");
System.out.println("5.修改密码");
System.out.println("6.退出");
System.out.println("7.注销账户");
System.out.println("请选择:");
int command=sc.nextInt();
switch (command){
case 1: showUserInfo();
break;
case 2: deposit();
break;
case 3: Withdrawal();
break;
case 4: transferMoney();
break;
case 5: changePassword();
return;
case 6: return;
case 7: delAccount();
return;
default:
System.out.println("没有这个指令请重新输入");
}
}
}
private void changePassword() {
while (true) {
System.out.println("=====修改密码界面======");
System.out.println("请输入原来的密码:");
String password=sc.next();
if(this.acc.getPassword().equals(password)){
while (true) {
System.out.println("请输入新密码:");
String newPassword=sc.next();
System.out.println("请输入确认密码:");
String okPassword=sc.next();
if(newPassword.equals(okPassword)){
this.acc.setPassword(newPassword);
System.out.println("您修改密码成功,返回开始界面");
return;
}else{
System.out.println("两次密码输入不一致,请重新输入");
}
}
}else{
System.out.println("您输入的旧密码错误");
}
}
}
private void delAccount() {
System.out.println("销户操作界面");
System.out.println("您确认要注销账户吗?(y/n)");
String command=sc.next();
switch (command){
case "y":
double money=this.acc.getMoney();
if(money==0){
accounts.remove(this.acc);
System.out.println("销户成功");
System.out.println("======================");
return;
}else{
System.out.println("您账户中有余额,不允许销户");
break;
}
default:
System.out.println("您选择不注销账户,返回登录界面");
break;
}
}
private void transferMoney() {
System.out.println("==转账操作==");
if(accounts.size()<2){
System.out.println("当前账户数目不足2个,请开户后再转账");
return;
}
while (true) {
System.out.println("请输入要转账的卡号:");
String cardId=sc.next();
Account acc1=getAccountByCardId(cardId);
if(acc1!=null){
while (true) {
String name="*"+acc1.getName().substring(1);
System.out.println("您当前要为"+name+"转账,请输入首字:");
String prename=sc.next();
if(acc1.getName().startsWith(prename)){
System.out.println("请输入要转账的金额");
double money=sc.nextDouble();
if(acc.getMoney()>money){
acc.setMoney(acc.getMoney()-money);
acc1.setMoney(acc1.getMoney()+money);
System.out.println("转账成功,您的当前余额为:"+acc.getMoney());
return;
}else{
System.out.println("您的余额不足,余额为:"+acc.getMoney());
}
}else{
System.out.println("首字母输入错误,请重新输入");
}
}
}else{
System.out.println("无此账户,请重新输入");
}
}
}
private void Withdrawal() {
System.out.println("==取款操作==");
if(this.acc.getMoney()>=100){
while (true) {
System.out.println("请输入您要取款的金额");
double money=sc.nextDouble();
if(money<=this.acc.getMoney()){
if(money<this.acc.getEdu()){
double mon1=this.acc.getMoney();
this.acc.setMoney(this.acc.getMoney()-money);
System.out.println("之前余额为:"+mon1+",取款:"+money+",当前余额:"+this.acc.getMoney());
return;
}else{
System.out.println("超出当前限额,限额为:"+this.acc.getEdu()+",余额为:"+this.acc.getMoney());
return;
}
}else{
System.out.println("您要取的金额为:"+money+",您的余额不足,您的当前余额为"+this.acc.getMoney());
return;
}
}
}else{
System.out.println("您的余额不足一百,您的当前余额为:"+this.acc.getMoney());
}
}
private void deposit() {
System.out.println("==存款操作==");
System.out.println("请输入要存入的金额:");
double depomoney=sc.nextDouble();
this.acc.setMoney(this.acc.getMoney()+depomoney);
System.out.println("您的当前余额为:"+this.acc.getMoney());
}
private void showUserInfo(){
System.out.println("卡号:"+this.acc.getCardId());
System.out.println("姓名:"+this.acc.getName());
System.out.println("性别:"+this.acc.getSex());
System.out.println("余额:"+this.acc.getMoney());
System.out.println("取额度:"+this.acc.getEdu());
}
public void start(){
while (true) {
System.out.println("开始界面:");
System.out.println("1.用户登录");
System.out.println("2.用户开户");
System.out.println("请选择:");
String command=sc.next();
switch (command){
case "1":
loginUser();
break;
case "2":
//用户开户
createAccount();
break;
default:
System.out.println("您输入的命令不正确,请重新输入");
}
}
}
}
Test类:
package univerisity.shanxi.atm;
public class Test {
public static void main(String[] args) {
ATM acc=new ATM();
acc.start();
}
}
系统界面图片:
刚开始系统中无用户,因此需要开户之后才能登录,直接登录会提示先开户再登录。
用户开户界面:
用户登录界面:
以上功能均可实现。
查询账户:
存款:
取款:
转账:
转账操作需要系统中有两个及以上用户的情况
修改密码:
退出:
注销账户:
无余额才能销户。
原文地址:https://blog.csdn.net/qq_45169358/article/details/136474746
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!