当前位置: 代码迷 >> Java相关 >> [求助]初学java 请看下这个200电话卡的程序〈问题已解决〉
  详细解决方案

[求助]初学java 请看下这个200电话卡的程序〈问题已解决〉

热度:93   发布时间:2006-09-10 22:24:58.0
[求助]初学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-9-10 23:30:34编辑过]


----------------解决方案--------------------------------------------------------
楼上的灌水

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

----------------解决方案--------------------------------------------------------
你应该把你的错误信息也帖出来
----------------解决方案--------------------------------------------------------
哦,对不起啊,忘了
运行的时候先提示你输入卡号:
我初始化时是把它初始化为从0-32号
输入任何一个都可以
然后提示输入密码,初始密码为0
键入即可,
然后可以选择1-4 四种服务,4号有问题

我是从C 转型到java的 才刚学java 请大家指教啊!
----------------解决方案--------------------------------------------------------
对了,还有
这是在JDK5.0以上版本才能运行的程序
----------------解决方案--------------------------------------------------------

GregorianCalendar availableUntil = new GregorianCalendar();

去掉前面的GregorianCalendar即可.


----------------解决方案--------------------------------------------------------


非常感谢


----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------
  相关解决方案