学习视频:/video/BV1Cv411372m
如侵权,请私信联系本人删除
文章目录
黑马程序员Java教程学习笔记(三)面向对象:设计对象、注意事项面向对象:内存机制面向对象:编程训练-模拟购物车面向对象:构造器、this面向对象:封装面向对象:标准JavaBean、综合案例String概述、对象创建原理String类常用APIString类案例:验证码、登录、隐私号码集合概述、ArrayList的使用ATM-01系统、项目介绍、功能演示ATM-02系统、系统准备、首页、开户功能ATM-03系统、用户登录、查询、存款取款ATM-04系统、用户转账ATM-05系统、用户修改密码、销户黑马程序员Java教程学习笔记(三)
面向对象:设计对象、注意事项
OOP(Object Oriented Programming
):面向对象程序设计
类:是对象共同特征的描述
对象:是真实存在的具体实例
在Java中,必须先设计类,才能创建对象并使用
public class Car{// 属性(成员变量)String name;double price;// 行为方法public void start(){}public void run(){}}
创建对象
类名 对象名 = new 类名();Car c = new Car();
使用对象
访问属性:对象名.成员变量访问行为:对象名.方法名(...)
定义类的补充注意事项:
类名首字母建议大写,且有意义,满足”驼峰模式“一个Java文件中可以定义多个class类,但只能一个类是public修饰,而且public修饰的类名必须成为代码文件名
(实际开发中建议还是一个文件定义一个class类)成员变量的完整定义格式是:修饰符 数据类型 变量名称 = 初始化值;
一般无需指定初始值,存在默认值
面向对象:内存机制
Java虚拟机内存的三部分:栈内存、堆内存、方法区
多个对象使用时内存处理过程:
类会先加载到方法区
,虚拟机会把方法区
中的类的main
方法加载到栈内存
中执行,在执行main
方法的过程中会把其中的对象加载到方法区
,然后对象中的属性(成员变量)会加载到栈内存
,而对象会在堆内存
开辟存储对象的信息的空间(属性,以及方法的引用地址(指向方法区
)),对象在堆内存
中的内存空间的地址赋值给栈内存
中的变量。
两个变量指向同一个对象内存图:
垃圾回收机制:当堆内存中的对象,没有被任何变量引用(指向)时,就会被判定为内存中的”垃圾“。Java存在自动垃圾回收器,会定期进行清理
面向对象:编程训练-模拟购物车
1.需求分析、架构搭建
2.添加商品到购物车、查看购物车信息
3.修改购买数量
4.结算支付金额
package com.mochu.project2;public class Commodity {// 商品类int id;String name;double price;int quantity;}
package com.mochu.project2;import java.util.Scanner;public class ShoppingCart {// 定义商品类,用于创建购物车对象public static void main(String[] args) {// 定义购物车对象,使用一个数组对象表示Commodity[] shopcart = new Commodity[100];while (true) {System.out.println("请您选择操作:");System.out.println("添加商品到购物车:add");System.out.println("查看商品到购物车:view");System.out.println("修改商品购买数量:update");System.out.println("结算商品的金额:pay");Scanner sc = new Scanner(System.in);System.out.print("请您输入命令:");String command = sc.next();switch (command) {case "add":add(shopcart, sc);break;case "view":view(shopcart);break;case "update":update(shopcart, sc);break;case "pay":pay(shopcart);break;default:System.out.println("没有该功能");}}}public static void add(Commodity[] shopcart, Scanner sc) {// 录入商品信息System.out.print("请输入购买商品编号:");int id = sc.nextInt();System.out.print("请输入购买商品的名称:");String name = sc.next();System.out.print("请输入购买商品的数量:");int quantity = sc.nextInt();System.out.print("请输入商品的价格:");double price = sc.nextDouble();// 把商品的信息分装成对象Commodity cd = new Commodity();cd.id = id;cd.name = name;cd.quantity = quantity;cd.price = price;// 把商品对象添加到购物车数组中for (int i = 0; i < shopcart.length; i++) {if (shopcart[i] == null) {shopcart[i] = cd;break;}}System.out.println("您的商品已添加到购物车");}public static void view(Commodity[] shopcart) {// 查看购物车中的商品信息System.out.println("=============购物车信息如下=============");System.out.println("编号\t\t\t名称\t\t\t价格\t\t\t数量");for (int i = 0; i < shopcart.length; i++) {Commodity cd = shopcart[i];if (cd != null) {// 展示商品信息System.out.println(cd.id + "\t\t\t" + cd.name + "\t\t\t" + cd.price + "\t\t\t" + cd.quantity);}else {break;}}}public static void update(Commodity[] shopcart, Scanner sc) {while (true) {System.out.print("请输入需要修改商品的ID:");int id = sc.nextInt();Commodity cd = getCommodityId(shopcart, id);if (cd == null) {System.out.println("不存在该商品");}else {System.out.print("请您输入" + cd.name + "商品购买数量:");int quantity = sc.nextInt();cd.quantity = quantity;System.out.println("商品购买数量修改完成");view(shopcart);break;}}}public static Commodity getCommodityId(Commodity[] shopcart, int id) {// 遍历寻找商品id与用户输入的id相同的商品for (int i = 0; i < shopcart.length; i++) {Commodity cd = shopcart[i];// 判断商品对象是否为空if (cd != null) {if (cd.id == id) {return cd;}else {return null; // 不为空的商品对象中不存在与用户输入的商品id相同的商品,返回为空}}}return null; // 遍历完所有商品对象数组也没有找到相同的商品id,返回为空}public static void pay(Commodity[] shopcart) {view(shopcart);double amount = 0;for (int i = 0; i < shopcart.length; i++) {Commodity cd = shopcart[i];if (cd != null) {amount += (cd.price * cd.quantity);}else {break;}}System.out.println("购物车所有商品总金额为:" + amount + "元");}}
面向对象:构造器、this
构造器的作用:
定义在类型中,可以用于初始化一个类的对象,并返回对象的地址
构造器的格式:
修饰符 类名(形参列表) {...}public class Car {// 无参数的构造器public Car() {...}// 有参数的构造器public Car(String name, double price) {...}}调用构造器得到对象的格式:类 变量名称 = new 构造器;
构造器的分类和作用:
无参数构造器(默认存在的):初始化对象时,成员变量的数据均采用默认值有参数构造器:在初始化对象的时候,同时可以接收参数为对象进行赋值
构造器注意事项:
任何类定义出来,默认就自带了无参数构造器,写不写都有。一旦定义了有参数构造器,那么无参数构造器就没有了,如果还想用无参数构造器,就需要自己手写一个无参数构造器了。
this
关键字是什么
可以出现在构造器、方法中代表当前对象的地址
this
关键字的作用:可以用于指定访问当前对象的成员变量,成员方法。
public Commodity(String name, double price) {this.name = name;this.price = price;}
面向对象:封装
面向对象的三大特征:封装、继承、多态封装:告诉我们,如何正确设计对象的属性和方法封装的原则:对象代表什么,就得封装对应的数据,并提供数据对应的行为如何进行更好的封装?
一般建议对成员变量使用private(私有、隐藏)关键字修饰(private修饰的成员只能在当前类中访问)。为每个成员变量提供配套public修饰的getter、setter方法暴露其取值和赋值
面向对象:标准JavaBean、综合案例
JavaBean
也可以称为实体类,其对象可以用于在程序中封装数据
标准JavaBean
需满足如下书写要求:
成员变量使用private
修饰提供成员变量对应的setXxx()
、getXxx()
方法需提供一个无参构造器;有参数构造器是可写不可写的
package com.mochu.JavaBean;public class Test {public static void main(String[] args) {User user = new User();user.setName("mochu7");user.setHeight(180);user.setSalary(2500);System.out.println(user.getName());System.out.println(user.getHeight());System.out.println(user.getSalary());System.out.println("-----------------");User user2 = new User("Slevin", 185, 3000);System.out.println(user2.getName());System.out.println(user2.getHeight());System.out.println(user2.getSalary());}}
package com.mochu.JavaBean;public class User {private String name;private double height;private double salary;public User() {}public User(String name, double height, double salary) {this.name = name;this.height = height;this.salary = salary;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}}
成员变量、局部变量的区别
package com.mochu.JavaBean;public class Test {public static void main(String[] args) {Movie[] movies = new Movie[3];movies[0] = new Movie("《长津湖》", 9.7, "吴京");movies[1] = new Movie("《我和我的父亲》", 9.6, "吴京");movies[2] = new Movie("《扑水少年》", 9.5, "王川");for (int i = 0; i < movies.length; i++) {Movie m = movies[i];System.out.println("电影名称:" + m.getName());System.out.println("电影评分:" + m.getScore());System.out.println("电影主演:" + m.getActor());}}}
package com.mochu.JavaBean;public class Movie {private String name;private double score;private String actor;public Movie() {}public Movie(String name, double price, String actor) {this.name = name;this.score = price;this.actor = actor;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}public String getActor() {return actor;}public void setActor(String actor) {this.actor = actor;}}
String概述、对象创建原理
Sting概述:
java.lang.String
类代表字符串,String类定义的变量可以用于指向字符串对象,然后操作该字符串。Java程序中的所有字符串文字(例如“abc”)都为此类的对象。
String类的特点详解:
String其实常被称为不可变字符串类型,它的对象在创建后不能被更改
package com.mochu.project1;public class StringDemo {public static void main(String[] args) {// 推荐用法String name = "末初";System.out.println(name);// 几乎不用String s1 = new String();System.out.println(s1);// 几乎不用String s2 = new String("末初");System.out.println(s2);char[] chars = {'a', 'b', 'c'};String s3 = new String(chars);System.out.println(s3);byte[] bytes = {97, 98, 99, 100};String s4 = new String(bytes);System.out.println(s4);}}
String类常用API
字符串内容比较
登陆失败的原因:==
比较的是地址,username
、password
指向的地址都是常量池的,而用户输入的用户和密码都存储在堆内存当中。
package com.mochu.project1;import java.util.Scanner;public class StringDemo {public static void main(String[] args) {String username = "mochu7";String password = "123456";Scanner sc = new Scanner(System.in);System.out.print("请输入用户名:");String inputName = sc.next();System.out.print("请输入密码:");String inputPass = sc.next();// 不忽略大小写的比较if (username.equals(inputName) && password.equals(inputPass)) {System.out.println("登录成功!");}else {System.out.println("用户名或密码错误");}// 忽略大小写的比较String verifyCode = "g6Hq4k";String userCode = "G6hQ4K";if (verifyCode.equalsIgnoreCase(userCode)) {System.out.println("验证成功");}}}
package com.mochu.project1;import java.util.Scanner;public class StringDemo {public static void main(String[] args) {String name = "mochu7";// 获取字符长度System.out.println(name.length());// 获取某个索引位置的字符System.out.println(name.charAt(2));for (int i = 0; i < name.length(); i++) {System.out.println(name.charAt(i));}// 把字符串转换成字符数组char[] chars = name.toCharArray();for (int i = 0; i < chars.length; i++) {System.out.println(chars[i]);}// 截断字符串System.out.println(name.substring(1, 5));System.out.println(name.substring(2));System.out.println(name.replace("7", "777"));// 判断字符串是否包含指定字符System.out.println(name.contains("chu"));// 字符串是否以指定字符串开始System.out.println(name.startsWith("mo"));System.out.println(name.startsWith("ch"));// 把字符串分割为字符串数组返回String content = "mochu7, slevin, shinn";String[] names = content.split(", ");for (int i = 0; i < names.length; i++) {System.out.println(names[i]);}}}
String类案例:验证码、登录、隐私号码
import java.util.Random;public class StringDemo {public static void main(String[] args) {String chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";Random rd = new Random();String verifycode = "";for (int i = 0; i < 5; i++) {int index = rd.nextInt(chars.length());char c = chars.charAt(index);verifycode += c;}System.out.println(verifycode);}}
import java.util.Scanner;public class StringDemo {public static void main(String[] args) {String username = "admin";String password = "admin888";Scanner sc = new Scanner(System.in);for (int i = 1; i <= 3; i++) {System.out.print("请输入用户名:");String inputname = sc.next();System.out.print("请输入账号密码:");String inputpass = sc.next();if (username.equals(inputname)) {if (password.equals(inputpass)) {System.out.println("登陆成功~~~");}else {System.out.println("密码错误");System.out.println("您还有" + (3 - i) + "次机会");}}else {System.out.println("用户名错误");System.out.println("您还有" + (3 - i) + "次机会");}}}}
import java.util.Scanner;public class StringDemo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("请输入手机号码:");String phonenumber = sc.next();if (phonenumber.length() == 11 ) {String part1 = phonenumber.substring(0, 3);String part2 = phonenumber.substring(7);System.out.println("您的手机号码:" + part1 + "****" + part2);}else {System.out.println("手机号码长度错误");}}}
集合概述、ArrayList的使用
ArrayList是集合中的一种,它支持索引
import java.util.ArrayList;public class ArrayLIstDemo {public static void main(String[] args) {// 创建ArrayList对象ArrayList list = new ArrayList();// 添加元素list.add("java");list.add("Python");list.add("PHP");list.add(100);list.add(99.9);list.add(true);System.out.println(list);// 给指定索引位置插入元素list.add(1, "C++");list.add(3, 59.9);System.out.println(list);}}
ArrayList对于泛型的支持
import java.util.ArrayList;public class ArrayLIstDemo {public static void main(String[] args) {// 创建ArrayList对象// ArrayList<String> list = new ArrayList<String>();ArrayList<String> list = new ArrayList<>(); // jdk1.7开始,泛型后面的类型可以不写list.add("Java");list.add("Python");list.add("C++");System.out.println(list);ArrayList<Integer> list1 = new ArrayList<>();list1.add(59);list1.add(90);list1.add(100);System.out.println(list1);}}
ArrayList常用API、遍历
import java.util.ArrayList;public class ArrayLIstDemo {public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();list.add("JAVA");list.add("C++");list.add("Python");// 根据索引取元素System.out.println(list.get(1));// 获取集合的大小System.out.println(list.size());// 遍历集合for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}// 删除指定元素System.out.println(list);list.remove(2);System.out.println(list);list.remove("C++");System.out.println(list);// 删除某个索引的元素list.set(0, "PHP");System.out.println(list);}}
import java.util.ArrayList;public class ArrayLIstDemo {public static void main(String[] args) {ArrayList<Integer> score = new ArrayList<>();score.add(99);score.add(77);score.add(66);score.add(89);score.add(79);score.add(50);score.add(100);System.out.println(score);// 第一种方法for (int i = 0; i < score.size(); i++) {if (score.get(i) < 80) {score.remove(i);i--; // 删除成功后往后退一位,这样可以保证下次回到这个位置,不会跳过数据}}System.out.println(score);// 第二种方法for (int i = score.size() - 1; i >= 0; i--) {if (score.get(i) < 80) {score.remove(i);}}System.out.println(score);}}
存储自定义类型的对象存储自定义类型的对象
元素搜索
package com.mochu.project1;public class Student {private String studentId;private String name;private int age;private String className;public Student() {}public Student(String studentId, String name, int age, String className) {this.studentId = studentId;this.name = name;this.age = age;this.className = className;}public String getStudentId() {return studentId;}public void setStudentId(String studentId) {this.studentId = studentId;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}}
package com.mochu.project1;import java.util.ArrayList;import java.util.Scanner;public class ArrayLIstDemo {public static void main(String[] args) {ArrayList<Student> students = new ArrayList<>();students.add(new Student("0302", "叶孤城", 23, "护理一班"));students.add(new Student("0303", "东方不败", 23, "推拿二班"));students.add(new Student("0304", "西门吹雪", 26, "中药学四班"));students.add(new Student("0305", "梅超风", 26, "神经科二班"));System.out.println("学号\t\t\t姓名\t\t年龄\t班级");for (int i = 0; i < students.size(); i++) {Student s = students.get(i);System.out.println(s.getStudentId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getClassName());}Scanner sc = new Scanner(System.in);while(true) {System.out.print("请输入需要查询的学生学号:");String StudentId = sc.next();Student s = searchStudent(students, StudentId);if(s == null) {System.out.println("对不起,查无此人~");}else{System.out.println("您查询的学生信息如下:");System.out.println(s.getStudentId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getClassName());}}}// 定义方法完成按照学号搜索功能public static Student searchStudent(ArrayList<Student> students, String StudentId) {for (int i = 0; i < students.size(); i++) {Student s = students.get(i);// 遍历对象,寻找学生IDif(s.getStudentId().equals(StudentId)) {return s;}}return null;}}
ATM-01系统、项目介绍、功能演示
首先需要开户、添加确认密码、设置当此取现额度、发放卡号操作界面包括查询、存款、取款、转账、修改密码、退出、注销账号ATM-02系统、系统准备、首页、开户功能
package com.mochu.ATM;/*** 账户类*/public class Account {/*** 成员变量*/private String cardId; // 卡号private String userName; // 用户名private String passWord; // 密码private double money; // 余额private double quotaMoney; // 每次取现额度public String getCardId() {return cardId;}public void setCardId(String cardId) {this.cardId = cardId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}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 getQuotaMoney() {return quotaMoney;}public void setQuotaMoney(double quotaMoney) {this.quotaMoney = quotaMoney;}}
用户开户功能实现
package com.mochu.ATM;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;/*** ATM系统入口类*/public class ATMSystem {public static void main(String[] args) {/*** 1. 定义账户类* 2. 定义一个集合容器,用于存储账户对象,方便对账户对象进行业务操作*/ArrayList<Account> accounts = new ArrayList<>();Scanner sc = new Scanner(System.in);while (true) {// 展示系统欢迎页System.out.println("==================欢迎来到ATM系统==================");System.out.println("账户登录请输入:1");System.out.println("账户开户请输入:2");System.out.print("请输入:");int command = sc.nextInt();switch (command) {case 1:// 账户登录break;case 2:// 账户开户register(accounts, sc);break;default:System.out.println("对不起,您输入的操作有误");}}}/*** 用户开户方法* @param accounts 接受的账户集合*/private static void register(ArrayList<Account> accounts, Scanner sc) {System.out.println("==================账户开户==================");// 创建对象用于信息封装Account account = new Account();// 录入账户信息System.out.println("请输入账户用户名:");String userName = sc.next();account.setUserName(userName);while (true) {System.out.println("请输入账户密码:");String passWord = sc.next();System.out.println("请确认账户密码:");String confirmPassWord = sc.next();if(passWord.equals(confirmPassWord)) {account.setPassWord(confirmPassWord);break;}else {System.out.println("对不起,您输入的密码不一致,请重新输入");}}System.out.println("请输入账户每日限额:");double quotaMoney = sc.nextDouble();account.setQuotaMoney(quotaMoney);// 生成卡号,八位随机数字,卡号不能重复System.out.println("请输入账户卡号:");String cardId = getUserCardId(accounts);account.setCardId(cardId);// 把账户对象添加到账户集合当中accounts.add(account);System.out.println("恭喜您," + userName + "先生/女士,开户成功。");System.out.println("您的卡号是:" + cardId);}/*** 为账户生成八位数字卡号,且卡号不能重复* @return*/private static String getUserCardId(ArrayList<Account> accounts) {Random rd = new Random();while (true) {String cardId = "";for (int i = 0; i < 8; i++) {cardId += rd.nextInt(10);}// 判断卡号是否重复,根据卡号查询账户对象Account acc = getAccountByCardId(accounts, cardId);if(acc == null) {// acc为null即这是一个新的卡号return cardId;}}}/*** 根据卡号查询对象* @param accounts 账户对象集合* @param cardId 卡号* @return 账户对象*/public static Account getAccountByCardId(ArrayList<Account> accounts, String cardId) {for (int i = 0; i < accounts.size(); i++) {Account acc = accounts.get(i);if(acc.getCardId().equals(cardId)) {return acc;}}return null;}}
ATM-03系统、用户登录、查询、存款取款
package com.mochu.ATM;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;/*** ATM系统入口类*/public class ATMSystem {public static void main(String[] args) {/*** 1. 定义账户类* 2. 定义一个集合容器,用于存储账户对象,方便对账户对象进行业务操作*/ArrayList<Account> accounts = new ArrayList<>();Scanner sc = new Scanner(System.in);while (true) {// 展示系统欢迎页System.out.println("==================欢迎来到ATM系统==================");System.out.println("账户登录请输入:1");System.out.println("账户开户请输入:2");System.out.print("请输入:");int command = sc.nextInt();switch (command) {case 1:// 账户登录login(accounts, sc);break;case 2:// 账户开户register(accounts, sc);break;default:System.out.println("对不起,您输入的操作有误");}}}/*** 登录功能* @param accounts 账户对象集合* @param sc 扫描器对象*/private static void login(ArrayList<Account> accounts, Scanner sc) {System.out.println("==================账户登录==================");// 判断账户对象集合中是否存在账户if(accounts.size() == 0) {System.out.println("对不起,当前账户系统中没有账户,请先开户");return ;}// 登陆操作while (true) {System.out.println("请输入账户登录卡号:");String cardId = sc.next();// 判断卡号是否存在Account acc = getAccountByCardId(accounts, cardId);if(acc != null) {while (true) {System.out.println("请输入账户登录密码:");String inputPassWord = sc.next();String passWord = acc.getPassWord();if(inputPassWord.equals(passWord)) {System.out.println("恭喜您" + acc.getUserName() + "先生/女士,您的账户登录成功!");System.out.println("您的卡号是:" + acc.getCardId());// 展示登陆后的操作页面showUserCommand(acc, sc);return ; // 结束当前方法}else{System.out.println("对不起您输入的密码有误");}}}else {System.out.println("对不起,系统中不存在该账户卡号");}}}/*** 用户操作展示页*/private static void showUserCommand(Account acc, Scanner sc) {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:// 查询账户,展示当前账户信息showAccount(acc);break;case 2:// 存款depositMoney(acc, sc);break;case 3:// 取款withdrawMoney(acc, sc);break;case 4:// 转账break;case 5:// 修改密码break;case 6:// 退出系统System.out.println("退出成功~");return ; // 展示当前方法case 7:// 注销账户break;default:System.out.println("对不起,您输入的命令有误");}}}/*** 取款* @param acc 当前账户对象* @param sc 扫描器对象*/private static void withdrawMoney(Account acc, Scanner sc) {System.out.println("==================取款页面==================");// 判断是否足够100元if(acc.getMoney() < 100) {System.out.println("对不起,当前账户余额不足100元,无法取款");return ;}while (true) {System.out.println("请输入输入取款金额:");double money = sc.nextDouble();if(money > acc.getQuotaMoney()) {// 判断取款金额是否超过了限额System.out.println("对不起,您当前取款金额超过每次限额,您的账户每次限额为:" + acc.getQuotaMoney() + "元");}else {if(money > acc.getMoney()) {System.out.println("对不起,你的余额不足,您的帐户当前余额为:" + acc.getMoney() + "元");}else {acc.setMoney(acc.getMoney() - money);System.out.println("取款成功~");showAccount(acc);return ;}}}}/*** 存款* @param acc 当前账户对象* @param sc 扫描器对象*/private static void depositMoney(Account acc, Scanner sc) {System.out.println("==================存款页面==================");System.out.println("请您输入存款金额:");double money = sc.nextDouble();acc.setMoney(acc.getMoney() + money);System.out.println("存款成功,当前账户信息如下:");showAccount(acc);}/*** 展示当前账户信息* @param acc 当前账户对象*/private static void showAccount(Account acc) {System.out.println("==================当前账户信息==================");System.out.println("卡号:" + acc.getCardId());System.out.println("户主:" + acc.getUserName());System.out.println("余额:" + acc.getMoney());System.out.println("限额:" + acc.getQuotaMoney());}/*** 用户开户方法* @param accounts 账户对象集合*/private static void register(ArrayList<Account> accounts, Scanner sc) {System.out.println("==================账户开户==================");// 创建对象用于信息封装Account account = new Account();// 录入账户信息System.out.println("请输入账户用户名:");String userName = sc.next();account.setUserName(userName);while (true) {System.out.println("请输入账户密码:");String passWord = sc.next();System.out.println("请确认账户密码:");String confirmPassWord = sc.next();if(passWord.equals(confirmPassWord)) {account.setPassWord(confirmPassWord);break;}else {System.out.println("对不起,您输入的密码不一致,请重新输入");}}System.out.println("请输入账户每日限额:");double quotaMoney = sc.nextDouble();account.setQuotaMoney(quotaMoney);// 生成卡号,八位随机数字,卡号不能重复System.out.println("请输入账户卡号:");String cardId = getUserCardId(accounts);account.setCardId(cardId);// 把账户对象添加到账户集合当中accounts.add(account);System.out.println("恭喜您," + userName + "先生/女士,开户成功。");System.out.println("您的卡号是:" + cardId);}/*** 为账户生成八位数字卡号,且卡号不能重复* @return*/private static String getUserCardId(ArrayList<Account> accounts) {Random rd = new Random();while (true) {String cardId = "";for (int i = 0; i < 8; i++) {cardId += rd.nextInt(10);}// 判断卡号是否重复,根据卡号查询账户对象Account acc = getAccountByCardId(accounts, cardId);if(acc == null) {// acc为null即这是一个新的卡号return cardId;}}}/*** 根据卡号查询对象* @param accounts 账户对象集合* @param cardId 卡号* @return 账户对象*/public static Account getAccountByCardId(ArrayList<Account> accounts, String cardId) {for (int i = 0; i < accounts.size(); i++) {Account acc = accounts.get(i);if(acc.getCardId().equals(cardId)) {return acc;}}return null;}}
ATM-04系统、用户转账
package com.mochu.ATM;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;/*** ATM系统入口类*/public class ATMSystem {public static void main(String[] args) {/*** 1. 定义账户类* 2. 定义一个集合容器,用于存储账户对象,方便对账户对象进行业务操作*/ArrayList<Account> accounts = new ArrayList<>();Scanner sc = new Scanner(System.in);while (true) {// 展示系统欢迎页System.out.println("==================欢迎来到ATM系统==================");System.out.println("账户登录请输入:1");System.out.println("账户开户请输入:2");System.out.print("请输入:");int command = sc.nextInt();switch (command) {case 1:// 账户登录login(accounts, sc);break;case 2:// 账户开户register(accounts, sc);break;default:System.out.println("对不起,您输入的操作有误");}}}/*** 登录功能* @param accounts 账户对象集合* @param sc 扫描器对象*/private static void login(ArrayList<Account> accounts, Scanner sc) {System.out.println("==================账户登录==================");// 判断账户对象集合中是否存在账户if(accounts.size() == 0) {System.out.println("对不起,当前账户系统中没有账户,请先开户");return ;}// 登陆操作while (true) {System.out.println("请输入账户登录卡号:");String cardId = sc.next();// 判断卡号是否存在Account acc = getAccountByCardId(accounts, cardId);if(acc != null) {while (true) {System.out.println("请输入账户登录密码:");String inputPassWord = sc.next();String passWord = acc.getPassWord();if(inputPassWord.equals(passWord)) {System.out.println("恭喜您" + acc.getUserName() + "先生/女士,您的账户登录成功!");System.out.println("您的卡号是:" + acc.getCardId());// 展示登陆后的操作页面showUserCommand(acc, sc, accounts);return ; // 结束当前方法}else{System.out.println("对不起您输入的密码有误");}}}else {System.out.println("对不起,系统中不存在该账户卡号");}}}/*** 用户操作展示页*/private static void showUserCommand(Account acc, Scanner sc, ArrayList<Account> accounts) {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:// 查询账户,展示当前账户信息showAccount(acc);break;case 2:// 存款depositMoney(acc, sc);break;case 3:// 取款withdrawMoney(acc, sc);break;case 4:// 转账transferMoney(acc, sc, accounts);break;case 5:// 修改密码break;case 6:// 退出系统System.out.println("退出成功~");return ; // 展示当前方法case 7:// 注销账户break;default:System.out.println("对不起,您输入的命令有误");}}}/*** 转账* @param acc 当前账户对象* @param sc 扫描器对象* @param accounts 所有账户对象集合*/private static void transferMoney(Account acc, Scanner sc, ArrayList<Account> accounts) {System.out.println("==================转账页面==================");if(accounts.size() < 2) {System.out.println("当前系统中只有一个账户,不能进行转账");return ;}if(acc.getMoney() == 0) {System.out.println("对不起,你的余额为:" + acc.getMoney() + "元,无法转账");return ;}while (true) {System.out.println("请您输入对方账户的卡号:");String cardId = sc.next();if(cardId.equals(acc.getCardId())) {System.out.println("对不起,您不能给自己转账");continue;}Account counterpartyAccount = getAccountByCardId(accounts, cardId);if(counterpartyAccount == null) {System.out.println("对不起,您输入对方的卡号不存在");}else {// 认证用户名String userName = counterpartyAccount.getUserName();String tip = '*' + userName.substring(1);System.out.println("请输入对方用户名:" + tip);String preName = sc.next();// 认证用户名是否师傅正确if(userName.startsWith(preName)) {while (true) {System.out.println("请输入转账金额:");double money = sc.nextDouble();if(money > acc.getMoney()) {System.out.println("对不起,余额不足");}else {acc.setMoney(acc.getMoney() - money);counterpartyAccount.setMoney(counterpartyAccount.getMoney() + money);System.out.println("转账成功,您的账户还剩余:" + acc.getMoney() + "元");return ;}}}else {System.out.println("对不起,您输入的对方用户名有误~");}}}}/*** 取款* @param acc 当前账户对象* @param sc 扫描器对象*/private static void withdrawMoney(Account acc, Scanner sc) {System.out.println("==================取款页面==================");// 判断是否足够100元if(acc.getMoney() < 100) {System.out.println("对不起,当前账户余额不足100元,无法取款");return ;}while (true) {System.out.println("请输入输入取款金额:");double money = sc.nextDouble();if(money > acc.getQuotaMoney()) {// 判断取款金额是否超过了限额System.out.println("对不起,您当前取款金额超过每次限额,您的账户每次限额为:" + acc.getQuotaMoney() + "元");}else {if(money > acc.getMoney()) {System.out.println("对不起,你的余额不足,您的帐户当前余额为:" + acc.getMoney() + "元");}else {acc.setMoney(acc.getMoney() - money);System.out.println("取款成功~");showAccount(acc);return ;}}}}/*** 存款* @param acc 当前账户对象* @param sc 扫描器对象*/private static void depositMoney(Account acc, Scanner sc) {System.out.println("==================存款页面==================");System.out.println("请您输入存款金额:");double money = sc.nextDouble();acc.setMoney(acc.getMoney() + money);System.out.println("存款成功,当前账户信息如下:");showAccount(acc);}/*** 展示当前账户信息* @param acc 当前账户对象*/private static void showAccount(Account acc) {System.out.println("==================当前账户信息==================");System.out.println("卡号:" + acc.getCardId());System.out.println("户主:" + acc.getUserName());System.out.println("余额:" + acc.getMoney());System.out.println("限额:" + acc.getQuotaMoney());}/*** 用户开户方法* @param accounts 账户对象集合*/private static void register(ArrayList<Account> accounts, Scanner sc) {System.out.println("==================账户开户==================");// 创建对象用于信息封装Account account = new Account();// 录入账户信息System.out.println("请输入账户用户名:");String userName = sc.next();account.setUserName(userName);while (true) {System.out.println("请输入账户密码:");String passWord = sc.next();System.out.println("请确认账户密码:");String confirmPassWord = sc.next();if(passWord.equals(confirmPassWord)) {account.setPassWord(confirmPassWord);break;}else {System.out.println("对不起,您输入的密码不一致,请重新输入");}}System.out.println("请输入账户每日限额:");double quotaMoney = sc.nextDouble();account.setQuotaMoney(quotaMoney);// 生成卡号,八位随机数字,卡号不能重复System.out.println("请输入账户卡号:");String cardId = getUserCardId(accounts);account.setCardId(cardId);// 把账户对象添加到账户集合当中accounts.add(account);System.out.println("恭喜您," + userName + "先生/女士,开户成功。");System.out.println("您的卡号是:" + cardId);}/*** 为账户生成八位数字卡号,且卡号不能重复* @return*/private static String getUserCardId(ArrayList<Account> accounts) {Random rd = new Random();while (true) {String cardId = "";for (int i = 0; i < 8; i++) {cardId += rd.nextInt(10);}// 判断卡号是否重复,根据卡号查询账户对象Account acc = getAccountByCardId(accounts, cardId);if(acc == null) {// acc为null即这是一个新的卡号return cardId;}}}/*** 根据卡号查询对象* @param accounts 账户对象集合* @param cardId 卡号* @return 账户对象*/public static Account getAccountByCardId(ArrayList<Account> accounts, String cardId) {for (int i = 0; i < accounts.size(); i++) {Account acc = accounts.get(i);if(acc.getCardId().equals(cardId)) {return acc;}}return null;}}
ATM-05系统、用户修改密码、销户
package com.mochu.ATM;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;/*** ATM系统入口类*/public class ATMSystem {public static void main(String[] args) {/*** 1. 定义账户类* 2. 定义一个集合容器,用于存储账户对象,方便对账户对象进行业务操作*/ArrayList<Account> accounts = new ArrayList<>();Scanner sc = new Scanner(System.in);while (true) {// 展示系统欢迎页System.out.println("==================欢迎来到ATM系统==================");System.out.println("账户登录请输入:1");System.out.println("账户开户请输入:2");System.out.print("请输入:");int command = sc.nextInt();switch (command) {case 1:// 账户登录login(accounts, sc);break;case 2:// 账户开户register(accounts, sc);break;default:System.out.println("对不起,您输入的操作有误");}}}/*** 登录功能* @param accounts 账户对象集合* @param sc 扫描器对象*/private static void login(ArrayList<Account> accounts, Scanner sc) {System.out.println("==================账户登录==================");// 判断账户对象集合中是否存在账户if(accounts.size() == 0) {System.out.println("对不起,当前账户系统中没有账户,请先开户");return ;}// 登陆操作while (true) {System.out.println("请输入账户登录卡号:");String cardId = sc.next();// 判断卡号是否存在Account acc = getAccountByCardId(accounts, cardId);if(acc != null) {while (true) {System.out.println("请输入账户登录密码:");String inputPassWord = sc.next();String passWord = acc.getPassWord();if(inputPassWord.equals(passWord)) {System.out.println("恭喜您" + acc.getUserName() + "先生/女士,您的账户登录成功!");System.out.println("您的卡号是:" + acc.getCardId());// 展示登陆后的操作页面showUserCommand(acc, sc, accounts);return ; // 结束当前方法}else{System.out.println("对不起您输入的密码有误");}}}else {System.out.println("对不起,系统中不存在该账户卡号");}}}/*** 用户操作展示页*/private static void showUserCommand(Account acc, Scanner sc, ArrayList<Account> accounts) {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:// 查询账户,展示当前账户信息showAccount(acc);break;case 2:// 存款depositMoney(acc, sc);break;case 3:// 取款withdrawMoney(acc, sc);break;case 4:// 转账transferMoney(acc, sc, accounts);break;case 5:// 修改密码updatePassWord(acc, sc);break;case 6:// 退出系统System.out.println("退出成功~");return ; // 展示当前方法case 7:// 注销账户if(deleteAccount(accounts, acc, sc)) {return ;}else{break;}default:System.out.println("对不起,您输入的命令有误");}}}/*** 注销账户* @param accounts 账户对象集合* @param acc 当前账户对象* @param sc 扫描器对象*/private static boolean deleteAccount(ArrayList<Account> accounts, Account acc, Scanner sc) {System.out.println("请确认是否要注销账户:Yes/No");String res = sc.next();switch(res) {case "Yes":if(acc.getMoney() > 0) {System.out.println("您的账户中还有存款,请取完再销户");}else {accounts.remove(acc);System.out.println("账户注销成功");return true;}break;case "No":break;default:System.out.println("您输入的操作命令不正确");break;}return false;}/*** 修改密码* @param acc 当前账户对象* @param sc 扫描器对象*/private static void updatePassWord(Account acc, Scanner sc) {System.out.println("==================密码修改==================");System.out.println("请输入当前账户密码:");String passWord = sc.next();if(passWord.equals(acc.getPassWord())) {while (true) {System.out.println("请您输入新密码:");String newPassWord = sc.next();System.out.println("请您确认新密码:");String newPasswordOk = sc.next();if(newPassWord.equals(newPasswordOk)) {acc.setPassWord(newPassWord);System.out.println("密码修改成功~");return ;}else {System.out.println("对不起,您输入的两次密码不一致");}}}else {System.out.println("您输入的密码不正确");}}/*** 转账* @param acc 当前账户对象* @param sc 扫描器对象* @param accounts 所有账户对象集合*/private static void transferMoney(Account acc, Scanner sc, ArrayList<Account> accounts) {System.out.println("==================转账页面==================");if(accounts.size() < 2) {System.out.println("当前系统中只有一个账户,不能进行转账");return ;}if(acc.getMoney() == 0) {System.out.println("对不起,你的余额为:" + acc.getMoney() + "元,无法转账");return ;}while (true) {System.out.println("请您输入对方账户的卡号:");String cardId = sc.next();if(cardId.equals(acc.getCardId())) {System.out.println("对不起,您不能给自己转账");continue;}Account counterpartyAccount = getAccountByCardId(accounts, cardId);if(counterpartyAccount == null) {System.out.println("对不起,您输入对方的卡号不存在");}else {// 认证用户名String userName = counterpartyAccount.getUserName();String tip = '*' + userName.substring(1);System.out.println("请输入对方用户名:" + tip);String preName = sc.next();// 认证用户名是否师傅正确if(userName.startsWith(preName)) {while (true) {System.out.println("请输入转账金额:");double money = sc.nextDouble();if(money > acc.getMoney()) {System.out.println("对不起,余额不足");}else {acc.setMoney(acc.getMoney() - money);counterpartyAccount.setMoney(counterpartyAccount.getMoney() + money);System.out.println("转账成功,您的账户还剩余:" + acc.getMoney() + "元");return ;}}}else {System.out.println("对不起,您输入的对方用户名有误~");}}}}/*** 取款* @param acc 当前账户对象* @param sc 扫描器对象*/private static void withdrawMoney(Account acc, Scanner sc) {System.out.println("==================取款页面==================");// 判断是否足够100元if(acc.getMoney() < 100) {System.out.println("对不起,当前账户余额不足100元,无法取款");return ;}while (true) {System.out.println("请输入输入取款金额:");double money = sc.nextDouble();if(money > acc.getQuotaMoney()) {// 判断取款金额是否超过了限额System.out.println("对不起,您当前取款金额超过每次限额,您的账户每次限额为:" + acc.getQuotaMoney() + "元");}else {if(money > acc.getMoney()) {System.out.println("对不起,你的余额不足,您的帐户当前余额为:" + acc.getMoney() + "元");}else {acc.setMoney(acc.getMoney() - money);System.out.println("取款成功~");showAccount(acc);return ;}}}}/*** 存款* @param acc 当前账户对象* @param sc 扫描器对象*/private static void depositMoney(Account acc, Scanner sc) {System.out.println("==================存款页面==================");System.out.println("请您输入存款金额:");double money = sc.nextDouble();acc.setMoney(acc.getMoney() + money);System.out.println("存款成功,当前账户信息如下:");showAccount(acc);}/*** 展示当前账户信息* @param acc 当前账户对象*/private static void showAccount(Account acc) {System.out.println("==================当前账户信息==================");System.out.println("卡号:" + acc.getCardId());System.out.println("户主:" + acc.getUserName());System.out.println("余额:" + acc.getMoney());System.out.println("限额:" + acc.getQuotaMoney());}/*** 用户开户方法* @param accounts 账户对象集合*/private static void register(ArrayList<Account> accounts, Scanner sc) {System.out.println("==================账户开户==================");// 创建对象用于信息封装Account account = new Account();// 录入账户信息System.out.println("请输入账户用户名:");String userName = sc.next();account.setUserName(userName);while (true) {System.out.println("请输入账户密码:");String passWord = sc.next();System.out.println("请确认账户密码:");String confirmPassWord = sc.next();if(passWord.equals(confirmPassWord)) {account.setPassWord(confirmPassWord);break;}else {System.out.println("对不起,您输入的密码不一致,请重新输入");}}System.out.println("请输入账户每日限额:");double quotaMoney = sc.nextDouble();account.setQuotaMoney(quotaMoney);// 生成卡号,八位随机数字,卡号不能重复System.out.println("请输入账户卡号:");String cardId = getUserCardId(accounts);account.setCardId(cardId);// 把账户对象添加到账户集合当中accounts.add(account);System.out.println("恭喜您," + userName + "先生/女士,开户成功。");System.out.println("您的卡号是:" + cardId);}/*** 为账户生成八位数字卡号,且卡号不能重复* @return*/private static String getUserCardId(ArrayList<Account> accounts) {Random rd = new Random();while (true) {String cardId = "";for (int i = 0; i < 8; i++) {cardId += rd.nextInt(10);}// 判断卡号是否重复,根据卡号查询账户对象Account acc = getAccountByCardId(accounts, cardId);if(acc == null) {// acc为null即这是一个新的卡号return cardId;}}}/*** 根据卡号查询对象* @param accounts 账户对象集合* @param cardId 卡号* @return 账户对象*/public static Account getAccountByCardId(ArrayList<Account> accounts, String cardId) {for (int i = 0; i < accounts.size(); i++) {Account acc = accounts.get(i);if(acc.getCardId().equals(cardId)) {return acc;}}return null;}}