pta java 7-1 jmu-Java-05集合(泛型)-10-GeneralStack分数 15 作者 郑如滨 单位 集美大学
·
以前定义的IntegerStack接口,只能用于存放Integer类型的数据。然而对于栈来说,不管内部存放的是什么类型的数据,基本操作与元素的具体类型无关。
1. 编写一个通用的GeneralStack接口,接口中的操作对任何引用类型的数据都适用。
一旦定义完毕,只能存放一种类型的数据,比如只能存放String或只能存放Integer。GeneralStack接口方法如下:
push(item); //如item为null,则不入栈直接返回null。 pop(); //出栈,如为栈为空,则返回null。 peek(); //获得栈顶元素,如为空,则返回null. public boolean empty();//如为空返回true public int size(); //返回栈中元素数量
2.定义GeneralStack的实现类ArrayListGeneralStack
内部使用ArrayList对象存储,属性名为list。
方法:public String toString()//该方法的代码为return list.toString();
提示:
- 不用使用top指针。
- 直接复用ArrayList中已有的方法。
- pop时应将相应的元素从ArrayList中移除。
- 代码中不要出现类型不安全的强制转换。
3.定义Car对象
属性:
private int id;
private String name;
方法:Eclipse自动生成setter/getter,toString方法。
4.main方法说明
- 输入选项,有
quit, Integer, Double, Car4个选项。如果输入quit,则直接退出。否则,输入整数m与n。m代表入栈个数,n代表出栈个数。然后声明栈变量stack。 - 输入
Integer,打印Integer Test。建立可以存放Integer类型的ArrayListGeneralStack。入栈m次,出栈n次。打印栈的toString方法。最后将栈中剩余元素出栈并累加输出。 - 输入
Double,打印Double Test。剩下的与输入Integer一样。 - 输入
Car,打印Car Test。其他操作与Integer、Double基本一样。只不过最后将栈中元素出栈,并将其name依次输出。
2、3、4步骤做完都要使用代码System.out.println(stack.getClass().getInterfaces()[0]);打印标识信息
特别注意:如果栈为空的时候继续出栈,则返回null
输入样例
Integer
5
2
1 2 3 4 5
Double
5
3
1.1 2.0 4.9 5.7 7.2
Car
3
2
1 Ford
2 Cherry
3 BYD
quit
输出样例
Integer Test
push:1
push:2
push:3
push:4
push:5
pop:5
pop:4
[1, 2, 3]
sum=6
interface GeneralStack
Double Test
push:1.1
push:2.0
push:4.9
push:5.7
push:7.2
pop:7.2
pop:5.7
pop:4.9
[1.1, 2.0]
sum=3.1
interface GeneralStack
Car Test
push:Car [id=1, name=Ford]
push:Car [id=2, name=Cherry]
push:Car [id=3, name=BYD]
pop:Car [id=3, name=BYD]
pop:Car [id=2, name=Cherry]
[Car [id=1, name=Ford]]
Ford
interface GeneralStack
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
public static void main(String[] args) throws ParseException
{
LandPhonelnlandRule landPhonelnlandRule = new LandPhonelnlandRule();
LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
LandlinePhoneCharging landlinePhoneCharging = new LandlinePhoneCharging();
landlinePhoneCharging.getChargeRules().add(landPhoneInCityRule);
landlinePhoneCharging.getChargeRules().add(landPhoneInProvinceRule);
landlinePhoneCharging.getChargeRules().add(landPhonelnlandRule);
ArrayList<User> UserList = new ArrayList<>();
Scanner in = new Scanner(System.in);
String m = in.nextLine();
while (!m.equals("end"))
{
Judge judge = new Judge();
if (judge.isUserInformation(m))
{
String regex= "(0791)[\\d]{7,8}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(m);
while (matcher.find())
{
int flag=0;
for (User user:UserList)
{
if (user.getNumber().equals(matcher.group(0)))
{
flag=1;
break;
}
}
if (flag==0)
{
UserList.add(new User(matcher.group(0)));
}
}
}
if (judge.isCallingInformation(m))
{
String []a = m.split(" ");
try
{
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date startTime =format.parse(a[2]+" "+a[3]);
Date endTime = format.parse(a[4]+" "+a[5]);
for (User user:UserList)
{
if (user.getNumber().equals(a[0].substring(2)))
{
if (judge.isInCityCode(a[1].substring(0,4)))
{
user.getUserRecords().addCallingInCityRecords(new CallRecord(startTime,endTime,a[0].substring(2,6),a[1].substring(0,4)));
}
else if (judge.isInProvinceCode(a[1].substring(0,4)))
{
user.getUserRecords().addCallingInProvinceRecords(new CallRecord(startTime,endTime,a[0].substring(2,6),a[1].substring(0,4)));
}
else
{
user.getUserRecords().addCallingInLandRecords(new CallRecord(startTime,endTime,a[0].substring(2,6),a[1].substring(0,4)));
}
}
}
}
catch (ParseException e)
{
e.printStackTrace();
}
}
m = in.nextLine();
}
for (User user:UserList)
{
user.setChargeMode(landlinePhoneCharging);
}
for (int i= 0;i<UserList.size();i++)
{
int index=-1;
Double min = Double.parseDouble(UserList.get(i).getNumber());
for (int j = i+1;j<UserList.size();j++)
{
if (Double.parseDouble(UserList.get(j).getNumber())<min)
{
min = Double.parseDouble(UserList.get(j).getNumber());
index=j;
}
}
if (min!=Double.parseDouble(UserList.get(i).getNumber()))
{
User user = UserList.get(i);
UserList.set(i,UserList.get(index));
UserList.set(index,user);
}
}
for (User user:UserList)
{
System.out.printf("%s %.1f %.1f\n",user.getNumber(),user.getChargeMode().calCost(user.getUserRecords()),user.getBalance());
}
}
}
abstract class CallChargeRule extends ChargeRule
{
public double calCost(ArrayList<CallRecord> callRecords)
{
return 0;
}
}
class CallRecord extends CommunicationRecord
{
private Date startTime;
private Date endTime;
private String callingAddressAreaCode;
private String answerAddressAreaCode;
public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode)
{
this.startTime = startTime;
this.endTime = endTime;
this.callingAddressAreaCode = callingAddressAreaCode;
this.answerAddressAreaCode = answerAddressAreaCode;
}
public Date getStartTime()
{
return startTime;
}
public void setStartTime(Date startTime)
{
this.startTime = startTime;
}
public Date getEndTime()
{
return endTime;
}
public void setEndTime(Date endTime)
{
this.endTime = endTime;
}
public String getCallingAddressAreaCode()
{
return callingAddressAreaCode;
}
public void setCallingAddressAreaCode(String callingAddressAreaCode)
{
this.callingAddressAreaCode = callingAddressAreaCode;
}
public String getAnswerAddressAreaCode()
{
return answerAddressAreaCode;
}
public void setAnswerAddressAreaCode(String answerAddressAreaCode)
{
this.answerAddressAreaCode = answerAddressAreaCode;
}
}
abstract class ChargeMode
{
private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public ArrayList<ChargeRule> getChargeRules()
{
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules)
{
this.chargeRules = chargeRules;
}
public double calCost(UserRecords userRecords)
{
return 0;
}
public double getMonthlyRent()
{
return 0;
}
}
abstract class ChargeRule
{
public double calCost(ArrayList<CallRecord> callRecords)
{
return 0;
}
}
abstract class CommunicationRecord
{
protected String callingNumber;
protected String answerNumber;
public String getCallingNumber()
{
return callingNumber;
}
public void setCallingNumber(String callingNumber)
{
this.callingNumber = callingNumber;
}
public String getAnswerNumber()
{
return answerNumber;
}
public void setAnswerNumber(String answerNumber)
{
this.answerNumber = answerNumber;
}
}
class Judge
{
public boolean isUserInformation(String s)
String pattern ="(u-0791)[\\d]{7,8}[\\s][0-2]";
return s.matches(pattern);
}
public boolean isCallingInformation(String s)
{
String pattern = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
return s.matches(pattern);
}
public boolean isInCityCode(String s)
{
String pattern = "0791";
return s.equals(pattern);
}
public boolean isInProvinceCode(String s)
{
String pattern = "((0790)|(0701)|((079)[2-9]))";
return s.matches(pattern);
}
}
class LandlinePhoneCharging extends ChargeMode
{
private double monthlyRent = 20;
public double calCost(UserRecords userRecords)
{
double sum=0;
sum = getChargeRules().get(0).calCost(userRecords.getCallingInCityRecords())+getChargeRules().get(1).calCost(userRecords.getCallingInProvinceRecords())+getChargeRules().get(2).calCost(userRecords.getCallingInLandRecords());
return sum;
}
public double getMonthlyRent()
{
return monthlyRent;
}
}
class LandPhoneCharging
{
private double monthly = 20;
}
class LandPhoneInCityRule extends CallChargeRule
{
public double calCost(ArrayList<CallRecord> callRecords)
{
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords)
{
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0)
{
minute = second/60+1;
}
else
{
minute = second/60;
}
sumCost = sumCost+minute*0.1;
}
return sumCost;
}
}
class LandPhoneInProvinceRule extends CallChargeRule
{
public double calCost(ArrayList<CallRecord> callRecords)
{
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords)
{
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0)
{
minute = second/60+1;
}
else
{
minute = second/60;
}
sumCost = sumCost+minute*0.3;
}
return sumCost;
}
}
class LandPhonelnlandRule extends CallChargeRule
{
public double calCost(ArrayList<CallRecord> callRecords)
{
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords)
{
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0)
{
minute = second/60+1;
}
else
{
minute = second/60;
}
sumCost = sumCost+minute*0.6;
}
return sumCost;
}
}
class MessageRecord extends CommunicationRecord
{
private String message;
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
}
class User
{
private UserRecords userRecords = new UserRecords();
private double balance = 100;
private ChargeMode chargeMode;
private String number;
public User(String number)
{
this.number = number;
}
public double calBalance()
{
return 0;
}
public double calCost()
{
return 0;
}
public double getBalance()
{
return balance-chargeMode.calCost(userRecords)-chargeMode.getMonthlyRent();
}
public UserRecords getUserRecords()
{
return userRecords;
}
public void setUserRecords(UserRecords userRecords)
{
this.userRecords = userRecords;
}
public ChargeMode getChargeMode()
{
return chargeMode;
}
public void setChargeMode(ChargeMode chargeMode)
{
this.chargeMode = chargeMode;
}
public String getNumber()
{
return number;
}
public void setNumber(String number)
{
this.number = number;
}
}
class UserRecords
{
private ArrayList<CallRecord> callingInCityRecords = new ArrayList<>();
private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>();
private ArrayList<CallRecord> callingInLandRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInCityRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInLandRecords = new ArrayList<>();
private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<>();
private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>();
public void addCallingInCityRecords(CallRecord callRecord)
{
callingInCityRecords.add(callRecord);
}
public void addCallingInProvinceRecords(CallRecord callRecord)
{
callingInProvinceRecords.add(callRecord);
}
public void addCallingInLandRecords(CallRecord callRecord)
{
callingInLandRecords.add(callRecord);
}
public void addAnswerInCityRecords(CallRecord callRecord)
{
answerInCityRecords.add(callRecord);
}
public void addAnswerInProvinceRecords(CallRecord callRecord)
{
answerInProvinceRecords.add(callRecord);
}
public void addAnswerInLandRecords(CallRecord callRecord)
{
answerInLandRecords.add(callRecord);
}
public ArrayList<CallRecord> getCallingInCityRecords()
{
return callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords()
{
return callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords()
{
return callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords()
{
return answerInCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords()
{
return answerInProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords()
{
return answerInLandRecords;
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)