| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 970 人关注过本帖
标题:[求助]初学java 请看下这个200电话卡的程序〈问题已解决〉
只看楼主 加入收藏
withoutme_hw
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2006-3-19
收藏
 问题点数:0 回复次数:8 
[求助]初学java 请看下这个200电话卡的程序〈问题已解决〉

/*--------------------------------------------------------------------------------------------------------
*Name:CardTest
*Author:withoutme_hw
*Description:a class which simulate a 200 telephone card ,and it has the following fuctions:dial,change
* password and so on
*Last updated:06 09 10
------------------------------------------------------------------------------------------------------*/
import java.util.*;

public class CardTest
{
public static void main(String[] args)
{
Card200[] cards = new Card200[32];
Scanner in = new Scanner(System.in);
int i;
for(i=0; i < 32; i++)
{
cards[i] = new Card200(50);
} //creat 32 new cards each with $50 initial money
//using the Card200s
System.out.println("please input your card number:");
int inCardNum = in.nextInt();
for(i=0; i < 32; i++)
{
if(cards[i].getID() == inCardNum)
{
System.out.println("enter the password:");
if(cards[i].rightKey(in.nextInt()))
break;
}
} //find the card to this user and check if he or she is the real user
if(i == 32)
{
System.out.println("you have entered wrong card number or wrong password");
return;
}
if(cards[i].isAvailable())
{ //has checked that he or she can use this card
System.out.println("access successfully");
while(true)
{
System.out.println("choose the services that you want to be provided:\n1.Dial 2.Change password 3.Inquire the availble money\n 4.Inquire the available time5.exit");
int choice = in.nextInt();
switch(choice)
{
case 1:
System.out.println("enter the number you want to dial:");
System.out.println("connecting to " + in.nextInt());
cards[i].dial();
break;
case 2:
System.out.println("enter the old password please:");
cards[i].changeKey(in.nextInt());
break;
case 3:
System.out.println("the available money now is $" + cards[i].getAvailableMoney());
break;
case 4:
//Date availableDate = new Date();
//availableDate = cards[i].getAvailableDate().getTime();
//System.out.println("the available time of this card is until " + availableDate);
// the three lines above is not correct but i don't konw why
cards[i].printAvailableDate();
break;
case 5:
return;
default:
System.out.println("please enter the numbers 1-4 to chose the services");
break;
}//end of switch()
}//end of while
}//end of if
}//end of main
}


class TeleCard
{

public TeleCard(double initialMoney)
{
ID = nextID;
nextID ++;
availableMoney = initialMoney;
GregorianCalendar availableUntil = new GregorianCalendar();
availableUntil.set(Calendar.YEAR, availableUntil.get(Calendar.YEAR) + 1);
availableUntil.set(Calendar.MONTH, (availableUntil.get(Calendar.MONTH) + 6) % 12);
}

public int getID()
{
return ID;
}

public void printAvailableDate()
{
//System.out.println("the available date of this card is until" + availableUntil.get(Calendar.YEAR) + availableUntil.get(Calendar.DAY_OF_YEAR));
System.out.println(availableUntil.get(Calendar.YEAR));
return;
}

public double getAvailableMoney()
{
return availableMoney;
}

public void dial()
{
GregorianCalendar calendar = new GregorianCalendar();//get the beginning time
System.out.println("connected... any key to hang up");
Scanner in = new Scanner(System.in);
String buffer = (String)in.nextLine();
GregorianCalendar calendar2 = new GregorianCalendar();//get the time when this call ends
this.availableMoney -= 0.1; //one call cost at least $0.1 no matter how short the connected time is
this.availableMoney -= ((calendar2.get(Calendar.HOUR) - calendar.get(Calendar.HOUR)) * 60 +
calendar2.get(Calendar.MINUTE) - calendar.get(Calendar.MINUTE)) * 0.6;
//deduct the expense of this call
System.out.println("call ended");
}

private int ID;
private static int nextID = 0000001;
private double availableMoney;
private GregorianCalendar availableUntil;
}

class Card200 extends TeleCard
{
public Card200(double initialMoney)
{
super(initialMoney);
key = 0;
} //initialize the card by using the father's construction function and initialize the key with 0

public void changeKey(int oldKey)
{
if(oldKey == this.key)
{
System.out.println("enter the new password: ");
Scanner in = new Scanner(System.in);
this.key = in.nextInt();
System.out.println("password changed.");
}
else
System.out.println("wrong password!");
} //change the password

//check if the input is the right password

public boolean rightKey(int inputedKey)
{
if(inputedKey == key)
{
cardAvailable = true;
return true;
}
else
return false;
}

public boolean isAvailable()
{
return cardAvailable;
}

private int key;
private boolean cardAvailable = false;
}




才学没几天 ,请大家来帮忙来了 谢啦!

[此贴子已经被作者于2006-9-11 21:04:45编辑过]

搜索更多相关主题的帖子: java 电话卡 初学 
2006-09-10 22:24
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 

代码好规范啊~~!
就是太长了,明天再看

[此贴子已经被作者于2006-9-10 23:30:34编辑过]


曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-10 22:47
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 
楼上的灌水

问问楼主 是不能编译还是不能出正确结果啊?

win32汇编
病毒 加密
目前兴趣所在
2006-09-11 07:07
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你应该把你的错误信息也帖出来

可惜不是你,陪我到最后
2006-09-11 09:00
withoutme_hw
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2006-3-19
收藏
得分:0 
哦,对不起啊,忘了
运行的时候先提示你输入卡号:
我初始化时是把它初始化为从0-32号
输入任何一个都可以
然后提示输入密码,初始密码为0
键入即可,
然后可以选择1-4 四种服务,4号有问题

我是从C 转型到java的 才刚学java 请大家指教啊!

好好学C 天天向上
2006-09-11 12:24
withoutme_hw
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2006-3-19
收藏
得分:0 
对了,还有
这是在JDK5.0以上版本才能运行的程序

好好学C 天天向上
2006-09-11 16:05
cumtkl
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-5-10
收藏
得分:0 

GregorianCalendar availableUntil = new GregorianCalendar();

去掉前面的GregorianCalendar即可.

2006-09-11 17:42
withoutme_hw
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2006-3-19
收藏
得分:0 


非常感谢


好好学C 天天向上
2006-09-11 20:28
lengxin
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2006-9-14
收藏
得分:0 

goog 不错啊,就是不知道能不能运行!!
继续努力 


2006-09-14 21:02
快速回复:[求助]初学java 请看下这个200电话卡的程序〈问题已解决〉
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019557 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved