java在线商店代码和实验报告.docx

上传人:b****2 文档编号:2751359 上传时间:2023-05-04 格式:DOCX 页数:18 大小:45.74KB
下载 相关 举报
java在线商店代码和实验报告.docx_第1页
第1页 / 共18页
java在线商店代码和实验报告.docx_第2页
第2页 / 共18页
java在线商店代码和实验报告.docx_第3页
第3页 / 共18页
java在线商店代码和实验报告.docx_第4页
第4页 / 共18页
java在线商店代码和实验报告.docx_第5页
第5页 / 共18页
java在线商店代码和实验报告.docx_第6页
第6页 / 共18页
java在线商店代码和实验报告.docx_第7页
第7页 / 共18页
java在线商店代码和实验报告.docx_第8页
第8页 / 共18页
java在线商店代码和实验报告.docx_第9页
第9页 / 共18页
java在线商店代码和实验报告.docx_第10页
第10页 / 共18页
java在线商店代码和实验报告.docx_第11页
第11页 / 共18页
java在线商店代码和实验报告.docx_第12页
第12页 / 共18页
java在线商店代码和实验报告.docx_第13页
第13页 / 共18页
java在线商店代码和实验报告.docx_第14页
第14页 / 共18页
java在线商店代码和实验报告.docx_第15页
第15页 / 共18页
java在线商店代码和实验报告.docx_第16页
第16页 / 共18页
java在线商店代码和实验报告.docx_第17页
第17页 / 共18页
java在线商店代码和实验报告.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

java在线商店代码和实验报告.docx

《java在线商店代码和实验报告.docx》由会员分享,可在线阅读,更多相关《java在线商店代码和实验报告.docx(18页珍藏版)》请在冰点文库上搜索。

java在线商店代码和实验报告.docx

java在线商店代码和实验报告

Java语言程序设计大作业实验报告

 

学院名称:

实验班级

学号:

姓名:

指导教师:

递交日期:

 

目录

1系统开发步骤(5分)1

2系统概述(10分)1

2.1ArrayList集合类功能说明1

2.2系统中实现的类以及类中的方法(20分)2

2.2.1ShoppingCart类2

2.3系统中实现的事件监听接口以及接口中方法的说明(10分)2

3实验中遇到的问题以及解决的方法(35分)3

4实验心得(10分)3

5对本课程的意见和建议(10分)

5.1理论部分3

5.2实验部分4

附件1

 

课程名称:

Java语言程序设计实验成绩:

实验名称:

在线购物系统班级:

实验指导老师学号:

实验总学时:

10学时实验起止时间:

2012-11-14—2012-12-30

▪1系统开发步骤(5分)

▪1.1安装java软件

▪1.2运行Java软件,建立Java项目,按照要求建立题目所要求的类

▪1.3创建类Estore(电子商店)、ShoppingCart(购货车)

▪1.4根据实验要求写在线商店的代码

▪1.5调试代码

▪1.6运行代码

▪2系统概述(10分)

▪2.1ArrayList集合类功能说明

▪2.1.1ArrayList是一个动态数组,它的特点是:

动态的增加和减少元素,实现了ICollection和IList接口,灵活的设置数组的大小。

Arraylist:

实现一个数组,它的规模可变并且能像链表一样被访问。

基于Array的ListVectorArrayList适合查询

▪2.1.2java中的ArrayList类的使用弥补了数组使用的麻烦,实现了List接口,此类还提供一些方法来操作内部用来存储列表的数组的大小。

▪2.1.3方法

Remove方法是用于删除一个元素,通过元素本身的引用来删除

Add方法是用于添加一个元素到当前列表的末尾

▪2.2系统中实现的类以及类中的方法(20分)

▪2.2.1ShoppingCart类

对该类的提供的功能进行描述

(1)将商品添加到购物车

(2)在购物车里将商品按照选择进行移除

(3)按要求进行结账

(4)购物车结账后进行重新购物并将上次购物结账单归零

▪2.2.2EStore

选则商品删除商品

结账清空商品

商品满五件清空后结账

▪2.3系统中实现的事件监听接口以及接口中方法的说明(10分)

事件监听器:

ActionListener

接口中方法:

privatevoidaddListeners()

系统中用于:

Button类

添加到在组件方法:

privatevoidaddListeners(){

/**让Applet作为各个组件的事件监听器(ActionListener)。

*/

purchaseButton.addActionListener(this);

removeButton.addActionListener(this);

checkoutButton.addActionListener(this);

resetButton.addActionListener(this);

}

▪3实验中遇到的问题以及解决的方法(35分)

在此次在线商店的大作业过程中遇到了许多的困难,先是拿到题目并看到老师给的部分代码很迷茫,不知道从哪里开始做,,没有头绪,然后大作业有三节课可以再课堂上做,所以就可以和同学讨论还有请教老师。

写代码的时候有些方法不是很熟悉,但这些都没有让我失去信心。

在使用监听器的时候也遇到了不少麻烦,不知道该怎么将监听器放到容器里面。

