lesson15homework
来源:互联网 发布:谷边由美 程序员 编辑:程序博客网 时间:2024/06/09 23:23
package com.xt.lesson20;
/**
* 简易自动提款机
1. 创建用户类User(包含卡号、姓名、密码、余额等属性),用户开卡时录入的姓名和密码(自动分配一个卡号、初始金额设置为0)。
2. 使用ArrayList或LinkedList存储全部注册用户
形式如:ArrayList<User> userList = new ArrayList<User>();
3. 实现功能
(1)存款
(2)取款(如果余额不足要提示)
(3)查询余额(只能查看当前卡号的余额)
4. 技术要求,使用异常处理用户的错误输入(即程序保护容错功能)。
* @author LENOVO
*
*/
public class User {
private String ID;//卡号
private String name;//姓名
private int password;//密码
private int balance;//余额
//构造方法
public User(String ID, String name, int password, int balance) {
super();
this.ID = ID;
this.name = name;
this.password = password;
this.balance = balance;
}
/**
* @return the iD
*/
public String getID() {
return ID;
}
/**
* @param iD the iD to set
*/
public void setID(String ID) {
this.ID = ID;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the password
*/
public int getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(int password) {
this.password = password;
}
/**
* @return the balance
*/
public int getBalance() {
return balance;
}
/**
* @param balance the balance to set
*/
public void setBalance(int balance) {
this.balance = balance;
}
//存款功能的实现
public boolean saveMoney(int m){
System.out.println("正在验钞,请稍后.......");
if(m<0){
if(checkMoney(-m)){
return true;
}else{
return false;
}
}else if(m>0){
this.balance+=m;
System.out.println("操作成功!");
return true;
}
return false;
}
//取款操作
public boolean checkMoney(int m){
if(m<0){
if(saveMoney(-m)){
return true;
}else{
return false;
}
}else if(m>0){
System.out.println("请输入密码......");
if(checkPassword()){
this.balance-=m;
System.out.println("操作成功!");
return true;
}
}
return false;
}
//查询余额
public void search(){
System.out.println("正在处理,请稍后......");
if(ID.equals(this.ID)){
System.out.println("账户"+ID+"的余额为:"+balance);
}else{
System.out.println("您输入的账户不存在,请认真核查后重新输入!");
}
}
//检验密码操作
public boolean checkPassword(){
int i;
for(i=0;i<3;i++){
if(i>0){
System.out.println("密码输入错误,请重新输入密码!");
}
int pas=EnterNum.enterNum();
if(pas==password){
return true;
}
}
if(i==3)
System.out.println("密码输入错误3次,此次操作中断!");
return false;
}
//选择操作方法
public void print(){
System.out.println("----------------------欢迎使用自动取款机------------------------");
System.out.println("[存款----1]");
System.out.println("[取款----2]");
System.out.println("[查询----3]");
System.out.println("[退卡----0]");
}
}
//写一个类,进行对输入数字的判断。
package com.xt.lesson20;
import java.util.Scanner;
public class EnterNum {
static Scanner scanner=new Scanner(System.in);
public static String enterStr(){
String str="";
try{
str=scanner.next();
}catch(Exception e){
}
return str;
}
public static int enterNum(){
int m = 0;
try{
m=Integer.parseInt(enterStr());
}catch(Exception e){
System.out.println("您输入的数据不合法!请输入整数......");
}
return m;
}
}
//写一个主方法,进行判断操作的选择。
package com.xt.lesson20;
import java.util.ArrayList;
public class MachineTest {
public static void main(String[] args) {
ArrayList<User> userList=new ArrayList<User>();
userList.add(new User("未知","未知",0,0));//默认值
userList.add(new User("200981501","聂庆平",5264,7000));
userList.add(new User("200981512","黎明",1562,17000));
first:while(true){
userList.get(0).print();
int a=EnterNum.enterNum();
second: switch (a){
case 1:{
System.out.println("请输入您要存钱的账号:");
String id=EnterNum.enterStr();
int i;
for (i=0;i<userList.size();i++){
if(id.equals(userList.get(i).getID())){
System.out.println("请输入您存的钱数:");
int m=EnterNum.enterNum();
if(userList.get(i).saveMoney(m)){
break second;
}
}
}
if(i==userList.size()){
System.out.println("您输入的账户不存在!");
break second;
}
}
case 2:{
System.out.println("请输入您要取钱的账号:");
String id=EnterNum.enterStr();
int i;
for(i=0;i<userList.size();i++){
if(id.equals(userList.get(i).getID())){
System.out.println("请输入您取的钱数:");
int m=EnterNum.enterNum();
userList.get(i).checkMoney(m);
break second;
}
}
if(i==userList.size()){
System.out.println("您输入的账户不存在!");
break second;
}
}
case 3:{
System.out.println("请输入您的卡号!");
String id=EnterNum.enterStr();
int i;
for(i=0;i<userList.size();i++){
if(id.equals(userList.get(i).getID())){
userList.get(i).search();
break second;
}
}
if(i==userList.size()){
System.out.println("您输入的账户不存在!");
break second;
}
}
case 0:{
System.out.println("感谢您的使用,再见!");
System.exit(0);
}
}
}
}
}
/**
* 简易自动提款机
1. 创建用户类User(包含卡号、姓名、密码、余额等属性),用户开卡时录入的姓名和密码(自动分配一个卡号、初始金额设置为0)。
2. 使用ArrayList或LinkedList存储全部注册用户
形式如:ArrayList<User> userList = new ArrayList<User>();
3. 实现功能
(1)存款
(2)取款(如果余额不足要提示)
(3)查询余额(只能查看当前卡号的余额)
4. 技术要求,使用异常处理用户的错误输入(即程序保护容错功能)。
* @author LENOVO
*
*/
public class User {
private String ID;//卡号
private String name;//姓名
private int password;//密码
private int balance;//余额
//构造方法
public User(String ID, String name, int password, int balance) {
super();
this.ID = ID;
this.name = name;
this.password = password;
this.balance = balance;
}
/**
* @return the iD
*/
public String getID() {
return ID;
}
/**
* @param iD the iD to set
*/
public void setID(String ID) {
this.ID = ID;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the password
*/
public int getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(int password) {
this.password = password;
}
/**
* @return the balance
*/
public int getBalance() {
return balance;
}
/**
* @param balance the balance to set
*/
public void setBalance(int balance) {
this.balance = balance;
}
//存款功能的实现
public boolean saveMoney(int m){
System.out.println("正在验钞,请稍后.......");
if(m<0){
if(checkMoney(-m)){
return true;
}else{
return false;
}
}else if(m>0){
this.balance+=m;
System.out.println("操作成功!");
return true;
}
return false;
}
//取款操作
public boolean checkMoney(int m){
if(m<0){
if(saveMoney(-m)){
return true;
}else{
return false;
}
}else if(m>0){
System.out.println("请输入密码......");
if(checkPassword()){
this.balance-=m;
System.out.println("操作成功!");
return true;
}
}
return false;
}
//查询余额
public void search(){
System.out.println("正在处理,请稍后......");
if(ID.equals(this.ID)){
System.out.println("账户"+ID+"的余额为:"+balance);
}else{
System.out.println("您输入的账户不存在,请认真核查后重新输入!");
}
}
//检验密码操作
public boolean checkPassword(){
int i;
for(i=0;i<3;i++){
if(i>0){
System.out.println("密码输入错误,请重新输入密码!");
}
int pas=EnterNum.enterNum();
if(pas==password){
return true;
}
}
if(i==3)
System.out.println("密码输入错误3次,此次操作中断!");
return false;
}
//选择操作方法
public void print(){
System.out.println("----------------------欢迎使用自动取款机------------------------");
System.out.println("[存款----1]");
System.out.println("[取款----2]");
System.out.println("[查询----3]");
System.out.println("[退卡----0]");
}
}
//写一个类,进行对输入数字的判断。
package com.xt.lesson20;
import java.util.Scanner;
public class EnterNum {
static Scanner scanner=new Scanner(System.in);
public static String enterStr(){
String str="";
try{
str=scanner.next();
}catch(Exception e){
}
return str;
}
public static int enterNum(){
int m = 0;
try{
m=Integer.parseInt(enterStr());
}catch(Exception e){
System.out.println("您输入的数据不合法!请输入整数......");
}
return m;
}
}
//写一个主方法,进行判断操作的选择。
package com.xt.lesson20;
import java.util.ArrayList;
public class MachineTest {
public static void main(String[] args) {
ArrayList<User> userList=new ArrayList<User>();
userList.add(new User("未知","未知",0,0));//默认值
userList.add(new User("200981501","聂庆平",5264,7000));
userList.add(new User("200981512","黎明",1562,17000));
first:while(true){
userList.get(0).print();
int a=EnterNum.enterNum();
second: switch (a){
case 1:{
System.out.println("请输入您要存钱的账号:");
String id=EnterNum.enterStr();
int i;
for (i=0;i<userList.size();i++){
if(id.equals(userList.get(i).getID())){
System.out.println("请输入您存的钱数:");
int m=EnterNum.enterNum();
if(userList.get(i).saveMoney(m)){
break second;
}
}
}
if(i==userList.size()){
System.out.println("您输入的账户不存在!");
break second;
}
}
case 2:{
System.out.println("请输入您要取钱的账号:");
String id=EnterNum.enterStr();
int i;
for(i=0;i<userList.size();i++){
if(id.equals(userList.get(i).getID())){
System.out.println("请输入您取的钱数:");
int m=EnterNum.enterNum();
userList.get(i).checkMoney(m);
break second;
}
}
if(i==userList.size()){
System.out.println("您输入的账户不存在!");
break second;
}
}
case 3:{
System.out.println("请输入您的卡号!");
String id=EnterNum.enterStr();
int i;
for(i=0;i<userList.size();i++){
if(id.equals(userList.get(i).getID())){
userList.get(i).search();
break second;
}
}
if(i==userList.size()){
System.out.println("您输入的账户不存在!");
break second;
}
}
case 0:{
System.out.println("感谢您的使用,再见!");
System.exit(0);
}
}
}
}
}
阅读全文
0 0
- lesson15homework
- hive中UDTF编写和使用(转)
- 设计模式知识连载(11)---继承_7:多继承
- 十进制格式化 DecimalFormat的用法
- 深度学习在单图像超分辨率上的应用:SRCNN、Perceptual loss、SRResNet
- DiskLruCache基础
- lesson15homework
- 获取input原来的值,input还原
- 什么时候不应当使用虚函数
- NetCDF一维数据的写入
- spring注入bean进入工具类的静态方法
- 2017.10.22 Ardupilot开发者大会会议笔记,绝对干货满满!
- python-文件操作
- socket 实现简单的计算器
- 图解powerdesigner连接MySQL数据库