案例一 删除 成绩中不满住80分的成绩
package com.heima.StringDemo; import java.util.ArrayList; public class ArrayList3 { public static void main(String[] args) { //创建ArrayList 对象 并使用泛型指定整数 ArrayList<Integer> list = new ArrayList(); //添加同学的成绩 list.add(98); list.add(77); list.add(66); list.add(89); list.add(79); list.add(50); list.add(100); //循环ArrayList for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); //判断成绩是否低于80 低于80 则删除 if (list.get(i) <=80) { list.remove(i); //从前面往后面删除 删除元素值后,后面的元素值要向前挪 所以i--; i--; } } //打印删除后的学生成绩 System.out.println(list); } }
案例二 影片信息在程序中的存储
Movie 类
package com.heima.StringDemo; public class Movie { private String name; private double scores; private String filmPerson; //创造无参构造器 public Movie() { } //创造有参构造器 public Movie(String name, double scores, String filmPerson) { this.name = name; this.scores = scores; this.filmPerson = filmPerson; } //set get方法创建 public String getName() { return name; } public void setName(String name) { this.name = name; } public double getScores() { return scores; } public void setScores(double scores) { this.scores = scores; } public String getFilmPerson() { return filmPerson; } public void setFilmPerson(String filmPerson) { this.filmPerson = filmPerson; } }
Test 测试类
package com.heima.StringDemo; import java.util.ArrayList; import java.util.Scanner; /* 电影Test类 需求:定义电影类(名称,分值,演员),创建3个电影对象,代表散步影片,存入集合中并遍历 */ public class Test { public static void main(String[] args) { //创造一个ArrayList 集合用来存储movies ArrayList<Movie> list = new ArrayList(); Movie a = new Movie("肖申克的救赎", 9.7, "罗伯特"); Movie b = new Movie("霸王别姬", 9.1, "虞姬"); Movie c = new Movie("阿甘正传", 10.0, "阿甘"); list.add(a); list.add(b); list.add(c); for (int i = 0; i < list.size(); i++) { Movie child=list.get(i); System.out.println(child.getName()+"评分"+child.getScores()+"主演"+child.getFilmPerson()); } } }
案例三 元素搜索
Student类
package com.heima.StringDemo; public class Student { private String name; private int studentNum; private int age; private String studentClass; //创建有参构造器 public Student(String name, int studentNum, int age, String studentClass) { this.name = name; this.studentNum = studentNum; this.age = age; this.studentClass = studentClass; } //创建无参构造器 public Student() { } //get set 创建 public String getName() { return name; } public void setName(String name) { this.name = name; } public double getStudentNum() { return studentNum; } public void setStudentNum(int studentNum) { this.studentNum = studentNum; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getStudentClass() { return studentClass; } public void setStudentClass(String studentClass) { this.studentClass = studentClass; } }
Test1 测试类
package com.heima.StringDemo; import java.util.ArrayList; import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //创建ArrayList 存储Student产生的对象 ArrayList<Student> studentList = new ArrayList<>(); //创建学生 Student Stu = new Student("叶孤城", 20189812, 18, "二年级一班"); Student Stu1 = new Student("东方不败", 20189813, 18, "二年级一班"); Student Stu2 = new Student("西门吹雪", 20189814, 18, "二年级一班"); Student Stu3 = new Student("梅超风", 20189815, 18, "二年级一班"); //添加进StudentList studentList.add(Stu); studentList.add(Stu1); studentList.add(Stu2); studentList.add(Stu3); //循环展示studentList集合中的元素信息 for (int i = 0; i < studentList.size(); i++) { Student child = studentList.get(i); System.out.println(child.getName() + "\t年纪 \t" + child.getAge() + "学号\t" + child.getStudentNum() + "年级\t" + child.getStudentClass()); } while (true) { System.out.println("请输入学号"); int num = sc.nextInt(); Student s = getSearchMsg(studentList, num); if(s!=null){ System.out.println(s.getName() + "\t年纪 \t" + s.getAge() + "学号\t" + s.getStudentNum() + "年级\t" + s.getStudentClass()); System.out.println("查询完毕"); }else{ System.out.println("查无此人"); } //查询结束 } } public static Student getSearchMsg(ArrayList<Student> a, int num) { for (int i = 0; i < a.size(); i++) { Student child = a.get(i); //相同 则匹配上 if (child.getStudentNum() == num) { return child; } } return null;//查无此学号 } }
案例四
Account类
public Account() { } //创建有参构造器 public Account(String user, int pass, double money, double basic) { this.user = user; this.pass = pass; this.money = money; this.basic = basic; } //get set方法 public String getUser() { return user; } public void setUser(String user) { this.user = user; } public int getPass() { return pass; } public void setPass(int pass) { this.pass = pass; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public double getBasic() { return basic; } public void setBasic(double basic) { this.basic = basic; } public int getAccounts() { return accounts; } public void setAccounts(int accounts) { this.accounts = accounts; } }
Test 类
package com.heima.ATM; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class Test { public static void main(String[] args) { //创建ArrayList 用来存储 Account实例对象 ArrayList<Account> accountList = new ArrayList<>(); while (true) { firstPage(accountList); } } //首页方法 public static void firstPage(ArrayList<Account> accountList) { Scanner sc = new Scanner(System.in); while (true) { System.out.println("欢迎登录ATM银行系统"); System.out.println("请输入你需要选择的内容"); System.out.println("----------------"); System.out.println("1 登录"); System.out.println("2 注册"); int command = sc.nextInt(); switch (command) { case 1: Account child = login(accountList); if (child == null) { System.out.println("不存在该账号"); } else { loginSuccess(accountList, child); } break; case 2: register(accountList); break; default: System.out.println("输入指令错误"); } } } //登录成功后方法 public static void loginSuccess(ArrayList<Account> accountList, Account child) { Scanner sc = new Scanner(System.in); //获取用户名 信息 //登录成功后 显示用户名 及要操作的内容 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: System.out.println("您的存款有: \t" + child.getMoney()); break; //存款 case 2: saveMoney(child); break; //取款 case 3: takeMoney(child); break; case 4: giveMoney(accountList, child); break; case 5: changePass(child); break; case 6: firstPage(accountList); return; case 7: deleteUser(accountList, child); break; default: System.out.println("指令输入错误"); } } } //登录 public static Account login(ArrayList<Account> accountList) { Scanner sc = new Scanner(System.in); while (true) { boolean flag = false; System.out.println("请输入用户名:user"); String user = sc.next(); System.out.println("请输入密码:pass"); int pass = sc.nextInt(); //锁 用来判断是否存在该账号 默认不存在 for (int i = 0; i < accountList.size(); i++) { Account child = accountList.get(i); if (child.getUser().equals(user)) { //存在 改变锁的状态 flag = true; //判断pass 是否存在 if (child.getPass() == pass) { //登录成功 System.out.println("登录成功"); return child; } else { System.out.println("密码输入错误,请重新输入"); break; } } } if (!flag) { return null; } } } //注册 public static void register(ArrayList<Account> accountList) { Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:user"); String user = sc.next(); System.out.println("请输入密码:pass"); int pass = sc.nextInt(); //创建Account实例 Account account = new Account(user, pass, 0, 5000); //向accountList中添加账户 accountList.add(account); //随机生成6为位数的账户 Random r = new Random(); boolean flag = false; int accounts = 0; while (true) { accounts = r.nextInt(99999) + 900000; //默认不存在该账户 flag = false; for (int j = 0; j < accountList.size(); j++) { if (accountList.get(j).getAccounts() == accounts) { //改变锁 flag = true; } } //账户中不存在该账户 if (!flag) { account.setAccounts(accounts); System.out.println("注册成功" + "您的卡号是" + accounts); break; } } } //存款 public static void saveMoney(Account child) { Scanner sc = new Scanner(System.in); while (true) { System.out.println("请输入您要存的金额"); //输入你要存的数目 double money = sc.nextInt(); if (money < 0) { System.out.println("输入金额错误"); } else { //存款 double oldMoney = child.getMoney(); double newMoney = money + oldMoney; //修改金钱数目 child.setMoney(newMoney); System.out.println("你存款成功" + "现在金额为" + child.getMoney()); break; } } } //取款 public static void takeMoney(Account child) { //获取输入流对象 Scanner sc = new Scanner(System.in); System.out.println("请输入要取款的金额"); double money = sc.nextDouble(); double oldMoney = child.getMoney(); if (money > oldMoney) { System.out.println("余额不足"); } else { System.out.println("你取出了" + money + "余额还有" + (oldMoney - money)); double newMoney = oldMoney - money; child.setMoney(newMoney); System.out.println("取款成功"); } } //转账 public static void giveMoney(ArrayList<Account> accountArrayList, Account child) { Scanner sc = new Scanner(System.in); //用户只有一个 if (accountArrayList.size() <= 1) { System.out.println("ATM系统中只有你 一个用户 ,请注册新账户"); } //存在多个用户 if (accountArrayList.size() > 1) { System.out.println("请输入要转账的账户卡号"); //获取要转账的号码 boolean result = false; Account newAccounts=null; while (true) { int accounts = sc.nextInt(); //卡号范围正确与否 if (accounts > 99999 && accounts < 1000000) { //定义锁 for (int i = 0; i < accountArrayList.size(); i++) { //存在账户 if (accountArrayList.get(i).getAccounts() == accounts && accountArrayList.get(i).getAccounts()!= child.getAccounts()) { newAccounts=accountArrayList.get(i); result = true; break; } } //存在账户 if(result){ //开始转账 旧账户 扣钱 新账户加钱 System.out.println("请输入要转账的金额"); double money=sc.nextDouble(); //金额合适 if(money>=0 && money<=child.getMoney()){ //扣钱 child.setMoney(child.getMoney()-money); System.out.println(child.getMoney()+"现在有多少钱"); //加钱 newAccounts.setMoney(newAccounts.getMoney()+money); System.out.println(newAccounts.getMoney()+"现在有多少钱"); System.out.println(newAccounts.getUser()+"钱2"+newAccounts.getMoney()); //转账完成 System.out.println("转账完成"); //跳出循环 loginSuccess(accountArrayList,child); break; }else{ System.out.println("金额错误 或者 你也没钱 转什么"); } } } else { System.out.println("您输入的账户长度有误,6位长度"); } } } } //修改密码 public static void changePass(Account child) { Scanner sc = new Scanner(System.in); System.out.println("请输入新的密码"); int newPass = sc.nextInt(); child.setPass(newPass); //修改成功 System.out.println("密码修改成功"); } //注销账户 public static void deleteUser(ArrayList<Account> accountArrayList, Account child) { //判断账户内是否有钱 if (child.getMoney() == 0) { //可以注销 for (int i = 0; i < accountArrayList.size(); i++) { Account kid = accountArrayList.get(i); if (kid.getUser().equals(child.getUser())) { //删除 System.out.println(accountArrayList.remove(i) + "删除成功"); firstPage(accountArrayList); //退出循环 break; } } } else { System.out.println("你的账户还有钱" + child.getMoney() + "取完才可以注销"); } } }