最后通过请教老师和同学最后成功地将监听器添加进去了,然后还有的问题就是,在里面添加一些提示语句,刚开始加语句的时候,加完程序没有反应,到最后连删除按钮都不起作用了,想了很多种办法,最后终于找到错误的所在,是因为大括号括的不对,所以让按钮失去了作用。

将大括号填对之后,代码就可以正确运行了。

▪4实验心得(10分)

通过这次的大作业我学到了很多平常没有注意和重视的知识,所以在写代码的过程中遇到了很多问题,不知道该怎么办,还好可以在课堂上和同学、老师交流一下,解决了不少的问题。

虽然这次的大作业对我来说比较难,但是我还是通过自己的不断努力把这次大作业顺利完成了。

我在这个不断摸索前进的过程中学到了一个道理,就是课堂和实验的知识是实现代码的重要基础,但是一个人在面对困难时更要有顽强的意志力和拼搏精神。

在此次大作业中,我不但巩固了之前学的知识还学到了一些平常不知道的知识,对自己来说真的是受益匪浅。

▪5对本课程的意见和建议(10分)

▪5.1理论部分

在理论部分,我觉得老师上课讲的很有意思,并且通过PPt课件的方式给我们讲课很生动,并且不断举例联系实际,让我们更能接受并理解。

▪5.2实验部分

在实验部分,通过每节课下发实验任务,让我们自己动手写代码,理论结合实际,在课堂上就能解决一些问题,在大家遇到共同的问题解决不了时,老师会集体讲解,我觉得这样很好。

 

附件1:

EStore的代码

importjava.applet.Applet;

importjava.awt.*;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

/*

*E-Store

*如果所购商品总额大于$100,免费运送,否则加$2.00的运送费。

*/

