⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atm.java

📁 字符界面模拟ATM。输入姓名等进行下一步。 比较适合JAVA初学者参考
💻 JAVA
字号:
package gx;
import java.io.*;
import java.util.*;
import java.text.*;
class PersonInfo{
	private int count;
	private String name;
	private Date datetime;
	private String cardId;
	private float money;
	public void setCount(int count){
		this.count=count;
	}
	public int getCount(){
		return count;
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
	public void setDate(Date datetime){
		this.datetime=datetime;
	}
	public Date getDate(){
		return datetime;
	}
	public void setCardId(String cardId){
		this.cardId=cardId;
	}
	public String getCardId(){
		return cardId;
	}	
	public void setMoney(float money){
		this.money=money;
	}
	public float getMoney(){
		return money;
	}
}
public class ATM {
	public static int count;
	private int flag;
	private float money;
	private Map map;
	private PersonInfo person;
	public void setPerson(PersonInfo person){
		this.person=person;
	}
	public PersonInfo getPerson(){
		return person;
	}
	public ATM(){

	}
	public void setSet(Map map){
		this.map=map;
	}
	public Map getMap(){
		return map;
	}
	InputStreamReader reader=new InputStreamReader(System.in);
	BufferedReader br=new BufferedReader(reader);
	
	public void init(Map map) throws IOException{
		System.out.println("请输入您的姓名:");	
		String name=br.readLine();
		Iterator<Map.Entry<Integer,PersonInfo>> it=map.entrySet().iterator();	
		while(it.hasNext()){
			Map.Entry entry=it.next();
			PersonInfo person=(PersonInfo)(entry.getValue());
			if(name.equals(person.getName())){ 
				do{
					initvalue();
					int flag=Integer.parseInt(br.readLine());
					switch(flag){
					case 1:
						try{
							store(person);
							System.out.println("是否继续,继续请输入4");
							flag=Integer.parseInt(br.readLine());
							if(flag==4)
								continue;
							else
								exit();
						}catch(IOException e){
							System.out.println(e.getMessage());
						}
						break;
					case 2:
						try{
							deposit(person);
							System.out.println("是否继续,继续请输入4");
							flag=Integer.parseInt(br.readLine());
							if(flag==4)
								continue;
							else
								exit();
						}catch(IOException e){
							System.out.println(e.getMessage());
						}	
						break;
					case 3:
						try{
							display(person);	
							System.out.println("是否继续,继续请输入4");
							flag=Integer.parseInt(br.readLine());
							if(flag==4)
								continue;
							else
								exit();
						}catch(IOException e){
							System.out.println(e.getMessage());
						}	
						break;				
					}	
					
				}while(flag==0);
				
			}
		}
		System.out.println("对不起!您还没有开户!");
	}
	public void store(PersonInfo person) throws IOException {		
		System.out.println("请输入您所要存的数目:");
		money=Float.parseFloat(br.readLine());
		person.setMoney(person.getMoney()+money);
	}
	public void exit() {
	   System.out.println("谢谢您的使用!");
	   System.exit(0);
	}
	public void initvalue(){
		System.out.println("存钱请输入1   取钱请输入2  余额查询请输入3    退出请输入0");	
	}
	public void deposit(PersonInfo person)throws IOException {
		System.out.println("请输入您所要取的数目:");	
		money=Float.parseFloat(br.readLine());
		if(money>person.getMoney())
			System.out.println("对不起!您的余额不足!现有金额为:"+person.getMoney());
		else
			person.setMoney(person.getMoney()-money);
	}
	public void display(PersonInfo person)throws IOException {
		System.out.println("您于:"+person.getDate()+"开户,帐户为:"+person.getCount());
		System.out.println("余额为:"+person.getMoney());		
	}
	
	public static void main(String[] args)throws Exception {
		Date date1,date2;
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		
		PersonInfo person1=new PersonInfo();
		person1.setCount(++count);
		person1.setName("小王");
	    date1=dateFormat.parse("2005-05-25");	
		person1.setDate(date1);
		person1.setCardId("123456789");
		person1.setMoney(500);
		
		PersonInfo person2=new PersonInfo();
		person2.setCount(++count);
		person2.setName("小张");
		date2=dateFormat.parse("2007-01-05");
		person2.setDate(date2);
		person2.setCardId("7894561233");
		person2.setMoney(1000);
		Map<Integer,PersonInfo> map=new HashMap<Integer,PersonInfo>();
		map.put(1, person1);
		map.put(2, person2);
		try{
		ATM atm=new ATM();
		atm.setSet(map);
		atm.init(map);
		}catch(Exception e){
			e.printStackTrace();
		}		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -