【上机作业】【java】2010.3.24
1.设计一个银行卡类,属性有卡号,密码,姓名
,金额 方法:存款,取款,查询,挂失,转帐
improt java.util.Scanner;
….
Scanner s=new Scanner(System.in);
int i=s.nextInt();//从键盘输入
import java.util.Scanner;
class card{
static int id=-1;
String card_id;
double money;
String name,pw;
Boolean lose_flag=false;
card(String i,String p,double m,String n){
this.card_id=i;
this.pw=p;
this.money=m;
this.name=n;
id++;
System.out.println(“创建成功!”);
}
void set_lose(){
this.lose_flag=true;
System.out.println(“设置挂失成功!”);
}
void save_money(){
double m;
Scanner sc=new Scanner(System.in);
System.out.print(“输入存款金额:”);
m=sc.nextDouble();
this.money+=m;
System.out.println(“存款成功!”);
}
void get_money(){
double m;
Scanner sc=new Scanner(System.in);
System.out.print(“输入取款金额:”);
m=sc.nextDouble();
if(this.money>=m){
this.money-=m;
System.out.println(“存款成功!”);
}
else
System.out.println(“余额不足!”);
}
static int get_id(card []c,String i){
int x=-1;
for(int m=0;m<=id;m++){
if(c[m].card_id.equals(i)){
x=m;
break;
}
}
return x;
}
void display_card(){
System.out.println(“卡号:”+this.card_id);
System.out.println(“金额:”+this.money);
System.out.println(“姓名:”+this.name);
System.out.println(“是否挂失:”+this.lose_flag);
}
static void transferofaccount(card []c,int id){
double m;
String i;
int n;
Scanner sc=new Scanner(System.in);
do{
System.out.print(“输入对方卡号:”);
i=sc.next();
n=get_id(c,i);
if(n==-1){
System.out.println(“未找到该卡!”);
}
}while(n==-1);
System.out.print(“输入转账金额::”);
m=sc.nextDouble();
if(c[id].money>=m){
c[id].money-=m;
c[n].money+=m;
System.out.println(“转账成功!当前余额:”+c[id].money);
}
else
System.out.println(“金额不足,转账失败!”);
}
public static void main(String []args){
int i=-1,chose;
String now_id;
String p;
Boolean pass=false;
Scanner sc=new Scanner(System.in);
card []cards=new card[100];
cards[0]=new card(“1001″,”123″,100,”zjl”);
cards[1]=new card(“1002″,”123″,100,”abc”);
cards[2]=new card(“1003″,”123″,100,”1234123”);
while(!pass){
System.out.println(“nn——————————- 账户管理系统登录 ——————————-“);
System.out.print(“输入卡号:”);
now_id=sc.next();
i=get_id(cards,now_id);
if(i==-1){
System.out.println(“未知卡号,请重新输入!”);
continue;
}
System.out.print(“输入密码:”);
p=sc.next();
if(p.equals(cards[i].pw)){
pass=true;
break;
}
else
System.out.println(“密码错误”);
}
do{
display();
chose=sc.nextInt();
switch(chose){
case 1:cards[i].save_money();break;
case 2:cards[i].get_money();break;
case 3:cards[i].display_card();break;
case 4:cards[i].set_lose();break;
case 5:transferofaccount(cards,i);break;
}
}while(chose!=0);
}
static void display(){
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(” * 0.退出 * “);
System.out.println(” ******************************************* “);
}
}
2.设计一个图书类,属性有书名,作者,出版社,价格,页数,字数(页数=字数/1200*系数(有图片的系数为0.75,无系数为0.95))
设计一个方法,输出图书的所有属性信息
class book{
String name,writer,publishing;
long word_count,PageCount;
double coefficient,value;
boolean image=false;
book(String n,String w,String p,long w_c,double v,boolean i){
this.name=n;
this.writer=w;
this.publishing=p;
this.value=v;
this.word_count=w_c;
this.image=i;
if(i){
this.coefficient=0.75;
}
else this.coefficient=0.95;
this.PageCount=(long)(w_c/1200*this.coefficient);
}
void show(){
System.out.println(“书名:”+this.name);
System.out.println(“作者:”+this.writer);
System.out.println(“出版社:”+this.publishing);
System.out.println(“字数:”+this.word_count);
System.out.println(“页数”+this.PageCount);
System.out.println(“价格:”+this.value);
}
public static void main(String []args){
book []books=new book[1000];
books[0]=new book(“测试书名”,”测试作者”,”测试出版社”,1200000,45.00,true);
books[1]=new book(“测试书名1″,”测试作者1″,”测试出版社3”,100000,22.00,false);
System.out.println(“—————————图书1—————————–“);
books[0].show();
System.out.println(“————————————————————-“);
System.out.println(“—————————图书2—————————–“);
books[1].show();
System.out.println(“————————————————————-“);
}
}