publicclassEStoreextendsAppletimplementsActionListener{

//常量

privatestaticfinalfloatCOAT_PRICE=50.00f;

privatestaticfinalfloatSHIRT_PRICE=25.50f;

privatestaticfinalfloatSHOES_PRICE=39.99f;

privatestaticfinalfloatHAT_PRICE=10.00f;

privatestaticfinalStringTOTAL_STR="Yourcurrenttotal=$";

privatestaticfinalStringRESET="RESET";

privatestaticfinalStringPURCHASE="PurchaseItem";

privatestaticfinalStringREMOVE="RemoveItem";

privatestaticfinalStringCHECK_OUT="Checkout";

privatestaticfinalStringCOAT_STR="WINTERCOAT";

privatestaticfinalStringSHIRT_STR="LONGSLEEVESHIRT";

privatestaticfinalStringSHOES_STR="LEATHERSHOE";

privatestaticfinalStringHAT_STR="UofACAP";

privatestaticfinalStringWELCOME="WelcometoCMPUT114E-Store";

privatestaticfinalintMAX_ITEMS=5;//购物篮中最多能容纳的商品件数

privatestaticfinalStringCHECK=null;

//实例变量

privateListproductList;

privatePanelmainPanel;

ButtonpurchaseButton,removeButton,checkoutButton,resetButton;

ShoppingCartcart;

LabeltotalLabel;

LabeltotalLabe2;

publicvoidinit(){

/*

*这是被applet调用的第一个方法

*初始化Applet:

初始化变量,添加部件等

*/

makeMainPanel();

this.add(mainPanel);

this.validate();

this.setBackground(Color.pink);

cart=newShoppingCart();

}

/*为完成你的类,你可以创建一些私有方法

*/

privatePanelmakeButtonPanel(){

/*

*定义按钮并将其添加到面板(Panel)里,并返回该面板

*/

PanelbuttonPanel=newPanel();

buttonPanel.setLayout(newGridLayout(2,2));

purchaseButton=newButton(EStore.PURCHASE);

buttonPanel.add(purchaseButton);

removeButton=newButton(EStore.REMOVE);

buttonPanel.add(removeButton);

checkoutButton=newButton(EStore.CHECK_OUT);

buttonPanel.add(checkoutButton);

resetButton=newButton(EStore.RESET);

buttonPanel.add(resetButton);

addListeners();

purchaseButton.setBackground(Color.yellow);

removeButton.setBackground(Color.yellow);

checkoutButton.setBackground(Color.yellow);

resetButton.setBackground(Color.yellow);

returnbuttonPanel;

}

privateScrollPanemakeListPanel(){

/*为列表定义一个滚动窗口(ScrollPane),并返回该滚动窗口

*/

ScrollPanesc=newScrollPane();

sc.add(makeList());

returnsc;

}

privatevoidmakeMainPanel(){

/*

*给主面板添加所有的组件:

标签,列表,按钮面板等。

*/

LabelwelcomeLabel=newLabel(WELCOME);

mainPanel=newPanel();

mainPanel.setLayout(newGridLayout(5,1));

mainPanel.add(welcomeLabel);

mainPanel.add(makeListPanel());

mainPanel.add(makeButtonPanel());

totalLabe2=newLabel();

mainPanel.add(totalLabe2);

totalLabel=newLabel(EStore.TOTAL_STR);

mainPanel.add(totalLabel);

mainPanel.validate();

}

privateListmakeList(){

/*

*给4件商品定义一个列表,一次只能选择一件商品

*/

productList=newList(4,false);

productList.add(EStore.COAT_STR+"------------------------"+EStore.COAT_PRICE);

productList.add(EStore.SHIRT_STR+"-----------------------"+EStore.SHIRT_PRICE);

productList.add(EStore.SHOES_STR+"-----------------------"+EStore.SHOES_PRICE);

productList.add(EStore.HAT_STR+"------------------------"+EStore.HAT_PRICE);

this.setBackground(Color.lightGray);

returnproductList;

}

privatevoidaddListeners(){

/*

*让Applet作为各个组件的事件监听器(ActionListener)。

*/

purchaseButton.addActionListener(this);

removeButton.addActionListener(this);

checkoutButton.addActionListener(this);

resetButton.addActionListener(this);

}

publicvoidactionPerformed(ActionEventarg0){

//TODO自动生成方法存根

StringwhichButton=arg0.getActionCommand();

if(whichButton.equals(EStore.PURCHASE)){

totalLabe2.setText("");

if(productList.getSelectedIndex()==0){

if(cart.add(EStore.COAT_STR,EStore.COAT_PRICE))

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

else

totalLabe2.setText("Yourcartisfull,checkoutthenstartagain");

}

if(productList.getSelectedIndex()==1){

if(cart.add(EStore.SHIRT_STR,EStore.SHIRT_PRICE))

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

else

totalLabe2.setText("Yourcartisfull,checkoutthenstartagain");

}

if(productList.getSelectedIndex()==2){

if(cart.add(EStore.SHOES_STR,EStore.SHOES_PRICE))

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

else

totalLabe2.setText("Yourcartisfull,checkoutthenstartagain");

}

if(productList.getSelectedIndex()==3){

if(cart.add(EStore.HAT_STR,EStore.HAT_PRICE))

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

else

totalLabe2.setText("Yourcartisfull,checkoutthenstartagain");

}

if(productList.getSelectedIndex()<0)

{

totalLabe2.setText("Error!

Noitemcurrentlyselected!

");

}}

if(whichButton.equals(EStore.REMOVE)){

totalLabe2.setText("");

if(productList.getSelectedIndex()==0){

if(cart.remove(EStore.COAT_STR,EStore.COAT_PRICE))

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

else

totalLabe2.setText("Nosuchitemtoremove!

");

}

if(productList.getSelectedIndex()==1){

if(cart.remove(EStore.SHIRT_STR,EStore.SHIRT_PRICE))

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

else

totalLabe2.setText("Nosuchitemtoremove!

");

}

if(productList.getSelectedIndex()==2){

if(cart.remove(EStore.SHOES_STR,EStore.SHOES_PRICE))

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

else

totalLabe2.setText("Nosuchitemtoremove!

");

}

if(productList.getSelectedIndex()==3){

if(cart.remove(EStore.HAT_STR,EStore.HAT_PRICE))

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

else

totalLabe2.setText("Nosuchitemtoremove!

");

}

if(productList.getSelectedIndex()<0)

{

totalLabe2.setText("Nosuchitemtoremove!

");

}}

if(whichButton.equals(EStore.CHECK_OUT)&&cart.getTotal()!

=0)

{

totalLabe2.setText("");

cart.checkOut();

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

totalLabe2.setText("Thankyou!

Click'RESET'tostartagain");

}

if(whichButton.equals(EStore.CHECK_OUT)&&cart.getTotal()==0)

totalLabe2.setText("Nothingtocheckout!

");

if(whichButton.equals(EStore.RESET)){

cart.reset();

totalLabel.setText(EStore.TOTAL_STR+cart.getTotal());

totalLabel.setText("Nothing");

}}

}

ShoppingCart的代码

importjava.util.Stack;

/*

*该类的一个实例将会:

模拟一个购物篮,该购物篮里将会保存在“在线购物商店”中所购买的商品(最多可以容纳5件商品)以及购物总额。

*/

publicclassShoppingCart{

//常量

privatestaticfinalfloatTAX=0.07f;

privatestaticfinalfloatSHIPPING=2.00f;

privatestaticfinalintCARTSIZE=5;

//实例变量

privateStackcart;

privateintcount;

privatefloattotal;

privatebooleancheckedOut;

publicShoppingCart(){

/*构造方法:

*初始化购物篮的状态:

购物总额(total)为0.00f,结帐状态(checkedOut)为false,购物总数(count)为0。

*/

cart=newStack();

this.total=0;

this.checkedOut=false;

this.count=0;

}

publicbooleanadd(Stringitem,floatprice){

/*

*如果购物篮非空(例如:

小于5件商品),就把该商品添加到购物篮,并修改购物总额(total)*的值,然后返回true,否则返回false。

*/

if(count

{cart.add(item);

this.total+=price;

count++;

returntrue;

}

elsereturnfalse;

}

publicbooleanremove(Stringitem,floatprice){

/*

*如果购物篮里有该商品,就删除该商品并修改购物总额(total)的值,然后返回true,

*否则返回false。

*/

if(cart.contains(item)){

cart.remove(item);

this.total-=price;

count--;

returntr

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2