Java 上机作业2020.4.17

定义一个商品类Goods

属性

:名称、数量、单价(都是private的访问权限) (3分)

方法:(都是public访问权限)

构造方法:

2个参数(名称、单价),数量取0,调用3个参数的构造方法进行赋值;(5分)

构造方法:

3个参数(名称、数量、单价),根据参数的值给对应属性赋值; (5分)

分别定义名称、数量、单价的getter、setter访问器;(10分)
buy方法:

1个参数(购买数量n),把商品的数量加n (4分)

sale方法:

1个参数(销售数量n),先判断商品数量是否足够销售,如果可以则把数量减n (5分)

print方法:

输出商品信息。 (3分)

定义GoodsManage类

属性:商品数组(Goods类型的数组) (5分)

方法:

构造方法:

1个参数(商品数组长度n),根据参数的值创建商品数组,然后循环遍历数组,输入每个商品的名称、数量、单价等信息,并调用Goods类的构造方法为每个数组元素创建Goods对象。 (10分)

search方法:

1个参数(名称),在商品数组中查找是否包含与参数名称相同的商品,如果有则调用其print方法输出商品信息,返回其下标。否则输出查找失败信息,返回-1。 (10分)

search方法:

2个参数(最低价格、最高价格),在商品数组中查找商品价格在最低价格和最高价格之间的所有商品,若查找成功则依次输出这些商品信息,返回true。若查找失败则输出错误信息,返回false。 (10分)

buyGoods方法:

两个参数(商品名称、购买数量),在商品数组中根据名称参数查找到对应的商品对象,并调用其buy方法实现购买操作。 (5分)

saleGoods方法:

两个参数(商品名称、销售数量),在商品数组中根据名称参数查找到对应的商品对象,并调用其sale方法实现销售操作。 (5分)

total方法:

遍历商品数组,求解数组所有商品的总价(注意:单个商品也要算总价=数量*单价)。 (5分)

printAll方法:

遍历数组,输出每个商品的信息。 (5分)

代码

package game;

import java.util.Scanner;

public class Java上机作业417{
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入创建多少个对象");
		GoodsManage aGoodsManage = new GoodsManage(in.nextInt());
		aGoodsManage.printAll();
		aGoodsManage.total();
		System.out.println("\n请输入要购买的商品数量");
		aGoodsManage.buyGoods(in.next(), in.nextInt());
		System.out.println("\n请输入要卖出的商品数量");
		aGoodsManage.saleGoods(in.next(), in.nextInt());
		System.out.println("\n请输入要查找的商品");
		aGoodsManage.search(in.next());
		System.out.println("\n请输入要查找的最低价格和最高价格");
		aGoodsManage.search(in.nextDouble(), in.nextDouble());
		in.close();
	}

}

class Goods{
	private String name;
	private  int number;
	private double prize;
	public Goods(String name, int number, double prize) {
		super();
		this.name = name;
		this.number = number;
		this.prize = prize;
	}
	public Goods(String name, double prize) {
		this(name, 0, prize);
	}
	public void setter_name(String a) { this.name = a; }
	public void setter_number(int a) { this.number = a; }
	public void setter_prize(double a) { this.prize = a; }
	
	public String getter_name() { return this.name; }
	public int getter_number() { return this.number; }
	public double getter_prize() { return this.prize; }
	
	public void bug(int n) {this.number+=n;}
	public void sale(int n) {
		if(this.number>=n) this.number-=n;
		else System.out.println("库存不足");
	}
	public void print() { System.out.println("商品名:"+this.name+"库存:"+this.number+"单价:"+this.prize); }
	
}
class GoodsManage{
	private Goods[] p;

	public GoodsManage(int n) {
		super();
		Scanner in = new Scanner(System.in);
		this.p = new Goods[n];
		Goods aGoods;
		System.out.println("请输入名字 数量 价格");
		for(int i = 0 ; i < n ; i++) {
			aGoods =new Goods(in.next(), in.nextInt(), in.nextDouble());
			this.p[i]= aGoods;
		}		
	}
	public Integer search(String a) {
		for(int i = 0 ; i < this.p.length ; i++)
			if(a.equals(p[i].getter_name())) {
				p[i].print();
				return null;
			}
		return -1;		
	}
	
	public boolean search(double low, double high) {
		boolean t =false;
		for(int i = 0 ; i < this.p.length ; i++) {
			if(p[i].getter_prize()>low && p[i].getter_prize()<high) {
				if(t==false) t = true;
				p[i].print();
			}
		}
		return t;
	}
	public void buyGoods(String a ,int n) {
		for(int i = 0 ; i < this.p.length ; i++)
			if(a.equals(p[i].getter_name())) {
				p[i].bug(n);
				return;
			}
	}
	public void saleGoods(String a ,int n) {
		for(int i = 0 ; i < this.p.length ; i++)
			if(a.equals(p[i].getter_name())) {
				p[i].sale(n);
				return;
			}
	}
	
	public void total() {
		double sum = 0;
		for(int i = 0 ; i < p.length ; i++) {
			System.out.println(p[i].getter_name()+"的总价为:"+(p[i].getter_number()*p[i].getter_prize()));
			sum+=p[i].getter_number()*p[i].getter_prize();
		}
		System.out.println("所有商品总价为:"+sum);
			
	}
	
	public void printAll() {
		for(int i = 0 ; i < p.length ; i++)
			p[i].print();
	}
	
}

总结

要建立一个数组类并且要调用类的构造器需要用

public GoodsManage(int n) {
    super();
    Scanner in = new Scanner(System.in);
    this.p = new Goods[n];
    Goods aGoods;
    System.out.println("请输入名字 数量 价格");
    for(int i = 0 ; i < n ; i++) {
	aGoods =new Goods(in.next(), in.nextInt(), in.nextDouble());
	this.p[i]= aGoods;
    }		
}

此时的this.p = new Goods[n];并没有创建n个对象
只是确定了p的大小,里面都是空的,没有地址
后面就是将创建好的对象赋值给p[i]所对应的空间中
类似C语言指针

发布了26 篇原创文章 · 获赞 11 · 访问量 2362

猜你喜欢

转载自blog.csdn.net/weixin_45696526/article/details/105577743