package demo;
从网站建设到定制行业解决方案,为提供成都网站制作、成都做网站服务体系,各种行业企业客户提供网站建设解决方案,助力业务快速发展。创新互联公司将不断加快创新步伐,提供优质的建站服务。
public class Fruit {
//水果类属性
String kind,color;
double weight;
//有参构造必须赋值
public Fruit(String kind,String color,double weight){
this.setKind(kind);
this.setColor(color);
this.setWeight(weight);
}
//属性封装
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
package demo;
import java.util.ArrayList;
import java.util.Scanner;
public class Box {
//箱子,有一个水果类集合
ArrayListFruit f=new ArrayListFruit();
public ArrayListFruit getF() {
return f;
}
public void setF(ArrayListFruit f) {
this.f = f;
}
//将水果添加到箱子(集合中)
public void addFruit(Fruit temp) {
this.f.add(temp);
}
void manage(){
Box box=new Box();
int choose;
try {
do{
System.out.println("请选择添加水果:1、苹果,2、橘子,3、梨,4、查看,5、退出");
Scanner input=new Scanner(System.in);
choose=input.nextInt();
switch (choose) {
case 1:
System.out.println("请输入类型");
String type1=input.next();
System.out.println("请输入颜色");
String color1=input.next();
System.out.println("请输入重量");
double weight1=input.nextDouble();
Fruit temp1=new Fruit(type1,color1,weight1);
box.addFruit(temp1);
break;
case 2:
System.out.println("请输入类型");
String type2=input.next();
System.out.println("请输入颜色");
String color2=input.next();
System.out.println("请输入重量");
double weight2=input.nextDouble();
Fruit temp2=new Fruit(type2,color2,weight2);
box.addFruit(temp2);
break;
case 3:
System.out.println("请输入类型");
String type3=input.next();
System.out.println("请输入颜色");
String color3=input.next();
System.out.println("请输入重量");
double weight3=input.nextDouble();
Fruit temp3=new Fruit(type3,color3,weight3);
box.addFruit(temp3);
break;
case 4:
double allWeight=0;
for (int i=0;ibox.getF().size();i++) {
allWeight+=box.getF().get(i).weight;
}
System.out.println("总重量:"+allWeight);
System.out.println("颜色:");
for(Fruit fruit:box.getF()){
System.out.println(fruit.getColor());
}
break;
case 5:
break;
default:
System.out.println("选择错误");
break;
}
}while(choose!=5);
} catch (Exception e) {
System.out.println("输入错误");
}
}
public static void main(String[] args) {
Box manage=new Box();
manage.manage();
}
}
时间不够 我得回宿舍 所有的验证部分都没做 代码可以运行 但前提是你得正确输入 否则不认
package com.Test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
//java程序题!!!求大神指教!谢了!
//有一个水果箱(Box),箱子里装有水果(Fruit),每一种水果都有不同的重量和颜色,
//水果有:苹果,梨,橘子。每个苹果(Apple)都有不同的重量和颜色,
//每个橘子(Orange)有不同的重量和颜色,每个梨(Pear)都有不同的重量和颜色,
//可以像水果箱(Box)里添加水果(addFruit),也可以取出水果(getFruit),
//还可以显示水果的重量和颜色,写出实现这些方法的代码,要求实现上述功能!
public class HelpTest {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
Box b=new Box();
while(true)
{
System.out.println(" 请选择功能 ");
System.out.println("1 添加水果");
System.out.println("2 删除水果");
System.out.println("3 显示水果信息");
System.out.println("4 退出系统");
int a =Integer.parseInt(br.readLine());
System.out.println(a);
switch (a) {
case 1:
System.out.println("请输入水果名称");
String name=br.readLine();
System.out.println("请输入水果颜色");
String color=br.readLine();
System.out.println("请输入水果重量");
float weight=Float.parseFloat(br.readLine());
Furit f=new Furit(name, color, weight);
b.AddFurit(f);
break;
case 2:
System.out.println("请输入水果名称");
String name1=br.readLine();
b.DelFurit(name1);
break;
case 3:
System.out.println("请输入水果名称");
String name2=br.readLine();
b.ShowInfo(name2);
break;
case 4:
System.exit(0);
break;
default:
break;
}
}
}
}
class Furit{
private String name;
private String color;
private float weight;
public Furit(String name,String color,float weight){
this.name=name;
this.color=color;
this.weight=weight;
}
public Furit(String name)
{
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
class Box{
ArrayListFurit al=new ArrayListFurit();
//添加水果的方法
public void AddFurit(Furit f){
al.add(f);
}
//删除水果的方法
public void DelFurit(String name)
{
for(int i=0;ial.size();i++)
{
if(al.get(i).getName().equals(name))
{
al.remove(i);
}
}
}
//显示水果的方法
public void ShowInfo(String name ){
for(int i=0;ial.size();i++)
{
Furit f=(Furit)al.get(i);
if(name.equals(f.getName()))
{
System.out.println(al.get(i).getName()+"的颜色是"+al.get(i).getColor()+" 重量是"+al.get(i).getWeight());
}
}
}
}
我给你写了这么多你采纳他的答案? 呵呵
可以定义一个FruitBox父类,然后定义水果苹果Apple,橘子Orange,梨Pear的子类,颜色color和重量weight封装成属性,添加子类的get,set方法,定义一个测试类测试一下.