1 Star 0 Fork 0

jmu2015017/javalearning

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
README.md 3.02 KB
Copy Edit Raw Blame History
jmu2015017 authored 2017-03-25 10:54 . shop

package shopping;

import java.util.List;

public class Total implements Shop{//总价 public double totalprice(List com){ double t=0; for(Commonity a:com){ t=t+a.getPrice()*a.getNumble(); } return t; }

@Override
public void shopcar(List<Commonity> com) {//购物车一览
	// TODO Auto-generated method stub
	for(Commonity a:com){
		System.out.println("书籍:"+a.getName()+"  价格:"+a.getPrice()+"  购买数量"+a.getNumble());
	}
	
}

}

package shopping;

public class Commonity {//商品类 private String name;//商品名字 private double price;//商品价格 private int numble;//商品购买的数量

public Commonity(String name,double price,int numble)
{
	this.name=name;
	this.numble=numble;
	this.price=price;
}

public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public double getPrice() {
	return price;
}
public void setPrice(double price) {
	this.price = price;
}
public int getNumble() {
	return numble;
}
public void setNumble(int numble) {
	this.numble = numble;
}
@Override
public String toString() {
	return "Commonity [name=" + name + ", price=" + price + ", numble=" + numble + "]";
}

}

package shopping;

import java.util.List;

public interface Shop {//接口 public double totalprice(List com);//购买的物品的总价 public void shopcar(List com);//购物车一览 }

package shopping;

import java.util.*;

public class test { public static void main(String[] args) { Scanner in=new Scanner(System.in); int i,t,r; String str; List com=new ArrayList(); Commonity[] a=new Commonity[10];//建立图书种类供人选择购买,只有列出图书 a[0]=new Commonity("数据结构",49.5,0); a[1]=new Commonity("JAVA",60.0,0); a[2]=new Commonity("高数",50.0,0); a[3]=new Commonity("计算机组成原理",58.5,0); a[4]=new Commonity("信号与系统",43.5,0); a[5]=new Commonity("c语言",72.5,0); a[6]=new Commonity("密码学",22.5,0); a[7]=new Commonity("离散数学",42.5,0); a[8]=new Commonity("新视野大学英语四级",36.5,0); a[9]=new Commonity("数字逻辑",48.5,0); System.out.println(); for(i=0;i<10;i++){ System.out.println("书籍:"+a[i].getName()+" "+"价格:"+a[i].getPrice());//列出全部的书籍供人选择 } for(i=0;;i++){ System.out.print("请输入商品名称:"); str=in.next(); System.out.print("请输入购买的数量:"); for(int j=0;j<10;j++){ if(a[j].getName().equals(str)){ Commonity b=new Commonity(a[j].getName(),a[j].getPrice(),in.nextInt()); com.add(b); break; } } System.out.println("是否继续购买,1.继续,0.退出"); r=in.nextInt(); if(r==1) continue; else if(r==0) break; } Total w=new Total(); w.shopcar(com); System.out.println(w.totalprice(com)); System.out.println("多谢惠顾"); }

}

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/gemola/javalearning.git
git@gitee.com:gemola/javalearning.git
gemola
javalearning
javalearning
shop

Search

344bd9b3 5694891 D2dac590 5694891