/*--------------------------------------------------------------------------------------------------------
*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编辑过]