本周我们小组在全力攻克Java大作业,由此也是积累了很多软件设计上的经验,下面以组长的作业为例,我们可以继续设想我们的软工项目实现方法。

  这次展示的对象采用了访问者模式,代码如下:

public abstract class VIP {
public abstract void accept(StoreVisitor v);
}

public interface StoreVisitor {
public void visit(BronzeVIP bronzevip);
public void visit(SilverVIP silvervip);
public void visit(GoldVIP goldvip);
}

public class BronzeVIP extends VIP{
String name;
String address;
BronzeVIP(String name,String address){
this.name=name;
this.address=address;
}
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public void accept(StoreVisitor v){
v.visit(this);
}
}

public class SilverVIP extends VIP{
String name;
String address;
SilverVIP(String name,String address){
this.name=name;
this.address=address;
}
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public void accept(StoreVisitor v){
v.visit(this);
}
}

public class GoldVIP extends VIP{
String name;
String address;
GoldVIP(String name,String address){
this.name=name;
this.address=address;
}
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public void accept(StoreVisitor v){
v.visit(this);
}
}

import java.util.Scanner;
public class ShoeStore implements StoreVisitor{
public void visit(BronzeVIP bronzevip){
String name=bronzevip.getName();
String address=bronzevip.getName();
Scanner reader=new Scanner(System.in);
System.out.println("请输入消费额度:");
double consume=-1;
consume=reader.nextDouble();
double deliverfee=10;
System.out.println(bronzevip.getName()+"共消费:"+consume*0.90+"+运费:"+deliverfee);
}
public void visit(SilverVIP silvervip){
String name=silvervip.getName();
String address=silvervip.getName();
Scanner reader=new Scanner(System.in);
System.out.println("请输入消费额度:");
double consume=-1;
consume=reader.nextDouble();
double deliverfee=10;
if(address!="上海"&&address!="江苏"&&address!="浙江")
System.out.println(silvervip.getName()+"共消费:"+consume*0.90+"+运费:"+deliverfee);
else
{
deliverfee=0;
System.out.println(silvervip.getName()+"共消费:"+consume*0.90+"+运费:"+deliverfee);
}
}
public void visit(GoldVIP goldvip){
String name=goldvip.getName();
String address=goldvip.getName();
Scanner reader=new Scanner(System.in);
System.out.println("请输入消费额度:");
double consume=-1;
consume=reader.nextDouble();
double deliverfee=10;
if(address!="上海"&&address!="江苏"&&address!="浙江")
System.out.println(goldvip.getName()+"共消费:"+consume*0.90+"+运费:"+deliverfee);
else
{
deliverfee=0;
System.out.println(goldvip.getName()+"共消费:"+consume*0.75+"+运费:"+deliverfee);
}

}
}

import java.util.Scanner;
public class BookStore implements StoreVisitor{
public void visit(BronzeVIP bronzevip){
String name=bronzevip.getName();
String address=bronzevip.getName();
Scanner reader=new Scanner(System.in);
System.out.println("请输入消费额度:");
double consume=-1;
consume=reader.nextDouble();
double deliverfee=15;
System.out.println(bronzevip.getName()+"共消费:"+consume*0.95+"+运费:"+deliverfee);
}
public void visit(SilverVIP silvervip){
String name=silvervip.getName();
String address=silvervip.getName();
Scanner reader=new Scanner(System.in);
System.out.println("请输入消费额度:");
double consume=-1;
consume=reader.nextDouble();
double deliverfee=10;
System.out.println(silvervip.getName()+"共消费:"+consume*0.90+"+运费:"+deliverfee);
}
public void visit(GoldVIP goldvip){
String name=goldvip.getName();
String address=goldvip.getName();
Scanner reader=new Scanner(System.in);
System.out.println("请输入消费额度:");
double consume=-1;
consume=reader.nextDouble();
double deliverfee=0;
System.out.println(goldvip.getName()+"共消费:"+consume*0.80+"+运费:"+deliverfee);
}
}

以上我们把需要的共有类都组织好了,这款软件有意构建了一组小型的网购系统,该网购系统对客户的VIP等级实行划分,从而按等级制定优惠方案,而访问者的类别就是一个个网店,系统可任意添加店铺,设计不同的优惠方案供供不同级别的客户体验优惠消费,再次,我们着重学习了《构建之法》第七章软件测试的内容,这里提供一个小小的测试方案:

import java.util.Scanner;

public class test {

public static void main(String[] args) {
// TODO Auto-generated method stub
StoreVisitor bookstore=new BookStore();
StoreVisitor shoestore=new ShoeStore();
Scanner reader=new Scanner(System.in);
System.out.println("********************");
System.out.println("请输入客户姓名:");
String cname="";
cname=reader.nextLine();
System.out.println("********************");
System.out.println("请输入客户地址:");
String cadd="";
cadd=reader.nextLine();
System.out.println("********************");
System.out.println("请输入店铺序号(1.BookStores,2.ShoeStore):");
int no=reader.nextInt();
System.out.println("********************");
System.out.println("请输入会员等级(1.青铜会员,2.白银会员,3.黄金会员):");
int level=reader.nextInt();
if(no==1&&level==1)
{
VIP vip=new BronzeVIP(cname,cadd);
vip.accept(bookstore);
}
else if(no==2&&level==1)
{
VIP vip=new BronzeVIP(cname,cadd);
vip.accept(shoestore);
}
else if(no==1&&level==2)
{
VIP vip=new SilverVIP(cname,cadd);
vip.accept(bookstore);
}
else if(no==2&&level==2)
{
VIP vip=new SilverVIP(cname,cadd);
vip.accept(shoestore);
}
else if(no==1&&level==3)
{
VIP vip=new GoldVIP(cname,cadd);
vip.accept(bookstore);
}
else if(no==2&&level==3)
{
VIP vip=new GoldVIP(cname,cadd);
vip.accept(shoestore);
}
}

}

运行结果显示

而应用程序部分的代码我们还有很多不满意的地方,比如我们想实现数据库的连接,再简单一点可以建立对象流实现大规模用户的建立,GUI设计也是一个很大的难题,我们希望通过窗口化的界面展示我们的生日好友互动软件,用流体布局等等。这些操作以后慢慢实现吧。

 posted on 2017-06-04 16:07  Excelent_RJ  阅读(106)  评论(0编辑  收藏  举报